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#ifndef CASA_VALUEHOLDER_H
27#define CASA_VALUEHOLDER_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Containers/ValueHolderRep.h>
32#include <casacore/casa/Arrays/Array.h>
33#include <memory>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// <summary>
38// A holder for a value of any basic Casacore data type.
39// </summary>
40
41// <use visibility=export>
42// <reviewed reviewer="" date="" tests="tValueHolder">
43// </reviewed>
44
45// <synopsis>
46// Class ValueHolder is meant to be used for holding a single Casacore value.
47// The value can be scalar or an array of any basic type (including complex
48// and string). Also a Record value is possible.
49// In this way varying typed data (e.g. the result of getCell in the table DO)
50// can be packed in a strongly typed variable.
51// <br>All unsigned integer type values are kept as signed 32-bit integers
52// because scripting languages usually only support those types.
53//
54// ValueHolder is an envelope class that holds a counted-referenced letter
55// object <linkto class=ValueHolderRep>ValueHolderRep</linkto>.
56// </synopsis>
57
58// <motivation>
59// This class comes handy in passing arbitrary values from a DO to
60// its environment.
61// </motivation>
62
64 public:
65 // Construct a null object.
67
68 // Create the object for the given value.
69 // <group>
79 explicit ValueHolder(const Complex& value);
80 explicit ValueHolder(const DComplex& value);
81 explicit ValueHolder(const Char* value);
82 explicit ValueHolder(const String& value);
83 explicit ValueHolder(const Array<Bool>& value);
84 explicit ValueHolder(const Array<uChar>& value);
85 explicit ValueHolder(const Array<Short>& value);
87 explicit ValueHolder(const Array<Int>& value);
88 explicit ValueHolder(const Array<uInt>& value);
89 explicit ValueHolder(const Array<Int64>& value);
90 explicit ValueHolder(const Array<Float>& value);
95 explicit ValueHolder(const Record& value);
96 // </group>
97
98 // Create an empty N-dim array (gets type TpOther).
100
101 // Create a ValueHolder from a ValueHolderRep.
102 // It takes over the pointer and deletes it in the destructor.
103 explicit ValueHolder(ValueHolderRep* rep) : itsRep(rep) {}
104
105 // Copy constructor (reference semantics).
107
108 // Destructor.
110
111 // Assignment (reference semantics).
113
114 // Is this a null object?
115 Bool isNull() const { return !itsRep; }
116
117 // Get the data type (as defined in DataType.h).
118 // Note that TpOther is returned for an empty untyped array.
119 DataType dataType() const;
120
121 // Get the value.
122 // If possible, it converts the data as needed.
123 // <group>
124 Bool asBool() const;
125 uChar asuChar() const;
126 Short asShort() const;
127 uShort asuShort() const;
128 Int asInt() const;
129 uInt asuInt() const;
130 Int64 asInt64() const;
131 Float asFloat() const;
132 Double asDouble() const;
133 Complex asComplex() const;
134 DComplex asDComplex() const;
135 const String& asString() const;
136 const Array<Bool> asArrayBool() const;
137 const Array<uChar> asArrayuChar() const;
138 const Array<Short> asArrayShort() const;
139 const Array<uShort> asArrayuShort() const;
140 const Array<Int> asArrayInt() const;
141 const Array<uInt> asArrayuInt() const;
142 const Array<Int64> asArrayInt64() const;
143 const Array<Float> asArrayFloat() const;
144 const Array<Double> asArrayDouble() const;
145 const Array<Complex> asArrayComplex() const;
146 const Array<DComplex> asArrayDComplex() const;
147 const Array<String> asArrayString() const;
148 const Record& asRecord() const;
149 // </group>
150
151 // Get the data in a way useful for templates.
152 // If possible, it converts the the data as needed.
153 // <group>
154 void getValue(Bool& value) const { value = asBool(); }
155 void getValue(uChar& value) const { value = asuChar(); }
156 void getValue(Short& value) const { value = asShort(); }
157 void getValue(uShort& value) const { value = asuShort(); }
158 void getValue(Int& value) const { value = asInt(); }
159 void getValue(uInt& value) const { value = asuInt(); }
160 void getValue(Int64& value) const { value = asInt64(); }
161 void getValue(Float& value) const { value = asFloat(); }
162 void getValue(Double& value) const { value = asDouble(); }
163 void getValue(Complex& value) const { value = asComplex(); }
164 void getValue(DComplex& value) const { value = asDComplex(); }
165 void getValue(String& value) const { value = asString(); }
166 void getValue(Array<Bool>& value) const { value.reference(asArrayBool()); }
167 void getValue(Array<uChar>& value) const { value.reference(asArrayuChar()); }
168 void getValue(Array<Short>& value) const { value.reference(asArrayShort()); }
169 void getValue(Array<uShort>& value) const { value.reference(asArrayuShort()); }
170 void getValue(Array<Int>& value) const { value.reference(asArrayInt()); }
171 void getValue(Array<uInt>& value) const { value.reference(asArrayuInt()); }
172 void getValue(Array<Int64>& value) const { value.reference(asArrayInt64()); }
173 void getValue(Array<Float>& value) const { value.reference(asArrayFloat()); }
174 void getValue(Array<Double>& value) const { value.reference(asArrayDouble()); }
175 void getValue(Array<Complex>& value) const { value.reference(asArrayComplex()); }
176 void getValue(Array<DComplex>& value) const { value.reference(asArrayDComplex()); }
177 void getValue(Array<String>& value) const { value.reference(asArrayString()); }
178 // </group>
179
180 // Put the value as a field in a record.
181 void toRecord(Record&, const RecordFieldId&) const;
182
183 // Construct the object from the value in a record.
184 static ValueHolder fromRecord(const Record&, const RecordFieldId&);
185
186 // Compare two ValueHolder objects.
187 // They must have the same data type.
188 bool operator<(const ValueHolder& right) const { return itsRep->operator<(*right.itsRep); }
189
190 // Write the ValueHolder to an output stream.
191 // Arrays are written as normal arrays using ArrayIO.h.
192 friend std::ostream& operator<<(std::ostream& os, const ValueHolder& vh) {
193 return vh.itsRep->write(os);
194 }
195
196 private:
197 std::shared_ptr<ValueHolderRep> itsRep;
198};
199
200inline DataType ValueHolder::dataType() const { return itsRep->dataType(); }
201inline void ValueHolder::toRecord(Record& rec, const RecordFieldId& id) const {
202 return itsRep->toRecord(rec, id);
203}
206}
207inline Bool ValueHolder::asBool() const { return itsRep->asBool(); }
208inline uChar ValueHolder::asuChar() const { return itsRep->asuChar(); }
209inline Short ValueHolder::asShort() const { return itsRep->asShort(); }
210inline uShort ValueHolder::asuShort() const { return itsRep->asuShort(); }
211inline Int ValueHolder::asInt() const { return itsRep->asInt(); }
212inline uInt ValueHolder::asuInt() const { return itsRep->asuInt(); }
213inline Int64 ValueHolder::asInt64() const { return itsRep->asInt64(); }
214inline Float ValueHolder::asFloat() const { return itsRep->asFloat(); }
215inline Double ValueHolder::asDouble() const { return itsRep->asDouble(); }
216inline Complex ValueHolder::asComplex() const { return itsRep->asComplex(); }
217inline DComplex ValueHolder::asDComplex() const { return itsRep->asDComplex(); }
218inline const String& ValueHolder::asString() const { return itsRep->asString(); }
219inline const Array<Bool> ValueHolder::asArrayBool() const { return itsRep->asArrayBool(); }
220inline const Array<uChar> ValueHolder::asArrayuChar() const { return itsRep->asArrayuChar(); }
221inline const Array<Short> ValueHolder::asArrayShort() const { return itsRep->asArrayShort(); }
222inline const Array<uShort> ValueHolder::asArrayuShort() const { return itsRep->asArrayuShort(); }
223inline const Array<Int> ValueHolder::asArrayInt() const { return itsRep->asArrayInt(); }
224inline const Array<uInt> ValueHolder::asArrayuInt() const { return itsRep->asArrayuInt(); }
225inline const Array<Int64> ValueHolder::asArrayInt64() const { return itsRep->asArrayInt64(); }
226inline const Array<Float> ValueHolder::asArrayFloat() const { return itsRep->asArrayFloat(); }
227inline const Array<Double> ValueHolder::asArrayDouble() const { return itsRep->asArrayDouble(); }
228inline const Array<Complex> ValueHolder::asArrayComplex() const { return itsRep->asArrayComplex(); }
230 return itsRep->asArrayDComplex();
231}
232inline const Array<String> ValueHolder::asArrayString() const { return itsRep->asArrayString(); }
233inline const Record& ValueHolder::asRecord() const { return itsRep->asRecord(); }
234
235} // namespace casacore
236
237#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
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:66
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).
bool operator<(const ValueHolder &right) const
Compare two ValueHolder objects.
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
For temporary backward namespace compatibility, use casa as alias for casacore.
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
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
double Double
Definition aipstype.h:53
char Char
Definition aipstype.h:44