#ifndef AL_OPTIONAL_H #define AL_OPTIONAL_H #include #include #include #include "almalloc.h" namespace al { struct nullopt_t { }; struct in_place_t { }; constexpr nullopt_t nullopt{}; constexpr in_place_t in_place{}; #define NOEXCEPT_AS(...) noexcept(noexcept(__VA_ARGS__)) /* Base storage struct for an optional. Defines a trivial destructor, for types * that can be trivially destructed. */ template::value> struct optstore_base { bool mHasValue{false}; union { char mDummy; T mValue; }; optstore_base() noexcept { } template explicit optstore_base(in_place_t, Args&& ...args) noexcept(std::is_nothrow_constructible::value) : mHasValue{true}, mValue{std::forward(args)...} { } ~optstore_base() = default; }; /* Specialization needing a non-trivial destructor. */ template struct optstore_base { bool mHasValue{false}; union { char mDummy; T mValue; }; optstore_base() noexcept { } template explicit optstore_base(in_place_t, Args&& ...args) noexcept(std::is_nothrow_constructible::value) : mHasValue{true}, mValue{std::forward(args)...} { } ~optstore_base() { if(mHasValue) al::destroy_at(std::addressof(mValue)); } }; /* Next level of storage, which defines helpers to construct and destruct the * stored object. */ template struct optstore_helper : public optstore_base { using optstore_base::optstore_base; template void construct(Args&& ...args) noexcept(std::is_nothrow_constructible::value) { al::construct_at(std::addressof(this->mValue), std::forward(args)...); this->mHasValue = true; } void reset() noexcept { if(this->mHasValue) al::destroy_at(std::addressof(this->mValue)); this->mHasValue = false; } void assign(const optstore_helper &rhs) noexcept(std::is_nothrow_copy_constructible::value && std::is_nothrow_copy_assignable::value) { if(!rhs.mHasValue) this->reset(); else if(this->mHasValue) this->mValue = rhs.mValue; else this->construct(rhs.mValue); } void assign(optstore_helper&& rhs) noexcept(std::is_nothrow_move_constructible::value && std::is_nothrow_move_assignable::value) { if(!rhs.mHasValue) this->reset(); else if(this->mHasValue) this->mValue = std::move(rhs.mValue); else this->construct(std::move(rhs.mValue)); } }; /* Define copy and move constructors and assignment operators, which may or may * not be trivial. Default definition is completely trivial. */ template::value, bool trivial_move = std::is_trivially_move_constructible::value, /* Trivial assignment is dependent on trivial construction. */ bool = trivial_copy && std::is_trivially_copy_assignable::value, bool = trivial_move && std::is_trivially_move_assignable::value> struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage&) = default; optional_storage(optional_storage&&) = default; optional_storage& operator=(const optional_storage&) = default; optional_storage& operator=(optional_storage&&) = default; }; /* Non-trivial move assignment. */ template struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage&) = default; optional_storage(optional_storage&&) = default; optional_storage& operator=(const optional_storage&) = default; optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; /* Non-trivial move construction. */ template struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage&) = default; optional_storage(optional_storage&& rhs) NOEXCEPT_AS(this->construct(std::move(rhs.mValue))) { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); } optional_storage& operator=(const optional_storage&) = default; optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; /* Non-trivial copy assignment. */ template struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage&) = default; optional_storage(optional_storage&&) = default; optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } optional_storage& operator=(optional_storage&&) = default; }; /* Non-trivial copy construction. */ template struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage &rhs) NOEXCEPT_AS(this->construct(rhs.mValue)) { if(rhs.mHasValue) this->construct(rhs.mValue); } optional_storage(optional_storage&&) = default; optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } optional_storage& operator=(optional_storage&&) = default; }; /* Non-trivial assignment. */ template struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage&) = default; optional_storage(optional_storage&&) = default; optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; /* Non-trivial assignment, non-trivial move construction. */ template struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage&) = default; optional_storage(optional_storage&& rhs) NOEXCEPT_AS(this->construct(std::move(rhs.mValue))) { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); } optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; /* Non-trivial assignment, non-trivial copy construction. */ template struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage &rhs) NOEXCEPT_AS(this->construct(rhs.mValue)) { if(rhs.mHasValue) this->construct(rhs.mValue); } optional_storage(optional_storage&&) = default; optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; /* Completely non-trivial. */ template struct optional_storage : public optstore_helper { using optstore_helper::optstore_helper; optional_storage() noexcept = default; optional_storage(const optional_storage &rhs) NOEXCEPT_AS(this->construct(rhs.mValue)) { if(rhs.mHasValue) this->construct(rhs.mValue); } optional_storage(optional_storage&& rhs) NOEXCEPT_AS(this->construct(std::move(rhs.mValue))) { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); } optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; template class optional { using storage_t = optional_storage; storage_t mStore{}; public: using value_type = T; optional() = default; optional(const optional&) = default; optional(optional&&) = default; optional(nullopt_t) noexcept { } template explicit optional(in_place_t, Args&& ...args) : mStore{al::in_place, std::forward(args)...} { } ~optional() = default; optional& operator=(const optional&) = default; optional& operator=(optional&&) = default; optional& operator=(nullopt_t) noexcept { mStore.reset(); return *this; } template std::enable_if_t::value && std::is_assignable::value && !std::is_same, optional>::value && (!std::is_same, T>::value || !std::is_scalar::value), optional&> operator=(U&& rhs) { if(mStore.mHasValue) mStore.mValue = std::forward(rhs); else mStore.construct(std::forward(rhs)); return *this; } const T* operator->() const { return std::addressof(mStore.mValue); } T* operator->() { return std::addressof(mStore.mValue); } const T& operator*() const& { return mStore.mValue; } T& operator*() & { return mStore.mValue; } const T&& operator*() const&& { return std::move(mStore.mValue); } T&& operator*() && { return std::move(mStore.mValue); } operator bool() const noexcept { return mStore.mHasValue; } bool has_value() const noexcept { return mStore.mHasValue; } T& value() & { return mStore.mValue; } const T& value() const& { return mStore.mValue; } T&& value() && { return std::move(mStore.mValue); } const T&& value() const&& { return std::move(mStore.mValue); } template T value_or(U&& defval) const& { return bool{*this} ? **this : static_cast(std::forward(defval)); } template T value_or(U&& defval) && { return bool{*this} ? std::move(**this) : static_cast(std::forward(defval)); } void reset() noexcept { mStore.reset(); } }; template inline optional> make_optional(T&& arg) { return optional>{in_place, std::forward(arg)}; } template inline optional make_optional(Args&& ...args) { return optional{in_place, std::forward(args)...}; } template inline optional make_optional(std::initializer_list il, Args&& ...args) { return optional{in_place, il, std::forward(args)...}; } #undef NOEXCEPT_AS } // namespace al #endif /* AL_OPTIONAL_H */