ROSE  0.11.96.11
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.
 

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...
 
void reset ()
 Reset as if default-constructed.
 
Value orDefault () const
 Obtain a value or a default. More...
 
template<class U >
bool assignTo (U &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...
 
const Optional orElse (const Optional &other) const
 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...
 

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

◆ 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 97 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 104 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 110 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 121 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 132 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 147 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 162 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 191 of file Optional.h.

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

◆ get() [1/2]

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

Referenced by Sawyer::Cached< T >::get(), Sawyer::Optional< Value >::isEqual(), Sawyer::Optional< Value >::operator*(), and Sawyer::Optional< Value >::operator->().

Here is the caller graph for this function:

◆ 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 202 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 215 of file Optional.h.

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

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

Definition at line 235 of file Optional.h.

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

Here is the caller graph for this function:

◆ 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 238 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 241 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 257 of file Optional.h.

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

Here is the caller graph for this function:

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

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

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

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

◆ 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:

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

Definition at line 327 of file Optional.h.


The documentation for this class was generated from the following file:
stringify::Rose::CodeGen::Object
const char * Object(int64_t)
Convert Rose::CodeGen::Object enum constant to a string.