52#include <initializer_list>
89 template<Type::SafeComponent T>
116 template<class... Args>
117 requires
Type::ConstructibleFrom<T, Args...>
120 emplace(std::forward<Args>(args)...);
132 template<
class U,
class... Args>
134 explicit Optional(std::in_place_t tag, std::initializer_list<U> values, Args&&... args):
Optional() {
136 emplace(values, std::forward<Args>(args)...);
266 (!
Type::SameAs<U,
std::nullopt_t>) &&
267 Type::ConstructibleFrom<T, U>
313 bool has_value() const noexcept {
return m_value !=
nullptr; }
319 explicit operator bool() const noexcept {
return has_value(); }
331 iterator end() noexcept {
return m_value ==
nullptr ? nullptr : m_value + 1; }
392 requires
Type::ConvertibleTo<U, T>
394 return has_value() ? *m_value : T(std::forward<U>(fallback));
406 return has_value() ? T(std::move(*m_value)) : T(std::forward<U>(fallback));
435 const T&& operator*() const &&;
449 const T* operator->() const;
456 bool operator==(const
Optional& other) const requires
Type::EqualityComparable<T> {
457 return has_value() == other.has_value() && (!
has_value() || *m_value == *other.m_value);
466 return !(*
this == other);
487 return has_value() ? std::strong_ordering::greater : std::strong_ordering::equivalent;
528 requires(
const T& left,
const U& right) { left <=> right; }
530 using Result = std::common_comparison_category_t<decltype(std::declval<const T&>() <=> std::declval<const U&>())>;
533 return static_cast<Result
>(*m_value <=>
value);
542 using Result = std::common_comparison_category_t<decltype(std::declval<const T&>() <=> std::declval<const T&>())>;
544 return static_cast<Result
>(*m_value <=> *other.m_value);
546 return Result::greater;
547 if (other.has_value())
549 return Result::equivalent;
574 return self == other;
584 requires requires(
const T& left,
const U& right) { left <=> right; }
586 using Result = std::common_comparison_category_t<decltype(std::declval<const T&>() <=> std::declval<const U&>())>;
588 return static_cast<Result
>(*m_value <=> *other);
590 return Result::greater;
591 if (other.has_value())
593 return Result::equivalent;
605 using Result = std::remove_cvref_t<std::invoke_result_t<F, T&>>;
608 return std::invoke(std::forward<F>(function), *m_value);
620 using Result = std::remove_cvref_t<std::invoke_result_t<F, const T&>>;
623 return std::invoke(std::forward<F>(function),
static_cast<const T&
>(*m_value));
635 using Result = std::remove_cvref_t<std::invoke_result_t<F, T&&>>;
638 return std::invoke(std::forward<F>(function), std::move(*m_value));
650 using Result = std::remove_cvref_t<std::invoke_result_t<F, T&>>;
665 using Result = std::remove_cvref_t<std::invoke_result_t<F, const T&>>;
668 return Optional<Result>(std::invoke(std::forward<F>(function),
static_cast<const T&
>(*m_value)));
680 using Result = std::remove_cvref_t<std::invoke_result_t<F, T&&>>;
683 return Optional<Result>(std::invoke(std::forward<F>(function), std::move(*m_value)));
697 return std::invoke(std::forward<F>(function));
710 return std::move(*
this);
711 return std::invoke(std::forward<F>(function));
721 template<
class... Args>
723 T* created = Make(std::forward<Args>(args)...);
738 template<
class U,
class... Args>
739 T&
emplace(std::initializer_list<U> values, Args&&... args) {
740 T* created = Make(values, std::forward<Args>(args)...);
758 return std::optional<T>(*m_value);
769 template<
class... Args>
770 static T* Make(Args&&... args) {
773 return new (block) T(std::forward<Args>(args)...);
798 template<SafeComponent T>
799 requires Type::IsSafe<T>::value
806 template<SafeComponent T>
814 template<SafeComponent T>
821 template<SafeComponent T>
830template<StormByte::Type::SafeComponent T>
838 if (!value.has_value())
848template<StormByte::Type::SafeComponent T>
std::optional stored on Base's heap.
Definition optional.hxx:90
T value_type
Contained type.
Definition optional.hxx:92
Optional & operator=(std::optional< T > &&value)
Move-assign a caller-owned STL optional.
T & value() &
Return the contained value.
Optional(std::optional< U > &&value)
Move a caller-owned STL optional of a convertible value.
Definition optional.hxx:203
friend void swap(Optional &left, Optional &right) noexcept
Exchange two optionals.
Definition optional.hxx:307
~Optional() noexcept
Destroy a held value and release its Base block.
T value_or(U &&fallback) const &
Return the contained value or a fallback.
Definition optional.hxx:393
void swap(Optional &other) noexcept
Exchange contained blocks.
bool operator!=(const Optional &other) const
Compare states and, when present, values.
Definition optional.hxx:465
T & operator*() &
Access the contained value.
Optional(const std::optional< T > &value)
Copy a caller-owned STL optional into Base storage.
Optional or_else(F &&function) const &
Return this optional when engaged, otherwise invoke a fallback.
Definition optional.hxx:694
auto transform(F &&function) &
Map the contained value to another Safe component.
Definition optional.hxx:649
Optional(U &&value)
Construct T from a value that can construct it.
Definition optional.hxx:164
T & reference
Mutable contained value.
Definition optional.hxx:95
auto transform(F &&function) &&
Map an rvalue contained value to another Safe component.
Definition optional.hxx:679
friend bool operator==(std::nullopt_t, const Optional &self) noexcept
Compare the empty tag with this optional.
Definition optional.hxx:480
const_iterator cend() const noexcept
Return the end read-only iterator.
Definition optional.hxx:355
T & emplace(std::initializer_list< U > values, Args &&... args)
Construct or replace T from an initializer list.
Definition optional.hxx:739
auto and_then(F &&function) &&
Invoke a function on an rvalue value and return its optional result.
Definition optional.hxx:634
Optional(std::optional< T > &&value)
Move a caller-owned STL optional into Base storage.
T value_or(U &&fallback) &&
Return the contained value or a fallback from an rvalue.
Definition optional.hxx:405
STORMBYTE_FORCE_INLINE auto operator<=>(const U &value) const
Order against a Safe component.
Definition optional.hxx:529
Optional(const Optional &other)
Copy the contained value into a new Base block.
STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(std::nullopt_t) const noexcept
Order empty before engaged.
Definition optional.hxx:486
Optional(const T &value)
Copy a value into Base storage.
bool operator==(const U &value) const
Compare the contained value with a Safe component.
Definition optional.hxx:500
Optional(std::in_place_t tag, std::initializer_list< U > values, Args &&... args)
Construct T from an initializer list and trailing arguments.
Definition optional.hxx:134
Optional(const std::optional< U > &value)
Copy a caller-owned STL optional of a convertible value.
Definition optional.hxx:190
auto and_then(F &&function) &
Invoke a function on the value and return its optional result.
Definition optional.hxx:604
const T * const_iterator
Read-only iterator over the single value, or end when empty.
Definition optional.hxx:94
iterator end() noexcept
Return the end mutable iterator.
Definition optional.hxx:331
const T & value() const &
Return the contained value.
STORMBYTE_FORCE_INLINE auto operator<=>(const Optional &other) const
Order empty before engaged, then order the values.
Definition optional.hxx:541
Optional() noexcept
Construct an empty optional.
Optional & operator=(const std::optional< T > &value)
Copy-assign a caller-owned STL optional.
T & emplace(Args &&... args)
Construct or replace the contained value.
Definition optional.hxx:722
friend bool operator==(const std::optional< U > &other, const Optional &self)
Compare a caller-owned STL optional with this optional.
Definition optional.hxx:573
auto and_then(F &&function) const &
Invoke a function on the value and return its optional result.
Definition optional.hxx:619
Optional(Optional &&other) noexcept
Take the contained block.
auto transform(F &&function) const &
Map the contained value to another Safe component.
Definition optional.hxx:664
Optional or_else(F &&function) &&
Return this rvalue optional, or invoke a fallback.
Definition optional.hxx:708
const_iterator end() const noexcept
Return the end read-only iterator.
Definition optional.hxx:343
void reset() noexcept
Destroy a held value and release its block.
STORMBYTE_FORCE_INLINE auto operator<=>(const std::optional< U > &other) const
Order against a caller-owned STL optional.
Definition optional.hxx:585
const T & const_reference
Read-only contained value.
Definition optional.hxx:96
const_iterator begin() const noexcept
Return the read-only iterator, or end when empty.
Definition optional.hxx:337
friend bool operator==(const U &value, const Optional &self)
Compare a Safe component with this optional.
Definition optional.hxx:515
Optional & operator=(std::nullopt_t value) noexcept
Reset this optional.
bool has_value() const noexcept
Test whether a value is held.
Definition optional.hxx:313
Optional(T &&value)
Move a value into Base storage.
const_iterator cbegin() const noexcept
Return the read-only iterator.
Definition optional.hxx:349
iterator begin() noexcept
Return the mutable iterator, or end when empty.
Definition optional.hxx:325
T * iterator
Mutable iterator over the single value, or end when empty.
Definition optional.hxx:93
const T & operator*() const &
Access the contained value.
bool operator==(const std::optional< U > &other) const
Compare with a caller-owned STL optional.
Definition optional.hxx:560
bool operator==(std::nullopt_t) const noexcept
Compare with the empty tag.
Definition optional.hxx:473
Recognizes a monadic result that can report an empty state.
Definition optional.hxx:77
Type constructible from Args (std::constructible_from).
Definition object_semantics.hxx:356
From is implicitly convertible to To (std::is_convertible).
Definition conversions.hxx:74
Type whose == and != both yield something convertible to bool.
Definition comparison.hxx:81
Regularly invocable with argument types Args (std::invocable).
Definition relations.hxx:94
Recognizes a conditional Safe component after stripping cv and references.
Definition safe.hxx:301
std::optional<U> or a Safe optional wrapper.
Definition wrappers.hxx:152
Recognized Safe component after stripping cv and references.
Definition safe.hxx:311
Same type after stripping cv and references from both sides.
Definition relations.hxx:139
Type whose <=> operator is well-formed.
Definition comparison.hxx:97
Private Base heap used by StormByte::Safe::Shared, StormByte::Safe::Unique and StormByte::Safe::Clona...
void * Allocate(std::size_t bytes)
Allocate bytes on Base's heap.
Owned values that cross a DLL without the caller's CRT.
STORMBYTE_FORCE_INLINE std::size_t HashCombine(std::size_t seed, std::size_t next) noexcept
Mix an existing hash with another.
Definition hash.hxx:83
@ Release
Release.
Definition memory_order.hxx:64
Root namespace of the StormByte suite.
Octet count. Defined in byte_size.hxx. Forwarded so Type::Numeral can name it.
STORMBYTE_FORCE_INLINE std::size_t operator()(const StormByte::Safe::Optional< T > &value) const noexcept
Hash value.
Definition optional.hxx:837
Cross-module hash.
Definition hash.hxx:95
Recognizes types whose DLL-boundary safety depends on documented provider requirements.
Definition safe.hxx:262
Identifies Safe optional wrappers.
Definition safe.hxx:329
Recognizes collection values with Base-controlled lifetime.
Definition safe.hxx:347
Recognizes Base Safe components, not universal ABI compatibility.
Definition safe.hxx:138
std::size_t operator()(const StormByte::Safe::Optional< T > &value) const noexcept
Hash value.
Definition optional.hxx:855
#define STORMBYTE_PUBLIC_TYPE
Definition visibility.h:57