// Copyright 2025 Steven Le Rouzic // // SPDX-License-Identifier: BSD-3-Clause #pragma once #include "asl/base/assert.hpp" #include "asl/base/meta.hpp" #include "asl/base/functional.hpp" #include "asl/base/annotations.hpp" #include "asl/types/maybe_uninit.hpp" #include "asl/hashing/hash.hpp" namespace asl { struct nullopt_t {}; static constexpr nullopt_t nullopt{}; // @Todo(option) Reference // @Todo(option) Function // @Todo(option) Arrays template class option; namespace option_internal { template concept not_constructible_from_option = !constructible_from&> && !constructible_from&> && !constructible_from&&> && !constructible_from&&>; template concept not_assignable_from_option = !assignable_from&> && !assignable_from&> && !assignable_from&&> && !assignable_from&&>; template concept not_constructible_assignable_from_option = not_constructible_from_option && not_assignable_from_option; } // namespace option_internal template concept is_option = requires { typename T::type; requires same_as, option>; }; template class option { static constexpr bool kHasNiche = has_niche; using HasValueMarker = select_t; maybe_uninit m_payload{}; ASL_NO_UNIQUE_ADDRESS HasValueMarker m_has_value{}; template friend class option; template constexpr void construct(Args&&... args) { ASL_ASSERT(!has_value()); if constexpr (!kHasNiche) { m_payload.construct_unsafe(std::forward(args)...); m_has_value = true; } else { if constexpr (move_assignable) { m_payload.assign_unsafe(T{std::forward(args)...}); } else { m_payload.destroy_unsafe(); m_payload.construct_unsafe(std::forward(args)...); } } } template constexpr void assign(U&& arg) { ASL_ASSERT(has_value()); m_payload.assign_unsafe(std::forward(arg)); } public: using type = T; constexpr option() : option{nullopt} {} // NOLINTNEXTLINE(*explicit*) constexpr option(nullopt_t) requires (!kHasNiche) {} // NOLINTNEXTLINE(*explicit*) constexpr option(nullopt_t) requires kHasNiche : m_payload{in_place, niche_t{}} {} template constexpr explicit (!convertible_to) option(U&& value) requires ( kHasNiche && constructible_from && !same_as, option> ) : m_payload{in_place, std::forward(value)} {} template constexpr explicit (!convertible_to) option(U&& value) requires ( !kHasNiche && constructible_from && !is_option ) : m_payload{in_place, std::forward(value)} , m_has_value{true} {} constexpr option(const option& other) requires trivially_copy_constructible = default; constexpr option(const option& other) requires (!copy_constructible) = delete; constexpr option(const option& other) requires copy_constructible && (!trivially_copy_constructible) : option{nullopt} { if (other.has_value()) { construct(other.m_payload.as_init_unsafe()); } } constexpr option(option&& other) requires trivially_move_constructible = default; constexpr option(option&& other) requires (!move_constructible) = delete; constexpr option(option&& other) requires move_constructible && (!trivially_move_constructible) : option{nullopt} { if (other.has_value()) { construct(std::move(other.m_payload.as_init_unsafe())); } } template constexpr explicit (!convertible_to) option(const option& other) requires ( constructible_from && option_internal::not_constructible_from_option ) : option{nullopt} { if (other.has_value()) { construct(other.m_payload.as_init_unsafe()); } } template constexpr explicit (!convertible_to) option(option&& other) requires ( constructible_from && option_internal::not_constructible_from_option ) : option{nullopt} { if (other.has_value()) { construct(std::move(other).m_payload.as_init_unsafe()); } } constexpr option& operator=(nullopt_t) & { reset(); return *this; } template constexpr option& operator=(U&& value) & requires ( assignable_from && constructible_from && !is_option ) { if (has_value()) { assign(std::forward(value)); } else { construct(std::forward(value)); } return *this; } constexpr option& operator=(const option& other) & requires (!copy_assignable) = delete; constexpr option& operator=(const option& other) & requires trivially_copy_assignable = default; constexpr option& operator=(const option& other) & requires copy_assignable && (!trivially_copy_constructible) { if (&other == this) { return *this; } if (other.has_value()) { if (has_value()) { assign(other.m_payload.as_init_unsafe()); } else { construct(other.m_payload.as_init_unsafe()); } } else if (has_value()) { reset(); } return *this; } constexpr option& operator=(option&& other) & requires (!move_assignable) = delete; constexpr option& operator=(option&& other) & requires trivially_move_assignable = default; constexpr option& operator=(option&& other) & requires move_assignable && (!trivially_move_constructible) { if (&other == this) { return *this; } if (other.has_value()) { if (has_value()) { assign(std::move(other.m_payload.as_init_unsafe())); } else { construct(std::move(other.m_payload.as_init_unsafe())); } } else if (has_value()) { reset(); } return *this; } template constexpr option& operator=(const option& other) & requires ( constructible_from && assignable_from && option_internal::not_constructible_assignable_from_option ) { if (other.has_value()) { if (has_value()) { assign(other.m_payload.as_init_unsafe()); } else { construct(other.m_payload.as_init_unsafe()); } } else if (has_value()) { reset(); } return *this; } template constexpr option& operator=(option&& other) & requires ( constructible_from && assignable_from && option_internal::not_constructible_assignable_from_option ) { if (other.has_value()) { if (has_value()) { assign(std::move(other).m_payload.as_init_unsafe()); } else { construct(std::move(other).m_payload.as_init_unsafe()); } } else if (has_value()) { reset(); } return *this; } constexpr ~option() requires trivially_destructible = default; constexpr ~option() requires (!trivially_destructible) { reset(); } constexpr void reset() { if (!has_value()) { return; } if constexpr (kHasNiche) { if constexpr (move_assignable) { m_payload.assign_unsafe(std::move(T{niche_t{}})); } else { m_payload.destroy_unsafe(); m_payload.construct_unsafe(niche_t{}); } } else { m_has_value = false; m_payload.destroy_unsafe(); } } [[nodiscard]] constexpr bool has_value() const { if constexpr (kHasNiche) { return m_payload.as_init_unsafe() != niche_t{}; } else { return m_has_value; } } constexpr auto&& value(this auto&& self) { ASL_ASSERT_RELEASE(self.has_value()); return std::forward(self).m_payload.as_init_unsafe(); } template constexpr T value_or(U&& other_value) const& requires copy_constructible && convertible_to { return has_value() ? value() : static_cast(std::forward(other_value)); } template constexpr T value_or(U&& other_value) && requires move_constructible && convertible_to { return has_value() ? std::move(value()) : static_cast(std::forward(other_value)); } constexpr T& emplace(auto&&... args) & requires constructible_from { if (has_value()) { reset(); } construct(std::forward(args)...); return value(); } template constexpr auto and_then(this auto&& self, F&& f) { using Result = invoke_result_t>; static_assert(is_option); if (self.has_value()) { return invoke(std::forward(f), std::forward(self).value()); } return Result{ asl::nullopt }; } template constexpr auto transform(this auto&& self, F&& f) { using Result = invoke_result_t>; if (self.has_value()) { return option>{ invoke(std::forward(f), std::forward(self).value()) }; } return option>{ asl::nullopt }; } template constexpr option or_else(F&& f) const& requires same_as>, option> { return has_value() ? *this : invoke(std::forward(f)); } template constexpr option or_else(F&& f) && requires same_as>, option> { return has_value() ? std::move(*this) : invoke(std::forward(f)); } template requires (!uniquely_represented