4 #include "includes/kratos_export_api.h"
5 #include "includes/exception.h"
21 template <
class TElement>
25 using PointerInteger = std::uintptr_t;
34 static_assert(1 < std::alignment_of<TElement>(),
35 "OptionalReference cannot be used with types of size 1");
38 static_assert(
sizeof(PointerInteger) ==
sizeof(TElement*));
41 static_assert(std::alignment_of<OptionalRef>() == std::alignment_of<TElement*>());
43 mpElement.pointer = 0;
49 PointerInteger p =
reinterpret_cast<PointerInteger
>(&rElement);
50 mpElement.integer = p |
static_cast<PointerInteger
>(1);
64 PointerInteger p =
reinterpret_cast<PointerInteger
>(&rElement);
65 mpElement.integer = p |
static_cast<PointerInteger
>(1);
71 return mpElement.integer &
static_cast<PointerInteger
>(1);
75 explicit operator bool () const noexcept
85 KRATOS_ERROR <<
"bad optional access";
87 PointerInteger p = mpElement.integer & ~static_cast<PointerInteger>(1);
88 return *
reinterpret_cast<TElement*
>(p);
102 mpElement.integer = 0;
An optional, modeling a pointer.
Definition: OptionalRef.hpp:23
void reset() noexcept
Clear the optional.
Definition: OptionalRef.hpp:100
OptionalRef(OptionalRef &&rRhs) noexcept=default
OptionalRef(const OptionalRef &rRhs) noexcept=default
TElement * pointer
Definition: OptionalRef.hpp:107
OptionalRef & operator=(TElement &rElement) noexcept
Assign the provided instance to the optional.
Definition: OptionalRef.hpp:62
TElement & value_type
Definition: OptionalRef.hpp:28
bool has_value() const noexcept
Check whether the optional is valid.
Definition: OptionalRef.hpp:69
OptionalRef & operator=(const OptionalRef &rRhs) noexcept=default
TElement & value() const
Access the stored reference.
Definition: OptionalRef.hpp:82
OptionalRef() noexcept
Construct an invalid (uninitialized) optional.
Definition: OptionalRef.hpp:31
TElement & operator*() const noexcept
Access the stored reference.
Definition: OptionalRef.hpp:94
OptionalRef(TElement &rElement) noexcept
Construct an optional reference to the provided instance.
Definition: OptionalRef.hpp:47
PointerInteger integer
Definition: OptionalRef.hpp:108
OptionalRef & operator=(OptionalRef &&rRhs) noexcept=default
Definition: MPIUtils.hpp:9