casa
5.7.0-16
|
Mark a value as valid or invalid. More...
#include <Fallible.h>
Public Member Functions | |
Fallible () | |
The default constructor creates an invalid object. More... | |
Fallible (const T &value) | |
Create a valid object. More... | |
Fallible (const Fallible< T > &other) | |
Fallible< T > & | operator= (const Fallible< T > &other) |
~Fallible () | |
operator T () const | |
Automatically convert a Fallible<T> to a T . More... | |
T | value () const |
Sometimes it's more convenient to not rely on a compiler supplied conversion, especially when the compiler is confused. More... | |
Bool | isValid () const |
Private Attributes | |
T | value_p |
Bool | isValid_p |
Mark a value as valid or invalid.
Public interface
This is to be used for values which might be fallible, i.e. might not be valid.
This class resembles the one in Scientific and Engineering C++ by Barton and Nackman. While it was written with that book closed, the class is simple enough that resemblances likely remain.
This class essentially just holds a value (with automatic conversion) and allows inquiry as to whether the value is valid. If the value is used and is indeed invalid an exception will be thrown.
A copy of the value is stored in the Fallible<T>
object, so making copies shouldn't be too expensive. It is anticipated that this class will most often be used with built in, or other small, types.
Suppose we write some code that turns a day/month/year into a day of the week:
And we also have some other function that needs a day name, for example just prints it:
Since the automatic conversions are defined, if we are certain that the dates are valid, we can just go ahead and use the value:
If, by some chance, you are wrong and a date fails, then an exception will be thrown and a run-time error will occur.
If, as is more likely the case, you don't know a priori whether a test will succeed, you can check it:
The alternatives are to have "special values" (e.g. have an "undefined
day" in the enumeration) or return a Boolean, or change a Boolean. While those solutions are often adequate, Fallible<T>
can often be more natural.
Definition at line 122 of file Fallible.h.
|
inline |
The default constructor creates an invalid object.
Definition at line 126 of file Fallible.h.
|
inline |
Create a valid object.
Definition at line 129 of file Fallible.h.
|
inline |
Definition at line 132 of file Fallible.h.
|
inline |
Definition at line 139 of file Fallible.h.
|
inline |
Definition at line 150 of file Fallible.h.
References casacore::Fallible< T >::isValid_p.
|
inline |
Automatically convert a Fallible<T>
to a T
.
Definition at line 142 of file Fallible.h.
References casacore::Fallible< T >::isValid_p, and casacore::Fallible< T >::value_p.
|
inline |
Definition at line 135 of file Fallible.h.
References casacore::Fallible< T >::isValid_p, and casacore::Fallible< T >::value_p.
|
inline |
Sometimes it's more convenient to not rely on a compiler supplied conversion, especially when the compiler is confused.
Definition at line 147 of file Fallible.h.
References casacore::Fallible< T >::isValid_p, and casacore::Fallible< T >::value_p.
|
private |
Definition at line 153 of file Fallible.h.
Referenced by casacore::Fallible< T >::isValid(), casacore::Fallible< T >::operator T(), casacore::Fallible< T >::operator=(), and casacore::Fallible< T >::value().
|
private |
Definition at line 152 of file Fallible.h.
Referenced by casacore::Fallible< T >::operator T(), casacore::Fallible< T >::operator=(), and casacore::Fallible< T >::value().