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