Nullable fixes

This commit is contained in:
Mateusz Karbowiak
2024-10-06 01:57:12 +02:00
parent a2874a189e
commit c9b1f6f516

View File

@@ -79,12 +79,11 @@ public:
/// <param name="other">The wrapped value to be moved.</param> /// <param name="other">The wrapped value to be moved.</param>
Nullable(Nullable&& other) noexcept Nullable(Nullable&& other) noexcept
{ {
_hasValue = other._hasValue; if (other._hasValue)
if (_hasValue)
{ {
new (&_value) T(MoveTemp(other._value)); // Placement new (move constructor) new (&_value) T(MoveTemp(other._value)); // Placement new (move constructor)
} }
_hasValue = other._hasValue;
other.Reset(); other.Reset();
} }
@@ -194,10 +193,10 @@ public:
} }
/// <summary> /// <summary>
/// Gets an instance of the wrapped value or a default value based on r-value reference, if the wrapped value is not valid. /// Gets a mutable reference to the wrapped value or a default value if the value is not valid.
/// </summary> /// </summary>
/// <returns>Copy of the wrapped value or the default value.</returns> /// <returns>Reference to the wrapped value or the default value.</returns>
FORCE_INLINE T GetValueOr(T&& defaultValue) const noexcept FORCE_INLINE T& GetValueOr(T& defaultValue) const
{ {
return _hasValue ? _value : defaultValue; return _hasValue ? _value : defaultValue;
} }
@@ -266,8 +265,8 @@ public:
/// </summary> /// </summary>
FORCE_INLINE void Reset() FORCE_INLINE void Reset()
{ {
_hasValue = false; // Reset the flag BEFORE the value is (potentially) destructed.
KillOld(); KillOld();
_hasValue = false; // Reset the flag AFTER the value is (potentially) destructed.
} }
/// <summary> /// <summary>