casa  5.7.0-16
 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>
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 (gets type TpOther).
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  // Note that TpOther is returned for an empty untyped array.
129  DataType dataType() const;
130 
131  // Get the value.
132  // If possible, it converts the data as needed.
133  // <group>
134  Bool asBool () const;
135  uChar asuChar () const;
136  Short asShort () const;
137  uShort asuShort () const;
138  Int asInt () const;
139  uInt asuInt () const;
140  Int64 asInt64 () const;
141  Float asFloat () const;
142  Double asDouble () const;
143  Complex asComplex () const;
144  DComplex asDComplex() const;
145  const String& asString () const;
146  const Array<Bool> asArrayBool () const;
147  const Array<uChar> asArrayuChar () const;
148  const Array<Short> asArrayShort () const;
149  const Array<uShort> asArrayuShort () const;
150  const Array<Int> asArrayInt () const;
151  const Array<uInt> asArrayuInt () const;
152  const Array<Int64> asArrayInt64 () const;
153  const Array<Float> asArrayFloat () const;
154  const Array<Double> asArrayDouble () const;
155  const Array<Complex> asArrayComplex () const;
156  const Array<DComplex> asArrayDComplex() const;
157  const Array<String> asArrayString () const;
158  const Record& asRecord () const;
159  // </group>
160 
161  // Get the data in a way useful for templates.
162  // If possible, it converts the the data as needed.
163  // <group>
164  void getValue (Bool& value) const { value = asBool(); }
165  void getValue (uChar& value) const { value = asuChar(); }
166  void getValue (Short& value) const { value = asShort(); }
167  void getValue (uShort& value) const { value = asuShort(); }
168  void getValue (Int& value) const { value = asInt(); }
169  void getValue (uInt& value) const { value = asuInt(); }
170  void getValue (Int64& value) const { value = asInt64(); }
171  void getValue (Float& value) const { value = asFloat(); }
172  void getValue (Double& value) const { value = asDouble(); }
173  void getValue (Complex& value) const { value = asComplex(); }
174  void getValue (DComplex& value) const { value = asDComplex(); }
175  void getValue (String& value) const { value = asString(); }
176  void getValue (Array<Bool>& value) const
177  { value.reference(asArrayBool()); }
179  { value.reference(asArrayuChar()); }
181  { value.reference(asArrayShort()); }
183  { value.reference(asArrayuShort()); }
184  void getValue (Array<Int>& value) const
185  { value.reference(asArrayInt()); }
186  void getValue (Array<uInt>& value) const
187  { value.reference(asArrayuInt()); }
189  { value.reference(asArrayInt64()); }
191  { value.reference(asArrayFloat()); }
193  { value.reference(asArrayDouble()); }
195  { value.reference(asArrayComplex()); }
197  { value.reference(asArrayDComplex()); }
199  { value.reference(asArrayString()); }
200  // </group>
201 
202  // Put the value as a field in a record.
203  void toRecord (Record&, const RecordFieldId&) const;
204 
205  // Construct the object from the value in a record.
206  static ValueHolder fromRecord (const Record&, const RecordFieldId&);
207 
208  // Compare two ValueHolder objects.
209  // They must have the same data type.
210  bool operator< (const ValueHolder& right) const
211  { return itsRep->operator< (*right.itsRep); }
212 
213  // Write the ValueHolder to an output stream.
214  // Arrays are written as normal arrays using ArrayIO.h.
215  friend std::ostream& operator<< (std::ostream& os, const ValueHolder& vh)
216  { return vh.itsRep->write (os); }
217 
218 private:
219 
221 };
222 
223 
224 inline DataType ValueHolder::dataType() const
225  { return itsRep->dataType(); }
226 inline void ValueHolder::toRecord (Record& rec, const RecordFieldId& id) const
227  { return itsRep->toRecord (rec, id); }
229  const RecordFieldId& id)
230  { return ValueHolder (ValueHolderRep::fromRecord (rec, id)); }
231 inline Bool ValueHolder::asBool() const
232  { return itsRep->asBool(); }
234  { return itsRep->asuChar(); }
236  { return itsRep->asShort(); }
238  { return itsRep->asuShort(); }
239 inline Int ValueHolder::asInt() const
240  { return itsRep->asInt(); }
241 inline uInt ValueHolder::asuInt() const
242  { return itsRep->asuInt(); }
244  { return itsRep->asInt64(); }
246  { return itsRep->asFloat(); }
248  { return itsRep->asDouble(); }
250  { return itsRep->asComplex(); }
252  { return itsRep->asDComplex(); }
253 inline const String& ValueHolder::asString() const
254  { return itsRep->asString(); }
256  { return itsRep->asArrayBool(); }
258  { return itsRep->asArrayuChar(); }
260  { return itsRep->asArrayShort(); }
262  { return itsRep->asArrayuShort(); }
264  { return itsRep->asArrayInt(); }
266  { return itsRep->asArrayuInt(); }
268  { return itsRep->asArrayInt64(); }
270  { return itsRep->asArrayFloat(); }
272  { return itsRep->asArrayDouble(); }
274  { return itsRep->asArrayComplex(); }
276  { return itsRep->asArrayDComplex(); }
278  { return itsRep->asArrayString(); }
279 inline const Record& ValueHolder::asRecord() const
280  { return itsRep->asRecord(); }
281 
282 
283 } //# NAMESPACE CASACORE - END
284 
285 #endif
DataType dataType() const
Get the data type (as defined in DataType.h).
Definition: ValueHolder.h:224
void getValue(Array< Int > &value) const
Definition: ValueHolder.h:184
const String & asString() const
Definition: ValueHolder.h:253
uChar asuChar() const
Definition: ValueHolder.h:233
Bool asBool() const
Get the value.
Definition: ValueHolder.h:231
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:263
void getValue(Int64 &value) const
Definition: ValueHolder.h:170
void getValue(Array< uShort > &value) const
Definition: ValueHolder.h:182
void getValue(Bool &value) const
Get the data in a way useful for templates.
Definition: ValueHolder.h:164
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:174
ValueHolder & operator=(const ValueHolder &)
Assignment (reference semantics).
const Array< String > asArrayString() const
Definition: ValueHolder.h:277
void getValue(Array< uInt > &value) const
Definition: ValueHolder.h:186
void getValue(Float &value) const
Definition: ValueHolder.h:171
Double asDouble() const
Definition: ValueHolder.h:247
void getValue(uChar &value) const
Definition: ValueHolder.h:165
DComplex asDComplex() const
Definition: ValueHolder.h:251
unsigned char uChar
Definition: aipstype.h:47
void getValue(Int &value) const
Definition: ValueHolder.h:168
const Array< uShort > asArrayuShort() const
Definition: ValueHolder.h:261
void getValue(Complex &value) const
Definition: ValueHolder.h:173
char Char
Definition: aipstype.h:46
void getValue(String &value) const
Definition: ValueHolder.h:175
const Array< Double > asArrayDouble() const
Definition: ValueHolder.h:271
ValueHolder(ValueHolderRep *rep)
Create a ValueHolder from a ValueHolderRep.
Definition: ValueHolder.h:109
uShort asuShort() const
Definition: ValueHolder.h:237
void getValue(uInt &value) const
Definition: ValueHolder.h:169
const Array< uChar > asArrayuChar() const
Definition: ValueHolder.h:257
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:48
void getValue(Short &value) const
Definition: ValueHolder.h:166
Float asFloat() const
Definition: ValueHolder.h:245
Int64 asInt64() const
Definition: ValueHolder.h:243
Bool isNull() const
Is this a null object?
Definition: ValueHolder.h:124
uInt asuInt() const
Definition: ValueHolder.h:241
Referenced counted pointer for constant data.
Definition: VisModelData.h:42
The identification of a record field.
Definition: RecordFieldId.h:91
void getValue(Array< Int64 > &value) const
Definition: ValueHolder.h:188
const Array< Int64 > asArrayInt64() const
Definition: ValueHolder.h:267
Complex asComplex() const
Definition: ValueHolder.h:249
void getValue(Double &value) const
Definition: ValueHolder.h:172
static ValueHolder fromRecord(const Record &, const RecordFieldId &)
Construct the object from the value in a record.
Definition: ValueHolder.h:228
void getValue(Array< Double > &value) const
Definition: ValueHolder.h:192
const Array< Short > asArrayShort() const
Definition: ValueHolder.h:259
void getValue(Array< Short > &value) const
Definition: ValueHolder.h:180
void getValue(Array< DComplex > &value) const
Definition: ValueHolder.h:196
double Double
Definition: aipstype.h:55
A holder for a value of any basic Casacore data type.
Definition: ValueHolder.h:67
const Array< Float > asArrayFloat() const
Definition: ValueHolder.h:269
const Array< Complex > asArrayComplex() const
Definition: ValueHolder.h:273
LatticeExprNode ndim(const LatticeExprNode &expr)
1-argument function to get the dimensionality of a lattice.
Short asShort() const
Definition: ValueHolder.h:235
void toRecord(Record &, const RecordFieldId &) const
Put the value as a field in a record.
Definition: ValueHolder.h:226
A hierarchical collection of named fields of various types.
Definition: Record.h:180
Int asInt() const
Definition: ValueHolder.h:239
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
const Array< Bool > asArrayBool() const
Definition: ValueHolder.h:255
void getValue(Array< Float > &value) const
Definition: ValueHolder.h:190
ValueHolder()
Construct a null object.
Definition: ValueHolder.h:71
float Float
Definition: aipstype.h:54
void getValue(Array< Bool > &value) const
Definition: ValueHolder.h:176
bool operator<(const ValueHolder &right) const
Compare two ValueHolder objects.
Definition: ValueHolder.h:210
void getValue(Array< Complex > &value) const
Definition: ValueHolder.h:194
String: the storage and methods of handling collections of characters.
Definition: String.h:223
CountedPtr< ValueHolderRep > itsRep
Definition: ValueHolder.h:220
const Array< DComplex > asArrayDComplex() const
Definition: ValueHolder.h:275
const Record & asRecord() const
Definition: ValueHolder.h:279
~ValueHolder()
Destructor.
Definition: ValueHolder.h:117
void getValue(Array< String > &value) const
Definition: ValueHolder.h:198
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:178
void getValue(uShort &value) const
Definition: ValueHolder.h:167
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:215
const Array< uInt > asArrayuInt() const
Definition: ValueHolder.h:265
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42