#ifndef AL_OPTIONAL_H #define AL_OPTIONAL_H #include #include #include #include "almalloc.h" namespace al { #define REQUIRES(...) bool rt_=true, std::enable_if_t = true struct nullopt_t { }; struct in_place_t { }; constexpr nullopt_t nullopt{}; constexpr in_place_t in_place{}; template class optional { bool mHasValue{false}; union { char mDummy[sizeof(T)]{}; T mValue; }; template void DoConstruct(Args&& ...args) { ::new (std::addressof(mValue)) T{std::forward(args)...}; mHasValue = true; } public: using value_type = T; optional() noexcept = default; optional(nullopt_t) noexcept { } template::value)> optional(const optional &rhs) { if(rhs) DoConstruct(*rhs); } template::value)> optional(optional&& rhs) { if(rhs) DoConstruct(std::move(*rhs)); } template::value)> explicit optional(in_place_t, Args&& ...args) : mHasValue{true} , mValue{std::forward(args)...} { } template&, Args...>::value)> explicit optional(in_place_t, std::initializer_list il, Args&& ...args) : mHasValue{true}, mValue{il, std::forward(args)...} { } template::value && !std::is_same::type, in_place_t>::value && !std::is_same::type, optional>::value && std::is_constructible::value)> constexpr explicit optional(U&& value) : mHasValue{true}, mValue{std::forward(value)} { } template::value && !std::is_same::type, in_place_t>::value && !std::is_same::type, optional>::value && !std::is_constructible::value)> constexpr optional(U&& value) : mHasValue{true}, mValue{std::forward(value)} { } ~optional() { if(mHasValue) al::destroy_at(std::addressof(mValue)); } optional& operator=(nullopt_t) noexcept { reset(); return *this; } template::value && std::is_copy_assignable::value)> optional& operator=(const optional &rhs) { if(!rhs) reset(); else if(*this) mValue = *rhs; else DoConstruct(*rhs); return *this; } template::value && std::is_move_assignable::value)> optional& operator=(optional&& rhs) { if(!rhs) reset(); else if(*this) mValue = std::move(*rhs); else DoConstruct(std::move(*rhs)); return *this; } template::value && std::is_assignable::value && !std::is_same::type, optional>::value && (!std::is_same::type, T>::value || !std::is_scalar::value))> optional& operator=(U&& rhs) { if(*this) mValue = std::forward(rhs); else DoConstruct(std::forward(rhs)); return *this; } const T* operator->() const { return std::addressof(mValue); } T* operator->() { return std::addressof(mValue); } const T& operator*() const& { return mValue; } T& operator*() & { return mValue; } const T&& operator*() const&& { return std::move(mValue); } T&& operator*() && { return std::move(mValue); } operator bool() const noexcept { return mHasValue; } bool has_value() const noexcept { return mHasValue; } T& value() & { return mValue; } const T& value() const& { return mValue; } T&& value() && { return std::move(mValue); } const T&& value() const&& { return std::move(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 { if(mHasValue) al::destroy_at(std::addressof(mValue)); mHasValue = false; } }; 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 REQUIRES } // namespace al #endif /* AL_SPAN_H */