casacore
Loading...
Searching...
No Matches
QuantumHolder.h
Go to the documentation of this file.
1// # QuantumHolder.h: A holder for Quantities to enable record conversions
2// # Copyright (C) 1998,1999,2000,2003
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_QUANTUMHOLDER_H
27#define CASA_QUANTUMHOLDER_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/ArrayFwd.h>
32#include <casacore/casa/Utilities/RecordTransformable.h>
33#include <casacore/casa/BasicSL/Complexfwd.h>
34#include <memory>
35
36namespace casacore { // # NAMESPACE CASACORE - BEGIN
37
38// # Forward Declarations
39class QBase;
40class String;
41class RecordInterface;
42class Record;
43template <class Qtype>
44class Quantum;
45
46// <summary> A holder for Quantums to enable record conversions </summary>
47
48// <use visibility=export>
49
50// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tQuantumHolder" demos="">
51// </reviewed>
52
53// <prerequisite>
54// <li> <linkto class=RecordInterface>RecordInterface</linkto> class
55// <li> <linkto class=Quantum>Quantity</linkto> class
56// </prerequisite>
57//
58// <etymology>
59// A Holder of general Quantums
60// </etymology>
61//
62// <synopsis>
63// This class can be used to handle a heterogeneous list of Quantums, and
64// can handle toRecord() and fromRecord() conversions.
65// A QuantumHolder
66// is created empty, from a Quantum (e.g. a <src>Quantum<Double></src>) or a
67// <src>Quantum<Vector<Float>></src>).
68//
69// The accepted range of Quantums is:
70// <ul>
71// <li> <src>Quantum<Int>, Quantum<Float>, Quantum<Double> == Quantity</src>
72// <li> <src>Quantum<Complex>, Quantum<DComplex></src>
73// <li> <src>Quantum<Vector<Int>>, Quantum<Vector<Float>></src>,
74// <src>Quantum<Vector<Double>></src>
75// <li> <src>Quantum<Vector<Complex>>, Quantum<Vector<DComplex>></src>
76// <li> <src>Quantum<Array<Int>>, Quantum<Array<Float>></src>,
77// <src>Quantum<Array<Double>></src>
78// <li> <src>Quantum<Array<Complex>>, Quantum<Array<DComplex>></src>
79// </ul>
80// Scalars in the same group can be converted to any in the same group (e.g.
81// Int to Double); Vectors of length 1 can be converted to scalars in the
82// corresponding group; Scalars can always be converted to Vectors in the
83// corresponding group. Real scalar values can be converted to Complex values.
84// Vectors cannot be converted to other type vectors.
85//
86// Checks on the contents can be made with functions like
87// <src>isQuantity</src> and the contents can be obtained with
88// functions like <src>asQuantity</src>. It is an error to try and
89// retrieve a Quantum of the wrong type and doing so will generate an
90// exception (AipsError).
91// </synopsis>
92//
93// <example>
94// <srcblock>
95// TableRecord rec; // an empty record
96// Quantity x(12.5, "km/s"); // a Quantity
97// String error; // an error message
98// if (!QuantumHolder(x).toRecord(error, rec)) { // make record
99// cout << error << endl;
100// };
101// Record grec; // a Record
102// if (!QuantumHolder(x).toRecord(error, grec)) { // make record
103// cout << error << endl;
104// };
105// // Note that for GlishRecords use can be made of the
106// // GlishRecord::to/fromrecord() methods.
107// </srcblock>
108// </example>
109//
110// <motivation>
111// To make general conversions between Quantums and records, without knowing
112// the actual Quantum being converted.
113// </motivation>
114
116 public:
117 // # Friends
118
119 // # Enumerations
120
121 // # Constructors
122 // Creates an empty holder
124 // Create from a Quantum (copy semantics)
126 // Copy a holder (copy semantics)
128 // # Destructor
130
131 // # Operators
132 // Assignment (copy semantics)
134
135 // # Member Functions
136 // Check if it holds a Quantity. Note that a Vector of length 1 will give
137 // True to scalar questions.
138 // <group>
139 Bool isEmpty() const;
141 Bool isScalar() const;
142 Bool isVector() const;
143 Bool isArray() const;
144 Bool isReal() const;
162 // </group>
163 // Get number of numeric elements (1 if scalar, else
164 // vector length) or dimensions (0 if scalar)
165 // <thrown>
166 // <li> AipsError if holder empty
167 // </thrown>
168 // <group>
169 Int nelements() const;
170 Int ndim() const;
171 // </group>
172
173 // Get a Quantum from the holder (with lifetime as long
174 // as holder exists). Conversions done if necessary and as described in
175 // introduction.
176 // <thrown>
177 // <li> AipsError if holder empty or no conversion possible
178 // </thrown>
179 // <group>
180 const QBase &asQuantum() const;
197 // </group>
198
199 // Create a Quantum from a record or a string.
200 // A valid record will contain the following fields:
201 // <ul>
202 // <li> value: contains a numeric value of Int, Float, Double, Complex,
203 // DComplex or a vector thereof
204 // <li> unit: a string with a valid unit string.
205 // </ul>
206 // A valid string will be one of the special time/angle formats or a
207 // value with a valid unit string.
208 // Illegal values or units will return False and write an error message.
209 // <group>
210 virtual Bool fromRecord(String &error, const RecordInterface &in);
211 virtual Bool fromString(String &error, const String &in);
212 // </group>
213 // Create a record from a Quantum. A False return and an error message is
214 // only generated if there is no valid Quantum in the holder.
215 virtual Bool toRecord(String &error, RecordInterface &out) const;
216 // this version throws an exception rather than returning false
217 virtual void toRecord(RecordInterface &out) const;
218 // this version throws an exception or returns the result Record.
219 virtual Record toRecord() const;
220
221 // Return identification
222 virtual const String &ident() const;
223
224 private:
225 // # Data Members
226 // Pointer to a Quantity
227 std::unique_ptr<QBase> hold_p;
228
229 // # General member functions
230 // Convert to a different real scalar quantum
231 void toReal(const uInt &tp);
232 // Convert to a different complex scalar quantum
233 void toComplex(const uInt &tp);
234 // Convert scalar to Vector
235 void toVector();
236 // Convert scalar to Array
237 void toArray();
238};
239
240} // namespace casacore
241
242#endif
const Quantum< Double > & asQuantity()
Bool isQuantumDouble() const
Bool isQuantumDComplex() const
const QBase & asQuantum() const
Get a Quantum from the holder (with lifetime as long as holder exists).
Bool isQuantumVectorComplex() const
Bool isQuantumArrayDComplex() const
Bool isQuantumArrayFloat() const
void toComplex(const uInt &tp)
Convert to a different complex scalar quantum.
Bool isEmpty() const
Check if it holds a Quantity.
const Quantum< Array< Float > > & asQuantumArrayFloat()
const Quantum< Array< Double > > & asQuantumArrayDouble()
QuantumHolder(const QuantumHolder &other)
Copy a holder (copy semantics).
Bool isQuantumVectorDouble() const
Bool isQuantumComplex() const
void toArray()
Convert scalar to Array.
Int nelements() const
Get number of numeric elements (1 if scalar, else vector length) or dimensions (0 if scalar).
QuantumHolder(const QBase &in)
Create from a Quantum (copy semantics).
Bool isQuantumArrayInt() const
const Quantum< Array< DComplex > > & asQuantumArrayDComplex()
void toReal(const uInt &tp)
Convert to a different real scalar quantum.
const Quantum< Double > & asQuantumDouble()
QuantumHolder()
Creates an empty holder.
QuantumHolder & operator=(const QuantumHolder &other)
Assignment (copy semantics).
Bool isQuantumVectorDComplex() const
virtual Bool toRecord(String &error, RecordInterface &out) const
Create a record from a Quantum.
virtual Bool fromString(String &error, const String &in)
Initialise the class from a String representation.
const Quantum< DComplex > & asQuantumDComplex()
const Quantum< Vector< Double > > & asQuantumVectorDouble()
virtual Record toRecord() const
this version throws an exception or returns the result Record.
std::unique_ptr< QBase > hold_p
Pointer to a Quantity.
const Quantum< Float > & asQuantumFloat()
const Quantum< Array< Complex > > & asQuantumArrayComplex()
const Quantum< Vector< Complex > > & asQuantumVectorComplex()
virtual Bool fromRecord(String &error, const RecordInterface &in)
Create a Quantum from a record or a string.
Bool isQuantumFloat() const
void toVector()
Convert scalar to Vector.
const Quantum< Int > & asQuantumInt()
Bool isQuantumVectorInt() const
Bool isQuantumArrayDouble() const
Bool isQuantumInt() const
Bool isQuantumVectorFloat() const
const Quantum< Vector< DComplex > > & asQuantumVectorDComplex()
const Quantum< Complex > & asQuantumComplex()
const Quantum< Array< Int > > & asQuantumArrayInt()
virtual const String & ident() const
Return identification.
virtual void toRecord(RecordInterface &out) const
this version throws an exception rather than returning false
const Quantum< Vector< Float > > & asQuantumVectorFloat()
const Quantum< Vector< Int > > & asQuantumVectorInt()
Bool isQuantumArrayComplex() const
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
RecordInterface()
The default constructor creates an empty record with a variable structure.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40