casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ValueHolder.h
Go to the documentation of this file.
00001 //# ValueHolder.h: A holder object for the standard AIPS++ 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: ValueHolder.h 20901 2010-06-09 07:23:37Z gervandiepen $
00028 
00029 
00030 #ifndef CASA_VALUEHOLDER_H
00031 #define CASA_VALUEHOLDER_H
00032 
00033 //# Includes
00034 #include <casa/Containers/ValueHolderRep.h>
00035 #include <casa/Arrays/Array.h>
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 
00040 // <summary>
00041 // A holder for a value of any basic AIPS++ data type.
00042 // </summary>
00043 
00044 // <use visibility=export>
00045 // <reviewed reviewer="" date="" tests="tValueHolder">
00046 // </reviewed>
00047 
00048 // <synopsis>
00049 // Class ValueHolder is meant to be used for holding a single AIPS++ value.
00050 // The value can be  scalar or an array of any basic type (including complex
00051 // and string). Also a Record value is possible.
00052 // In this way varying typed data (e.g. the result of getCell in the table DO)
00053 // can be packed in a strongly typed variable.
00054 // <br>All unsigned integer type values are kept as signed 32-bit integers
00055 // because scripting languages usually only support those types.
00056 //
00057 // ValueHolder is an envelope class that holds a counted-referenced letter
00058 // object <linkto class=ValueHolderRep>ValueHolderRep</linkto>.
00059 // </synopsis>
00060 
00061 // <motivation>
00062 // This class comes handy in passing arbitrary values from a DO to
00063 // its environment.
00064 // </motivation>
00065 
00066 class ValueHolder
00067 {
00068 public:
00069   // Construct a null object.
00070   ValueHolder()
00071     : itsRep(0)
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.
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   // It throws an exception if the data type is incorrect.
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   // <group>
00162   void getValue (Bool& value) const            { value = asBool(); }
00163   void getValue (uChar& value) const           { value = asuChar(); }
00164   void getValue (Short& value) const           { value = asShort(); }
00165   void getValue (uShort& value) const          { value = asuShort(); }
00166   void getValue (Int& value) const             { value = asInt(); }
00167   void getValue (uInt& value) const            { value = asuInt(); }
00168   void getValue (Int64& value) const           { value = asInt64(); }
00169   void getValue (Float& value) const           { value = asFloat(); }
00170   void getValue (Double& value) const          { value = asDouble(); }
00171   void getValue (Complex& value) const         { value = asComplex(); }
00172   void getValue (DComplex& value) const        { value = asDComplex(); }
00173   void getValue (String& value) const          { value = asString(); }
00174   void getValue (Array<Bool>& value) const
00175     { value.reference(asArrayBool()); }
00176   void getValue (Array<uChar>& value) const
00177     { value.reference(asArrayuChar()); }
00178   void getValue (Array<Short>& value) const
00179     { value.reference(asArrayShort()); }
00180   void getValue (Array<uShort>& value) const
00181     { value.reference(asArrayuShort()); }
00182   void getValue (Array<Int>& value) const
00183     { value.reference(asArrayInt()); }
00184   void getValue (Array<uInt>& value) const
00185     { value.reference(asArrayuInt()); }
00186   void getValue (Array<Int64>& value) const
00187     { value.reference(asArrayInt64()); }
00188   void getValue (Array<Float>& value) const
00189     { value.reference(asArrayFloat()); }
00190   void getValue (Array<Double>& value) const
00191     { value.reference(asArrayDouble()); }
00192   void getValue (Array<Complex>& value) const
00193     { value.reference(asArrayComplex()); }
00194   void getValue (Array<DComplex>& value) const
00195     { value.reference(asArrayDComplex()); }
00196   void getValue (Array<String>& value) const
00197     { value.reference(asArrayString()); }
00198   // </group>
00199 
00200   // Put the value as a field in a record.
00201   void toRecord (Record&, const RecordFieldId&) const;
00202 
00203   // Construct the object from the value in a record.
00204   static ValueHolder fromRecord (const Record&, const RecordFieldId&);
00205 
00206   // Write the ValueHolder to an output stream.
00207   // Arrays are written as normal arrays using ArrayIO.h. 
00208   friend std::ostream& operator<< (std::ostream& os, const ValueHolder& vh)
00209     { return vh.itsRep->write (os); }
00210 
00211 private:
00212 
00213   CountedPtr<ValueHolderRep> itsRep;
00214 };
00215 
00216 
00217 inline DataType ValueHolder::dataType() const
00218   { return itsRep->dataType(); }
00219 inline void ValueHolder::toRecord (Record& rec, const RecordFieldId& id) const
00220   { return itsRep->toRecord (rec, id); }
00221 inline ValueHolder ValueHolder::fromRecord (const Record& rec,
00222                                             const RecordFieldId& id)
00223   { return ValueHolder (ValueHolderRep::fromRecord (rec, id)); }
00224 inline Bool ValueHolder::asBool() const
00225   { return itsRep->asBool(); }
00226 inline uChar ValueHolder::asuChar() const
00227   { return itsRep->asuChar(); }
00228 inline Short ValueHolder::asShort() const
00229   { return itsRep->asShort(); }
00230 inline uShort ValueHolder::asuShort() const
00231   { return itsRep->asuShort(); }
00232 inline Int ValueHolder::asInt() const
00233   { return itsRep->asInt(); }
00234 inline uInt ValueHolder::asuInt() const
00235   { return itsRep->asuInt(); }
00236 inline Int64 ValueHolder::asInt64() const
00237   { return itsRep->asInt64(); }
00238 inline Float ValueHolder::asFloat() const
00239   { return itsRep->asFloat(); }
00240 inline Double ValueHolder::asDouble() const
00241   { return itsRep->asDouble(); }
00242 inline Complex ValueHolder::asComplex() const
00243   { return itsRep->asComplex(); }
00244 inline DComplex ValueHolder::asDComplex() const
00245   { return itsRep->asDComplex(); }
00246 inline const String& ValueHolder::asString() const
00247   { return itsRep->asString(); }
00248 inline const Array<Bool> ValueHolder::asArrayBool() const
00249   { return itsRep->asArrayBool(); }
00250 inline const Array<uChar> ValueHolder::asArrayuChar() const
00251   { return itsRep->asArrayuChar(); }
00252 inline const Array<Short> ValueHolder::asArrayShort() const
00253   { return itsRep->asArrayShort(); }
00254 inline const Array<uShort> ValueHolder::asArrayuShort() const
00255   { return itsRep->asArrayuShort(); }
00256 inline const Array<Int> ValueHolder::asArrayInt() const
00257   { return itsRep->asArrayInt(); }
00258 inline const Array<uInt> ValueHolder::asArrayuInt() const
00259   { return itsRep->asArrayuInt(); }
00260 inline const Array<Int64> ValueHolder::asArrayInt64() const
00261   { return itsRep->asArrayInt64(); }
00262 inline const Array<Float> ValueHolder::asArrayFloat() const
00263   { return itsRep->asArrayFloat(); }
00264 inline const Array<Double> ValueHolder::asArrayDouble() const
00265   { return itsRep->asArrayDouble(); }
00266 inline const Array<Complex> ValueHolder::asArrayComplex() const
00267   { return itsRep->asArrayComplex(); }
00268 inline const Array<DComplex> ValueHolder::asArrayDComplex() const
00269   { return itsRep->asArrayDComplex(); }
00270 inline const Array<String> ValueHolder::asArrayString() const
00271   { return itsRep->asArrayString(); }
00272 inline const Record& ValueHolder::asRecord() const
00273   { return itsRep->asRecord(); }
00274 
00275 
00276 } //# NAMESPACE CASA - END
00277 
00278 #endif