ROSE 0.11.145.147
Public Types | Public Member Functions | List of all members
Sawyer::Optional< T > Class Template Reference

Description

template<typename T>
class Sawyer::Optional< T >

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>

Inheritance diagram for Sawyer::Optional< T >:
Inheritance graph
[legend]

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.
 
Optionaloperator= (const Value &value)
 Value assignment.
 
Optionaloperator= (const Nothing &)
 Nothing assignment.
 
Optionaloperator= (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 Valueoperator* () const
 Dereference to obtain value.
 
Valueoperator* ()
 Dereference to obtain value.
 
const Valueget () const
 Dereference to obtain value.
 
Valueget ()
 Dereference to obtain value.
 
const Valueoperator-> () const
 Obtain a pointer to the value.
 
Valueoperator-> ()
 Obtain a pointer to the value.
 
const ValueorElse (const Value &dflt) const
 Obtain value or something else.
 
const ValueorElse (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 OptionalandThen (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.
 

Member Typedef Documentation

◆ Value

template<typename T >
typedef T Sawyer::Optional< T >::Value

Type of stored value.

Definition at line 119 of file Optional.h.

Constructor & Destructor Documentation

◆ Optional() [1/4]

template<typename T >
Sawyer::Optional< T >::Optional ( )
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().

◆ Optional() [2/4]

template<typename T >
Sawyer::Optional< T >::Optional ( const Value v)
inline

Construct from value.

Constructs an optional object that holds a copy of v.

Definition at line 129 of file Optional.h.

◆ Optional() [3/4]

template<typename T >
Sawyer::Optional< T >::Optional ( const Nothing )
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.

◆ Optional() [4/4]

template<typename T >
Sawyer::Optional< T >::Optional ( const Optional< T > &  other)
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.

◆ ~Optional()

template<typename T >
Sawyer::Optional< T >::~Optional ( )
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.

Member Function Documentation

◆ operator=() [1/3]

template<typename T >
Optional & Sawyer::Optional< T >::operator= ( const Value value)
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.

◆ operator=() [2/3]

template<typename T >
Optional & Sawyer::Optional< T >::operator= ( const Nothing )
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.

◆ operator=() [3/3]

template<typename T >
Optional & Sawyer::Optional< T >::operator= ( const Optional< T > &  other)
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.

◆ reset()

template<typename T >
void Sawyer::Optional< T >::reset ( )
inline

Reset as if default-constructed.

Definition at line 213 of file Optional.h.

◆ operator*() [1/2]

template<typename T >
const Value & Sawyer::Optional< T >::operator* ( ) const
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().

◆ operator*() [2/2]

template<typename T >
Value & 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 226 of file Optional.h.

References Sawyer::Optional< T >::get().

◆ get() [1/2]

template<typename T >
const Value & Sawyer::Optional< T >::get ( ) const
inline

◆ get() [2/2]

template<typename T >
Value & 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 234 of file Optional.h.

◆ operator->() [1/2]

template<typename T >
const Value * Sawyer::Optional< T >::operator-> ( ) const
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().

◆ operator->() [2/2]

template<typename T >
Value * Sawyer::Optional< T >::operator-> ( )
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().

◆ orElse() [1/3]

template<typename T >
const Value & Sawyer::Optional< T >::orElse ( const Value dflt) const
inline

Obtain value or something else.

Returns a reference to the contained value if it exists, otherwise returns a reference to the argument.

Object bar = ...;
Object foo = objects.getOptional(key).orElse(bar);
Optional<Object> baz = ...;
std::cerr <<"baz is " <<baz.orElse(bar) <<"\n";
Holds a value or nothing.
Definition Optional.h:56

Definition at line 267 of file Optional.h.

Referenced by Sawyer::Attribute::Storage< SyncTag >::attributeOrElse().

◆ orElse() [2/3]

template<typename T >
const Value & Sawyer::Optional< T >::orElse ( Value dflt)
inline

Obtain value or something else.

Returns a reference to the contained value if it exists, otherwise returns a reference to the argument.

Object bar = ...;
Object foo = objects.getOptional(key).orElse(bar);
Optional<Object> baz = ...;
std::cerr <<"baz is " <<baz.orElse(bar) <<"\n";

Definition at line 270 of file Optional.h.

◆ orElse() [3/3]

template<typename T >
const Optional Sawyer::Optional< T >::orElse ( const Optional< T > &  other) const
inline

Obtain value or something else.

Returns a reference to the contained value if it exists, otherwise returns a reference to the argument.

Object bar = ...;
Object foo = objects.getOptional(key).orElse(bar);
Optional<Object> baz = ...;
std::cerr <<"baz is " <<baz.orElse(bar) <<"\n";

Definition at line 273 of file Optional.h.

◆ andThen() [1/2]

template<typename T >
Optional Sawyer::Optional< T >::andThen ( const Value value) const
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().

◆ andTHen()

template<typename T >
Optional Sawyer::Optional< T >::andTHen ( Value value) const
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().

◆ andThen() [2/2]

template<typename T >
const Optional & Sawyer::Optional< T >::andThen ( const Optional< T > &  other) const
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.

◆ orDefault()

template<typename T >
Value Sawyer::Optional< T >::orDefault ( ) const
inline

Obtain a value or a default.

Returns a copy of the contained value if it exists, otherwise returns a default constructed value.

Object bar = ...;
Object foo = objects.getOptional(key).orDefault();
Optional<Object> baz = ...;
std::cerr <<"baz is " <<baz.orDefault() <<"\n";

Definition at line 305 of file Optional.h.

Referenced by Sawyer::Attribute::Storage< SyncTag >::getAttribute().

◆ assignTo()

template<typename T >
template<class U >
bool Sawyer::Optional< T >::assignTo ( U &  out) const
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:

unsigned key = ...;
std::string value;
IntervalMap<Interval<unsigned>, std::string> imap = ...;
while (imap.getOptional(key).assignTo(value)) ...

where the alternative would be

unsigned key = ...;
IntervalMap<Interval<unsigned>, std::string> imap = ...;
while (Optional<std::string> opt = imap.getOptional(key)) {
std::string value = *opt;

Definition at line 331 of file Optional.h.

Referenced by Rose::BinaryAnalysis::Partitioner2::GraphViz::BaseEmitter< G >::emitVertex().

◆ isEqual() [1/3]

template<typename T >
bool Sawyer::Optional< T >::isEqual ( const Optional< T > &  other) const
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().

◆ isEqual() [2/3]

template<typename T >
bool Sawyer::Optional< T >::isEqual ( const Value other) const
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().

◆ isEqual() [3/3]

template<typename T >
bool Sawyer::Optional< T >::isEqual ( const Nothing ) const
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.

◆ fmap()

template<typename T >
template<typename F >
auto Sawyer::Optional< T >::fmap ( F &&  f) -> Optional<decltype(f(get()))>
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().

◆ operator unspecified_bool()

template<typename T >
Sawyer::Optional< T >::operator unspecified_bool ( ) const
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:

if (x) {
//this is reached
}

Definition at line 389 of file Optional.h.


The documentation for this class was generated from the following file: