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 (gets type TpOther).
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).
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
casacore::ValueHolder::asArrayInt
const Array< Int > asArrayInt() const
Definition: ValueHolder.h:263
casacore::ValueHolder::ValueHolder
ValueHolder(Bool value)
Create the object for the given value.
casacore::ValueHolder::getValue
void getValue(uInt &value) const
Definition: ValueHolder.h:169
casacore::ValueHolder::asArrayDouble
const Array< Double > asArrayDouble() const
Definition: ValueHolder.h:271
casacore::ValueHolder::ValueHolder
ValueHolder(const Record &value)
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< uShort > &value)
casacore::ValueHolder::ValueHolder
ValueHolder(uInt ndim, Bool dummy)
Create an empty N-dim array (gets type TpOther).
casacore::ValueHolder::asuInt
uInt asuInt() const
Definition: ValueHolder.h:241
casacore::ValueHolder::asArrayuChar
const Array< uChar > asArrayuChar() const
Definition: ValueHolder.h:257
casacore::ValueHolder::getValue
void getValue(Complex &value) const
Definition: ValueHolder.h:173
casacore::ValueHolder::asArrayInt64
const Array< Int64 > asArrayInt64() const
Definition: ValueHolder.h:267
casacore::ValueHolder::toRecord
void toRecord(Record &, const RecordFieldId &) const
Put the value as a field in a record.
Definition: ValueHolder.h:226
casacore::ValueHolder::ValueHolder
ValueHolder(Int64 value)
Complexfwd_global_functions_Complexfwd::casacore::DComplex
std::complex< Double > DComplex
Definition: Complexfwd.h:50
casacore::ValueHolder::asArrayString
const Array< String > asArrayString() const
Definition: ValueHolder.h:277
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< DComplex > &value)
casacore::CountedPtr
Referenced counted pointer for constant data.
Definition: CountedPtr.h:81
casacore::ValueHolder::asDComplex
DComplex asDComplex() const
Definition: ValueHolder.h:251
casacore::uChar
unsigned char uChar
Definition: aipstype.h:47
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< Bool > &value)
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< Int64 > &value)
casacore::ndim
LatticeExprNode ndim(const LatticeExprNode &expr)
1-argument function to get the dimensionality of a lattice.
casacore::ValueHolder::getValue
void getValue(DComplex &value) const
Definition: ValueHolder.h:174
casacore::ValueHolder::ValueHolder
ValueHolder(const Char *value)
casacore::ValueHolder::asArrayBool
const Array< Bool > asArrayBool() const
Definition: ValueHolder.h:255
casacore::ValueHolder::operator<<
friend std::ostream & operator<<(std::ostream &os, const ValueHolder &vh)
Write the ValueHolder to an output stream.
Definition: ValueHolder.h:215
casacore::ValueHolder::getValue
void getValue(Array< DComplex > &value) const
Definition: ValueHolder.h:196
casacore::ValueHolder::getValue
void getValue(Array< Bool > &value) const
Definition: ValueHolder.h:176
casacore::ValueHolderRep
A holder for a value of any basic type.
Definition: ValueHolderRep.h:66
casacore::ValueHolder::asRecord
const Record & asRecord() const
Definition: ValueHolder.h:279
casacore::ValueHolder::getValue
void getValue(Float &value) const
Definition: ValueHolder.h:171
casacore::ValueHolder::asComplex
Complex asComplex() const
Definition: ValueHolder.h:249
casacore::ValueHolder::fromRecord
static ValueHolder fromRecord(const Record &, const RecordFieldId &)
Construct the object from the value in a record.
Definition: ValueHolder.h:228
casacore::ValueHolder::asArrayFloat
const Array< Float > asArrayFloat() const
Definition: ValueHolder.h:269
casacore::ValueHolder::getValue
void getValue(String &value) const
Definition: ValueHolder.h:175
casacore::ValueHolder::asInt64
Int64 asInt64() const
Definition: ValueHolder.h:243
casacore::ValueHolder::dataType
DataType dataType() const
Get the data type (as defined in DataType.h).
Definition: ValueHolder.h:224
casacore::ValueHolder::asDouble
Double asDouble() const
Definition: ValueHolder.h:247
casacore::Float
float Float
Definition: aipstype.h:54
casacore::ValueHolder::ValueHolder
ValueHolder(const ValueHolder &)
Copy constructor (reference semantics).
casacore::ValueHolder::ValueHolder
ValueHolder(ValueHolderRep *rep)
Create a ValueHolder from a ValueHolderRep.
Definition: ValueHolder.h:109
casacore::value
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
casacore::uShort
unsigned short uShort
Definition: aipstype.h:49
casacore::Double
double Double
Definition: aipstype.h:55
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< uInt > &value)
casacore::ValueHolder::getValue
void getValue(Array< Int64 > &value) const
Definition: ValueHolder.h:188
casacore::ValueHolder::getValue
void getValue(Bool &value) const
Get the data in a way useful for templates.
Definition: ValueHolder.h:164
casacore::ValueHolder::asArrayDComplex
const Array< DComplex > asArrayDComplex() const
Definition: ValueHolder.h:275
casacore::ValueHolder::asArrayuInt
const Array< uInt > asArrayuInt() const
Definition: ValueHolder.h:265
casacore::ValueHolder::ValueHolder
ValueHolder(Float value)
casacore::uInt
unsigned int uInt
Definition: aipstype.h:51
casacore::ValueHolder::getValue
void getValue(uChar &value) const
Definition: ValueHolder.h:165
casacore::ValueHolder::asInt
Int asInt() const
Definition: ValueHolder.h:239
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< Complex > &value)
casacore::ValueHolder::getValue
void getValue(Int64 &value) const
Definition: ValueHolder.h:170
casacore::ValueHolder::asShort
Short asShort() const
Definition: ValueHolder.h:235
casacore::ValueHolder::getValue
void getValue(uShort &value) const
Definition: ValueHolder.h:167
casacore::ValueHolder::getValue
void getValue(Array< String > &value) const
Definition: ValueHolder.h:198
casacore::ValueHolderRep::fromRecord
static ValueHolderRep * fromRecord(const Record &rec, const RecordFieldId &)
Construct the object from the value in a record.
casacore::ValueHolder::getValue
void getValue(Array< Double > &value) const
Definition: ValueHolder.h:192
casacore::ValueHolder::asuShort
uShort asuShort() const
Definition: ValueHolder.h:237
casacore::ValueHolder::ValueHolder
ValueHolder(uInt value)
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< Int > &value)
casacore::ValueHolder::ValueHolder
ValueHolder(uShort value)
casacore::Int
int Int
Definition: aipstype.h:50
casacore
this file contains all the compiler specific defines
Definition: mainpage.dox:28
casacore::ValueHolder::itsRep
CountedPtr< ValueHolderRep > itsRep
Definition: ValueHolder.h:220
casacore::ValueHolder::isNull
Bool isNull() const
Is this a null object?
Definition: ValueHolder.h:124
casacore::ValueHolder::ValueHolder
ValueHolder(uChar value)
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< String > &value)
casacore::ValueHolder::ValueHolder
ValueHolder(const String &value)
casacore::ValueHolder::getValue
void getValue(Double &value) const
Definition: ValueHolder.h:172
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< Short > &value)
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< Float > &value)
casacore::ValueHolder::getValue
void getValue(Array< Int > &value) const
Definition: ValueHolder.h:184
casacore::ValueHolder::getValue
void getValue(Array< uInt > &value) const
Definition: ValueHolder.h:186
casacore::ValueHolder::~ValueHolder
~ValueHolder()
Destructor.
Definition: ValueHolder.h:117
casacore::ValueHolder::getValue
void getValue(Array< uShort > &value) const
Definition: ValueHolder.h:182
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< Double > &value)
casacore::Int64
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
casacore::ValueHolder::ValueHolder
ValueHolder(Double value)
casacore::ValueHolder::asString
const String & asString() const
Definition: ValueHolder.h:253
casacore::ValueHolder::ValueHolder
ValueHolder(const Complex &value)
casacore::ValueHolder::getValue
void getValue(Array< Short > &value) const
Definition: ValueHolder.h:180
casacore::Array< Bool >
casacore::ValueHolder::getValue
void getValue(Array< uChar > &value) const
Definition: ValueHolder.h:178
casacore::ValueHolder::getValue
void getValue(Short &value) const
Definition: ValueHolder.h:166
casacore::String
String: the storage and methods of handling collections of characters.
Definition: String.h:223
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
casacore::ValueHolder
A holder for a value of any basic Casacore data type.
Definition: ValueHolder.h:68
casacore::ValueHolder::getValue
void getValue(Array< Float > &value) const
Definition: ValueHolder.h:190
Complexfwd_global_functions_Complexfwd::casacore::Complex
std::complex< Float > Complex
Definition: Complexfwd.h:49
casacore::ValueHolder::ValueHolder
ValueHolder(Int value)
casacore::ValueHolder::asArrayShort
const Array< Short > asArrayShort() const
Definition: ValueHolder.h:259
casacore::ValueHolder::asArrayComplex
const Array< Complex > asArrayComplex() const
Definition: ValueHolder.h:273
casacore::Record
A hierarchical collection of named fields of various types.
Definition: Record.h:181
casacore::ValueHolder::asArrayuShort
const Array< uShort > asArrayuShort() const
Definition: ValueHolder.h:261
casacore::Short
short Short
Definition: aipstype.h:48
casacore::ValueHolder::asFloat
Float asFloat() const
Definition: ValueHolder.h:245
casacore::ValueHolder::asuChar
uChar asuChar() const
Definition: ValueHolder.h:233
casacore::ValueHolder::ValueHolder
ValueHolder(const Array< uChar > &value)
casacore::ValueHolder::getValue
void getValue(Array< Complex > &value) const
Definition: ValueHolder.h:194
casacore::ValueHolder::getValue
void getValue(Int &value) const
Definition: ValueHolder.h:168
casacore::ValueHolder::ValueHolder
ValueHolder()
Construct a null object.
Definition: ValueHolder.h:71
casacore::ValueHolder::ValueHolder
ValueHolder(Short value)
casacore::Char
char Char
Definition: aipstype.h:46
casacore::ValueHolder::operator=
ValueHolder & operator=(const ValueHolder &)
Assignment (reference semantics).
casacore::RecordFieldId
The identification of a record field.
Definition: RecordFieldId.h:92
casacore::ValueHolder::asBool
Bool asBool() const
Get the value.
Definition: ValueHolder.h:231
casacore::ValueHolder::ValueHolder
ValueHolder(const DComplex &value)
casacore::ValueHolder::operator<
bool operator<(const ValueHolder &right) const
Compare two ValueHolder objects.
Definition: ValueHolder.h:210