casacore
Loading...
Searching...
No Matches
Fallible.h
Go to the documentation of this file.
1//# Fallible.h: Identifies a value as valid or invalid
2//# Copyright (C) 1994,1995,1999
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_FALLIBLE_H
27#define CASA_FALLIBLE_H
28
29#include <casacore/casa/aips.h>
30
31namespace casacore { //# NAMESPACE CASACORE - BEGIN
32
33//# The following function is to be found in Fallible2.cc not Fallible.cc
34//# because it's a non-templated function and template instantiators normally
35//# do not like them in the same .cc file with templated functions.
36//
37// <summary> throw exception on access of an invalid object </summary>
38//
39// This function gets called when an invalid object is accessed. It
40// just throws an exception. Since we have inline functions, let's keep
41// the throw out of them to keep them from moving out of line.
42// <thrown>
43// <li> AipsError
44// </thrown>
45//
46// <group name=invalid_access>
47void AccessInvalidFallibleObject();
48// </group>
49
50// <summary> Mark a value as valid or invalid. </summary>
51// <use visibility=export>
52// <reviewed reviewer="Gareth Hunt" date="1994/09/14" tests="tFallible,TestCenter" demos="">
53// </reviewed>
54
55// <etymology>
56// This is to be used for values which might be fallible, i.e. might not
57// be valid.
58// </etymology>
59
60// <synopsis>
61// This class resembles the one in <em>Scientific and Engineering C++</em>
62// by Barton and Nackman. While it was written with that book closed, the
63// class is simple enough that resemblances likely remain.
64//
65// This class essentially just holds a value (with automatic conversion)
66// and allows inquiry as to whether the value is valid. If the value is
67// used and is indeed invalid an exception will be thrown.
68//
69// A copy of the value is stored in the <src>Fallible<T></src> object, so
70// making copies shouldn't be too expensive. It is anticipated that this
71// class will most often be used with built in, or other small, types.
72// </synopsis>
73
74// <example>
75// Suppose we write some code that turns a day/month/year into a day
76// of the week:
77// <srcblock>
78// enum DayName {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
79// Saturday};
80// Fallible<DayName> dayFromDate(uInt day, uInt month, uInt year); // a func.
81// </srcblock>
82// And we also have some other function that needs a day name, for example
83// just prints it:
84// <srcblock>
85// ostream &operator<<(ostream &os, DayName day); // Print name as string
86// </srcblock>
87//
88// Since the automatic conversions are defined, if we are certain that the
89// dates are valid, we can just go ahead and use the value:
90// <srcblock>
91// cout << dayFromData(2, 1, 1962) << endl; // A valid date
92// </srcblock>
93// If, by some chance, you are wrong and a date fails, then an exception will
94// be thrown and a run-time error will occur.
95//
96// If, as is more likely the case, you don't know a priori whether a test will
97// succeed, you can check it:
98// <srcblock>
99// Fallible<DayName> result = dayFromDate(d,m,y); // who knows if valid?
100// if (result.isValid()) {
101// cout << result << endl;
102// } else {
103// // some corrective action
104// }
105// </srcblock>
106// </example>
107
108// <motivation>
109// The alternatives are to have "special values" (e.g. have an "undefined
110// day" in the enumeration) or return a Boolean, or change a Boolean. While
111// those solutions are often adequate, <src>Fallible<T></src> can often be
112// more natural.
113// </motivation>
114
115// <templating arg=T>
116// <LI> default constructor
117// <LI> copy constructor
118// </templating>
119
120template<class T> class Fallible
121{
122public:
123 // The default constructor creates an invalid object.
125
126 // Create a valid object
128
129 //# Actually, the default copy ctor and assignment operator would work
130 Fallible(const Fallible<T> &other) : value_p(other.value_p),
131 isValid_p(other.isValid_p) {}
132
134 {value_p = other.value_p; isValid_p = other.isValid_p;
135 return *this;}
136
138
139 // Automatically convert a <src>Fallible<T></src> to a <src>T</src>.
140 operator T() const { if (! isValid_p) AccessInvalidFallibleObject();
141 return value_p; }
142
143 // Sometimes it's more convenient to not rely on a compiler supplied
144 // conversion, especially when the compiler is confused.
145 T value() const { if (! isValid_p) AccessInvalidFallibleObject();
146 return value_p; }
147
148 Bool isValid() const {return isValid_p;}
149private:
152};
153
154
155} //# NAMESPACE CASACORE - END
156
157#endif
158
Mark a value as valid or invalid.
Definition Fallible.h:121
Bool isValid() const
Definition Fallible.h:148
T value() const
Sometimes it's more convenient to not rely on a compiler supplied conversion, especially when the com...
Definition Fallible.h:145
Fallible(const Fallible< T > &other)
Definition Fallible.h:130
Fallible()
The default constructor creates an invalid object.
Definition Fallible.h:124
Fallible< T > & operator=(const Fallible< T > &other)
Definition Fallible.h:133
Fallible(const T &value)
Create a valid object.
Definition Fallible.h:127
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41