casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ValueHolderRep.h
Go to the documentation of this file.
00001 //# ValueHolderRep.h: A holder object for the standard AIPS++ data
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: ValueHolderRep.h 20901 2010-06-09 07:23:37Z gervandiepen $
00028 
00029 
00030 #ifndef CASA_VALUEHOLDERREP_H
00031 #define CASA_VALUEHOLDERREP_H
00032 
00033 //# Includes
00034 #include <casa/aips.h>
00035 #include <casa/Utilities/DataType.h>
00036 #include <iosfwd>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 //# Forward Declarations
00041 class AipsIO;
00042 template<class T> class Array;
00043 class Record;
00044 class RecordFieldId;
00045 
00046 
00047 // <summary>
00048 // A holder for a value of any basic type.
00049 // </summary>
00050 
00051 // <use visibility=local>
00052 // <reviewed reviewer="" date="" tests="tValueHolder">
00053 // </reviewed>
00054 
00055 // <synopsis>
00056 // Class ValueHolderRep is the letter class for the envelope class ValueHolder.
00057 // See <linkto class=ValueHolder>that class</linkto> for more information.
00058 // </synopsis>
00059 
00060 // <motivation>
00061 // Copying ValueHolders should be as cheap as possible, so a counted
00062 // referenced letter class is used.
00063 // </motivation>
00064 
00065 class ValueHolderRep
00066 {
00067 public:
00068   // Create the object for the given value.
00069   // <group>
00070   explicit ValueHolderRep (Bool value);
00071   explicit ValueHolderRep (uChar value);
00072   explicit ValueHolderRep (Short value);
00073   explicit ValueHolderRep (uShort value);
00074   explicit ValueHolderRep (Int value);
00075   explicit ValueHolderRep (uInt value);
00076   explicit ValueHolderRep (Int64 value);
00077   explicit ValueHolderRep (Float value);
00078   explicit ValueHolderRep (Double value);
00079   explicit ValueHolderRep (const Complex& value);
00080   explicit ValueHolderRep (const DComplex& value);
00081   explicit ValueHolderRep (const Char* value);
00082   explicit ValueHolderRep (const String& value);
00083   explicit ValueHolderRep (const Array<Bool>& value);
00084   explicit ValueHolderRep (const Array<uChar>& value);
00085   explicit ValueHolderRep (const Array<Short>& value);
00086   explicit ValueHolderRep (const Array<uShort>& value);
00087   explicit ValueHolderRep (const Array<Int>& value);
00088   explicit ValueHolderRep (const Array<uInt>& value);
00089   explicit ValueHolderRep (const Array<Int64>& value);
00090   explicit ValueHolderRep (const Array<Float>& value);
00091   explicit ValueHolderRep (const Array<Double>& value);
00092   explicit ValueHolderRep (const Array<Complex>& value);
00093   explicit ValueHolderRep (const Array<DComplex>& value);
00094   explicit ValueHolderRep (const Array<String>& value);
00095   explicit ValueHolderRep (const Record& value);
00096   // </group>
00097 
00098   // Create an empty N-dim array.
00099   ValueHolderRep (uInt ndim, Bool dummy);
00100 
00101   // Destructor.
00102   ~ValueHolderRep();
00103 
00104   void link()
00105     { itsCount++; }
00106 
00107   static void unlink (ValueHolderRep* rep)
00108     { if (rep != 0 && --rep->itsCount == 0) delete rep; }
00109 
00110   // Get the data type (as defined in DataType.h).
00111   DataType dataType() const;
00112     
00113   // Get the value.
00114   // It throws an exception if the data type is incorrect.
00115   // <group>
00116   Bool                  asBool    () const;
00117   uChar                 asuChar   () const;
00118   Short                 asShort   () const;
00119   uShort                asuShort  () const;
00120   Int                   asInt     () const;
00121   uInt                  asuInt    () const;
00122   Int64                 asInt64   () const;
00123   Float                 asFloat   () const;
00124   Double                asDouble  () const;
00125   Complex               asComplex () const;
00126   DComplex              asDComplex() const;
00127   const String&         asString  () const;
00128   const Array<Bool>     asArrayBool    () const;
00129   const Array<uChar>    asArrayuChar   () const;
00130   const Array<Short>    asArrayShort   () const;
00131   const Array<uShort>   asArrayuShort  () const;
00132   const Array<Int>      asArrayInt     () const;
00133   const Array<uInt>     asArrayuInt    () const;
00134   const Array<Int64>    asArrayInt64   () const;
00135   const Array<Float>    asArrayFloat   () const;
00136   const Array<Double>   asArrayDouble  () const;
00137   const Array<Complex>  asArrayComplex () const; 
00138   const Array<DComplex> asArrayDComplex() const;
00139   const Array<String>   asArrayString  () const;
00140   const Record&         asRecord       () const;
00141   // </group>
00142 
00143   // Put the value as a field in a record.
00144   void toRecord (Record&, const RecordFieldId&) const;
00145 
00146   // Construct the object from the value in a record.
00147   static ValueHolderRep* fromRecord (const Record& rec, const RecordFieldId&);
00148 
00149   // Write the ValueHolderRep to an output stream.
00150   // Arrays are written as normal arrays using ArrayIO.h. 
00151   std::ostream& write (std::ostream& os) const;
00152 
00153 private:
00154   // Forbid copy ctor and assignment.
00155   //# There is no fundamental reason to forbid them, but it saves
00156   //# implementation work as long as they are not needed.
00157   // <group>
00158   ValueHolderRep (const ValueHolderRep&);
00159   ValueHolderRep& operator= (const ValueHolderRep&);
00160   // </group>
00161 
00162 
00163   Int      itsCount;
00164   uInt     itsNdim;
00165   DataType itsType;
00166   union {
00167     Bool   itsBool;
00168     uChar  itsUChar;
00169     Short  itsShort;
00170     Int    itsInt;
00171     Int64  itsInt64;
00172     Float  itsFloat;
00173     Double itsDouble;
00174     void*  itsPtr;
00175   };
00176 };
00177 
00178 
00179 inline DataType ValueHolderRep::dataType() const
00180 {
00181   return itsType;
00182 }
00183 
00184 
00185 } //# NAMESPACE CASA - END
00186 
00187 #endif