Loading...
Searching...
No Matches
casacore::Fallible< T > Class Template Reference
casa package (libcasa_casa) » Utilities_module

Mark a value as valid or invalid. More...

`#include <Fallible.h>`

## Public Member Functions

| Fallible ()
| The default constructor creates an invalid object.
| Fallible (const T &value)
| Create a valid object.
| Fallible (const Fallible< T > &other)
Fallible< T > & | operator= (const Fallible< T > &other)
| ~Fallible ()
| operator T () const
| Automatically convert a Fallible<T> to a T.
T | value () const
| Sometimes it's more convenient to not rely on a compiler supplied conversion, especially when the compiler is confused.
Bool | isValid () const

## Private Attributes

T | value_p
Bool | isValid_p

## Detailed Description

template<class T>
class casacore::Fallible< T >

Mark a value as valid or invalid.

### Intended use:

Public interface

### Review Status

Reviewed By:
  Gareth Hunt
Date Reviewed:
  1994/09/14
Test programs:
  tFallible,TestCenter

### Etymology

This is to be used for values which might be fallible, i.e. might not be valid.

### Synopsis

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.

### Example

Suppose we write some code that turns a day/month/year into a day of the week:

enum DayName {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday};
Fallible<DayName> dayFromDate(uInt day, uInt month, uInt year); // a func.
casacore::Fallible
Mark a value as valid or invalid.
Definition Fallible.h:121
casacore::uInt
unsigned int uInt
Definition aipstype.h:49
casacore::day
TableExprNode day(const TableExprNode &node)
Definition ExprNode.h:1527
casacore::month
TableExprNode month(const TableExprNode &node)
Definition ExprNode.h:1523
casacore::year
TableExprNode year(const TableExprNode &node)
Definition ExprNode.h:1519

And we also have some other function that needs a day name, for example just prints it:

ostream &operator<<(ostream &os, DayName day); // Print name as string
casacore::operator<<
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.

Since the automatic conversions are defined, if we are certain that the dates are valid, we can just go ahead and use the value:

cout << dayFromData(2, 1, 1962) << endl; // A valid date

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:

Fallible<DayName> result = dayFromDate(d,m,y); // who knows if valid?
if (result.isValid()) {
cout << result << endl;
} else {
// some corrective action
}
casacore::Fallible::isValid
Bool isValid() const
Definition Fallible.h:148
casacore::Fallible::Fallible
Fallible()
The default constructor creates an invalid object.
Definition Fallible.h:124

### Motivation

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.

### Template Type Argument Requirements (T)

- default constructor
- copy constructor

Definition at line 120 of file Fallible.h.

## Constructor & Destructor Documentation

## Fallible() [1/3]

template<class T>

casacore::Fallible< T >::Fallible | ( |  | ) |

inline

The default constructor creates an invalid object.

Definition at line 124 of file Fallible.h.

References casacore::False, isValid_p, and value_p.

Referenced by Fallible(), and operator=().

## Fallible() [2/3]

template<class T>

casacore::Fallible< T >::Fallible | ( | const T & | value | ) |

inline

Create a valid object.

Definition at line 127 of file Fallible.h.

References isValid_p, casacore::True, value(), and value_p.

## Fallible() [3/3]

template<class T>

casacore::Fallible< T >::Fallible | ( | const Fallible< T > & | other | ) |

inline

Definition at line 130 of file Fallible.h.

References Fallible(), isValid_p, and value_p.

## ~Fallible()

template<class T>

casacore::Fallible< T >::~Fallible | ( |  | ) |

inline

Definition at line 137 of file Fallible.h.

## Member Function Documentation

## isValid()

template<class T>

Bool casacore::Fallible< T >::isValid | ( |  | ) | const

inline

Definition at line 148 of file Fallible.h.

References isValid_p.

## operator T()

template<class T>

casacore::Fallible< T >::operator T | ( |  | ) | const

inline

Automatically convert a Fallible<T> to a T.

Definition at line 140 of file Fallible.h.

References isValid_p, and value_p.

## operator=()

template<class T>

Fallible< T > & casacore::Fallible< T >::operator= | ( | const Fallible< T > & | other | ) |

inline

Definition at line 133 of file Fallible.h.

References Fallible(), isValid_p, and value_p.

## value()

template<class T>

T casacore::Fallible< T >::value | ( |  | ) | const

inline

Sometimes it's more convenient to not rely on a compiler supplied conversion, especially when the compiler is confused.

Definition at line 145 of file Fallible.h.

References isValid_p, and value_p.

Referenced by Fallible().

## Member Data Documentation

## isValid_p

template<class T>

Bool casacore::Fallible< T >::isValid_p

private

Definition at line 151 of file Fallible.h.

Referenced by Fallible(), Fallible(), Fallible(), isValid(), operator T(), operator=(), and value().

## value_p

template<class T>

T casacore::Fallible< T >::value_p

private

Definition at line 150 of file Fallible.h.

Referenced by Fallible(), Fallible(), Fallible(), operator T(), operator=(), and value().

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

- casa/Utilities/Fallible.h