POD-compatible optional wrapper.
More...
#include <jh/pods/optional.h>
|
|
using | value_type = T |
| | Alias of contained type.
|
|
|
constexpr | optional () noexcept=default |
| | Default constructor (empty state).
|
| void | store (const T &value) noexcept |
| | Store a value by copying raw memory.
|
|
void | clear () noexcept |
| | Clear the stored value (set to empty).
|
| T * | get () noexcept |
| | Get mutable pointer to stored value.
|
| const T * | get () const noexcept |
| | Get const pointer to stored value.
|
|
bool | has () const noexcept |
| | Whether a value is present.
|
|
bool | empty () const noexcept |
| | Whether the optional is empty.
|
| T & | ref () noexcept |
| | Access stored value by reference.
|
| const T & | ref () const noexcept |
| | Access stored value by const reference.
|
| T | value_or (T fallback) const noexcept |
| | Return stored value or fallback.
|
| constexpr bool | operator== (const optional &rhs) const noexcept |
| | Equality comparison with another optional.
|
|
|
std::byte | storage [sizeof(T)] |
| | Raw storage; flattens type ABI, never access directly.
|
|
bool | has_value |
| | Presence flag (true = has value).
|
template<cv_free_pod_like T>
struct jh::pod::optional< T >
POD-compatible optional wrapper.
Stores raw bytes for T and a boolean flag. Provides POD-level semantics similar to std::optional.
Equality Semantics:
-
If one has value and the other not → false
-
If both are empty → true (ignores raw storage)
-
If both have value → compare storage bytes (
memcmp)
Usage Model:
- Template Parameters
-
| T | Element type. Must satisfy pod_like<T> and be free of const/volatile qualifiers (i.e. satisfy cv_free_pod_like<T>). Using a cv-qualified type would make the optional itself non-POD due to loss of trivial assignment and standard layout guarantees. |
◆ get() [1/2]
template<cv_free_pod_like T>
Get const pointer to stored value.
- Returns
- Pointer to const
T, must check .has() before use.
◆ get() [2/2]
template<cv_free_pod_like T>
Get mutable pointer to stored value.
- Returns
- Pointer to
T, must check .has() before use.
◆ operator==()
template<cv_free_pod_like T>
Equality comparison with another optional.
Semantics are aligned with std::optional:
-
If one has a value and the other does not →
false
-
If both are empty →
true
-
If both have a value → compare the underlying storage bytes
- Parameters
-
| rhs | Other optional to compare with. |
- Returns
true if both optionals have the same state and (if present) identical raw byte content, false otherwise.
- Note
- This operator does not rely on
= default, because the default comparison would also require raw storage equality when has_value == false. That would force meaningless zeroing of storage in .clear(). Instead, we define comparison explicitly: empty optionals are always equal regardless of storage content.
-
Raw comparison is performed with
std::memcmp, ensuring POD-level semantics without invoking T::operator==.
◆ ref() [1/2]
template<cv_free_pod_like T>
Access stored value by const reference.
- Returns
- Const reference to
T. Undefined if .has() == false.
◆ ref() [2/2]
template<cv_free_pod_like T>
Access stored value by reference.
- Returns
- Reference to
T. Undefined if .has() == false.
◆ store()
template<cv_free_pod_like T>
Store a value by copying raw memory.
- Parameters
-
| value | Source value to copy. |
◆ value_or()
template<cv_free_pod_like T>
Return stored value or fallback.
- Parameters
-
| fallback | Value to return if empty. |
- Returns
- Copy of stored or fallback value.
The documentation for this struct was generated from the following file: