BaseMappedArrayEngine.h

Go to the documentation of this file.
00001 //# BaseMappedArrayEngine.h: Abstract virtual column engine for virtual->stored mapping
00002 //# Copyright (C) 1995,1996,1997,1999,2001,2002
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$
00027 
00028 #ifndef TABLES_BASEMAPPEDARRAYENGINE_H
00029 #define TABLES_BASEMAPPEDARRAYENGINE_H
00030 
00031 //# Includes
00032 #include <tables/Tables/VirtColEng.h>
00033 #include <tables/Tables/VirtArrCol.h>
00034 #include <casa/Arrays/IPosition.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 //# Forward Declarations
00039 template<class T> class ROArrayColumn;
00040 template<class T> class ArrayColumn;
00041 class TableColumn;
00042 
00043 
00044 // <summary>
00045 // Templated virtual column engine for a table array of any type.
00046 // </summary>
00047 
00048 // <use visibility=export>
00049 
00050 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00051 // </reviewed>
00052 
00053 // <prerequisite>
00054 //# Classes you should understand before using this one.
00055 //   <li> <linkto class=VirtualColumnEngine>VirtualColumnEngine</linkto>
00056 //   <li> <linkto class=VirtualArrayColumn>VirtualArrayColumn</linkto>
00057 // </prerequisite>
00058 
00059 // <etymology>
00060 // BaseMappedArrayEngine contains for the 1-1 mapping of a virtual
00061 // column to a stored column (both containing arrays). 
00062 // </etymology>
00063 
00064 // <synopsis> 
00065 // BaseMappedArrayEngine is an abstract base class for virtual column engines
00066 // which map data from the arrays in the virtual column to
00067 // the arrays in the stored column. Note the the stored column does not need
00068 // to be stored; it can be another virtual column, but usually it will be a
00069 // stored column.
00070 // Examples of classes using this base class are
00071 // <linkto class=ScaledArrayEngine>ScaledArrayEngine</linkto> and
00072 // <linkto class=RetypedArrayEngine>RetypedArrayEngine</linkto>.
00073 //
00074 // The virtual column has to be bound to the virtual column engine used
00075 // for it. The stored column will usually be bound to a storage manager,
00076 // but any other suitable data manager is possible. E.g. it is
00077 // possible to use <src>MappedArrayEngine<StokesVector,float></src>
00078 // to map a StokesVector to a float column, which in its turn uses
00079 // <src>ScaledArrayEngine<float,Int></src> to store it as integers.
00080 // Note that the names of the virtual and stored column have to be different,
00081 // otherwise the table system cannot distinguish them.
00082 //
00083 // This base class does several tasks for the derived classes.
00084 // The main one is to keep and handle the information about the virtual
00085 // and stored column. The name of the stored column is written as a keyword
00086 // in the virtual column. In this way the stored column is known when
00087 // a table is read back. It also creates <src>(RO)ArrayColumn<T></src>
00088 // objects to access the stored column. The function roColumn gives
00089 // read access, while rwColumn gives write access.
00090 //
00091 // An engine object should be used for one column only, because the stored
00092 // column name is part of the engine. If it would be used for more than
00093 // one column, they would all share the same stored column.
00094 // When the engine is bound to a column, it is checked if the name
00095 // of that column matches the given virtual column name.
00096 //
00097 // The engine can be used for a column containing any kind of array
00098 // (thus direct or indirect, fixed or variable shaped)) as long as the
00099 // virtual array can be stored in the stored array. Thus a fixed shaped
00100 // virtual can use a variable shaped stored, but not vice versa.
00101 // A fixed shape indirect virtual can use a stored with direct arrays.
00102 //
00103 // The DataManager framework contains various virtual functions.
00104 // This class implements several, but not all of them. Furthermore
00105 // some implementations may not be optimal or correct for derived classes.
00106 // Hereafter follows a list of functions which may need implementation
00107 // in derived classes. The classes mentioned in the examples below show
00108 // implementations of these functions.
00109 // <ul>
00110 // <li>
00111 // The following (virtual) functions have to be implemented:
00112 // <dl>
00113 // <dt><src>
00114 //       ~... (the destructor)
00115 // </src>
00116 // <dt><src>
00117 //        DataManager* clone() const;
00118 // </src>
00119 // <dt><src>
00120 //        String dataManagerType() const;
00121 // </src>
00122 // <dt><src>
00123 //        static void registerClass();
00124 // </src>
00125 // <dt><src>
00126 //        static DataManager* makeObject (const String& dataManagerType);
00127 // </src>
00128 // <dt><src>
00129 //        void getArray (uInt rownr, Array<T>& data);
00130 // </src>
00131 // <dt><src>
00132 //        void putArray (uInt rownr, const Array<T>& data);
00133 // </src>
00134 // (only if the virtual column is writable).
00135 // </dl>
00136 // <li>
00137 // For efficiency reasons it could be better to implement the following
00138 // functions:
00139 // <dl>
00140 // <dt><src>
00141 //        void getSlice (uInt rownr, const Slicer& slicer, Array<T>& data);
00142 // </src>
00143 // <dt><src>
00144 //        void putSlice (uInt rownr, const Slicer& slicer,
00145 //                       const Array<T>& data);
00146 // </src>
00147 // <dt><src>
00148 //        void getArrayColumn (Array<T>& data);
00149 // </src>
00150 // <dt><src>
00151 //        void putArrayColumn (const Array<T>& data);
00152 // </src>
00153 // <dt><src>
00154 //        void getColumnSlice (const Slicer& slicer, Array<T>& data);
00155 // </src>
00156 // <dt><src>
00157 //        void putColumnSlice (const Slicer& slicer, const Array<T>& data);
00158 // </src>
00159 // </dl>
00160 // <li>
00161 // The following functions have to be implemented when the shapes
00162 // of the virtual and stored arrays are not the same.
00163 // <dl>
00164 // <dt><src>
00165 //    void setShapeColumn (const IPosition& shape);
00166 // </src>
00167 // <dt><src>
00168 //    void setShape (uInt rownr, const IPosition& shape);
00169 // </src>
00170 // <dt><src>
00171 //    uInt ndim (uInt rownr);
00172 // </src>
00173 // <dt><src>
00174 //    IPosition shape (uInt rownr);
00175 // </src>
00176 // </dl>
00177 // <li>
00178 // The following functions deal with the initialization and persistence
00179 // of engine specific variables. When the class has variables of its
00180 // own, these functions may need to be implemented. Implementations of
00181 // create and prepare have to call the similar functions in this base class.
00182 // <dl>
00183 // <dt><src>
00184 //    void close (AipsIO& ios);
00185 // </src>
00186 // <dt><src>
00187 //    void create (uInt nrrow);
00188 // </src>
00189 // <dt><src>
00190 //    void open (uInt nrrow, AipsIO& ios);
00191 // </src>
00192 // <dt><src>
00193 //    void prepare();
00194 // </src>
00195 // </dl>
00196 // <li>
00197 // The following functions do not need to be declared and implemented
00198 // in derived classes unless it is a very special case.
00199 // <dl>
00200 // <dt><src>
00201 //    String dataManagerName() const;
00202 // </src>
00203 // <dt><src>
00204 //    Bool canAddRow() const;
00205 // </src>
00206 // <dt><src>
00207 //    Bool canRemoveRow() const;
00208 // </src>
00209 // <dt><src>
00210 //    void addRow (uInt nrrow);
00211 // </src>
00212 // <dt><src>
00213 //    void removeRow (uInt rownr);
00214 // </src>
00215 // <dt><src>
00216 //    DataManagerColumn* makeDirArrColumn (const String& columnName,
00217 //                                               int dataType,
00218 //                                               const String& dataTypeId);
00219 // </src>
00220 // <dt><src>
00221 //    DataManagerColumn* makeIndArrColumn (const String& columnName,
00222 //                                               int dataType,
00223 //                                               const String& dataTypeId);
00224 // </src>
00225 // <dt><src>
00226 //    Bool isWritable() const;
00227 // </src>
00228 // <dt><src>
00229 //    Bool isShapeDefined (uInt rownr);
00230 // </src>
00231 // </dl>
00232 // </ul>
00233 // </synopsis>
00234 
00235 // <example>
00236 // The derived classes
00237 // <linkto class=ScaledArrayEngine>ScaledArrayEngine</linkto> and
00238 // <linkto class=RetypedArrayEngine>RetypedArrayEngine</linkto>
00239 // are two examples of how to derive a class from this base class.
00240 // Note that ScaledArrayEngine does not need to implement functions
00241 // dealing with shapes, because it can use them from this base class.
00242 // On the other hand they need to be implemented in RetypedArrayEngine.
00243 // </example>
00244 
00245 // <motivation>
00246 // This base class implements several functions making the implementation
00247 // of derived classes simpler. Many details are implemented here, so often
00248 // only the basic mapping functions (get, put) need to be implemented
00249 // in a derived class.
00250 // </motivation>
00251 
00252 // <templating arg=VirtualType>
00253 //  <li> default constructor
00254 //  <li> copy constructor
00255 //  <li> assignment operator
00256 //  <li> <src>static String dataTypeId();   // unique name of the class</src>
00257 // </templating>
00258 // <templating arg=StoredType>
00259 //  <li> Default constructor
00260 //  <li> Copy constructor
00261 //  <li> Assignment operator
00262 // </templating>
00263 
00264 
00265 template<class VirtualType, class StoredType> class BaseMappedArrayEngine : public VirtualColumnEngine, public VirtualArrayColumn<VirtualType>
00266 {
00267 public:
00268     // Adding rows is possible for this engine.
00269     virtual Bool canAddRow() const;
00270 
00271     // Deleting rows is possible for this engine.
00272     virtual Bool canRemoveRow() const;
00273 
00274     // Get the virtual column name.
00275     const String& virtualName() const;
00276 
00277     // Get the stored column name.
00278     const String& storedName() const;
00279 
00280     // The column is writable if the underlying stored column is writable.
00281     virtual Bool isWritable() const;
00282 
00283 protected:
00284 
00285     // Construct an engine to convert the virtual column to the stored column.
00286     // StoredColumnName is the name of the column where the converted
00287     // data will be put and must have data type StoredType.
00288     // The virtual column using this engine must have data type VirtualType.
00289     // By default the virtual column is assumed to be writable.
00290     // Use setWritable to unset it.
00291     BaseMappedArrayEngine (const String& virtualColumnName,
00292                            const String& storedColumnName);
00293 
00294     // Destructor is mandatory.
00295     ~BaseMappedArrayEngine();
00296 
00297     // The default constructor is required for reconstruction of the
00298     // engine when a table is read back.
00299     BaseMappedArrayEngine();
00300 
00301     // Copy constructor is only used by copy constructor of derived classes.
00302     // (so it is made protected).
00303     BaseMappedArrayEngine
00304                       (const BaseMappedArrayEngine<VirtualType, StoredType>&);
00305 
00306     // Set if the column is writable or not.
00307     void setWritable (Bool isWritable);
00308 
00309     // Set the virtual and stored column name.
00310     void setNames (const String& virtualName, const String& storedName);
00311 
00312     // Give readonly access to the stored column.
00313     // This can be used by the derived classes to get data.
00314     inline ROArrayColumn<StoredType>& roColumn();
00315 
00316     // Give read/write access to the stored column.
00317     // This can be used by the derived classes to put data.
00318     inline ArrayColumn<StoredType>& rwColumn();
00319 
00320     // Create the column object for the array column in this engine.
00321     // It will check if the given column name matches the virtual
00322     // column name. This assures that the engine is bound to the
00323     // correct column.
00324     virtual DataManagerColumn* makeIndArrColumn (const String& columnName,
00325                                                  int dataType,
00326                                                  const String& dataTypeId);
00327 
00328     // Initialize the object for a new table.
00329     // It defines a virtual column keyword telling the stored column name.
00330     // Initially the table has the given number of rows.
00331     // A derived class can have its own create function, but that should
00332     // always call this create function.
00333     virtual void create (uInt initialNrrow);
00334 
00335     // Preparing consists of setting the writable switch and
00336     // adding the initial number of rows in case of create.
00337     // It reads the stored column name from the virtual column keywords.
00338     // A derived class can have its own prepare function, but that should
00339     // always call this prepare function.
00340     virtual void prepare();
00341 
00342     // Do the 2 stages of the prepare (define columns and adding rows).
00343     // <group>
00344     void prepare1();
00345     void prepare2();
00346     // </group>
00347 
00348     // Reopen the engine for read/write access.
00349     // It makes the column writable if the underlying column is writable.
00350     virtual void reopenRW();
00351 
00352     // Rows are added to the end of the table.
00353     // If the virtual column has FixedShape arrays and the stored not,
00354     // the shape in each stored row will be set.
00355     // This assures that the arrays are properly defined in each row,
00356     // so putSlice can be used without problems.
00357     // <br>The second version is used by prepare2, because in case a column is
00358     // added to an already existing table, table.nrow() gives the existing
00359     // number of columns instead of 0.
00360     // <group>
00361     virtual void addRow (uInt nrrow);
00362     virtual void addRowInit (uInt startRow, uInt nrrow);
00363     // </group>
00364 
00365     // Deleting rows is possible and is a no-op for this engine.
00366     virtual void removeRow (uInt rownr);
00367 
00368     // Set the shape of the FixedShape arrays in the column.
00369     // This function only gets called if the column has FixedShape arrays.
00370     // The shape gets saved and used to set the shape of the arrays
00371     // in the stored in case the stored has non-FixedShape arrays.
00372     // This implementation assumes the shape of virtual and stored arrays
00373     // are the same. If not, it has to be overidden in a derived class.
00374     virtual void setShapeColumn (const IPosition& shape);
00375 
00376     // Define the shape of the array in the given row.
00377     // It will define the shape of the (underlying) array.
00378     // This implementation assumes the shape of virtual and stored arrays
00379     // are the same. If not, it has to be overidden in a derived class.
00380     virtual void setShape (uInt rownr, const IPosition& shape);
00381 
00382     // Test if the (underlying) array is defined in the given row.
00383     virtual Bool isShapeDefined (uInt rownr);
00384 
00385     // Get the dimensionality of the (underlying) array in the given row.
00386     // This implementation assumes the dimensionality of virtual and
00387     // stored arrays are the same. If not, it has to be overidden in a
00388     // derived class.
00389     virtual uInt ndim (uInt rownr);
00390 
00391     // Get the shape of the (underlying) array in the given row.
00392     // This implementation assumes the shape of virtual and stored arrays
00393     // are the same. If not, it has to be overidden in a derived class.
00394     virtual IPosition shape (uInt rownr);
00395 
00396     // The data manager can handle changing the shape of an existing array
00397     // when the underlying stored column can do it.
00398     virtual Bool canChangeShape() const;
00399 
00400     // Make a table column object for the given column.
00401     // This has to be used in the create function, otherwise it could not
00402     // create a TableColumn object to store data in the column keywords.
00403     TableColumn makeTableColumn (const String& columnName);
00404     
00405 
00406 private:
00407     // Assignment is not needed and therefore forbidden
00408     // (so it is made private and not implemented).
00409     BaseMappedArrayEngine<VirtualType, StoredType>& operator=
00410                      (const BaseMappedArrayEngine<VirtualType, StoredType>&);
00411 
00412 
00413     //# Now define the data members.
00414     String         virtualName_p;        //# virtual column name
00415     String         storedName_p;         //# stored column name
00416     Bool           isWritable_p;         //# is virtual column writable?
00417     Bool           tempWritable_p;       //# True =  create phase, so column
00418     //#                                              is temporarily writable
00419     //#                                      False = asks stored column
00420     uInt           initialNrrow_p;       //# initial #rows in case of create
00421     Bool           arrayIsFixed_p;       //# True = virtual is FixedShape array
00422     IPosition      shapeFixed_p;         //# shape in case FixedShape array
00423     ROArrayColumn<StoredType>*  roColumn_p;    //# the stored column (for get)
00424     ArrayColumn<StoredType>*    column_p;      //# the stored column (for put)
00425 };
00426 
00427 
00428 
00429 template<class VirtualType, class StoredType>
00430 inline const String&
00431 BaseMappedArrayEngine<VirtualType, StoredType>::virtualName() const
00432     { return virtualName_p; }
00433 
00434 template<class VirtualType, class StoredType>
00435 inline const String&
00436 BaseMappedArrayEngine<VirtualType, StoredType>::storedName() const
00437     { return storedName_p; }
00438 
00439 template<class VirtualType, class StoredType>
00440 inline void
00441 BaseMappedArrayEngine<VirtualType, StoredType>::setNames
00442                     (const String& virtualName, const String& storedName)
00443 {
00444     virtualName_p = virtualName;
00445     storedName_p  = storedName;
00446 }
00447 
00448 template<class VirtualType, class StoredType>
00449 inline void
00450 BaseMappedArrayEngine<VirtualType, StoredType>::setWritable (Bool isWritable)
00451     { isWritable_p = isWritable; }
00452 
00453 template<class VirtualType, class StoredType>
00454 inline ROArrayColumn<StoredType>&
00455 BaseMappedArrayEngine<VirtualType, StoredType>::roColumn()
00456     { return *roColumn_p; }
00457 
00458 template<class VirtualType, class StoredType>
00459 inline ArrayColumn<StoredType>&
00460 BaseMappedArrayEngine<VirtualType, StoredType>::rwColumn()
00461     { return *column_p; }
00462 
00463 
00464 
00465 } //# NAMESPACE CASA - END
00466 
00467 #ifndef AIPS_NO_TEMPLATE_SRC
00468 #include <tables/Tables/BaseMappedArrayEngine.cc>
00469 #endif //# AIPS_NO_TEMPLATE_SRC
00470 #endif

Generated on Mon Sep 1 22:36:13 2008 for NRAOCASA by  doxygen 1.5.1