ROSE 0.11.145.192
|
Holds a value or nothing.
This class is similar to boost::optional except simpler in order to avoid problems we were seeing with Microsoft compilers.
The stored value type (Value) cannot be a reference type.
Definition at line 56 of file Optional.h.
#include <Sawyer/Optional.h>
Public Types | |
typedef T | Value |
Type of stored value. | |
Public Member Functions | |
Optional () | |
Default constructs nothing. | |
Optional (const Value &v) | |
Construct from value. | |
Optional (const Nothing &) | |
Construct from nothing. | |
Optional (const Optional &other) | |
Copy constructor. | |
~Optional () | |
Destructor. | |
Optional & | operator= (const Value &value) |
Value assignment. | |
Optional & | operator= (const Nothing &) |
Nothing assignment. | |
Optional & | operator= (const Optional &other) |
Optional assignment. | |
void | reset () |
Reset as if default-constructed. | |
Value | orDefault () const |
Obtain a value or a default. | |
template<class U > | |
bool | assignTo (U &out) const |
Conditionally save a value. | |
template<typename F > | |
auto | fmap (F &&f) -> Optional< decltype(f(get()))> |
Conditionally apply a functor f to the contents of an Optional. | |
operator unspecified_bool () const | |
Type for Boolean context. | |
const Value & | operator* () const |
Dereference to obtain value. | |
Value & | operator* () |
Dereference to obtain value. | |
const Value & | get () const |
Dereference to obtain value. | |
Value & | get () |
Dereference to obtain value. | |
const Value * | operator-> () const |
Obtain a pointer to the value. | |
Value * | operator-> () |
Obtain a pointer to the value. | |
const Value & | orElse (const Value &dflt) const |
Obtain value or something else. | |
const Value & | orElse (Value &dflt) |
Obtain value or something else. | |
const Optional | orElse (const Optional &other) const |
Obtain value or something else. | |
Optional | andThen (const Value &value) const |
If a value is present, return something else. | |
Optional | andTHen (Value &value) const |
If a value is present, return something else. | |
const Optional & | andThen (const Optional &other) const |
If a value is present, return something else. | |
bool | isEqual (const Optional &other) const |
Compare two values. | |
bool | isEqual (const Value &other) const |
Compare two values. | |
bool | isEqual (const Nothing &) const |
Compare two values. | |
typedef T Sawyer::Optional< T >::Value |
Type of stored value.
Definition at line 119 of file Optional.h.
|
inline |
Default constructs nothing.
Constructs an optional value that points to nothing. The default constructor for Value is not called.
Definition at line 124 of file Optional.h.
Referenced by Sawyer::Optional< T >::andThen(), and Sawyer::Optional< T >::andTHen().
|
inline |
Construct from value.
Constructs an optional object that holds a copy of v
.
Definition at line 129 of file Optional.h.
|
inline |
Construct from nothing.
Constructs an optional object that holds nothing. The default constructor for Value is not called.
Definition at line 136 of file Optional.h.
|
inline |
Copy constructor.
Copy constructs an optional so that the new object has the same state (nothing or not) as the source object. If the source contains an object (not nothing) then the object is copy constructed into this optional.
Definition at line 142 of file Optional.h.
|
inline |
Destructor.
The destructor invokes the destructor for the value if a value is stored, otherwise it does nothing.
Definition at line 153 of file Optional.h.
|
inline |
Value assignment.
Assigns the value
to this optional. If this optional previously contained a value then the Value assignment operator is used, otherwise the Value copy constructor is used.
Definition at line 164 of file Optional.h.
|
inline |
Nothing assignment.
Assigns nothing to this optional. If this optional previously contained a value then the value's destructor is called.
Definition at line 179 of file Optional.h.
|
inline |
Optional assignment.
Causes this optional to contain nothing or a copy of the other
value depending on whether other
contains a value or not. If this didn't contain a value but other
did, then the Value copy constructor is used; if both contained a value then the Value assignment operator is used; if this contained a value but other
doesn't, then the Value destructor is called.
Definition at line 194 of file Optional.h.
|
inline |
Reset as if default-constructed.
Definition at line 213 of file Optional.h.
|
inline |
Dereference to obtain value.
If this optional contains a value then a reference to that value is returned. Otherwise an std::domain_error
is thrown (the value is not in the container's domain).
Definition at line 223 of file Optional.h.
References Sawyer::Optional< T >::get().
|
inline |
Dereference to obtain value.
If this optional contains a value then a reference to that value is returned. Otherwise an std::domain_error
is thrown (the value is not in the container's domain).
Definition at line 226 of file Optional.h.
References Sawyer::Optional< T >::get().
|
inline |
Dereference to obtain value.
If this optional contains a value then a reference to that value is returned. Otherwise an std::domain_error
is thrown (the value is not in the container's domain).
Definition at line 229 of file Optional.h.
Referenced by Sawyer::Optional< T >::fmap(), Sawyer::Cached< T >::get(), Sawyer::Cached< T >::get(), Sawyer::Optional< T >::isEqual(), Sawyer::Optional< T >::isEqual(), Sawyer::Cached< T >::operator*(), Sawyer::Optional< T >::operator*(), Sawyer::Cached< T >::operator*(), Sawyer::Optional< T >::operator*(), Sawyer::Cached< T >::operator->(), Sawyer::Optional< T >::operator->(), Sawyer::Cached< T >::operator->(), and Sawyer::Optional< T >::operator->().
|
inline |
Dereference to obtain value.
If this optional contains a value then a reference to that value is returned. Otherwise an std::domain_error
is thrown (the value is not in the container's domain).
Definition at line 234 of file Optional.h.
|
inline |
Obtain a pointer to the value.
If this optional contains a value then a pointer to the value is returned. Otherwise an std::domain_error
is thrown (the value is not in the container's domain).
Definition at line 247 of file Optional.h.
References Sawyer::Optional< T >::get().
|
inline |
Obtain a pointer to the value.
If this optional contains a value then a pointer to the value is returned. Otherwise an std::domain_error
is thrown (the value is not in the container's domain).
Definition at line 250 of file Optional.h.
References Sawyer::Optional< T >::get().
|
inline |
Obtain value or something else.
Returns a reference to the contained value if it exists, otherwise returns a reference to the argument.
Definition at line 267 of file Optional.h.
Referenced by Sawyer::Attribute::Storage< SyncTag >::attributeOrElse().
|
inline |
Obtain value or something else.
Returns a reference to the contained value if it exists, otherwise returns a reference to the argument.
Definition at line 270 of file Optional.h.
|
inline |
Obtain value or something else.
Returns a reference to the contained value if it exists, otherwise returns a reference to the argument.
Definition at line 273 of file Optional.h.
|
inline |
If a value is present, return something else.
If this optional has a value, then return the argument, otherwise return nothing.
Definition at line 283 of file Optional.h.
References Sawyer::Optional< T >::Optional().
|
inline |
If a value is present, return something else.
If this optional has a value, then return the argument, otherwise return nothing.
Definition at line 286 of file Optional.h.
References Sawyer::Optional< T >::Optional().
|
inline |
If a value is present, return something else.
If this optional has a value, then return the argument, otherwise return nothing.
Definition at line 289 of file Optional.h.
|
inline |
Obtain a value or a default.
Returns a copy of the contained value if it exists, otherwise returns a default constructed value.
Definition at line 305 of file Optional.h.
Referenced by Sawyer::Attribute::Storage< SyncTag >::getAttribute().
|
inline |
Conditionally save a value.
If this optional object has a value then its value is written to out
and this method returns true; otherwise the value of out
is not changed and this method returns false. This method is convenient to use in conditional statements like this:
where the alternative would be
Definition at line 331 of file Optional.h.
Referenced by Rose::BinaryAnalysis::Partitioner2::GraphViz::BaseEmitter< G >::emitVertex().
|
inline |
Compare two values.
Compares two optionals and returns true if they are both empty or if neither is empty and their values compare equal. This method should be used instead of ==
. The ==
operator is disabled because it is prone to misuse in the presense of implicit conversion to bool
.
Definition at line 347 of file Optional.h.
References Sawyer::Optional< T >::get().
|
inline |
Compare two values.
Compares two optionals and returns true if they are both empty or if neither is empty and their values compare equal. This method should be used instead of ==
. The ==
operator is disabled because it is prone to misuse in the presense of implicit conversion to bool
.
Definition at line 350 of file Optional.h.
References Sawyer::Optional< T >::get().
|
inline |
Compare two values.
Compares two optionals and returns true if they are both empty or if neither is empty and their values compare equal. This method should be used instead of ==
. The ==
operator is disabled because it is prone to misuse in the presense of implicit conversion to bool
.
Definition at line 353 of file Optional.h.
|
inline |
Conditionally apply a functor f
to the contents of an Optional.
If this optional object has a value then fmap builds a new Optional using f
to construct the value. If it does not have a value, the new Optional is also empty.
Definition at line 365 of file Optional.h.
References Sawyer::Optional< T >::get().
|
inline |
Type for Boolean context.
Implicit conversion to a type that can be used in a boolean context such as an if
or while
statement. For instance:
Definition at line 389 of file Optional.h.