casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
VirtualTaQLColumn.h
Go to the documentation of this file.
00001 //# VirtualTaQLColumn.h: Virtual column engine based on TaQL
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 //# $Id: VirtualTaQLColumn.h 21051 2011-04-20 11:46:29Z gervandiepen $
00027 
00028 #ifndef TABLES_VIRTUALTAQLCOLUMN_H
00029 #define TABLES_VIRTUALTAQLCOLUMN_H
00030 
00031 //# Includes
00032 #include <tables/Tables/VirtColEng.h>
00033 #include <tables/Tables/DataManager.h>
00034 #include <casa/Arrays/IPosition.h>
00035 
00036 namespace casa {
00037 //# Forward Declarations
00038 class TableExprNode;
00039 
00040 
00041 // <category lib=aips module="Tables" sect="Virtual Columns">
00042 // <summary> Virtual scalar column using TaQL</summary>
00043 // <reviewed reviewer="GvD" date="2004/07/09" tests="">
00044 //
00045 // <prerequisite>
00046 //# Classes you should understand before using this one.
00047 //   <li> VirtualScalarColumn
00048 // </prerequisite>
00049 //
00050 // <synopsis> 
00051 // VirtualTaQLColumn is a virtual column engine to define the contents of a
00052 // column as a TaQL expression in which possibly other columns are used.
00053 // It is (of course) only possible to get data from the column; puts cannot
00054 // be done.
00055 // <br>
00056 // The expression result can be a scalar or array of the basic TaQL data types.
00057 // The column data type has to be conformant with that TaQL type, thus a
00058 // column of any integer type has to be used for an integer TaQL result.
00059 // <note role=caution> One has to be careful with deleting columns. If in an
00060 // existing table a TaQL expression uses a deleted column, the expression
00061 // cannot be parsed anymore and the table cannot be opened anymore.
00062 // In the future the Table System will be made more forgiving.
00063 // </note>
00064 // </synopsis> 
00065 //
00066 // <example>
00067 // The following example creates a table with a few columns.
00068 // One column is virtual and has a random value if Col3 is true.
00069 // Otherwise it has value 0.
00070 // <srcblock>
00071 //    // Create the table description.
00072 //    TableDesc td;
00073 //    td.addColumn (ScalarColumnDesc<DComplex>("Col1"));
00074 //    td.addColumn (ScalarColumnDesc<Int>("Col2"));
00075 //    td.addColumn (ScalarColumnDesc<Bool>("Col3"));
00076 //    td.addColumn (ScalarColumnDesc<Double>("ColVirt"));
00077 //  
00078 //    // Now create a new table from the description.
00079 //    SetupNewTable newTab("tmtest", td, Table::New);
00080 //    // Define the expression of the virtual column and bind the column to it.
00081 //    // The other columns are by default bound to StandardStMan.
00082 //    VirtualTaQLColumn engine("iif(Col3,rand(),0)");
00083 //    newTab.bindColumn("ColVirt", engine);
00084 //    Table tab(newTab);
00085 // </srcblock>
00086 // </example>
00087 
00088 class VirtualTaQLColumn : public VirtualColumnEngine, public DataManagerColumn
00089 {
00090 public:
00091 
00092   // Construct it with the given TaQL expression.
00093   VirtualTaQLColumn (const String& expr);
00094 
00095   // Construct it with the given specification.
00096   VirtualTaQLColumn (const Record& spec);
00097 
00098   // Destructor is mandatory.
00099   virtual ~VirtualTaQLColumn();
00100 
00101   // Clone the engine object.
00102   virtual DataManager* clone() const;
00103 
00104   // Get the data manager specification.
00105   virtual Record dataManagerSpec() const;
00106 
00107   // Return the type name of the engine.
00108   // (i.e. its class name VirtualTaQLColumn).
00109   virtual String dataManagerType() const;
00110 
00111   // Return the name of the class.
00112   static String className();
00113 
00114   // Register the class name and the static makeObject "constructor".
00115   // This will make the engine known to the table system.
00116   static void registerClass();
00117 
00118   // Define the "constructor" to construct this engine when a
00119   // table is read back.
00120   // This "constructor" has to be registered by the user of the engine.
00121   // If the engine is commonly used, its registration can be added
00122   // into the registerAllCtor function in DataManReg.cc. 
00123   // This function gets automatically invoked by the table system.
00124   static DataManager* makeObject (const String& dataManagerName,
00125                                   const Record& spec);
00126 
00127   // Return the TaQL expression used.
00128   const String& expression() const
00129     { return itsExpr; }
00130 
00131   // Functions to return column info.
00132   // <group>
00133   virtual int dataType() const;
00134   virtual Bool isWritable() const;
00135   virtual uInt ndim (uInt rownr);
00136   virtual IPosition shape (uInt rownr);
00137   virtual Bool isShapeDefined (uInt rownr);
00138   // </group>
00139 
00140 private:
00141   // Copy is not needed and therefore forbidden (so it is made private).
00142   VirtualTaQLColumn (const VirtualTaQLColumn&);
00143 
00144   // Assignment is not needed and therefore forbidden (so it is made private).
00145   VirtualTaQLColumn& operator= (const VirtualTaQLColumn&);
00146 
00147   // Create the column object for the scalar column in this engine.
00148   virtual DataManagerColumn* makeScalarColumn (const String& columnName,
00149                                                int dataType, const String&);
00150 
00151   // Create the column object for the indirect array column in this engine.
00152   virtual DataManagerColumn* makeIndArrColumn (const String& columnName,
00153                                                int dataType,
00154                                                const String& dataTypeId);
00155 
00156   // Let the engine initialize the object for a new table.
00157   // It defines a column keyword holding the expression.
00158   virtual void create (uInt);
00159 
00160   // Prepare compiles the expression.
00161   virtual void prepare();
00162 
00163   //# We could also define the getBlockXXV functions, but
00164   //# that is not required. The default implementation gets
00165   //# one value. Possible optimization can be done by
00166   //# implementing it here.
00167   //# The same is true for getColumn.
00168 
00169   // Get the scalar value in the given row.
00170   // The default implementation throws an "invalid operation" exception.
00171   // <group>
00172   virtual void getBoolV     (uInt rownr, Bool* dataPtr);
00173   virtual void getuCharV    (uInt rownr, uChar* dataPtr);
00174   virtual void getShortV    (uInt rownr, Short* dataPtr);
00175   virtual void getuShortV   (uInt rownr, uShort* dataPtr);
00176   virtual void getIntV      (uInt rownr, Int* dataPtr);
00177   virtual void getuIntV     (uInt rownr, uInt* dataPtr);
00178   virtual void getfloatV    (uInt rownr, float* dataPtr);
00179   virtual void getdoubleV   (uInt rownr, double* dataPtr);
00180   virtual void getComplexV  (uInt rownr, Complex* dataPtr);
00181   virtual void getDComplexV (uInt rownr, DComplex* dataPtr);
00182   virtual void getStringV   (uInt rownr, String* dataPtr);
00183   // </group>
00184 
00185   // Get the array value in the given row.
00186   // The argument dataPtr is in fact an Array<T>*, but a void*
00187   // is needed to be generic.
00188   // The array pointed to by dataPtr has to have the correct shape
00189   // (which is guaranteed by the ArrayColumn get function).
00190   // The default implementation throws an "invalid operation" exception.
00191   virtual void getArrayV (uInt rownr, void* dataPtr);
00192 
00193   // Get the result.
00194   IPosition getResult (uInt rownr, void* dataPtr);
00195 
00196   // Clear the result cache.
00197   void clearCurResult();
00198 
00199 
00200   //# Now define the data members.
00201   int            itsDataType;
00202   Bool           itsIsArray;
00203   String         itsColumnName;
00204   String         itsExpr;             //# TaQL expression
00205   TableExprNode* itsNode;             //# compiled TaQL expression
00206   Bool           itsTempWritable;
00207   Int            itsCurRow;           //# Currently evaluated row
00208   void*          itsCurResult;        //# result in itsCurRow
00209   IPosition      itsCurShape;         //# shape in itsCurRow
00210 };
00211 
00212 
00213 } //end namespace casa
00214 
00215 #endif