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> class Quantum;
44
45// <summary> A holder for Quantums to enable record conversions </summary>
46
47// <use visibility=export>
48
49// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tQuantumHolder" demos="">
50// </reviewed>
51
52// <prerequisite>
53// <li> <linkto class=RecordInterface>RecordInterface</linkto> class
54// <li> <linkto class=Quantum>Quantity</linkto> class
55// </prerequisite>
56//
57// <etymology>
58// A Holder of general Quantums
59// </etymology>
60//
61// <synopsis>
62// This class can be used to handle a heterogeneous list of Quantums, and
63// can handle toRecord() and fromRecord() conversions.
64// A QuantumHolder
65// is created empty, from a Quantum (e.g. a <src>Quantum<Double></src>) or a
66// <src>Quantum<Vector<Float>></src>).
67//
68// The accepted range of Quantums is:
69// <ul>
70// <li> <src>Quantum<Int>, Quantum<Float>, Quantum<Double> == Quantity</src>
71// <li> <src>Quantum<Complex>, Quantum<DComplex></src>
72// <li> <src>Quantum<Vector<Int>>, Quantum<Vector<Float>></src>,
73// <src>Quantum<Vector<Double>></src>
74// <li> <src>Quantum<Vector<Complex>>, Quantum<Vector<DComplex>></src>
75// <li> <src>Quantum<Array<Int>>, Quantum<Array<Float>></src>,
76// <src>Quantum<Array<Double>></src>
77// <li> <src>Quantum<Array<Complex>>, Quantum<Array<DComplex>></src>
78// </ul>
79// Scalars in the same group can be converted to any in the same group (e.g.
80// Int to Double); Vectors of length 1 can be converted to scalars in the
81// corresponding group; Scalars can always be converted to Vectors in the
82// corresponding group. Real scalar values can be converted to Complex values.
83// Vectors cannot be converted to other type vectors.
84//
85// Checks on the contents can be made with functions like
86// <src>isQuantity</src> and the contents can be obtained with
87// functions like <src>asQuantity</src>. It is an error to try and
88// retrieve a Quantum of the wrong type and doing so will generate an
89// exception (AipsError).
90// </synopsis>
91//
92// <example>
93// <srcblock>
94// TableRecord rec; // an empty record
95// Quantity x(12.5, "km/s"); // a Quantity
96// String error; // an error message
97// if (!QuantumHolder(x).toRecord(error, rec)) { // make record
98// cout << error << endl;
99// };
100// Record grec; // a Record
101// if (!QuantumHolder(x).toRecord(error, grec)) { // make record
102// cout << error << endl;
103// };
104// // Note that for GlishRecords use can be made of the
105// // GlishRecord::to/fromrecord() methods.
106// </srcblock>
107// </example>
108//
109// <motivation>
110// To make general conversions between Quantums and records, without knowing
111// the actual Quantum being converted.
112// </motivation>
113
115
116public:
117
118//# Friends
119
120//# Enumerations
121
122//# Constructors
123 // Creates an empty holder
125 // Create from a Quantum (copy semantics)
127 // Copy a holder (copy semantics)
129//# Destructor
131
132//# Operators
133 // Assignment (copy semantics)
135
136//# Member Functions
137 // Check if it holds a Quantity. Note that a Vector of length 1 will give
138 // True to scalar questions.
139 // <group>
140 Bool isEmpty() const;
142 Bool isScalar() const;
143 Bool isVector() const;
144 Bool isArray() const;
145 Bool isReal() const;
163 // </group>
164 // Get number of numeric elements (1 if scalar, else
165 // vector length) or dimensions (0 if scalar)
166 // <thrown>
167 // <li> AipsError if holder empty
168 // </thrown>
169 // <group>
170 Int nelements() const;
171 Int ndim() const;
172 // </group>
173
174 // Get a Quantum from the holder (with lifetime as long
175 // as holder exists). Conversions done if necessary and as described in
176 // introduction.
177 // <thrown>
178 // <li> AipsError if holder empty or no conversion possible
179 // </thrown>
180 // <group>
181 const QBase &asQuantum() const;
198 // </group>
199
200 // Create a Quantum from a record or a string.
201 // A valid record will contain the following fields:
202 // <ul>
203 // <li> value: contains a numeric value of Int, Float, Double, Complex,
204 // DComplex or a vector thereof
205 // <li> unit: a string with a valid unit string.
206 // </ul>
207 // A valid string will be one of the special time/angle formats or a
208 // value with a valid unit string.
209 // Illegal values or units will return False and write an error message.
210 // <group>
211 virtual Bool fromRecord(String &error, const RecordInterface &in);
212 virtual Bool fromString(String &error, const String &in);
213 // </group>
214 // Create a record from a Quantum. A False return and an error message is
215 // only generated if there is no valid Quantum in the holder.
216 virtual Bool toRecord(String &error, RecordInterface &out) const;
217 // this version throws an exception rather than returning false
218 virtual void toRecord(RecordInterface &out) const;
219 // this version throws an exception or returns the result Record.
220 virtual Record toRecord() const;
221
222
223 // Return identification
224 virtual const String &ident() const;
225
226private:
227
228//# Data Members
229 // Pointer to a Quantity
230 std::unique_ptr<QBase> hold_p;
231
232//# General member functions
233 // Convert to a different real scalar quantum
234 void toReal(const uInt &tp);
235 // Convert to a different complex scalar quantum
236 void toComplex(const uInt &tp);
237 // Convert scalar to Vector
238 void toVector();
239 // Convert scalar to Array
240 void toArray();
241};
242
243
244} //# NAMESPACE CASACORE - END
245
246#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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40