8 #ifndef INLCUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_OPTIONAL_H
9 #define INLCUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_OPTIONAL_H
16 namespace Molassembler {
43 std::is_same<T, std::decay_t<T>>::
value,
44 "T must not be a reference or const-qualified type"
49 constexpr
explicit Optional(T
value) : value_(std::move(value)), hasValue_(true) {
51 std::is_same<T, std::decay_t<T>>::value,
52 "T must not be a reference or const-qualified type"
82 template<
class UnaryFunction>
83 constexpr
auto map(UnaryFunction&&
function)
const {
85 using U = decltype(
function(value_));
102 template<
class UnaryFunction>
103 constexpr
auto flatMap(UnaryFunction&&
function)
const {
105 using OptionalU = decltype(
function(value_));
108 return OptionalU {
function(value_)};
144 if(!hasValue_ && !other.hasValue_) {
148 if(hasValue_ && other.hasValue_) {
149 return value_ == other.value_;
157 return !(*
this == other);
163 if(!hasValue_ && !other.hasValue_) {
168 if(!hasValue_ && other.hasValue_) {
173 if(hasValue_ && !other.hasValue_) {
179 value_ < other.value_
185 return (other < *
this);
191 bool hasValue_ =
false;
234 template<
class UnaryFunction>
235 constexpr
auto map(UnaryFunction&&
function)
const {
237 using U = decltype(
function(ref_));
254 template<
class UnaryFunction>
255 constexpr
auto flatMap(UnaryFunction&&
function)
const {
257 using OptionalU = decltype(
function(ref_));
260 return OptionalU {
function(ref_)};
288 if(!hasValue_ && !other.hasValue_) {
292 if(hasValue_ && other.hasValue_) {
293 return ref_ == other.ref_;
301 return !(*
this == other);
307 if(!hasValue_ && !other.hasValue_) {
312 if(!hasValue_ && other.hasValue_) {
317 if(hasValue_ && !other.hasValue_) {
329 return (other < *
this);
336 bool hasValue_ =
false;
PURITY_WEAK constexpr bool operator>(const Optional &other) const
Lexicographical-like comparison.
Definition: Optional.h:184
PURITY_WEAK constexpr bool hasValue() const
Returns whether the optional contains a value.
Definition: Optional.h:215
#define PURITY_WEAK
Definition: Preprocessor.h:36
PURITY_WEAK constexpr bool operator==(const Optional &other) const
Compares on basis of contained value. Nones do compare equal.
Definition: Optional.h:143
An Option monadic type.
Definition: Optional.h:33
constexpr Optional(T value)
Value constructor.
Definition: Optional.h:49
constexpr Optional(T &value)
Value constructor.
Definition: Optional.h:206
constexpr auto flatMap(UnaryFunction &&function) const
Monadic bind with function of signature T -> Optional<U>
Definition: Optional.h:255
PURITY_WEAK constexpr bool hasValue() const
Returns whether the optional contains a value.
Definition: Optional.h:63
constexpr Optional()
Default constructor.
Definition: Optional.h:41
constexpr auto flatMap(UnaryFunction &&function) const
Monadic bind with function of signature T -> Optional<U>
Definition: Optional.h:103
PURITY_WEAK constexpr bool operator<(const Optional &other) const
Lexicographical-like comparison.
Definition: Optional.h:161
PURITY_WEAK constexpr bool operator!=(const Optional &other) const
Compares on basis of contained value. Nones do compare equal.
Definition: Optional.h:156
Defines a set of useful preprocessor macros.
PURITY_WEAK constexpr T & value() const
Returns the contained value unchecked.
Definition: Optional.h:224
PURITY_WEAK constexpr T value() const
Returns the contained value unchecked.
Definition: Optional.h:72
PURITY_WEAK constexpr T valueOr(const T &alternative) const
Returns a value if initialized, and another if not.
Definition: Optional.h:118
constexpr auto map(UnaryFunction &&function) const
Monadic bind with function of signature T -> U.
Definition: Optional.h:235
constexpr auto map(UnaryFunction &&function) const
Monadic bind with function of signature T -> U.
Definition: Optional.h:83
PURITY_WEAK constexpr T valueOr(const T &alternative) const
Returns a value if initialized, and another if not.
Definition: Optional.h:270
constexpr Optional & operator=(T assignment)
Assignment from T.
Definition: Optional.h:130
constexpr Optional()
Default constructor.
Definition: Optional.h:203