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