diff --git a/deps/ox/src/ox/std/optional.hpp b/deps/ox/src/ox/std/optional.hpp index 38da7db4..aa0f1f87 100644 --- a/deps/ox/src/ox/std/optional.hpp +++ b/deps/ox/src/ox/std/optional.hpp @@ -11,6 +11,7 @@ #include "bit.hpp" #include "initializerlist.hpp" #include "iterator.hpp" +#include "memory.hpp" #include "new.hpp" #include "types.hpp" #include "utility.hpp" @@ -32,8 +33,6 @@ class Optional { constexpr Optional(const Optional &other) { if (other.m_ptr) { m_ptr = new(m_data) T(*other.m_ptr); - } else { - m_ptr = nullptr; } } @@ -43,14 +42,12 @@ class Optional { } if (other.m_ptr) { m_ptr = new(m_data) T(std::move(*other.m_ptr)); - } else { - m_ptr = nullptr; } } constexpr ~Optional() { if (m_ptr) { - m_ptr->~T(); + reset(); } } @@ -98,8 +95,7 @@ class Optional { } *m_ptr = *other.m_ptr; } else if (m_ptr) { - m_ptr->~T(); - m_ptr = nullptr; + reset(); } return *this; } @@ -114,22 +110,29 @@ class Optional { } *m_ptr = std::move(*other.m_ptr); } else if (m_ptr) { - m_ptr->~T(); - m_ptr = nullptr; + reset(); } return *this; } template constexpr T &emplace(Args &&...args) { - m_ptr = std::construct_at(m_ptr, ox::forward(args)...); + if (std::is_constant_evaluated()) { + m_ptr = new T(ox::forward(args)...); + } else { + m_ptr = std::construct_at(reinterpret_cast(m_data), ox::forward(args)...); + } return *m_ptr; } template constexpr T &emplace_subclass(Args &&...args) { static_assert(sizeof(U) <= buffSize, "Subclass is too large for this Optional"); - m_ptr = std::construct_at(m_ptr, ox::forward(args)...); + if (std::is_constant_evaluated()) { + m_ptr = new U(ox::forward(args)...); + } else { + m_ptr = std::construct_at(reinterpret_cast(m_data), ox::forward(args)...); + } return *m_ptr; } @@ -143,7 +146,11 @@ class Optional { } constexpr void reset() noexcept { - get()->~T(); + if (std::is_constant_evaluated()) { + ox::safeDelete(m_ptr); + } else { + get()->~T(); + } m_ptr = nullptr; } @@ -155,4 +162,4 @@ constexpr Optional::Optional(Args &&... args) { emplace(ox::forward(args)...); } -} \ No newline at end of file +}