casacore
Loading...
Searching...
No Matches
ValueHolder.h
Go to the documentation of this file.
1//# ValueHolder.h: A holder object for the standard Casacore data types
2//# Copyright (C) 2005
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
27#ifndef CASA_VALUEHOLDER_H
28#define CASA_VALUEHOLDER_H
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Containers/ValueHolderRep.h>
33#include <casacore/casa/Arrays/Array.h>
34#include <memory>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38
39// <summary>
40// A holder for a value of any basic Casacore data type.
41// </summary>
42
43// <use visibility=export>
44// <reviewed reviewer="" date="" tests="tValueHolder">
45// </reviewed>
46
47// <synopsis>
48// Class ValueHolder is meant to be used for holding a single Casacore value.
49// The value can be scalar or an array of any basic type (including complex
50// and string). Also a Record value is possible.
51// In this way varying typed data (e.g. the result of getCell in the table DO)
52// can be packed in a strongly typed variable.
53// <br>All unsigned integer type values are kept as signed 32-bit integers
54// because scripting languages usually only support those types.
55//
56// ValueHolder is an envelope class that holds a counted-referenced letter
57// object <linkto class=ValueHolderRep>ValueHolderRep</linkto>.
58// </synopsis>
59
60// <motivation>
61// This class comes handy in passing arbitrary values from a DO to
62// its environment.
63// </motivation>
64
66{
67public:
68 // Construct a null object.
70 {}
71
72 // Create the object for the given value.
73 // <group>
78 explicit ValueHolder (Int value);
83 explicit ValueHolder (const Complex& value);
84 explicit ValueHolder (const DComplex& value);
85 explicit ValueHolder (const Char* value);
86 explicit ValueHolder (const String& value);
87 explicit ValueHolder (const Array<Bool>& value);
88 explicit ValueHolder (const Array<uChar>& value);
89 explicit ValueHolder (const Array<Short>& value);
90 explicit ValueHolder (const Array<uShort>& value);
91 explicit ValueHolder (const Array<Int>& value);
92 explicit ValueHolder (const Array<uInt>& value);
93 explicit ValueHolder (const Array<Int64>& value);
94 explicit ValueHolder (const Array<Float>& value);
95 explicit ValueHolder (const Array<Double>& value);
96 explicit ValueHolder (const Array<Complex>& value);
98 explicit ValueHolder (const Array<String>& value);
99 explicit ValueHolder (const Record& value);
100 // </group>
101
102 // Create an empty N-dim array (gets type TpOther).
104
105 // Create a ValueHolder from a ValueHolderRep.
106 // It takes over the pointer and deletes it in the destructor.
108 : itsRep (rep)
109 {}
110
111 // Copy constructor (reference semantics).
113
114 // Destructor.
116 {}
117
118 // Assignment (reference semantics).
120
121 // Is this a null object?
122 Bool isNull() const
123 { return !itsRep; }
124
125 // Get the data type (as defined in DataType.h).
126 // Note that TpOther is returned for an empty untyped array.
127 DataType dataType() const;
128
129 // Get the value.
130 // If possible, it converts the data as needed.
131 // <group>
132 Bool asBool () const;
133 uChar asuChar () const;
134 Short asShort () const;
135 uShort asuShort () const;
136 Int asInt () const;
137 uInt asuInt () const;
138 Int64 asInt64 () const;
139 Float asFloat () const;
140 Double asDouble () const;
141 Complex asComplex () const;
142 DComplex asDComplex() const;
143 const String& asString () const;
144 const Array<Bool> asArrayBool () const;
145 const Array<uChar> asArrayuChar () const;
146 const Array<Short> asArrayShort () const;
147 const Array<uShort> asArrayuShort () const;
148 const Array<Int> asArrayInt () const;
149 const Array<uInt> asArrayuInt () const;
150 const Array<Int64> asArrayInt64 () const;
151 const Array<Float> asArrayFloat () const;
152 const Array<Double> asArrayDouble () const;
153 const Array<Complex> asArrayComplex () const;
154 const Array<DComplex> asArrayDComplex() const;
155 const Array<String> asArrayString () const;
156 const Record& asRecord () const;
157 // </group>
158
159 // Get the data in a way useful for templates.
160 // If possible, it converts the the data as needed.
161 // <group>
162 void getValue (Bool& value) const { value = asBool(); }
163 void getValue (uChar& value) const { value = asuChar(); }
164 void getValue (Short& value) const { value = asShort(); }
165 void getValue (uShort& value) const { value = asuShort(); }
166 void getValue (Int& value) const { value = asInt(); }
167 void getValue (uInt& value) const { value = asuInt(); }
168 void getValue (Int64& value) const { value = asInt64(); }
169 void getValue (Float& value) const { value = asFloat(); }
170 void getValue (Double& value) const { value = asDouble(); }
171 void getValue (Complex& value) const { value = asComplex(); }
172 void getValue (DComplex& value) const { value = asDComplex(); }
173 void getValue (String& value) const { value = asString(); }
175 { value.reference(asArrayBool()); }
177 { value.reference(asArrayuChar()); }
179 { value.reference(asArrayShort()); }
181 { value.reference(asArrayuShort()); }
183 { value.reference(asArrayInt()); }
185 { value.reference(asArrayuInt()); }
187 { value.reference(asArrayInt64()); }
189 { value.reference(asArrayFloat()); }
191 { value.reference(asArrayDouble()); }
193 { value.reference(asArrayComplex()); }
195 { value.reference(asArrayDComplex()); }
197 { value.reference(asArrayString()); }
198 // </group>
199
200 // Put the value as a field in a record.
201 void toRecord (Record&, const RecordFieldId&) const;
202
203 // Construct the object from the value in a record.
204 static ValueHolder fromRecord (const Record&, const RecordFieldId&);
205
206 // Compare two ValueHolder objects.
207 // They must have the same data type.
208 bool operator< (const ValueHolder& right) const
209 { return itsRep->operator< (*right.itsRep); }
210
211 // Write the ValueHolder to an output stream.
212 // Arrays are written as normal arrays using ArrayIO.h.
213 friend std::ostream& operator<< (std::ostream& os, const ValueHolder& vh)
214 { return vh.itsRep->write (os); }
215
216private:
217
218 std::shared_ptr<ValueHolderRep> itsRep;
219};
220
221
222inline DataType ValueHolder::dataType() const
223 { return itsRep->dataType(); }
224inline void ValueHolder::toRecord (Record& rec, const RecordFieldId& id) const
225 { return itsRep->toRecord (rec, id); }
227 const RecordFieldId& id)
228 { return ValueHolder (ValueHolderRep::fromRecord (rec, id)); }
230 { return itsRep->asBool(); }
232 { return itsRep->asuChar(); }
234 { return itsRep->asShort(); }
236 { return itsRep->asuShort(); }
238 { return itsRep->asInt(); }
240 { return itsRep->asuInt(); }
242 { return itsRep->asInt64(); }
244 { return itsRep->asFloat(); }
246 { return itsRep->asDouble(); }
247inline Complex ValueHolder::asComplex() const
248 { return itsRep->asComplex(); }
249inline DComplex ValueHolder::asDComplex() const
250 { return itsRep->asDComplex(); }
251inline const String& ValueHolder::asString() const
252 { return itsRep->asString(); }
254 { return itsRep->asArrayBool(); }
256 { return itsRep->asArrayuChar(); }
258 { return itsRep->asArrayShort(); }
260 { return itsRep->asArrayuShort(); }
262 { return itsRep->asArrayInt(); }
264 { return itsRep->asArrayuInt(); }
266 { return itsRep->asArrayInt64(); }
268 { return itsRep->asArrayFloat(); }
270 { return itsRep->asArrayDouble(); }
272 { return itsRep->asArrayComplex(); }
274 { return itsRep->asArrayDComplex(); }
276 { return itsRep->asArrayString(); }
277inline const Record& ValueHolder::asRecord() const
278 { return itsRep->asRecord(); }
279
280
281} //# NAMESPACE CASACORE - END
282
283#endif
const RecordInterface & asRecord(const RecordFieldId &) const override
String: the storage and methods of handling collections of characters.
Definition String.h:223
static ValueHolderRep * fromRecord(const Record &rec, const RecordFieldId &)
Construct the object from the value in a record.
void getValue(Float &value) const
ValueHolder(const Array< uChar > &value)
ValueHolder(uShort value)
void getValue(Complex &value) const
std::shared_ptr< ValueHolderRep > itsRep
Complex asComplex() const
const Array< Complex > asArrayComplex() const
ValueHolder(const Array< uInt > &value)
const Array< String > asArrayString() const
~ValueHolder()
Destructor.
ValueHolder(ValueHolderRep *rep)
Create a ValueHolder from a ValueHolderRep.
ValueHolder(Float value)
const Array< DComplex > asArrayDComplex() const
Double asDouble() const
ValueHolder(const Array< Bool > &value)
Int64 asInt64() const
ValueHolder(uInt ndim, Bool dummy)
Create an empty N-dim array (gets type TpOther).
void toRecord(Record &, const RecordFieldId &) const
Put the value as a field in a record.
void getValue(Array< Float > &value) const
ValueHolder(const Array< DComplex > &value)
ValueHolder(const DComplex &value)
ValueHolder(const Array< Float > &value)
void getValue(Array< uShort > &value) const
static ValueHolder fromRecord(const Record &, const RecordFieldId &)
Construct the object from the value in a record.
void getValue(String &value) const
Float asFloat() const
void getValue(Array< uChar > &value) const
Bool isNull() const
Is this a null object?
const Array< Int64 > asArrayInt64() const
Short asShort() const
void getValue(Bool &value) const
Get the data in a way useful for templates.
void getValue(Array< String > &value) const
ValueHolder(const Array< String > &value)
ValueHolder(const Array< Short > &value)
friend std::ostream & operator<<(std::ostream &os, const ValueHolder &vh)
Write the ValueHolder to an output stream.
void getValue(Array< DComplex > &value) const
const Array< uChar > asArrayuChar() const
ValueHolder(const Array< Complex > &value)
uChar asuChar() const
ValueHolder(Short value)
const Array< uShort > asArrayuShort() const
ValueHolder(const Array< Int > &value)
const Array< uInt > asArrayuInt() const
ValueHolder(const Complex &value)
void getValue(uShort &value) const
void getValue(Array< Short > &value) const
ValueHolder(const ValueHolder &)
Copy constructor (reference semantics).
const Record & asRecord() const
void getValue(DComplex &value) const
ValueHolder()
Construct a null object.
Definition ValueHolder.h:69
ValueHolder(const Record &value)
const String & asString() const
void getValue(Short &value) const
uShort asuShort() const
void getValue(Array< Double > &value) const
void getValue(Array< uInt > &value) const
void getValue(Array< Bool > &value) const
void getValue(Double &value) const
const Array< Float > asArrayFloat() const
const Array< Bool > asArrayBool() const
void getValue(Int &value) const
void getValue(Array< Int > &value) const
DataType dataType() const
Get the data type (as defined in DataType.h).
void getValue(uInt &value) const
ValueHolder(uInt value)
const Array< Short > asArrayShort() const
ValueHolder(const Array< Double > &value)
ValueHolder(Int64 value)
Bool asBool() const
Get the value.
const Array< Int > asArrayInt() const
ValueHolder & operator=(const ValueHolder &)
Assignment (reference semantics).
const Array< Double > asArrayDouble() const
ValueHolder(Double value)
void getValue(Int64 &value) const
DComplex asDComplex() const
ValueHolder(const Char *value)
void getValue(uChar &value) const
ValueHolder(const Array< uShort > &value)
ValueHolder(uChar value)
ValueHolder(Bool value)
Create the object for the given value.
ValueHolder(const String &value)
ValueHolder(const Array< Int64 > &value)
void getValue(Array< Int64 > &value) const
void getValue(Array< Complex > &value) const
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned char uChar
Definition aipstype.h:45
LatticeExprNode ndim(const LatticeExprNode &expr)
1-argument function to get the dimensionality of a lattice.
short Short
Definition aipstype.h:46
unsigned int uInt
Definition aipstype.h:49
unsigned short uShort
Definition aipstype.h:47
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
double Double
Definition aipstype.h:53
char Char
Definition aipstype.h:44