From f56207f1a40846749ea9122111d81b1dbf8a611b Mon Sep 17 00:00:00 2001 From: Mateusz Karbowiak <69864511+mtszkarbowiak@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:17:23 +0200 Subject: [PATCH] `Nullable.Reset` fix, killing inlining --- Source/Engine/Core/Types/Nullable.h | 55 ++++++++++++++++++----------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/Source/Engine/Core/Types/Nullable.h b/Source/Engine/Core/Types/Nullable.h index 985e66090..43fe5342a 100644 --- a/Source/Engine/Core/Types/Nullable.h +++ b/Source/Engine/Core/Types/Nullable.h @@ -24,17 +24,6 @@ private: }; bool _hasValue; - /// - /// Ends the lifetime of the wrapped value by calling its destructor, if the lifetime has not ended yet. Otherwise, does nothing. - /// - FORCE_INLINE void KillOld() - { - if (_hasValue) - { - _value.~T(); - } - } - public: /// /// Initializes by setting the wrapped value to null. @@ -48,7 +37,10 @@ public: ~Nullable() { - KillOld(); + if (_hasValue) + { + _value.~T(); + } } /// @@ -104,7 +96,10 @@ public: template::Value>::Type> auto operator=(const T& value) -> Nullable& { - KillOld(); + if (_hasValue) + { + _value.~T(); + } new (&_value) T(value); // Placement new (copy constructor) _hasValue = true; @@ -117,7 +112,10 @@ public: /// auto operator=(T&& value) noexcept -> Nullable& { - KillOld(); + if (_hasValue) + { + _value.~T(); + } new (&_value) T(MoveTemp(value)); // Placement new (move constructor) _hasValue = true; @@ -131,12 +129,16 @@ public: template::Value>::Type> auto operator=(const Nullable& other) -> Nullable& { - KillOld(); + if (_hasValue) + { + _value.~T(); + } if (other._hasValue) { new (&_value) T(other._value); // Placement new (copy constructor) } + _hasValue = other._hasValue; // Set the flag AFTER the value is copied. return *this; @@ -152,14 +154,19 @@ public: return *this; } - KillOld(); - if (_hasValue) + { + _value.~T(); + } + + if (other._hasValue) { new (&_value) T(MoveTemp(other._value)); // Placement new (move constructor) - other.Reset(); + other._value.~T(); // Kill the old value in the source object. + other._hasValue = false; } + _hasValue = other._hasValue; // Set the flag AFTER the value is moved. return *this; @@ -235,7 +242,10 @@ public: /// The value to be moved. FORCE_INLINE void SetValue(T&& value) noexcept { - KillOld(); + if (_hasValue) + { + _value.~T(); + } new (&_value) T(MoveTemp(value)); // Placement new (move constructor) _hasValue = true; // Set the flag AFTER the value is moved. @@ -279,8 +289,13 @@ public: /// FORCE_INLINE void Reset() { + if (!_hasValue) + { + return; + } + _hasValue = false; // Reset the flag BEFORE the value is (potentially) destructed. - KillOld(); + _value.~T(); } ///