cgv
Loading...
Searching...
No Matches
cgv::data::optional< T > Class Template Reference

A simple and naiive implementation of an optional value. More...

#include <optional.h>

Public Member Functions

 optional ()
 Construct without a contained value.
 
 optional (T v)
 Construct with a contained value.
 
optional< T > & operator= (const T &v)
 Assign a non-optional value of type T.
 
bool operator== (const optional< T > &rhs) const
 Compare with another optional of the same type T.
 
 operator bool () const
 Allow conversion to bool.
 
void reset ()
 Mark the optional as not containing a valid value.
 
bool has_value () const
 Test if a value is contained.
 
value () const
 Access the contained value.
 

Detailed Description

template<typename T>
class cgv::data::optional< T >

A simple and naiive implementation of an optional value.

This class handles an optional contained value that may or may not be present and can be used as the return type of a function that may fail. This specific implementation is very simple and shall only be used with simple data types. The contained type T must be default constructible and the implementation will always hold a valid instance of the value, hence retrieving the value will always work and never throw an exception. The actually contained value shall be considered valid only if has_value returns true. Due to the fact that objects are always constructed, this implementation will be inefficient for types that have complex constructors.

For more sophisticated implementations of an optional type see std::optional (since C++17) or: https://github.com/akrzemi1/Optional.

Template Parameters
Tthe value type.

Definition at line 22 of file optional.h.

Constructor & Destructor Documentation

◆ optional() [1/2]

template<typename T >
cgv::data::optional< T >::optional ( )
inline

Construct without a contained value.

Definition at line 31 of file optional.h.

◆ optional() [2/2]

template<typename T >
cgv::data::optional< T >::optional ( v)
inline

Construct with a contained value.

Parameters
vThe contained value.

Definition at line 35 of file optional.h.

Member Function Documentation

◆ has_value()

template<typename T >
bool cgv::data::optional< T >::has_value ( ) const
inline

Test if a value is contained.

Returns
true if a valid value if contained, false otherwise.

Definition at line 70 of file optional.h.

Referenced by cgv::data::optional< T >::operator==().

◆ operator bool()

template<typename T >
cgv::data::optional< T >::operator bool ( ) const
inlineexplicit

Allow conversion to bool.

Definition at line 57 of file optional.h.

◆ operator=()

template<typename T >
optional< T > & cgv::data::optional< T >::operator= ( const T &  v)
inline

Assign a non-optional value of type T.

After the assignment, has_value returns true.

Parameters
vThe value to assign.
Returns
A reference to this object.

Definition at line 43 of file optional.h.

◆ operator==()

template<typename T >
bool cgv::data::optional< T >::operator== ( const optional< T > &  rhs) const
inline

Compare with another optional of the same type T.

Parameters
rhsThe object to compare to.
Returns
The result of operator== n both contained values, if both optionals have values, false otherwise.

Definition at line 52 of file optional.h.

References cgv::data::optional< T >::has_value(), and cgv::data::optional< T >::value().

◆ reset()

template<typename T >
void cgv::data::optional< T >::reset ( )
inline

Mark the optional as not containing a valid value.

This will not alter the contained value to prevent the additional overhead of constructing a new instance.

Definition at line 64 of file optional.h.

◆ value()

template<typename T >
T cgv::data::optional< T >::value ( ) const
inline

Access the contained value.

If has_value would return false, the object returned by this method is still valid but the exact value is undefined.

Returns
The contained value.

Definition at line 80 of file optional.h.

Referenced by cgv::data::optional< T >::operator==().


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