ROSE  0.9.9.109
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 49 of file Optional.h.

#include <Optional.h>

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

Public Types

typedef T Value
 Type of stored value. More...
 

Public Member Functions

 Optional ()
 Default constructs nothing. More...
 
 Optional (const Value &v)
 Construct from value. More...
 
 Optional (const Nothing &)
 Construct from nothing. More...
 
 Optional (const Optional &other)
 Copy constructor. More...
 
 ~Optional ()
 Destructor. More...
 
Optionaloperator= (const Value &value)
 Value assignment. More...
 
Optionaloperator= (const Nothing &)
 Nothing assignment. More...
 
Optionaloperator= (const Optional &other)
 Optional assignment. More...
 
Value orDefault () const
 Obtain a value or a default. More...
 
bool assignTo (Value &out) const
 Conditionally save a value. More...
 
 operator unspecified_bool () const
 Type for Boolean context. More...
 
const Valueoperator* () const
 Dereference to obtain value. More...
 
Valueoperator* ()
 Dereference to obtain value. More...
 
const Valueget () const
 Dereference to obtain value. More...
 
Valueget ()
 Dereference to obtain value. More...
 
const Valueoperator-> () const
 Obtain a pointer to the value. More...
 
Valueoperator-> ()
 Obtain a pointer to the value. More...
 
const ValueorElse (const Value &dflt) const
 Obtain value or something else. More...
 
const ValueorElse (Value &dflt)
 Obtain value or something else. More...
 
bool isEqual (const Optional &other) const
 Compare two values. More...
 
bool isEqual (const Value &other) const
 Compare two values. More...
 
bool isEqual (const Nothing &) const
 Compare two values. More...
 

Member Typedef Documentation

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

Type of stored value.

Definition at line 87 of file Optional.h.

Constructor & Destructor Documentation

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 92 of file Optional.h.

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 97 of file Optional.h.

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 104 of file Optional.h.

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 110 of file Optional.h.

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 121 of file Optional.h.

Member Function Documentation

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 132 of file Optional.h.

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 147 of file Optional.h.

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 162 of file Optional.h.

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 186 of file Optional.h.

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 189 of file Optional.h.

template<typename T>
const Value& Sawyer::Optional< T >::get ( ) 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 192 of file Optional.h.

Referenced by Sawyer::Cached< bool >::get(), and Sawyer::Optional< Value >::isEqual().

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 197 of file Optional.h.

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 210 of file Optional.h.

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 213 of file Optional.h.

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";

Definition at line 230 of file Optional.h.

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

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 233 of file Optional.h.

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 249 of file Optional.h.

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

template<typename T>
bool Sawyer::Optional< T >::assignTo ( Value 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 274 of file Optional.h.

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 290 of file Optional.h.

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 293 of file Optional.h.

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 296 of file Optional.h.

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:

Optional<int> x = 0;
if (x) {
//this is reached
}

Definition at line 318 of file Optional.h.


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