casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TVec.h
Go to the documentation of this file.
00001 //# TVec.h: Templated base class for table vectors
00002 //# Copyright (C) 1994,1995,1999
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 //# $Id: TVec.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef TABLES_TVEC_H
00029 #define TABLES_TVEC_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/Vector.h>
00034 
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // <summary>
00039 // Enumeration of possible table vectors
00040 // </summary>
00041 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00042 // </reviewed>
00043 // <use visibility=local>
00044 // <synopsis>
00045 // Define the type of table vectors.
00046 // Alas, this enum has to be defined outside the class, because
00047 // some compilers do not support an enum in a templated class.
00048 // </synopsis>
00049 // <group name=enum>
00050 enum TabVecTag {
00051     // Table Vector is a scalar column
00052     TagScaCol   = 1,
00053     // Table Vector is a temporary vector (i.e. a regular vector).
00054     TagTemp     = 2
00055 };
00056 // </group>
00057 
00058 
00059 
00060 // <summary>
00061 // Templated base class for table vectors
00062 // </summary>
00063 
00064 // <use visibility=local>
00065 
00066 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00067 // </reviewed>
00068 
00069 // <prerequisite>
00070 //# Classes you should understand before using this one.
00071 //   <li> TableVector
00072 // </prerequisite>
00073 
00074 // <etymology>
00075 // TabVecRep is the representation of a table vector.
00076 // </etymology>
00077 
00078 // <synopsis> 
00079 // TabVecRep is the counted referenced letter class for the envelope
00080 // class TableVector. It is an abstract base class for the actual
00081 // table vector classes TabVecScaCol and TabVecTemp.
00082 //
00083 // All operations defined for TableVector are immediately passed to
00084 // the corresponding virtual TabVecRep function.
00085 // The header files TVecMath.h and TVecLogic.h declare all the
00086 // mathematical and logical functions for TabVecRep.
00087 // </synopsis> 
00088 
00089 // <motivation>
00090 // A virtual function call only works when used with an object
00091 //  pointer or reference. To allow the use of virtual functions
00092 // in value objects, an extra level of indirection is used.
00093 // This is called the letter/envelope idiom and is described in
00094 // "Advanced C++" by J. Coplien.
00095 // Class TableVector is the envelope to the letters TabVecRep and
00096 // its derivations.
00097 // </motivation>
00098 
00099 // <todo asof="$DATE:$">
00100 //# A List of bugs, limitations, extensions or planned refinements.
00101 //   <li> put the TabVecTag enum inside the class definition
00102 //   <li> support array columns
00103 // </todo>
00104 
00105 
00106 template<class T>
00107 class TabVecRep
00108 {
00109 public:
00110 
00111     // Create empty table vector.
00112     // TabVecRep cannot be contructed by the user, because it is an
00113     // abstract base class (it contains pure virtual functions).
00114     TabVecRep();
00115 
00116     // Destruct the object.
00117     virtual ~TabVecRep();
00118 
00119     // Get nr of dimensions.
00120     inline uInt ndim() const;
00121 
00122     // Get nr of elements (ie. vector length).
00123     inline uInt nelements() const;
00124 
00125     // Test if vector shape conforms another table vector.
00126     inline Bool conform(const TabVecRep<T>&) const;
00127 
00128     // Test if vector shape conforms another vector.
00129     inline Bool conform(const Vector<T>&) const;
00130 
00131     // Check internal consistency.
00132     Bool ok() const;
00133 
00134     // Increments the reference count.
00135     inline TabVecRep<T>* link();
00136 
00137     // Decrements the reference count and returns the resulting count.
00138     inline uInt unlink();
00139 
00140     // Get the tag (the type of vector).
00141     inline TabVecTag getTag() const;
00142 
00143     // Get a value.
00144     virtual T value (uInt index) const = 0;
00145 
00146     // Get a value.
00147     virtual void getVal (uInt index, T&) const = 0;
00148 
00149     // Put a value.
00150     virtual void putVal (uInt index, const T&) = 0;
00151 
00152     // Set entire vector to a value.
00153     virtual void set (const T&) = 0;
00154 
00155     // Set to another table vector.
00156     virtual void assign (const TabVecRep<T>&);
00157 
00158 protected:
00159     uInt      count_p;               //# reference count
00160     TabVecTag tag_p;
00161     Int       nrel_p;                //# #elements (<0 = ask derived class)
00162 
00163     // Get nr of elements.
00164     virtual uInt nelem() const;
00165 
00166 public:
00167     // Check if vectors are comformant.
00168     void validateConformance (uInt) const;
00169 
00170     // Create a new temporary vector (for result of math operations).
00171     // TabVecTemp<T>& cannot be used, because the template instantiation
00172     // mechanism instantiates TabVecTemp, which depends on TabVecRep and
00173     // therefore gives errors.
00174     void* newVec() const;
00175 };
00176 
00177 
00178 
00179 template<class T>
00180 inline uInt TabVecRep<T>::ndim() const
00181     { return 1; }
00182 
00183 template<class T>
00184 inline uInt TabVecRep<T>::nelements() const
00185     { return (nrel_p<0  ?  nelem() : nrel_p); }
00186 
00187 //# Check if 2 table vectors are conformant.
00188 template<class T>
00189 inline Bool TabVecRep<T>::conform (const TabVecRep<T>& vec) const
00190     { return (nelements() == vec.nelements()  ?  True : False); }
00191 template<class T>
00192 inline Bool TabVecRep<T>::conform (const Vector<T>& vec) const
00193     { return (nelements() == vec.nelements()  ?  True : False); }
00194 
00195 //# Maintain reference count.
00196 template<class T>
00197 inline TabVecRep<T>* TabVecRep<T>::link()
00198 {
00199     count_p++;
00200     return this;
00201 }
00202 template<class T>
00203 inline uInt TabVecRep<T>::unlink()
00204     { return --count_p; }
00205 
00206 //# Return the tag.
00207 template<class T>
00208 inline TabVecTag TabVecRep<T>::getTag() const
00209     { return tag_p; }
00210 
00211 
00212 
00213 
00214 } //# NAMESPACE CASA - END
00215 
00216 #ifndef CASACORE_NO_AUTO_TEMPLATES
00217 #include <tables/Tables/TVec.tcc>
00218 #endif //# CASACORE_NO_AUTO_TEMPLATES
00219 #endif