00001 //# ValueHolder.h: A holder object for the standard Casacore data types 00002 //# Copyright (C) 2005 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# 00027 //# $Id$ 00028 00029 00030 #ifndef CASA_VALUEHOLDER_H 00031 #define CASA_VALUEHOLDER_H 00032 00033 //# Includes 00034 #include <casacore/casa/aips.h> 00035 #include <casacore/casa/Containers/ValueHolderRep.h> 00036 #include <casacore/casa/Arrays/Array.h> 00037 00038 namespace casacore { //# NAMESPACE CASACORE - BEGIN 00039 00040 00041 // <summary> 00042 // A holder for a value of any basic Casacore data type. 00043 // </summary> 00044 00045 // <use visibility=export> 00046 // <reviewed reviewer="" date="" tests="tValueHolder"> 00047 // </reviewed> 00048 00049 // <synopsis> 00050 // Class ValueHolder is meant to be used for holding a single Casacore value. 00051 // The value can be scalar or an array of any basic type (including complex 00052 // and string). Also a Record value is possible. 00053 // In this way varying typed data (e.g. the result of getCell in the table DO) 00054 // can be packed in a strongly typed variable. 00055 // <br>All unsigned integer type values are kept as signed 32-bit integers 00056 // because scripting languages usually only support those types. 00057 // 00058 // ValueHolder is an envelope class that holds a counted-referenced letter 00059 // object <linkto class=ValueHolderRep>ValueHolderRep</linkto>. 00060 // </synopsis> 00061 00062 // <motivation> 00063 // This class comes handy in passing arbitrary values from a DO to 00064 // its environment. 00065 // </motivation> 00066 00067 class ValueHolder 00068 { 00069 public: 00070 // Construct a null object. 00071 ValueHolder() 00072 {} 00073 00074 // Create the object for the given value. 00075 // <group> 00076 explicit ValueHolder (Bool value); 00077 explicit ValueHolder (uChar value); 00078 explicit ValueHolder (Short value); 00079 explicit ValueHolder (uShort value); 00080 explicit ValueHolder (Int value); 00081 explicit ValueHolder (uInt value); 00082 explicit ValueHolder (Int64 value); 00083 explicit ValueHolder (Float value); 00084 explicit ValueHolder (Double value); 00085 explicit ValueHolder (const Complex& value); 00086 explicit ValueHolder (const DComplex& value); 00087 explicit ValueHolder (const Char* value); 00088 explicit ValueHolder (const String& value); 00089 explicit ValueHolder (const Array<Bool>& value); 00090 explicit ValueHolder (const Array<uChar>& value); 00091 explicit ValueHolder (const Array<Short>& value); 00092 explicit ValueHolder (const Array<uShort>& value); 00093 explicit ValueHolder (const Array<Int>& value); 00094 explicit ValueHolder (const Array<uInt>& value); 00095 explicit ValueHolder (const Array<Int64>& value); 00096 explicit ValueHolder (const Array<Float>& value); 00097 explicit ValueHolder (const Array<Double>& value); 00098 explicit ValueHolder (const Array<Complex>& value); 00099 explicit ValueHolder (const Array<DComplex>& value); 00100 explicit ValueHolder (const Array<String>& value); 00101 explicit ValueHolder (const Record& value); 00102 // </group> 00103 00104 // Create an empty N-dim array (gets type TpOther). 00105 ValueHolder (uInt ndim, Bool dummy); 00106 00107 // Create a ValueHolder from a ValueHolderRep. 00108 // It takes over the pointer and deletes it in the destructor. 00109 explicit ValueHolder (ValueHolderRep* rep) 00110 : itsRep (rep) 00111 {} 00112 00113 // Copy constructor (reference semantics). 00114 ValueHolder (const ValueHolder&); 00115 00116 // Destructor. 00117 ~ValueHolder() 00118 {} 00119 00120 // Assignment (reference semantics). 00121 ValueHolder& operator= (const ValueHolder&); 00122 00123 // Is this a null object? 00124 Bool isNull() const 00125 { return itsRep.null(); } 00126 00127 // Get the data type (as defined in DataType.h). 00128 DataType dataType() const; 00129 00130 // Get the value. 00131 // If possible, it converts the data as needed. 00132 // <group> 00133 Bool asBool () const; 00134 uChar asuChar () const; 00135 Short asShort () const; 00136 uShort asuShort () const; 00137 Int asInt () const; 00138 uInt asuInt () const; 00139 Int64 asInt64 () const; 00140 Float asFloat () const; 00141 Double asDouble () const; 00142 Complex asComplex () const; 00143 DComplex asDComplex() const; 00144 const String& asString () const; 00145 const Array<Bool> asArrayBool () const; 00146 const Array<uChar> asArrayuChar () const; 00147 const Array<Short> asArrayShort () const; 00148 const Array<uShort> asArrayuShort () const; 00149 const Array<Int> asArrayInt () const; 00150 const Array<uInt> asArrayuInt () const; 00151 const Array<Int64> asArrayInt64 () const; 00152 const Array<Float> asArrayFloat () const; 00153 const Array<Double> asArrayDouble () const; 00154 const Array<Complex> asArrayComplex () const; 00155 const Array<DComplex> asArrayDComplex() const; 00156 const Array<String> asArrayString () const; 00157 const Record& asRecord () const; 00158 // </group> 00159 00160 // Get the data in a way useful for templates. 00161 // If possible, it converts the the data as needed. 00162 // <group> 00163 void getValue (Bool& value) const { value = asBool(); } 00164 void getValue (uChar& value) const { value = asuChar(); } 00165 void getValue (Short& value) const { value = asShort(); } 00166 void getValue (uShort& value) const { value = asuShort(); } 00167 void getValue (Int& value) const { value = asInt(); } 00168 void getValue (uInt& value) const { value = asuInt(); } 00169 void getValue (Int64& value) const { value = asInt64(); } 00170 void getValue (Float& value) const { value = asFloat(); } 00171 void getValue (Double& value) const { value = asDouble(); } 00172 void getValue (Complex& value) const { value = asComplex(); } 00173 void getValue (DComplex& value) const { value = asDComplex(); } 00174 void getValue (String& value) const { value = asString(); } 00175 void getValue (Array<Bool>& value) const 00176 { value.reference(asArrayBool()); } 00177 void getValue (Array<uChar>& value) const 00178 { value.reference(asArrayuChar()); } 00179 void getValue (Array<Short>& value) const 00180 { value.reference(asArrayShort()); } 00181 void getValue (Array<uShort>& value) const 00182 { value.reference(asArrayuShort()); } 00183 void getValue (Array<Int>& value) const 00184 { value.reference(asArrayInt()); } 00185 void getValue (Array<uInt>& value) const 00186 { value.reference(asArrayuInt()); } 00187 void getValue (Array<Int64>& value) const 00188 { value.reference(asArrayInt64()); } 00189 void getValue (Array<Float>& value) const 00190 { value.reference(asArrayFloat()); } 00191 void getValue (Array<Double>& value) const 00192 { value.reference(asArrayDouble()); } 00193 void getValue (Array<Complex>& value) const 00194 { value.reference(asArrayComplex()); } 00195 void getValue (Array<DComplex>& value) const 00196 { value.reference(asArrayDComplex()); } 00197 void getValue (Array<String>& value) const 00198 { value.reference(asArrayString()); } 00199 // </group> 00200 00201 // Put the value as a field in a record. 00202 void toRecord (Record&, const RecordFieldId&) const; 00203 00204 // Construct the object from the value in a record. 00205 static ValueHolder fromRecord (const Record&, const RecordFieldId&); 00206 00207 // Compare two ValueHolder objects. 00208 // They must have the same data type. 00209 bool operator< (const ValueHolder& right) const 00210 { return itsRep->operator< (*right.itsRep); } 00211 00212 // Write the ValueHolder to an output stream. 00213 // Arrays are written as normal arrays using ArrayIO.h. 00214 friend std::ostream& operator<< (std::ostream& os, const ValueHolder& vh) 00215 { return vh.itsRep->write (os); } 00216 00217 private: 00218 00219 CountedPtr<ValueHolderRep> itsRep; 00220 }; 00221 00222 00223 inline DataType ValueHolder::dataType() const 00224 { return itsRep->dataType(); } 00225 inline void ValueHolder::toRecord (Record& rec, const RecordFieldId& id) const 00226 { return itsRep->toRecord (rec, id); } 00227 inline ValueHolder ValueHolder::fromRecord (const Record& rec, 00228 const RecordFieldId& id) 00229 { return ValueHolder (ValueHolderRep::fromRecord (rec, id)); } 00230 inline Bool ValueHolder::asBool() const 00231 { return itsRep->asBool(); } 00232 inline uChar ValueHolder::asuChar() const 00233 { return itsRep->asuChar(); } 00234 inline Short ValueHolder::asShort() const 00235 { return itsRep->asShort(); } 00236 inline uShort ValueHolder::asuShort() const 00237 { return itsRep->asuShort(); } 00238 inline Int ValueHolder::asInt() const 00239 { return itsRep->asInt(); } 00240 inline uInt ValueHolder::asuInt() const 00241 { return itsRep->asuInt(); } 00242 inline Int64 ValueHolder::asInt64() const 00243 { return itsRep->asInt64(); } 00244 inline Float ValueHolder::asFloat() const 00245 { return itsRep->asFloat(); } 00246 inline Double ValueHolder::asDouble() const 00247 { return itsRep->asDouble(); } 00248 inline Complex ValueHolder::asComplex() const 00249 { return itsRep->asComplex(); } 00250 inline DComplex ValueHolder::asDComplex() const 00251 { return itsRep->asDComplex(); } 00252 inline const String& ValueHolder::asString() const 00253 { return itsRep->asString(); } 00254 inline const Array<Bool> ValueHolder::asArrayBool() const 00255 { return itsRep->asArrayBool(); } 00256 inline const Array<uChar> ValueHolder::asArrayuChar() const 00257 { return itsRep->asArrayuChar(); } 00258 inline const Array<Short> ValueHolder::asArrayShort() const 00259 { return itsRep->asArrayShort(); } 00260 inline const Array<uShort> ValueHolder::asArrayuShort() const 00261 { return itsRep->asArrayuShort(); } 00262 inline const Array<Int> ValueHolder::asArrayInt() const 00263 { return itsRep->asArrayInt(); } 00264 inline const Array<uInt> ValueHolder::asArrayuInt() const 00265 { return itsRep->asArrayuInt(); } 00266 inline const Array<Int64> ValueHolder::asArrayInt64() const 00267 { return itsRep->asArrayInt64(); } 00268 inline const Array<Float> ValueHolder::asArrayFloat() const 00269 { return itsRep->asArrayFloat(); } 00270 inline const Array<Double> ValueHolder::asArrayDouble() const 00271 { return itsRep->asArrayDouble(); } 00272 inline const Array<Complex> ValueHolder::asArrayComplex() const 00273 { return itsRep->asArrayComplex(); } 00274 inline const Array<DComplex> ValueHolder::asArrayDComplex() const 00275 { return itsRep->asArrayDComplex(); } 00276 inline const Array<String> ValueHolder::asArrayString() const 00277 { return itsRep->asArrayString(); } 00278 inline const Record& ValueHolder::asRecord() const 00279 { return itsRep->asRecord(); } 00280 00281 00282 } //# NAMESPACE CASACORE - END 00283 00284 #endif