cgv
|
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. | |
T | value () const |
Access the contained value. | |
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.
T | the value type. |
Definition at line 22 of file optional.h.
|
inline |
Construct without a contained value.
Definition at line 31 of file optional.h.
|
inline |
Construct with a contained value.
v | The contained value. |
Definition at line 35 of file optional.h.
|
inline |
Test if a value is contained.
Definition at line 70 of file optional.h.
Referenced by cgv::data::optional< T >::operator==().
|
inlineexplicit |
Allow conversion to bool.
Definition at line 57 of file optional.h.
|
inline |
Assign a non-optional value of type T.
After the assignment, has_value returns true.
v | The value to assign. |
Definition at line 43 of file optional.h.
|
inline |
Compare with another optional of the same type T.
rhs | The object to compare to. |
Definition at line 52 of file optional.h.
References cgv::data::optional< T >::has_value(), and cgv::data::optional< T >::value().
|
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.
|
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.
Definition at line 80 of file optional.h.
Referenced by cgv::data::optional< T >::operator==().