casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ForwardCol.h
Go to the documentation of this file.
00001 //# ForwardCol.h: Virtual Column Engine to forward to other columns
00002 //# Copyright (C) 1995,1996,1997,2001
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: ForwardCol.h 21155 2011-12-12 07:56:57Z gervandiepen $
00027 
00028 #ifndef TABLES_FORWARDCOL_H
00029 #define TABLES_FORWARDCOL_H
00030 
00031 //# Includes
00032 #include <tables/Tables/VirtColEng.h>
00033 #include <tables/Tables/DataManager.h>
00034 #include <tables/Tables/Table.h>
00035 #include <tables/Tables/TableColumn.h>
00036 #include <casa/Containers/Block.h>
00037 #include <casa/BasicSL/String.h>
00038 
00039 namespace casa { //# NAMESPACE CASA - BEGIN
00040 
00041 //# Forward Declarations
00042 class ForwardColumnEngine;
00043 class BaseColumn;
00044 
00045 
00046 // <summary>
00047 // Virtual column forwarding to another column
00048 // </summary>
00049 
00050 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00051 // </reviewed>
00052 
00053 // <use visibility=local>
00054 
00055 // <prerequisite>
00056 //# Classes you should understand before using this one.
00057 //   <li> ForwardColumnEngine
00058 //   <li> DataManagerColumn
00059 // </prerequisite>
00060 
00061 // <synopsis>
00062 // ForwardColumn represents a virtual column which forwards the
00063 // gets and puts to a column with the same name in another table.
00064 // It is, in fact, a reference to the other column.
00065 // The name of the other table is stored as a keyword in the
00066 // forwarding column. When there is a forwarding chain (i.e.
00067 // forwarding to a forwarding column), the name of the last
00068 // table in the chain is stored in the keyword. In this way, the
00069 // length of the chain is kept to a minimum. Otherwise a very long
00070 // chain could occur, which would slow things down.
00071 //
00072 // Addition and deletion of rows is allowed, but the functions addRow and
00073 // removeRow do not do anything at all. They are implemented to override
00074 // the default "throw exception" implementation. Because the engine
00075 // allows this, it can be used in a table supporting addition and removal
00076 // of rows.
00077 //
00078 // An object of this class is created (and deleted) by
00079 // <linkto class="ForwardColumnEngine:description">ForwardColumnEngine</linkto>
00080 // which creates a ForwardColumn object for each column being forwarded.
00081 // </synopsis> 
00082 
00083 // <motivation>
00084 // This class will be used by the calibration software.
00085 // Most columns in a measurement table will be forwarded, while
00086 // a few (i.e. the data themselves) will be calculated by a dedicated
00087 // calibration engine.
00088 // </motivation>
00089 
00090 class ForwardColumn : public DataManagerColumn
00091 {
00092 public:
00093 
00094     // Construct it for the given column.
00095     ForwardColumn (ForwardColumnEngine* enginePtr,
00096                    const String& columnName,
00097                    int dataType,
00098                    const String& dataTypeId,
00099                    const Table& referencedTable);
00100 
00101     // Destructor is mandatory.
00102     virtual ~ForwardColumn();
00103 
00104     // Define the special keyword containing the name of the original table.
00105     // If the column in the referenced table contains that special keyword,
00106     // it is in its turn a forwarding column. In that case the special
00107     // keyword value will be copied over to shortcut the forwarding chain.
00108     // The suffix is appended to the keyword name when defining it.
00109     // This makes this function usable for derived classes.
00110     void fillTableName (const Table& thisTable, const Table& referencedTable);
00111 
00112     // Initialize the object.
00113     // This means binding the column to the column with the same name
00114     // in the original table.
00115     // It checks if the description of both columns is the same.
00116     // It also determines if the column is writable.
00117     virtual void prepare (const Table& thisTable);
00118 
00119     // Set the column to writable if its underlying table is writable.
00120     void setRW();
00121 
00122 protected:
00123     // Do the preparation of the base class column object.
00124     void basePrepare (const Table& thisTable, Bool writable);
00125 
00126     BaseColumn* colPtr() const
00127         { return colPtr_p; }
00128 
00129 private:
00130     // Copy constructor is not needed and therefore forbidden
00131     // (so make it private).
00132     ForwardColumn (const ForwardColumn&);
00133 
00134     // Assignment is not needed and therefore forbidden (so make it private).
00135     ForwardColumn& operator= (const ForwardColumn&);
00136 
00137     // Create a SetupNewTable object with the given name and option
00138     // and with the description from the given table.
00139     // The SetupNewTable object will use a single ForwardColumn
00140     // engine which forwards all columns to the given table.
00141     // Later the SetupNewTable::bind functions can be used to bind one
00142     // or more columns to another data manager.
00143     static SetupNewTable setupNewTable (const Table& table,
00144                                         const String& tableName,
00145                                         Table::TableOption option);
00146 
00147     // This data manager may be able to handle changing array shapes.
00148     Bool canChangeShape() const;
00149 
00150     // This data manager may be able to do get/putScalarColumn.
00151     Bool canAccessScalarColumn (Bool& reask) const;
00152 
00153     // This data manager may be able to do get/putArrayColumn.
00154     Bool canAccessArrayColumn (Bool& reask) const;
00155 
00156     // This data manager may be able to do get/putSlice.
00157     Bool canAccessSlice (Bool& reask) const;
00158 
00159     // This data manager may be able to do get/putColumnSlice.
00160     Bool canAccessColumnSlice (Bool& reask) const;
00161 
00162     // Get the data type of the column as defined in DataType.h.
00163     int dataType() const;
00164 
00165     // Get the data type id of the column for dataType==TpOther.
00166     // This function is required for virtual column engines handling
00167     // non-standard data types. It is used to check the data type.
00168     String dataTypeId() const;
00169 
00170     // Test if data can be put into this column.
00171     Bool isWritable() const;
00172 
00173     // Set the shape of an direct array.
00174     // This only checks if the shape matches the referenced column.
00175     void setShapeColumn (const IPosition& shape);
00176 
00177     // Set the shape of an (indirect) array in the given row.
00178     void setShape (uInt rownr, const IPosition& shape);
00179 
00180     // Is the value shape defined in the given row?
00181     Bool isShapeDefined (uInt rownr);
00182 
00183     // Get the dimensionality of the item in the given row.
00184     uInt ndim (uInt rownr);
00185 
00186     // Get the shape of the item in the given row.
00187     IPosition shape (uInt rownr);
00188 
00189     // Get the scalar value with a standard data type in the given row.
00190     // <group>
00191     void getBoolV     (uInt rownr, Bool* dataPtr);
00192     void getuCharV    (uInt rownr, uChar* dataPtr);
00193     void getShortV    (uInt rownr, Short* dataPtr);
00194     void getuShortV   (uInt rownr, uShort* dataPtr);
00195     void getIntV      (uInt rownr, Int* dataPtr);
00196     void getuIntV     (uInt rownr, uInt* dataPtr);
00197     void getfloatV    (uInt rownr, float* dataPtr);
00198     void getdoubleV   (uInt rownr, double* dataPtr);
00199     void getComplexV  (uInt rownr, Complex* dataPtr);
00200     void getDComplexV (uInt rownr, DComplex* dataPtr);
00201     void getStringV   (uInt rownr, String* dataPtr);
00202     // </group>
00203 
00204     // Get the scalar value with a non-standard data type in the given row.
00205     void getOtherV    (uInt rownr, void* dataPtr);
00206 
00207     // Put the scalar value with a standard data type into the given row.
00208     // <group>
00209     void putBoolV     (uInt rownr, const Bool* dataPtr);
00210     void putuCharV    (uInt rownr, const uChar* dataPtr);
00211     void putShortV    (uInt rownr, const Short* dataPtr);
00212     void putuShortV   (uInt rownr, const uShort* dataPtr);
00213     void putIntV      (uInt rownr, const Int* dataPtr);
00214     void putuIntV     (uInt rownr, const uInt* dataPtr);
00215     void putfloatV    (uInt rownr, const float* dataPtr);
00216     void putdoubleV   (uInt rownr, const double* dataPtr);
00217     void putComplexV  (uInt rownr, const Complex* dataPtr);
00218     void putDComplexV (uInt rownr, const DComplex* dataPtr);
00219     void putStringV   (uInt rownr, const String* dataPtr);
00220     // </group>
00221 
00222     // Put the scalar value with a non-standard data type into the given row.
00223     void putOtherV    (uInt rownr, const void* dataPtr);
00224 
00225     // Get all scalar values in the column.
00226     // The argument dataPtr is in fact a Vector<T>*, but a void*
00227     // is needed to be generic.
00228     // The vector pointed to by dataPtr has to have the correct length
00229     // (which is guaranteed by the ScalarColumn getColumn function).
00230     void getScalarColumnV (void* dataPtr);
00231 
00232     // Put all scalar values in the column.
00233     // The argument dataPtr is in fact a const Vector<T>*, but a const void*
00234     // is needed to be generic.
00235     // The vector pointed to by dataPtr has to have the correct length
00236     // (which is guaranteed by the ScalarColumn putColumn function).
00237     void putScalarColumnV (const void* dataPtr);
00238 
00239     // Get some scalar values in the column.
00240     // The argument dataPtr is in fact a Vector<T>*, but a void*
00241     // is needed to be generic.
00242     // The vector pointed to by dataPtr has to have the correct length
00243     // (which is guaranteed by the ScalarColumn getColumn function).
00244     virtual void getScalarColumnCellsV (const RefRows& rownrs,
00245                                         void* dataPtr);
00246 
00247     // Put some scalar values in the column.
00248     // The argument dataPtr is in fact a const Vector<T>*, but a const void*
00249     // is needed to be generic.
00250     // The vector pointed to by dataPtr has to have the correct length
00251     // (which is guaranteed by the ScalarColumn getColumn function).
00252     virtual void putScalarColumnCellsV (const RefRows& rownrs,
00253                                         const void* dataPtr);
00254 
00255     // Get the array value in the given row.
00256     // The argument dataPtr is in fact a Array<T>*, but a void*
00257     // is needed to be generic.
00258     // The array pointed to by dataPtr has to have the correct shape
00259     // (which is guaranteed by the ArrayColumn get function).
00260     void getArrayV (uInt rownr, void* dataPtr);
00261 
00262     // Put the array value into the given row.
00263     // The argument dataPtr is in fact a const Array<T>*, but a const void*
00264     // is needed to be generic.
00265     // The array pointed to by dataPtr has to have the correct shape
00266     // (which is guaranteed by the ArrayColumn put function).
00267     void putArrayV (uInt rownr, const void* dataPtr);
00268 
00269     // Get a section of the array in the given row.
00270     // The argument dataPtr is in fact a Array<T>*, but a void*
00271     // is needed to be generic.
00272     // The array pointed to by dataPtr has to have the correct shape
00273     // (which is guaranteed by the ArrayColumn getSlice function).
00274     void getSliceV (uInt rownr, const Slicer& slicer, void* dataPtr);
00275 
00276     // Put into a section of the array in the given row.
00277     // The argument dataPtr is in fact a const Array<T>*, but a const void*
00278     // is needed to be generic.
00279     // The array pointed to by dataPtr has to have the correct shape
00280     // (which is guaranteed by the ArrayColumn putSlice function).
00281     void putSliceV (uInt rownr, const Slicer& slicer, const void* dataPtr);
00282 
00283     // Get all scalar values in the column.
00284     // The argument dataPtr is in fact a Vector<T>*, but a void*
00285     // is needed to be generic.
00286     // The vector pointed to by dataPtr has to have the correct length
00287     // (which is guaranteed by the ScalarColumn getColumn function).
00288     void getArrayColumnV (void* dataPtr);
00289 
00290     // Put all scalar values in the column.
00291     // The argument dataPtr is in fact a const Vector<T>*, but a const void*
00292     // is needed to be generic.
00293     // The vector pointed to by dataPtr has to have the correct length
00294     // (which is guaranteed by the ScalarColumn putColumn function).
00295     void putArrayColumnV (const void* dataPtr);
00296 
00297     // Get some array values in the column.
00298     // The argument dataPtr is in fact an Array<T>*, but a void*
00299     // is needed to be generic.
00300     // The vector pointed to by dataPtr has to have the correct length
00301     // (which is guaranteed by the ArrayColumn getColumn function).
00302     virtual void getArrayColumnCellsV (const RefRows& rownrs,
00303                                        void* dataPtr);
00304 
00305     // Put some array values in the column.
00306     // The argument dataPtr is in fact an const Array<T>*, but a const void*
00307     // is needed to be generic.
00308     // The vector pointed to by dataPtr has to have the correct length
00309     // (which is guaranteed by the ArrayColumn getColumn function).
00310     virtual void putArrayColumnCellsV (const RefRows& rownrs,
00311                                        const void* dataPtr);
00312 
00313     // Get a section of all arrays in the column.
00314     // The argument dataPtr is in fact a Array<T>*, but a void*
00315     // is needed to be generic.
00316     // The array pointed to by dataPtr has to have the correct shape
00317     // (which is guaranteed by the ArrayColumn getColumn function).
00318     void getColumnSliceV (const Slicer& slicer, void* dataPtr);
00319 
00320     // Put a section into all arrays in the column.
00321     // The argument dataPtr is in fact a const Array<T>*, but a const void*
00322     // is needed to be generic.
00323     // The array pointed to by dataPtr has to have the correct shape
00324     // (which is guaranteed by the ArrayColumn putColumn function).
00325     void putColumnSliceV (const Slicer& slicer, const void* dataPtr);
00326 
00327     // Get a section of some arrays in the column.
00328     // The argument dataPtr is in fact an Array<T>*, but a void*
00329     // is needed to be generic.
00330     // The array pointed to by dataPtr has to have the correct shape
00331     // (which is guaranteed by the ArrayColumn getColumn function).
00332     virtual void getColumnSliceCellsV (const RefRows& rownrs,
00333                                        const Slicer& slicer, void* dataPtr);
00334 
00335     // Put into a section of some arrays in the column.
00336     // The argument dataPtr is in fact a const Array<T>*, but a const void*
00337     // is needed to be generic.
00338     // The array pointed to by dataPtr has to have the correct shape
00339     // (which is guaranteed by the ArrayColumn putColumn function).
00340     virtual void putColumnSliceCellsV (const RefRows& rownrs,
00341                                        const Slicer& slicer,
00342                                        const void* dataPtr);
00343 
00344 
00345     //# Now define the data members.
00346     ForwardColumnEngine* enginePtr_p;  //# pointer to parent engine
00347     String        colName_p;           //# The name of the column
00348     int           dataType_p;          //# data type of the column
00349     String        dataTypeId_p;        //# data type Id of the column
00350     ROTableColumn refCol_p;            //# Column in referenced table
00351     //#                                    This is only filled in when
00352     //#                                    a new table is created.
00353     Bool          writable_p;          //# True = column is writable
00354     Table         origTable_p;         //# The original table for this column
00355     BaseColumn*   colPtr_p;            //# pointer to column in original table
00356 };
00357 
00358 
00359 
00360 
00361 // <summary>
00362 // Virtual column engine forwarding to other columns
00363 // </summary>
00364 
00365 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00366 // </reviewed>
00367 
00368 // <use visibility=export>
00369 
00370 // <prerequisite>
00371 //# Classes you should understand before using this one.
00372 //   <li> VirtualColumnEngine
00373 // </prerequisite>
00374 
00375 // <synopsis>
00376 // ForwardColumnEngine is a data manager which forwards
00377 // the gets and puts of columns to columns with the same names in
00378 // another table.
00379 // It is, in fact, a reference to the other table columns.
00380 // The engine consists of a set of
00381 // <linkto class="ForwardColumn:description">ForwardColumn</linkto>
00382 // objects, which handle the actual gets and puts.
00383 // </synopsis> 
00384 
00385 // <motivation>
00386 // This class will be used by the calibration software.
00387 // Most columns in a measurement table will be forwarded
00388 // (thus bound to a ForwardColumnEngine object), while
00389 // a few (i.e. the data themselves) will be calculated by a dedicated
00390 // calibration engine.
00391 // </motivation>
00392 
00393 // <example>
00394 // <srcblock>
00395 //    // The original table.
00396 //    Table tab("someTable");
00397 //    // Create another table with the same description.
00398 //    SetupNewTable newtab("tForwardCol1.data", tab.tableDesc(), Table::New);
00399 //    // Create an engine which forwards to the original table.
00400 //    // Bind all columns in the new table to the forwarding engine.
00401 //    ForwardColumnEngine fce(tab);
00402 //    newtab.bindAll (fce);
00403 //    // Create the new table.
00404 //    // Every get and put on this table is forwarded to the original table.
00405 //    // NB. Puts cannot be done here, because the original table was
00406 //    //     opened as readonly.
00407 //    // Of course, some columns could have been bound to another
00408 //    // data manager (storage manager, calibration engine, ...).
00409 //    Table forwTab(newtab);
00410 // </srcblock>
00411 // </example>
00412 
00413 class ForwardColumnEngine : public VirtualColumnEngine
00414 {
00415 public:
00416 
00417     // The default constructor is required for reconstruction of the
00418     // engine when a table is read back.
00419     ForwardColumnEngine (const String& dataManagerName, const Record& spec);
00420 
00421     // Create the engine.
00422     // The columns using this engine will reference the given table.
00423     // The data manager gets the given name.
00424     ForwardColumnEngine (const Table& referencedTable,
00425                          const String& dataManagerName);
00426 
00427     // Create the engine.
00428     // The columns using this engine will reference the given table.
00429     // The data manager has no name.
00430     ForwardColumnEngine (const Table& referencedTable);
00431 
00432     // Destructor is mandatory.
00433     ~ForwardColumnEngine();
00434 
00435     // Clone the engine object.
00436     DataManager* clone() const;
00437 
00438     // Return the name of the data manager. This is the name of this
00439     // instantiation of the data manager, thus not its type name.
00440     String dataManagerName() const;
00441 
00442     // Return the type of the engine (i.e. its class name ForwardColumnEngine).
00443     String dataManagerType() const;
00444 
00445     // Record a record containing data manager specifications.
00446     virtual Record dataManagerSpec() const;
00447 
00448     // Get the suffix to be used for names.
00449     const String& suffix() const;
00450 
00451     // Return the name of the class.
00452     static String className();
00453 
00454     // Register the class name and the static makeObject "constructor".
00455     // This will make the engine known to the table system.
00456     static void registerClass();
00457 
00458 protected:
00459     // Set the suffix.
00460     void setSuffix (const String& suffix);
00461 
00462     // Add a ForwardColumn object to the block.
00463     void addForwardColumn (ForwardColumn* colp);
00464 
00465     // Get access to the refTable_p data member.
00466     const Table& refTable() const
00467         { return refTable_p; }
00468 
00469     // Do the creation (i.e. initialization) of the engine.
00470     void baseCreate();
00471 
00472     // Do the preparation of the engine by preparing all columns.
00473     void basePrepare();
00474 
00475 private:
00476     // The copy constructor is forbidden (so it is private).
00477     ForwardColumnEngine (const ForwardColumnEngine&);
00478 
00479     // Assignment is forbidden (so it is private).
00480     ForwardColumnEngine& operator= (const ForwardColumnEngine&);
00481 
00482     // This data manager allows to add rows.
00483     Bool canAddRow() const;
00484 
00485     // This data manager allows to delete rows.
00486     Bool canRemoveRow() const;
00487 
00488     // Add rows to all columns.
00489     // This is not doing anything (but needed to override the default).
00490     void addRow (uInt nrrow);
00491 
00492     // Delete a row from all columns.
00493     // This is not doing anything (but needed to override the default).
00494     void removeRow (uInt rownr);
00495 
00496     // This data manager allows to add columns.
00497     Bool canAddColumn() const;
00498 
00499     // This data manager allows to delete columns.
00500     Bool canRemoveColumn() const;
00501 
00502     // Add a column.
00503     void addColumn (DataManagerColumn*);
00504 
00505     // Delete a column.
00506     void removeColumn (DataManagerColumn*);
00507 
00508     // Create the column object for the scalar column in this engine.
00509     DataManagerColumn* makeScalarColumn (const String& columnName,
00510                                          int dataType,
00511                                          const String& dataTypeId);
00512 
00513     // Create the column object for the indirect array column in this engine.
00514     DataManagerColumn* makeIndArrColumn (const String& columnName,
00515                                          int dataType,
00516                                          const String& dataTypeId);
00517 
00518     // Initialize the object for a new table.
00519     // It defines the column keywords containing the name of the
00520     // original table, which can be the parent of the referenced table.
00521     void create (uInt initialNrrow);
00522 
00523     // Initialize the engine.
00524     // It gets the name of the original table(s) from the column keywords,
00525     // opens those tables and attaches the ForwardColumn objects to the
00526     // columns in those tables.
00527     void prepare();
00528 
00529     // Reopen the engine for read/write access.
00530     // It makes all its columns writable if their underlying table is writable.
00531     void reopenRW();
00532 
00533 
00534     // Define the various engine column objects.
00535     PtrBlock<ForwardColumn*> refColumns_p;
00536     // The referenced table.
00537     // For newly created tables this is filled in by the constructor.
00538     // For existing tables this is filled in by the first ForwardColumn
00539     // object being constructed.
00540     Table refTable_p;
00541     // The name of the data manager.
00542     String dataManName_p;
00543     // The suffix to be used in names.
00544     String suffix_p;
00545 
00546 
00547 public:
00548     // Set RefTable_p if not set yet.
00549     // This is done by ForwardColumn to cover the case for existing
00550     // tables where the default constructor of ForwardColumnEngine
00551     // is used and refTable_p is not filled in.
00552     void setRefTable (const Table& refTable);
00553 
00554     // Define the "constructor" to construct this engine when a
00555     // table is read back.
00556     // This "constructor" has to be registered by the user of the engine.
00557     // If the engine is commonly used, its registration can be added
00558     // into the registerAllCtor function in DataManReg.cc. 
00559     // This function gets automatically invoked by the table system.
00560     static DataManager* makeObject (const String& dataManagerType,
00561                                     const Record& spec);
00562 };
00563 
00564 
00565 
00566 inline const String& ForwardColumnEngine::suffix() const
00567     { return suffix_p; }
00568 
00569 inline void ForwardColumnEngine::setSuffix (const String& suffix)
00570     { suffix_p = suffix; }
00571 
00572 
00573 
00574 } //# NAMESPACE CASA - END
00575 
00576 #endif