37 #ifndef HERMES_HERMES_COMMON_OPTIONAL_H
38 #define HERMES_HERMES_COMMON_OPTIONAL_H
76 new(
reinterpret_cast<T *
>(&value_)) T(v);
81 new(
reinterpret_cast<T *
>(&value_)) T(std::move(v));
94 *
this = std::move(other);
108 if (other.has_value_) {
110 new(
reinterpret_cast<T *
>(&value_)) T(other.
value());
119 if (other.has_value_) {
121 new(
reinterpret_cast<T *
>(&value_)) T(std::move(other.value()));
130 new(
reinterpret_cast<T *
>(&value_)) T(v);
139 new(
reinterpret_cast<T *
>(&value_)) T(std::move(v));
177 return *
reinterpret_cast<T *
>(&value_);
182 return *
reinterpret_cast<const T *
>(&value_);
185 bool has_value_{
false};
186 typename std::aligned_storage<
sizeof(T),
alignof(T)>::type value_;
198 inline std::ostream &operator<<(std::ostream &o,
const Optional<T> &optional) {
200 return o <<
"Optional<" <<
typeid(T).name() <<
"> = " << optional.
hasValue();
201 return o <<
"Optional<" <<
typeid(T).name() <<
"> = [no value]";
Works just as std::optional, but supports GPU code. It may contain a value or not.
Definition: optional.h:65
HERMES_DEVICE_CALLABLE const T & operator*() const
Gets value const reference.
Definition: optional.h:155
HERMES_DEVICE_CALLABLE Optional & operator=(const Optional &other)
Copy assignment.
Definition: optional.h:106
HERMES_DEVICE_CALLABLE bool hasValue() const
Checks if this holds a value.
Definition: optional.h:168
HERMES_DEVICE_CALLABLE T & operator*()
Gets value reference.
Definition: optional.h:152
HERMES_DEVICE_CALLABLE Optional(const Optional &other)
Copy constructor.
Definition: optional.h:88
HERMES_DEVICE_CALLABLE Optional & operator=(T &&v)
Move value assignment.
Definition: optional.h:137
HERMES_DEVICE_CALLABLE const T * operator->() const
Gets const value pointer.
Definition: optional.h:149
HERMES_DEVICE_CALLABLE Optional & operator=(Optional &&other) noexcept
Move assinment.
Definition: optional.h:117
HERMES_DEVICE_CALLABLE const T & value() const
Gets value's const reference.
Definition: optional.h:181
HERMES_DEVICE_CALLABLE Optional & operator=(const T &v)
Value assignment.
Definition: optional.h:128
HERMES_DEVICE_CALLABLE T & value()
Gets value's reference.
Definition: optional.h:176
HERMES_DEVICE_CALLABLE void reset()
Destroys stored value (if present)
Definition: optional.h:160
HERMES_DEVICE_CALLABLE Optional(T &&v)
Move value constructor.
Definition: optional.h:80
HERMES_DEVICE_CALLABLE Optional(const T &v)
Value constructor.
Definition: optional.h:75
HERMES_DEVICE_CALLABLE Optional()
Default constructor.
Definition: optional.h:72
HERMES_DEVICE_CALLABLE Optional(Optional &&other) noexcept
Move constructor.
Definition: optional.h:93
HERMES_DEVICE_CALLABLE T valueOr(const T &v) const
Gets value copy (if present)
Definition: optional.h:173
HERMES_DEVICE_CALLABLE T * operator->()
Gets value pointer.
Definition: optional.h:146
#define HERMES_DEVICE_CALLABLE
Specifies that the function can be called from both host and device sides.
Definition: defs.h:45