4#ifndef GREM_CORE_DATA_OPTIONAL_HPP
5#define GREM_CORE_DATA_OPTIONAL_HPP
16#include <initializer_list>
44 [[nodiscard]] const
char*
what() const noexcept
override {
45 return "Bad optional access.";
83 template <
typename... Args>
84 constexpr explicit Optional(std::in_place_t tag, Args&&... args)
requires(std::is_constructible_v<T, Args...>)
85 : variant(std::in_place_type<T>, std::forward<Args>(args)...) {
102 template <
typename U,
typename... Args>
103 constexpr Optional(std::in_place_t tag, std::initializer_list<U> ilist, Args&&... args)
requires(std::is_constructible_v<T, std::initializer_list<U>&, Args...>)
104 : variant(std::in_place_type<T>, ilist, std::forward<Args>(args)...) {
116 template <
typename U = std::remove_cv_t<T>>
118 requires(!std::is_same_v<std::remove_cvref_t<U>, std::in_place_t> && !std::is_same_v<std::remove_cvref_t<U>,
Optional> &&
120 std::is_convertible_v<U &&, T>)
121 : variant(std::in_place_type<T>, std::forward<U>(
value)) {}
131 template <
typename U = std::remove_cv_t<T>>
133 requires(!std::is_same_v<std::remove_cvref_t<U>, std::in_place_t> && !std::is_same_v<std::remove_cvref_t<U>,
Optional> &&
135 !std::is_convertible_v<U &&, T>)
136 : variant(std::in_place_type<T>, std::forward<U>(
value)) {}
146 std::is_nothrow_move_constructible_v<T>) =
default;
149 template <
typename U>
151 requires((std::is_same_v<std::remove_cv_t<T>,
bool> ||
152 (!std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> &&
153 !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> &&
154 !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T>)) &&
155 std::is_constructible_v<T, const U&> && std::is_convertible_v<const U&, T>)
163 template <
typename U>
165 requires((std::is_same_v<std::remove_cv_t<T>,
bool> ||
166 (!std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> &&
167 !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> &&
168 !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T>)) &&
169 std::is_constructible_v<T, const U&> && !std::is_convertible_v<const U&, T>)
177 template <
typename U>
179 requires((std::is_same_v<std::remove_cv_t<T>,
bool> ||
180 (!std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> &&
181 !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> &&
182 !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T>)) &&
183 std::is_constructible_v<T, U &&> && std::is_convertible_v<U &&, T>)
191 template <
typename U>
193 requires((std::is_same_v<std::remove_cv_t<T>,
bool> ||
194 (!std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> &&
195 !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> &&
196 !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T>)) &&
197 std::is_constructible_v<T, U &&> && !std::is_convertible_v<U &&, T>)
222 template <
typename U = std::remove_cv_t<T>>
224 requires(!std::is_same_v<std::remove_cvref_t<U>,
Optional> && std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> &&
225 (!std::is_scalar_v<T> || !std::is_same_v<T, std::decay_t<U>>)) {
229#pragma warning(disable : 4267)
230#elif defined(__clang__)
231#pragma clang diagnostic push
232#pragma clang diagnostic ignored "-Wshorten-64-to-32"
233#pragma clang diagnostic ignored "-Wfloat-conversion"
234#elif defined(__GNUC__)
235#pragma GCC diagnostic push
236#pragma GCC diagnostic ignored "-Wconversion"
237#pragma GCC diagnostic ignored "-Wfloat-conversion"
239 **
this = std::forward<U>(
value);
242#elif defined(__clang__)
243#pragma clang diagnostic pop
244#elif defined(__GNUC__)
245#pragma GCC diagnostic pop
258 std::is_nothrow_move_constructible_v<T> &&
259 std::is_nothrow_move_assignable_v<T>) =
default;
262 template <
typename U>
264 requires(std::is_constructible_v<T, const U&> && std::is_assignable_v<T&, const U&> && !std::is_constructible_v<T, Optional<U>&> &&
265 !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> && !std::is_constructible_v<T, const Optional<U> &&> &&
266 !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> && !std::is_convertible_v<Optional<U> &&, T> &&
267 !std::is_convertible_v<const Optional<U> &&, T> && !std::is_assignable_v<T&, Optional<U>&> && !std::is_assignable_v<T&, const Optional<U>&> &&
268 !std::is_assignable_v<T&, Optional<U> &&> && !std::is_assignable_v<T&, const Optional<U> &&>) {
282 template <
typename U>
284 requires(std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> && !std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> &&
285 !std::is_constructible_v<T, Optional<U> &&> && !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> &&
286 !std::is_convertible_v<const Optional<U>&, T> && !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T> &&
287 !std::is_assignable_v<T&, Optional<U>&> && !std::is_assignable_v<T&, const Optional<U>&> && !std::is_assignable_v<T&, Optional<U> &&> &&
288 !std::is_assignable_v<T&, const Optional<U> &&>) {
291 **
this = std::move(**other);
308 [[nodiscard]]
constexpr const T*
operator->() const noexcept {
330 [[nodiscard]]
constexpr const T&
operator*() const& noexcept {
331 return variant.template as<T>();
342 return variant.template as<T>();
352 [[nodiscard]]
constexpr const T&&
operator*() const&& noexcept {
353 return std::move(variant.template as<T>());
364 return std::move(variant.template as<T>());
373 constexpr explicit operator bool() const noexcept {
383 [[nodiscard]]
constexpr bool has_value() const noexcept {
384 return variant.template is<T>();
394 [[nodiscard]]
constexpr T&
value() & {
408 [[nodiscard]]
constexpr const T&
value() const& {
422 [[nodiscard]]
constexpr T&&
value() && {
426 return std::move(**
this);
436 [[nodiscard]]
constexpr const T&&
value() const&& {
440 return std::move(**
this);
452 template <
typename U>
453 [[nodiscard]]
constexpr T
value_or(U&& defaultValue)
const& {
454 return (*
this) ? **this :
static_cast<T
>(std::forward<U>(defaultValue));
466 template <
typename U>
467 [[nodiscard]]
constexpr T
value_or(U&& defaultValue) && {
468 return (*
this) ? std::move(**
this) :
static_cast<T
>(std::forward<U>(defaultValue));
483 template <
typename F>
486 return std::invoke(std::forward<F>(f), **
this);
488 return std::remove_cvref_t<std::invoke_result_t<F, T&>>{};
503 template <
typename F>
506 return std::invoke(std::forward<F>(f), **
this);
508 return std::remove_cvref_t<std::invoke_result_t<F, const T&>>{};
523 template <
typename F>
526 return std::invoke(std::forward<F>(f), std::move(**
this));
528 return std::remove_cvref_t<std::invoke_result_t<F, T>>{};
543 template <
typename F>
546 return std::invoke(std::forward<F>(f), std::move(**
this));
548 return std::remove_cvref_t<std::invoke_result_t<F, const T>>{};
563 template <
typename F>
565 using U = std::remove_cv_t<std::invoke_result_t<F, T&>>;
567 return Optional<U>{std::in_place, std::invoke(std::forward<F>(f), **
this)};
584 template <
typename F>
586 using U = std::remove_cv_t<std::invoke_result_t<F, const T&>>;
588 return Optional<U>{std::in_place, std::invoke(std::forward<F>(f), **
this)};
605 template <
typename F>
607 using U = std::remove_cv_t<std::invoke_result_t<F, T>>;
609 return Optional<U>{std::in_place, std::invoke(std::forward<F>(f), std::move(**
this))};
626 template <
typename F>
628 using U = std::remove_cv_t<std::invoke_result_t<F, const T>>;
630 return Optional<U>{std::in_place, std::invoke(std::forward<F>(f), std::move(**
this))};
644 template <
typename F>
646 return (*
this) ? *this : std::invoke(std::forward<F>(f));
658 template <
typename F>
660 return (*
this) ? std::move(*
this) : std::invoke(std::forward<F>(f));
672 std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>) {
673 variant.swap(other.variant);
705 template <
typename... Args>
706 constexpr T&
emplace(Args&&... args)
requires(std::is_constructible_v<T, Args...>) {
707 return variant.template
emplace<1>(std::forward<Args>(args)...);
720 template <
typename U,
typename... Args>
721 constexpr T&
emplace(std::initializer_list<U> ilist, Args&&... args)
requires(std::is_constructible_v<T, std::initializer_list<U>&, Args...>) {
722 return variant.template
emplace<1>(ilist, std::forward<Args>(args)...);
741template <
typename T,
typename U>
746 return static_cast<bool>(a) ==
static_cast<bool>(b);
760 return !
static_cast<bool>(opt);
774 return !
static_cast<bool>(opt);
789template <
typename T,
typename U>
794#pragma warning(disable : 4389)
795#elif defined(__clang__)
796#pragma clang diagnostic push
797#pragma clang diagnostic ignored "-Wsign-compare"
798#elif defined(__GNUC__)
799#pragma GCC diagnostic push
800#pragma GCC diagnostic ignored "-Wsign-compare"
802 return (opt) ? *opt == value :
false;
805#elif defined(__clang__)
806#pragma clang diagnostic pop
807#elif defined(__GNUC__)
808#pragma GCC diagnostic pop
824template <
typename T,
typename U>
828#pragma clang diagnostic push
829#pragma clang diagnostic ignored "-Wsign-compare"
830#elif defined(__GNUC__)
831#pragma GCC diagnostic push
832#pragma GCC diagnostic ignored "-Wsign-compare"
834 return (opt) ? *opt == value :
false;
836#pragma clang diagnostic pop
837#elif defined(__GNUC__)
838#pragma GCC diagnostic pop
855template <
typename T,
typename U>
860 return static_cast<bool>(a) <=>
static_cast<bool>(b);
874 return static_cast<bool>(opt) <=>
false;
890template <
typename T,
typename U>
893 return (opt) ? *opt <=> value : std::strong_ordering::less;
909template <
typename T,
typename U>
912 return (opt) ? value <=> *opt : std::strong_ordering::greater;
918struct std::hash<
grem::Optional<T>> {
923 return static_cast<std::size_t
>(0xFFFFFFFFFFFFF2FBull);
927 [[no_unique_address]] std::hash<std::remove_const_t<T>> hasher;
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
constexpr void swap(Optional &other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_swappable_v< T >)
Swap this optional's value with that of another.
Definition Optional.hpp:671
constexpr ~Optional()=default
Destructor.
constexpr T && value() &&
Access the underlying value.
Definition Optional.hpp:422
constexpr T & emplace(std::initializer_list< U > ilist, Args &&... args)
Construct a new underlying value, with an initializer list as the first constructor argument,...
Definition Optional.hpp:721
constexpr T value_or(U &&defaultValue) const &
Get the underlying value, or a fallback value in case the optional is empty.
Definition Optional.hpp:453
constexpr Optional(std::in_place_t tag, Args &&... args)
Construct an optional with an active value in-place.
Definition Optional.hpp:84
constexpr Optional(Optional< U > &&other)
Implicit converting move constructor.
Definition Optional.hpp:178
constexpr Optional & operator=(U &&value)
Converting assignment.
Definition Optional.hpp:223
constexpr T & emplace(Args &&... args)
Construct a new underlying value, destroying the old value if present.
Definition Optional.hpp:706
constexpr T && operator*() &&noexcept
Access the underlying value without a safety check.
Definition Optional.hpp:363
constexpr Optional & operator=(Nullopt) noexcept
Destroy the underlying value if present and reset the optional to an empty state.
Definition Optional.hpp:208
constexpr Optional(const Optional &other)=default
Copy constructor.
constexpr Optional & operator=(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T > &&//NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor) std::is_nothrow_move_assignable_v< T >)=default
Move assignment.
constexpr bool has_value() const noexcept
Check if the optional contains a value.
Definition Optional.hpp:383
constexpr Optional(U &&value)
Implicit converting constructor.
Definition Optional.hpp:117
constexpr auto transform(F &&f) &&
Invoke a function with the underlying value, or return an empty optional in case the optional is empt...
Definition Optional.hpp:606
constexpr const T && value() const &&
Access the underlying value.
Definition Optional.hpp:436
constexpr auto and_then(F &&f) &
Invoke a function with the underlying value, or return a default-constructed result in case the optio...
Definition Optional.hpp:484
constexpr Optional(std::in_place_t tag, std::initializer_list< U > ilist, Args &&... args)
Construct an optional with an active value in-place, with an initializer list as the first constructo...
Definition Optional.hpp:103
constexpr void reset() noexcept
Destroy the underlying value if present and reset the optional to an empty state.
Definition Optional.hpp:693
constexpr auto transform(F &&f) const &&
Invoke a function with the underlying value, or return an empty optional in case the optional is empt...
Definition Optional.hpp:627
constexpr auto and_then(F &&f) const &
Invoke a function with the underlying value, or return a default-constructed result in case the optio...
Definition Optional.hpp:504
friend constexpr void swap(Optional &a, Optional &b) noexcept(noexcept(a.swap(b)))
Swap the values of two optionals.
Definition Optional.hpp:685
constexpr auto and_then(F &&f) &&
Invoke a function with the underlying value, or return a default-constructed result in case the optio...
Definition Optional.hpp:524
constexpr auto transform(F &&f) &
Invoke a function with the underlying value, or return an empty optional in case the optional is empt...
Definition Optional.hpp:564
constexpr T value_or(U &&defaultValue) &&
Get the underlying value, or a fallback value in case the optional is empty.
Definition Optional.hpp:467
constexpr const T * operator->() const noexcept
Access the underlying value by pointer without a safety check.
Definition Optional.hpp:308
constexpr Optional or_else(F &&f) const &
Get an optional containing the underlying value, or the result of a function in case the optional is ...
Definition Optional.hpp:645
constexpr T & operator*() &noexcept
Access the underlying value without a safety check.
Definition Optional.hpp:341
constexpr Optional(Nullopt value) noexcept
Construct an empty optional.
Definition Optional.hpp:70
constexpr Optional(U &&value)
Explicit converting constructor.
Definition Optional.hpp:132
constexpr Optional & operator=(const Optional< U > &other)
Converting copy assignment.
Definition Optional.hpp:263
constexpr const T & operator*() const &noexcept
Access the underlying value without a safety check.
Definition Optional.hpp:330
constexpr T * operator->() noexcept
Access the underlying value by pointer without a safety check.
Definition Optional.hpp:319
constexpr Optional(const Optional< U > &other)
Explicit converting copy constructor.
Definition Optional.hpp:164
constexpr Optional & operator=(const Optional &other)=default
Copy assignment.
constexpr auto transform(F &&f) const &
Invoke a function with the underlying value, or return an empty optional in case the optional is empt...
Definition Optional.hpp:585
constexpr const T && operator*() const &&noexcept
Access the underlying value without a safety check.
Definition Optional.hpp:352
constexpr Optional(Optional< U > &&other)
Explicit converting move constructor.
Definition Optional.hpp:192
constexpr Optional & operator=(Optional< U > &&other)
Converting move assignment.
Definition Optional.hpp:283
constexpr T & value() &
Access the underlying value.
Definition Optional.hpp:394
constexpr Optional(const Optional< U > &other)
Implicit converting copy constructor.
Definition Optional.hpp:150
constexpr Optional or_else(F &&f) &&
Get an optional containing the underlying value, or the result of a function in case the optional is ...
Definition Optional.hpp:659
constexpr Optional() noexcept
Construct an empty optional.
Definition Optional.hpp:63
constexpr const T & value() const &
Access the underlying value.
Definition Optional.hpp:408
constexpr auto and_then(F &&f) const &&
Invoke a function with the underlying value, or return a default-constructed result in case the optio...
Definition Optional.hpp:544
constexpr Optional(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T >)=default
Move constructor.
Tagged union value type that holds a value of one of the given types.
Definition Variant.hpp:293
Definition concepts.hpp:160
Definition concepts.hpp:41
Definition concepts.hpp:163
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
constexpr Nullopt nullopt
Unit value representing an empty Optional.
Definition Optional.hpp:35
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
Exception type that is thrown on an attempt to erroneously access an empty Optional when using a safe...
Definition Optional.hpp:41
BadOptionalAccess() noexcept=default
const char * what() const noexcept override
Definition Optional.hpp:44
Unit type for representing an empty Optional.
Definition Optional.hpp:25
constexpr Nullopt(int)
Constructor ensuring that Nullopt can only be constructed explicitly.
Definition Optional.hpp:29
std::size_t operator()(const grem::Optional< T > &opt) const
Definition Optional.hpp:919