5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_OPTIONAL_OPTIONAL_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_OPTIONAL_OPTIONAL_HPP_
24#include "arene/base/optional/detail/optional_base.hpp"
25#include "arene/base/optional/detail/optional_value_interface.hpp"
28#include "arene/base/stdlib_choice/declval.hpp"
29#include "arene/base/stdlib_choice/enable_if.hpp"
30#include "arene/base/stdlib_choice/forward.hpp"
31#include "arene/base/stdlib_choice/is_assignable.hpp"
32#include "arene/base/stdlib_choice/is_constructible.hpp"
33#include "arene/base/stdlib_choice/is_convertible.hpp"
34#include "arene/base/stdlib_choice/is_copy_assignable.hpp"
35#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
36#include "arene/base/stdlib_choice/is_move_assignable.hpp"
37#include "arene/base/stdlib_choice/is_move_constructible.hpp"
38#include "arene/base/stdlib_choice/is_same.hpp"
39#include "arene/base/stdlib_choice/move.hpp"
40#include "arene/base/stdlib_choice/remove_cv.hpp"
41#include "arene/base/stdlib_choice/remove_reference.hpp"
62struct unspecified_t {};
79 constexpr explicit nullopt_t(detail::unspecified_t)
noexcept {}
119 : optional_detail::optional_base<T>
122 ,
public optional_detail::optional_value_interface<optional<T>> {
125 using base_type = optional_detail::optional_base<T>;
130 using storage_type =
typename base_type::storage_type;
140 using interface_base = optional_detail::optional_value_interface<optional<T>>;
183 std::is_nothrow_move_constructible<T>::value
194 std::enable_if_t<
std::is_constructible<T, U&&>::value>> =
nullptr>
196 constexpr
optional(U&& other) noexcept(
std::is_nothrow_constructible<T, U&&>::value)
197 : base_type(
std::forward<U>(other)),
244 : base_type(in_place,
std::forward<Args>(args)...),
265 if (
this != &other) {
266 internal_do_assign(other);
294 if (this->has_value()) {
305 using base_type::has_value;
310 constexpr explicit operator bool() const noexcept {
return this->has_value(); }
320 constexpr auto operator*() const& noexcept -> T const& {
323 return this->storage().value;
328 return this->storage().value;
330 constexpr auto operator*() const&& noexcept -> T const&& {
439 template <
typename... Args>
456 template <
typename F>
461 "handle_value must return an optional"
465 template <
typename F>
470 "handle_value must return an optional"
474 template <
typename F>
479 "handle_value must return an optional"
483 template <
typename F>
488 "handle_value must return an optional"
515 "handle_null must be void invocable and return an optional with the same type as this."
527 "handle_null must be void invocable and return an optional with the same type as this."
547 template <
typename F>
552 "value_transformer must return a type that can be validly placed in an optional"
556 template <
typename F>
561 "value_transformer must return a type that can be validly placed in an optional"
565 template <
typename F>
570 "value_transformer must return a type that can be validly placed in an optional"
574 template <
typename F>
579 "value_transformer must return a type that can be validly placed in an optional"
607 if (this->has_value() && other.has_value()) {
609 }
else if (this->has_value()) {
612 }
else if (other.has_value()) {
644 using base_type::reset;
652 return lhs ? strong_ordering::greater : strong_ordering::equal;
665 template <
typename U, constra
ints<std::enable_if_t<!has_fast_inequality_check_v<T, U>>> =
nullptr>
683 template <
typename U, constra
ints<std::enable_if_t<has_fast_inequality_check_v<T, U>>> =
nullptr>
686 if (lhs.has_value() != rhs.has_value()) {
692 return T::fast_inequality_check(*lhs, *rhs);
719 return strong_ordering::greater;
722 return strong_ordering::less;
724 return strong_ordering::equal;
764 return T::fast_inequality_check(*lhs, rhs);
789 return strong_ordering::less;
794 ARENE_IGNORE_ALL(
"-Wfloat-equal",
"Can only suppress this here, not in user code; std::vector does the same thing");
795 ARENE_IGNORE_ALL(
"-Wsign-compare",
"Can only suppress this here, not in user code; std::vector does the same thing");
815 friend constexpr auto operator==(Self
const& lhs, U
const& rhs)
noexcept ->
bool {
897 return !(lhs == rhs);
912 void construct_from(Args&&... args)
noexcept {
914 this->storage().~storage_type();
916 this->set_has_value();
927 void construct_from(Args&&... args) {
934 auto make_empty_on_throw =
on_scope_exit([
this]()
noexcept { construct_at(&this->storage()); });
939 this->set_has_value();
940 make_empty_on_throw.cancel();
946 template <
typename OtherOptional>
947 constexpr void internal_do_assign(OtherOptional&& other)
noexcept(
948 std::is_nothrow_constructible<T, decltype(*std::forward<OtherOptional>(other))>::value &&
951 if (this->has_value() && other.has_value()) {
953 }
else if (this->has_value()) {
955 }
else if (other.has_value()) {
964 template <
typename U>
977 template <
typename Self,
typename U, constra
ints<std::enable_if_t<decays_to_v<Self, optional>>> =
nullptr>
978 static constexpr auto value_or(Self&& self, U&& default_value)
noexcept(
noexcept(T{default_value}
981 : static_cast<T>(
std::forward<U>(default_value));
992 template <
typename Self,
typename F, constra
ints<std::enable_if_t<decays_to_v<Self, optional>>> =
nullptr>
993 static constexpr auto value_or_else(Self&& self, F&& default_generator)
noexcept(
noexcept(T{
997 : static_cast<T>(::arene::base::
invoke(std::
forward<F>(default_generator)));
1010 template <
typename Self,
typename F, constra
ints<std::enable_if_t<decays_to_v<Self, optional>>> =
nullptr>
1011 static constexpr auto and_then(Self&& self, F&& handle_value)
1014 if (self.has_value()) {
1015 return ::arene::base::invoke(
1020 return f_optional_t{};
1032 template <
typename Self,
typename F, constra
ints<std::enable_if_t<decays_to_v<Self, optional>>> =
nullptr>
1034 if (self.has_value()) {
1051 template <
typename Self,
typename F, constra
ints<std::enable_if_t<decays_to_v<Self, optional>>> =
nullptr>
1052 static constexpr auto transform(Self&& self, F&& value_transformer)
1056 if (self.has_value()) {
1062 return optional_t{};
constexpr generic_ordering_from_three_way_compare_and_equals() noexcept=default
A class that has the same implicit constructors as another type.
Definition implicit_constructor_base.hpp:31
backport of std::in_place_t from C++17
Definition in_place.hpp:23
Empty class type used to indicate that arene::base::optional does not contain a value.
Definition optional.hpp:68
constexpr nullopt_t(detail::unspecified_t) noexcept
constructor for nullopt_t
Definition optional.hpp:79
Implementation of std::optional for pre-C++17 compilers, and which replaces defined undefined behavio...
Definition optional.hpp:122
ARENE_IGNORE_ALL("-Wfloat-equal", "Can only suppress this here, not in user code; std::vector does the same thing")
constexpr auto operator*() const &noexcept -> T const &
Accesses the value in the optional.
Definition optional.hpp:320
constexpr auto operator*() const &&noexcept -> T const &&
Accesses the value in the optional.
Definition optional.hpp:330
constexpr void emplace(Args &&... args) noexcept(noexcept(std::declval< optional< T > >().construct_from(std::forward< Args >(args)...)))
Destroy the old value if there is one, and construct a new value.
Definition optional.hpp:440
constexpr optional(optional &&) noexcept(//NOLINTNEXTLINE(hicpp-noexcept-move) std::is_nothrow_move_constructible< T >::value)=default
Move constructor.
constexpr auto operator=(optional &&other) noexcept(std::is_nothrow_move_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value) -> optional &
Move assign.
Definition optional.hpp:275
static constexpr auto fast_inequality_check(optional const &lhs, optional< U > const &rhs) noexcept -> inequality_heuristic
Fast inequality check between two optional instances, where there is not a fast inequality check betw...
Definition optional.hpp:666
friend constexpr auto operator==(Self const &lhs, optional< U > const &rhs) noexcept -> bool
equality comparison between two optional instances
Definition optional.hpp:849
constexpr optional(optional< U > &&other) noexcept(noexcept(T(std::declval< U && >())))
Construct from another optional.
Definition optional.hpp:212
constexpr auto value_or(U &&default_value) &&noexcept(noexcept(optional::value_or(std::declval< optional && >(), std::forward< U >(default_value)))) -> T
Gets the value held in the optional, or else the provided default.
Definition optional.hpp:393
friend constexpr auto operator==(Self const &lhs, U const &rhs) noexcept -> bool
equality comparison between an optional instance and a value
Definition optional.hpp:815
constexpr optional(in_place_t, Args &&... args) noexcept(noexcept(T(std::forward< Args >(args)...)))
Construct from a set of arguments.
Definition optional.hpp:243
constexpr auto and_then(F &&handle_value) const &-> invoke_result_t< F, T const & >
Monadic API which invokes a functor with the contents of value() if has_value() is true.
Definition optional.hpp:466
value_type value_type
Definition optional.hpp:148
constexpr optional(optional const &) noexcept(std::is_nothrow_copy_constructible< T >::value)=default
Copy constructor.
constexpr auto and_then(F &&handle_value) &-> invoke_result_t< F, T & >
Monadic API which invokes a functor with the contents of value() if has_value() is true.
Definition optional.hpp:457
static constexpr auto three_way_compare(optional const &lhs, nullopt_t) noexcept -> strong_ordering
Three-way comparison between an optional and nullopt_t.
Definition optional.hpp:651
friend constexpr auto operator!=(optional const &lhs, nullopt_t) noexcept -> bool
inequality comparison between an optional instance and an explicit nullopt
Definition optional.hpp:872
constexpr auto transform(F &&value_transformer) const &-> optional< invoke_result_t< F, T const & > >
Calls a provided functor with value if it is populated, and produces a new optional with the return f...
Definition optional.hpp:557
constexpr auto or_else(F &&handle_null) &&-> optional< T >
Monadic API which invokes a functor if has_value() is false.
Definition optional.hpp:524
constexpr auto transform(F &&value_transformer) const &&-> optional< invoke_result_t< F, T const && > >
Calls a provided functor with value if it is populated, and produces a new optional with the return f...
Definition optional.hpp:575
friend constexpr auto operator!=(Self const &lhs, optional< U > const &rhs) noexcept -> bool
inequality comparison between two optional instances
Definition optional.hpp:896
constexpr auto operator=(optional const &other) noexcept(std::is_nothrow_copy_constructible< T >::value &&std::is_nothrow_copy_assignable< T >::value) -> optional &
Copy assign.
Definition optional.hpp:263
static constexpr auto three_way_compare(Self const &lhs, optional< U > const &rhs) noexcept -> strong_ordering
Three-way comparison between two optional instances.
Definition optional.hpp:714
~optional()=default
Default destructor.
constexpr auto value_or(U &&default_value) const &noexcept(noexcept(optional::value_or(std::declval< optional const & >(), std::forward< U >(default_value)))) -> T
Gets the value held in the optional, or else the provided default.
Definition optional.hpp:382
constexpr auto and_then(F &&handle_value) const &&-> invoke_result_t< F, T const && >
Monadic API which invokes a functor with the contents of value() if has_value() is true.
Definition optional.hpp:484
auto operator=(U &&other) noexcept(std::is_nothrow_assignable< T, U >::value &&noexcept(std::declval< optional & >().construct_from(std::forward< U >(other)))) -> optional &
Assign from a value.
Definition optional.hpp:291
static constexpr auto three_way_compare(Self const &lhs, U const &rhs) noexcept -> strong_ordering
Three-way comparison between an optional instance and a value.
Definition optional.hpp:785
constexpr auto value_or_else(F &&default_generator) const &noexcept(noexcept(optional::value_or_else(std::declval< optional const & >(), std::forward< F >(default_generator)))) -> T
Gets the value held in the optional, or else invokes the provided callable to generate one.
Definition optional.hpp:416
ARENE_IGNORE_ALL("-Wsign-compare", "Can only suppress this here, not in user code; std::vector does the same thing")
friend constexpr auto operator==(optional const &lhs, nullopt_t) noexcept -> bool
equality comparison between an optional instance and an explicit nullopt
Definition optional.hpp:867
constexpr optional(nullopt_t) noexcept
Explicitly construct to an empty state.
Definition optional.hpp:168
constexpr auto transform(F &&value_transformer) &-> optional< invoke_result_t< F, T & > >
Calls a provided functor with value if it is populated, and produces a new optional with the return f...
Definition optional.hpp:548
constexpr auto transform(F &&value_transformer) &&-> optional< invoke_result_t< F, T && > >
Calls a provided functor with value if it is populated, and produces a new optional with the return f...
Definition optional.hpp:566
constexpr auto value_or_else(F &&default_generator) &&noexcept(noexcept(optional::value_or_else(std::declval< optional && >(), std::forward< F >(default_generator)))) -> T
Gets the value held in the optional, or else invokes the provided callable to generate one.
Definition optional.hpp:427
friend class optional
Definition optional.hpp:965
constexpr auto and_then(F &&handle_value) &&-> invoke_result_t< F, T && >
Monadic API which invokes a functor with the contents of value() if has_value() is true.
Definition optional.hpp:475
friend constexpr void swap(optional &lhs, optional &rhs) noexcept(noexcept(lhs.swap(rhs)))
swaps the elements between two optionals.
Definition optional.hpp:636
static constexpr auto fast_inequality_check(optional const &lhs, U const &rhs) noexcept -> inequality_heuristic
Fast inequality check between an optional instance and a value, where there is a fast inequality chec...
Definition optional.hpp:760
friend constexpr auto operator!=(nullopt_t, optional const &rhs) noexcept -> bool
inequality comparison between an explicit nullopt and an optional instance
Definition optional.hpp:877
constexpr auto operator*() &&noexcept -> T &&
Accesses the value in the optional.
Definition optional.hpp:337
constexpr auto or_else(F &&handle_null) const &-> optional< T >
Monadic API which invokes a functor if has_value() is false.
Definition optional.hpp:512
constexpr auto operator=(nullopt_t) noexcept -> optional &
Reset via assignment.
Definition optional.hpp:253
constexpr optional(optional< U > const &other) noexcept(noexcept(T(std::declval< U const & >())))
Construct from another optional.
Definition optional.hpp:229
constexpr auto operator->() const noexcept -> T const *
Access a pointer to the value in the optional.
Definition optional.hpp:354
static constexpr auto fast_inequality_check(optional const &lhs, U const &) noexcept -> inequality_heuristic
Fast inequality check between an optional instance and a value, where there is not a fast inequality ...
Definition optional.hpp:741
constexpr void swap(optional &other) noexcept(//is_nothrow_swappable_v< U > &&std::is_nothrow_move_constructible< U >::value)
Exchanges the state of two optionals.
Definition optional.hpp:604
constexpr optional() noexcept
Default construct to an empty state.
Definition optional.hpp:154
constexpr auto operator*() &noexcept -> T &
Accesses the value in the optional.
Definition optional.hpp:325
constexpr auto operator->() noexcept -> T *
Access a pointer to the value in the optional.
Definition optional.hpp:358
static constexpr value_type value
Definition integral_constant.hpp:34
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
#define ARENE_CPP14_INLINE_VARIABLE_INIT(name,...)
Emulates C++17's inline variable declarations for an arbitrary initializer expression.
Definition cpp14_inline.hpp:95
Provides the decays_to_v<T, U> trait.
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
Implementation of a backport of std::forward_like.
Provides backports of std::in_place_t, std::in_place_type_t, and std::in_place_index_t from C++17.
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr bool is_nothrow_swappable_v
Trait query indicating if a type can be noexcept-swapped.
constexpr auto move(InIterator first, InIterator last, OutIterator output) noexcept(noexcept(*first++) &&noexcept(output++) &&//CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) noexcept(first !=last) &&//CODEQLFP(A5-2-6) std::is_nothrow_assignable<, typename std::iterator_traits< InIterator >::value_type >::value) -> OutIterator
Moves the elements in the range [first, last), to another range beginning at output,...
Definition move.hpp:46
constexpr bool is_instantiation_of_v
Definition is_instantiation_of.hpp:16
constexpr bool is_invocable_v
Determine if a functor type is invocable with the specified argument types.
strong_ordering_detail::strong_ordering strong_ordering
The enumeration specifying the result of an ordering comparison.
Definition strong_ordering.hpp:147
constexpr bool decays_to_v
Determine if one type decays to be the same as another type.
three_way_compare_detail::compare_three_way compare_three_way
Function object that does a three-way comparison between operands, equivalent to std::compare_three_w...
Definition compare_three_way.hpp:214
constexpr bool substitution_succeeds
Determines if a template can be instantiated successfully, similar to C++20's requires clause.
typename invoke_result< Fn, Args... >::type invoke_result_t
The return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:271
constexpr auto invoke(F &&func, Args &&... args) noexcept(is_nothrow_invocable_v< F, Args... >) ->
Implementation of std::invoke from C++23: invokes the specified callable with the specified arguments...
Definition invoke.hpp:281
nullopt_t null_opt_t
alias to maintain backwards compatibility with older versions of Arene Base.
Definition optional.hpp:85
constexpr bool has_overloaded_address_operator_v
Type trait to check if a type has an overloaded address operator; the value is true if the type has a...
ARENE_NODISCARD auto on_scope_exit(CallableType &&func) noexcept(std::is_nothrow_constructible< remove_cvref_t< CallableType >, CallableType >::value) -> scope_guard< remove_cvref_t< CallableType > >
Declarative helper for constructing scope_guard instances.
Definition scope_guard.hpp:122
constexpr auto forward_like
A backport of C++23's std::forward_like, which allows forwarding a type with the same reference and c...
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
inequality_heuristic
An enumeration used for the return value of fast_inequality_check functions to indicate if two values...
Definition operators.hpp:696
@ may_be_equal_or_not_equal
The values checked may or may not be equal to each other; a more detailed check is required.
Definition operators.hpp:700
@ definitely_not_equal
The two values checked are definitely not equal to each other.
Definition operators.hpp:698
Definition accumulate.hpp:36
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24