#pragma once #include #include "maybe_traits.h" #include "yexception.h" #include #include #include #include namespace NMaybe { struct TPolicyUndefinedExcept { [[noreturn]] static void OnEmpty(const std::type_info& valueTypeInfo); }; struct TPolicyUndefinedFail { [[noreturn]] static void OnEmpty(const std::type_info& valueTypeInfo); }; } struct TNothing { explicit constexpr TNothing(int) noexcept { } }; constexpr TNothing NothingObject{0}; constexpr TNothing Nothing() noexcept { return NothingObject; } constexpr bool operator==(TNothing, TNothing) noexcept { return true; } template class TMaybe: private TMaybeBase { public: using TInPlace = NMaybe::TInPlace; private: static_assert(!std::is_same, TNothing>::value, "Instantiation of TMaybe with a TNothing type is ill-formed"); static_assert(!std::is_same, TInPlace>::value, "Instantiation of TMaybe with a TInPlace type is ill-formed"); static_assert(!std::is_reference::value, "Instantiation of TMaybe with reference type is ill-formed"); static_assert(std::is_destructible::value, "Instantiation of TMaybe with non-destructible type is ill-formed"); template struct TConstructibleFromMaybeSomehow { public: static constexpr bool value = std::is_constructible&>::value || std::is_constructible&>::value || std::is_constructible&&>::value || std::is_constructible&&>::value || std::is_convertible&, T>::value || std::is_convertible&, T>::value || std::is_convertible&&, T>::value || std::is_convertible&&, T>::value; }; template struct TAssignableFromMaybeSomehow { public: static constexpr bool value = TConstructibleFromMaybeSomehow::value || std::is_assignable&>::value || std::is_assignable&>::value || std::is_assignable&&>::value || std::is_assignable&&>::value; }; template struct TImplicitCopyCtor { public: static constexpr bool value = std::is_constructible::value && std::is_convertible::value && !TConstructibleFromMaybeSomehow::value; }; template struct TExplicitCopyCtor { public: static constexpr bool value = std::is_constructible::value && !std::is_convertible::value && !TConstructibleFromMaybeSomehow::value; }; template struct TImplicitMoveCtor { public: static constexpr bool value = std::is_constructible::value && std::is_convertible::value && !TConstructibleFromMaybeSomehow::value; }; template struct TExplicitMoveCtor { public: static constexpr bool value = std::is_constructible::value && !std::is_convertible::value && !TConstructibleFromMaybeSomehow::value; }; template struct TCopyAssignable { public: static constexpr bool value = std::is_constructible::value && std::is_assignable::value && !TAssignableFromMaybeSomehow::value; }; template struct TMoveAssignable { public: static constexpr bool value = std::is_constructible::value && std::is_assignable::value && !TAssignableFromMaybeSomehow::value; }; template struct TImplicitAnyCtor { public: using UDec = std::decay_t; static constexpr bool value = std::is_constructible::value && std::is_convertible::value && !std::is_same::value && !std::is_same::value; }; template struct TExplicitAnyCtor { public: using UDec = std::decay_t; static constexpr bool value = std::is_constructible::value && !std::is_convertible::value && !std::is_same::value && !std::is_same::value; }; template struct TAssignableFromAny { public: using UDec = std::decay_t; static constexpr bool value = !std::is_same::value && std::is_constructible::value && std::is_assignable::value && (!std::is_scalar::value || !std::is_same::value); }; using TBase = TMaybeBase; public: using value_type = T; using TValueType = value_type; TMaybe() noexcept = default; constexpr TMaybe(const TMaybe&) = default; constexpr TMaybe(TMaybe&&) = default; template constexpr explicit TMaybe(TInPlace, Args&&... args) : TBase(TInPlace{}, std::forward(args)...) { } template constexpr explicit TMaybe(TInPlace, std::initializer_list il, TArgs&&... args) : TBase(TInPlace{}, il, std::forward(args)...) { } constexpr TMaybe(TNothing) noexcept { } template ::value>> TMaybe(const TMaybe& right) { if (right.Defined()) { new (Data()) T(right.GetRef()); this->Defined_ = true; } } template ::value, bool> = false> explicit TMaybe(const TMaybe& right) { if (right.Defined()) { new (Data()) T(right.GetRef()); this->Defined_ = true; } } template ::value>> TMaybe(TMaybe&& right) noexcept(std::is_nothrow_constructible::value) { if (right.Defined()) { new (Data()) T(std::move(right.GetRef())); this->Defined_ = true; } } template ::value, bool> = false> explicit TMaybe(TMaybe&& right) noexcept(std::is_nothrow_constructible::value) { if (right.Defined()) { new (Data()) T(std::move(right.GetRef())); this->Defined_ = true; } } template ::value>> constexpr TMaybe(U&& right) : TBase(TInPlace{}, std::forward(right)) { } template ::value, bool> = false> constexpr explicit TMaybe(U&& right) : TBase(TInPlace{}, std::forward(right)) { } ~TMaybe() = default; constexpr TMaybe& operator=(const TMaybe&) = default; constexpr TMaybe& operator=(TMaybe&&) = default; TMaybe& operator=(TNothing) noexcept { Clear(); return *this; } template std::enable_if_t::value, TMaybe&> operator=(U&& right) { if (Defined()) { *Data() = std::forward(right); } else { Init(std::forward(right)); } return *this; } template std::enable_if_t::value, TMaybe&> operator=(const TMaybe& right) { if (right.Defined()) { if (Defined()) { *Data() = right.GetRef(); } else { Init(right.GetRef()); } } else { Clear(); } return *this; } template std::enable_if_t::value, TMaybe&> operator=(TMaybe&& right) noexcept( std::is_nothrow_assignable::value&& std::is_nothrow_constructible::value) { if (right.Defined()) { if (Defined()) { *Data() = std::move(right.GetRef()); } else { Init(std::move(right.GetRef())); } } else { Clear(); } return *this; } template T& ConstructInPlace(Args&&... args) { Clear(); Init(std::forward(args)...); return *Data(); } Y_REINITIALIZES_OBJECT void Clear() noexcept { if (Defined()) { this->Defined_ = false; Data()->~T(); } } constexpr bool Defined() const noexcept { return this->Defined_; } Y_PURE_FUNCTION constexpr bool Empty() const noexcept { return !Defined(); } void CheckDefined() const { if (Y_UNLIKELY(!Defined())) { Policy::OnEmpty(typeid(TValueType)); } } const T* Get() const noexcept { return Defined() ? Data() : nullptr; } T* Get() noexcept { return Defined() ? Data() : nullptr; } constexpr const T& GetRef() const& { CheckDefined(); return *Data(); } constexpr T& GetRef() & { CheckDefined(); return *Data(); } constexpr const T&& GetRef() const&& { CheckDefined(); return std::move(*Data()); } constexpr T&& GetRef() && { CheckDefined(); return std::move(*Data()); } constexpr const T& operator*() const& { return GetRef(); } constexpr T& operator*() & { return GetRef(); } constexpr const T&& operator*() const&& { return std::move(GetRef()); } constexpr T&& operator*() && { return std::move(GetRef()); } constexpr const T* operator->() const { return &GetRef(); } constexpr T* operator->() { return &GetRef(); } constexpr const T& GetOrElse(const T& elseValue) const { return Defined() ? *Data() : elseValue; } constexpr T& GetOrElse(T& elseValue) { return Defined() ? *Data() : elseValue; } constexpr const TMaybe& OrElse(const TMaybe& elseValue) const noexcept { return Defined() ? *this : elseValue; } constexpr TMaybe& OrElse(TMaybe& elseValue) { return Defined() ? *this : elseValue; } template TMaybe Cast() const { return Defined() ? TMaybe(*Data()) : TMaybe(); } constexpr explicit operator bool() const noexcept { return Defined(); } void Save(IOutputStream* out) const { const bool defined = Defined(); ::Save(out, defined); if (defined) { ::Save(out, *Data()); } } void Load(IInputStream* in) { bool defined; ::Load(in, defined); if (defined) { if (!Defined()) { ConstructInPlace(); } ::Load(in, *Data()); } else { Clear(); } } void Swap(TMaybe& other) { if (this->Defined_ == other.Defined_) { if (this->Defined_) { ::DoSwap(this->Data_, other.Data_); } } else { if (this->Defined_) { other.Init(std::move(this->Data_)); this->Clear(); } else { this->Init(std::move(other.Data_)); other.Clear(); } } } void swap(TMaybe& other) { Swap(other); } private: constexpr const T* Data() const noexcept { return std::addressof(this->Data_); } constexpr T* Data() noexcept { return std::addressof(this->Data_); } template void Init(Args&&... args) { new (Data()) T(std::forward(args)...); this->Defined_ = true; } }; template using TMaybeFail = TMaybe; template constexpr TMaybe, TPolicy> MakeMaybe(T&& value) { return TMaybe, TPolicy>(std::forward(value)); } template constexpr TMaybe MakeMaybe(TArgs&&... args) { return TMaybe(typename TMaybe::TInPlace{}, std::forward(args)...); } template constexpr TMaybe MakeMaybe(std::initializer_list il, TArgs&&... args) { return TMaybe(typename TMaybe::TInPlace{}, il, std::forward(args)...); } template void Swap(TMaybe& lhs, TMaybe& rhs) { lhs.Swap(rhs); } template void swap(TMaybe& lhs, TMaybe& rhs) { lhs.Swap(rhs); } template struct THash> { constexpr size_t operator()(const TMaybe& data) const { return (data.Defined()) ? THash()(data.GetRef()) : 42; } }; // Comparisons between TMaybe template constexpr bool operator==(const ::TMaybe& left, const ::TMaybe& right) { return (static_cast(left) != static_cast(right)) ? false : ( !static_cast(left) ? true : *left == *right); } template constexpr bool operator!=(const TMaybe& left, const TMaybe& right) { return !(left == right); } template constexpr bool operator<(const TMaybe& left, const TMaybe& right) { return (!static_cast(right)) ? false : ( !static_cast(left) ? true : (*left < *right)); } template constexpr bool operator>(const TMaybe& left, const TMaybe& right) { return right < left; } template constexpr bool operator<=(const TMaybe& left, const TMaybe& right) { return !(right < left); } template constexpr bool operator>=(const TMaybe& left, const TMaybe& right) { return !(left < right); } // Comparisons with TNothing template constexpr bool operator==(const TMaybe& left, TNothing) noexcept { return !static_cast(left); } template constexpr bool operator==(TNothing, const TMaybe& right) noexcept { return !static_cast(right); } template constexpr bool operator!=(const TMaybe& left, TNothing) noexcept { return static_cast(left); } template constexpr bool operator!=(TNothing, const TMaybe& right) noexcept { return static_cast(right); } template constexpr bool operator<(const TMaybe&, TNothing) noexcept { return false; } template constexpr bool operator<(TNothing, const TMaybe& right) noexcept { return static_cast(right); } template constexpr bool operator<=(const TMaybe& left, TNothing) noexcept { return !static_cast(left); } template constexpr bool operator<=(TNothing, const TMaybe&) noexcept { return true; } template constexpr bool operator>(const TMaybe& left, TNothing) noexcept { return static_cast(left); } template constexpr bool operator>(TNothing, const TMaybe&) noexcept { return false; } template constexpr bool operator>=(const TMaybe&, TNothing) noexcept { return true; } template constexpr bool operator>=(TNothing, const TMaybe& right) noexcept { return !static_cast(right); } // Comparisons with T template constexpr bool operator==(const TMaybe& maybe, const T& value) { return static_cast(maybe) ? *maybe == value : false; } template constexpr bool operator==(const T& value, const TMaybe& maybe) { return static_cast(maybe) ? *maybe == value : false; } template constexpr bool operator!=(const TMaybe& maybe, const T& value) { return static_cast(maybe) ? !(*maybe == value) : true; } template constexpr bool operator!=(const T& value, const TMaybe& maybe) { return static_cast(maybe) ? !(*maybe == value) : true; } template constexpr bool operator<(const TMaybe& maybe, const T& value) { return static_cast(maybe) ? std::less{}(*maybe, value) : true; } template constexpr bool operator<(const T& value, const TMaybe& maybe) { return static_cast(maybe) ? std::less{}(value, *maybe) : false; } template constexpr bool operator<=(const TMaybe& maybe, const T& value) { return !(maybe > value); } template constexpr bool operator<=(const T& value, const TMaybe& maybe) { return !(value > maybe); } template constexpr bool operator>(const TMaybe& maybe, const T& value) { return static_cast(maybe) ? value < maybe : false; } template constexpr bool operator>(const T& value, const TMaybe& maybe) { return static_cast(maybe) ? maybe < value : true; } template constexpr bool operator>=(const TMaybe& maybe, const T& value) { return !(maybe < value); } template constexpr bool operator>=(const T& value, const TMaybe& maybe) { return !(value < maybe); } // Comparison with values convertible to T template ::value, int> = 0> constexpr bool operator==(const ::TMaybe& maybe, const U& value) { return static_cast(maybe) ? *maybe == value : false; } template ::value, int> = 0> constexpr bool operator==(const U& value, const ::TMaybe& maybe) { return static_cast(maybe) ? *maybe == value : false; } template ::value, int> = 0> constexpr bool operator!=(const TMaybe& maybe, const U& value) { return static_cast(maybe) ? !(*maybe == value) : true; } template ::value, int> = 0> constexpr bool operator!=(const U& value, const TMaybe& maybe) { return static_cast(maybe) ? !(*maybe == value) : true; } template ::value, int> = 0> constexpr bool operator<(const TMaybe& maybe, const U& value) { return static_cast(maybe) ? std::less{}(*maybe, value) : true; } template ::value, int> = 0> constexpr bool operator<(const U& value, const TMaybe& maybe) { return static_cast(maybe) ? std::less{}(value, *maybe) : false; } template ::value, int> = 0> constexpr bool operator<=(const TMaybe& maybe, const U& value) { return !(maybe > value); } template ::value, int> = 0> constexpr bool operator<=(const U& value, const TMaybe& maybe) { return !(value > maybe); } template ::value, int> = 0> constexpr bool operator>(const TMaybe& maybe, const U& value) { return static_cast(maybe) ? value < maybe : false; } template ::value, int> = 0> constexpr bool operator>(const U& value, const TMaybe& maybe) { return static_cast(maybe) ? maybe < value : true; } template ::value, int> = 0> constexpr bool operator>=(const TMaybe& maybe, const U& value) { return !(maybe < value); } template ::value, int> = 0> constexpr bool operator>=(const U& value, const TMaybe& maybe) { return !(value < maybe); } class IOutputStream; template inline IOutputStream& operator<<(IOutputStream& out, const TMaybe& maybe) { if (maybe.Defined()) { out << *maybe; } else { out << TStringBuf("(empty maybe)"); } return out; }