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#include <casacore/casa/Exceptions/Error.h>
31
32namespace casacore { // # NAMESPACE CASACORE - BEGIN
33
34// <summary> throw exception on access of an invalid object </summary>
35//
36// This function gets called when an invalid object is accessed. It
37// just throws an exception.
38// <thrown>
39// <li> AipsError
40// </thrown>
41//
42// <group name=invalid_access>
43[[deprecated("Fallible is replaced by std::optional")]]
44inline void AccessInvalidFallibleObject() {
45 throw(
46 AipsError("Fallible<T>:: invalid object accessed. Sorry I don't know"
47 " from where"));
48}
49// </group>
50
51// <summary> Mark a value as valid or invalid. </summary>
52// <use visibility=export>
53// <reviewed reviewer="Gareth Hunt" date="1994/09/14" tests="tFallible,TestCenter" demos="">
54// </reviewed>
55
56// <etymology>
57// This is to be used for values which might be fallible, i.e. might not
58// be valid.
59// </etymology>
60
61// <synopsis>
62// This class resembles the one in <em>Scientific and Engineering C++</em>
63// by Barton and Nackman. While it was written with that book closed, the
64// class is simple enough that resemblances likely remain.
65//
66// This class essentially just holds a value (with automatic conversion)
67// and allows inquiry as to whether the value is valid. If the value is
68// used and is indeed invalid an exception will be thrown.
69//
70// A copy of the value is stored in the <src>Fallible<T></src> object, so
71// making copies shouldn't be too expensive. It is anticipated that this
72// class will most often be used with built in, or other small, types.
73// </synopsis>
74
75// <example>
76// Suppose we write some code that turns a day/month/year into a day
77// of the week:
78// <srcblock>
79// enum DayName {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
80// Saturday};
81// Fallible<DayName> dayFromDate(uInt day, uInt month, uInt year); // a func.
82// </srcblock>
83// And we also have some other function that needs a day name, for example
84// just prints it:
85// <srcblock>
86// ostream &operator<<(ostream &os, DayName day); // Print name as string
87// </srcblock>
88//
89// Since the automatic conversions are defined, if we are certain that the
90// dates are valid, we can just go ahead and use the value:
91// <srcblock>
92// cout << dayFromData(2, 1, 1962) << endl; // A valid date
93// </srcblock>
94// If, by some chance, you are wrong and a date fails, then an exception will
95// be thrown and a run-time error will occur.
96//
97// If, as is more likely the case, you don't know a priori whether a test will
98// succeed, you can check it:
99// <srcblock>
100// Fallible<DayName> result = dayFromDate(d,m,y); // who knows if valid?
101// if (result.isValid()) {
102// cout << result << endl;
103// } else {
104// // some corrective action
105// }
106// </srcblock>
107// </example>
108
109// <motivation>
110// The alternatives are to have "special values" (e.g. have an "undefined
111// day" in the enumeration) or return a Boolean, or change a Boolean. While
112// those solutions are often adequate, <src>Fallible<T></src> can often be
113// more natural.
114// </motivation>
115
116// <templating arg=T>
117// <LI> default constructor
118// <LI> copy constructor
119// </templating>
120
121template <class T>
122class [[deprecated("Use std::optional")]] Fallible {
123 public:
124 // The default constructor creates an invalid object.
126
127 // Create a valid object
129
130 // # Actually, the default copy ctor and assignment operator would work
131 Fallible(const Fallible<T> &other) : value_p(other.value_p), isValid_p(other.isValid_p) {}
132
134 value_p = other.value_p;
135 isValid_p = other.isValid_p;
136 return *this;
137 }
138
140
141 // Automatically convert a <src>Fallible<T></src> to a <src>T</src>.
142 operator T() const {
143 if (!isValid_p) AccessInvalidFallibleObject();
144 return value_p;
145 }
146
147 // Sometimes it's more convenient to not rely on a compiler supplied
148 // conversion, especially when the compiler is confused.
149 T value() const {
150 if (!isValid_p) AccessInvalidFallibleObject();
151 return value_p;
152 }
153
154 Bool isValid() const { return isValid_p; }
155
156 private:
159};
160
161} // namespace casacore
162
163#endif
Bool isValid() const
Definition Fallible.h:154
T value() const
Sometimes it's more convenient to not rely on a compiler supplied conversion, especially when the com...
Definition Fallible.h:149
Fallible(const Fallible< T > &other)
Definition Fallible.h:131
Fallible()
The default constructor creates an invalid object.
Definition Fallible.h:125
Fallible< T > & operator=(const Fallible< T > &other)
Definition Fallible.h:133
Fallible(const T &value)
Create a valid object.
Definition Fallible.h:128
For temporary backward namespace compatibility, use casa as alias for casacore.
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