casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ScalarColumn.h
Go to the documentation of this file.
00001 //# SclarColumn.h: access to a scalar table column with arbitrary data type
00002 //# Copyright (C) 1994,1995,1996,1997,1998
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: ScalarColumn.h 21298 2012-12-07 14:53:03Z gervandiepen $
00027 
00028 #ifndef TABLES_SCALARCOLUMN_H
00029 #define TABLES_SCALARCOLUMN_H
00030 
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <tables/Tables/TableColumn.h>
00035 #include <tables/Tables/ColumnCache.h>
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 //# Forward Declarations
00040 class BaseColumn;
00041 class RefRows;
00042 template<class T> class Vector;
00043 class String;
00044 
00045 
00046 // <summary>
00047 // Access to a scalar table column with arbitrary data type
00048 // </summary>
00049 
00050 // <use visibility=export>
00051 
00052 // <reviewed reviewer="dschieb" date="1994/08/10" tests="none">
00053 // </reviewed>
00054  
00055 // <prerequisite>
00056 //   <li> Table
00057 //   <li> TableColumn
00058 // </prerequisite>
00059 
00060 // <etymology>
00061 // ScalarColumn<T> gives read and write access to a column in a table
00062 // containing a scalar with data type T.
00063 // </etymology>
00064 
00065 // <synopsis> 
00066 // The class ScalarColumn allows read and write access to a column
00067 // containing scalar values with an arbitrary data type.
00068 // It is possible to get the data in an individual cell (i.e. table row)
00069 // and to get the column as a whole.
00070 //
00071 // A default constructor is defined to allow construction of an array
00072 // of ScalarColumn objects. However, this constructs an object not
00073 // referencing a column. Functions like get, etc. will fail (i.e. result
00074 // in a segmentation fault) when used on such objects. The functions
00075 // isNull and throwIfNull can be used to test on this.
00076 // The functions attach and reference can fill in the object.
00077 // </synopsis> 
00078 
00079 // <example>
00080 // See module <linkto module="Tables#open">Tables</linkto>.
00081 // </example>
00082 
00083 template<class T>
00084 class ScalarColumn : public TableColumn
00085 {
00086 public:
00087 
00088     // The default constructor creates a null object, i.e. it
00089     // does not reference a table column.
00090     // The sole purpose of this constructor is to allow construction
00091     // of an array of ScalarColumn objects.
00092     // The functions reference and attach can be used to make a null object
00093     // reference a column.
00094     // Note that get functions, etc. will cause a segmentation fault
00095     // when operating on a null object. It was felt it was too expensive
00096     // to test on null over and over again. The user should use the isNull
00097     // or throwIfNull function in case of doubt.
00098     ScalarColumn();
00099 
00100     // Construct for the given column in the given table.
00101     ScalarColumn (const Table&, const String& columnName);
00102 
00103     // Construct from the given table column.
00104     // This constructor is useful if first a table column was constructed,
00105     // its type is determined and thereafter used to construct the
00106     // correct column object.
00107     explicit ScalarColumn (const TableColumn&);
00108 
00109     // Copy constructor (reference semantics).
00110     ScalarColumn (const ScalarColumn<T>&);
00111 
00112     ~ScalarColumn();
00113 
00114     // Clone the object.
00115     virtual TableColumn* clone() const;
00116 
00117     // Assignment uses reference semantics, thus works the same
00118     // as function reference.
00119     ScalarColumn<T>& operator= (const ScalarColumn<T>&);
00120 
00121     // Change the reference to another column.
00122     // This is in fact an assignment operator with reference semantics.
00123     // It removes the reference to the current column and creates
00124     // a reference to the column referenced in the other object.
00125     // It will handle null objects correctly.
00126     void reference (const ScalarColumn<T>&);
00127 
00128     // Attach a column to the object.
00129     // This is in fact only a shorthand for 
00130     // <br><src> reference (ScalarColumn<T> (table, columnName)); </src>
00131     void attach (const Table& table, const String& columnName)
00132         { reference (ScalarColumn<T> (table, columnName)); }
00133 
00134     // Get the data from a particular cell (i.e. table row).
00135     // The row numbers count from 0 until #rows-1.
00136     // <group>
00137     void get (uInt rownr, T& value) const
00138     {
00139         TABLECOLUMNCHECKROW(rownr);
00140         Int off = colCachePtr_p->offset(rownr);
00141         if (off >= 0) {
00142             value = ((T*)(colCachePtr_p->dataPtr()))[off];
00143         }else{
00144             baseColPtr_p->get (rownr, &value);
00145         }
00146     }
00147     T get (uInt rownr) const
00148     {
00149         T value;
00150         get (rownr, value);
00151         return value;
00152     }
00153     T operator() (uInt rownr) const
00154     {
00155         T value;
00156         get (rownr, value);
00157         return value;
00158     }
00159     // </group>
00160 
00161     // Get the vector of all values in the column.
00162     // According to the assignment rules of class Array, the destination
00163     // vector must be empty or its length must be the number of cells
00164     // in the column (i.e. the number of rows in the table).
00165     void getColumn (Vector<T>& vec, Bool resize = False) const;
00166 
00167     // Get the vector of all values in the column.
00168     Vector<T> getColumn() const;
00169 
00170     // Get the vector of a range of values in the column.
00171     // The Slicer object can be used to specify start, end (or length),
00172     // and stride of the rows to get.
00173     // According to the assignment rules of class Array, the destination
00174     // vector must be empty or its length must be the number of cells
00175     // in the column (i.e. the number of rows in the slicer).
00176     void getColumnRange (const Slicer& rowRange, Vector<T>& vec,
00177                          Bool resize = False) const;
00178 
00179     // Get the vector of a range of values in the column.
00180     // The Slicer object can be used to specify start, end (or length),
00181     // and stride of the rows to get..
00182     Vector<T> getColumnRange (const Slicer& rowRange) const;
00183 
00184     // Get the vector of some values in the column.
00185     // The Slicer object can be used to specify start, end (or length),
00186     // and stride of the rows to get.
00187     // According to the assignment rules of class Array, the destination
00188     // vector must be empty or its length must be the number of cells
00189     // in the column (i.e. the number of rows in the RefRows object).
00190     void getColumnCells (const RefRows& rownrs, Vector<T>& vec,
00191                          Bool resize = False) const;
00192 
00193     // Get the vector of some values in the column.
00194     Vector<T> getColumnCells (const RefRows& rownrs) const;
00195 
00196     // Put the value in a particular cell (i.e. table row).
00197     // The row numbers count from 0 until #rows-1.
00198     void put (uInt rownr, const T& value)
00199         { TABLECOLUMNCHECKROW(rownr); checkWritable();
00200           baseColPtr_p->put (rownr, &value); }
00201 
00202     // Copy the value of a cell of that column to a cell of this column.
00203     // The data types of both columns must be the same.
00204     // <group>
00205     // Use the same row numbers for both cells.
00206     void put (uInt rownr, const ScalarColumn<T>& that)
00207         { put (rownr, that, rownr); }
00208     // Use possibly different row numbers for that (i.e. input) and
00209     // and this (i.e. output) cell.
00210     void put (uInt thisRownr, const ScalarColumn<T>& that, uInt thatRownr);
00211     // </group>
00212 
00213     // Copy the value of a cell of that column to a cell of this column.
00214     // This function uses a generic TableColumn object as input.
00215     // If possible the data will be promoted to the data type of this column.
00216     // Otherwise an exception is thrown.
00217     // <group>
00218     // Use the same row numbers for both cells.
00219     void put (uInt rownr, const TableColumn& that)
00220         { put (rownr, that, rownr); }
00221     // Use possibly different row numbers for that (i.e. input) and
00222     // and this (i.e. output) cell.
00223     void put (uInt thisRownr, const TableColumn& that, uInt thatRownr);
00224     // </group>
00225 
00226     // Put the vector of all values in the column.
00227     // The length of the vector must be the number of cells in the column
00228     // (i.e. the number of rows in the table).
00229     void putColumn (const Vector<T>& vec);
00230 
00231     // Put the vector of a range of values in the column.
00232     // The Slicer object can be used to specify start, end (or length),
00233     // and stride of the rows to put.
00234     // The length of the vector must be the number of cells in the slice.
00235     void putColumnRange (const Slicer& rowRange, const Vector<T>& vec);
00236 
00237     // Put the vector of some values in the column.
00238     // The length of the vector must be the number of cells in the RefRows
00239     // object.
00240     void putColumnCells (const RefRows& rownrs, const Vector<T>& vec);
00241 
00242     // Put the same value in all cells of the column.
00243     void fillColumn (const T& value);
00244 
00245     // Put the contents of a column with the same data type into this column.
00246     // To put the contents of a column with a different data type into
00247     // this column, the function TableColumn::putColumn can be used
00248     // (provided the data type promotion is possible).
00249     // In fact, this function is an assignment operator with copy semantics.
00250     void putColumn (const ScalarColumn<T>& that);
00251 
00252 private:
00253     // Check if the data type matches the column data type.
00254     void checkDataType() const;
00255 
00256 protected:
00257     // Keep a switch to determine if an entire column can be accessed.
00258     // True = yes;  False = no.
00259     mutable Bool canAccessColumn_p;
00260     // Keep a switch to know if access knowledge is permanent or has
00261     // to be asked again the next time.
00262     mutable Bool reaskAccessColumn_p;
00263 };
00264 
00265 
00266 //# Make old name ROScalarColumn still available.
00267 #define ROScalarColumn ScalarColumn
00268 
00269 
00270 } //# NAMESPACE CASA - END
00271 
00272 #ifndef CASACORE_NO_AUTO_TEMPLATES
00273 #include <tables/Tables/ScalarColumn.tcc>
00274 #endif //# CASACORE_NO_AUTO_TEMPLATES
00275 #endif