casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TableKeyword.h
Go to the documentation of this file.
00001 //# TableKeyword.h: A keyword value representing a table
00002 //# Copyright (C) 1996,1997,1999,2000,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: TableKeyword.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef TABLES_TABLEKEYWORD_H
00029 #define TABLES_TABLEKEYWORD_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <tables/Tables/TableAttr.h>
00034 #include <casa/BasicSL/String.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 //# Forward Declarations
00039 class Table;
00040 
00041 
00042 // <summary>
00043 // Keyword value representing a table
00044 // </summary>
00045 
00046 // <use visibility=local>
00047 
00048 // <reviewed reviewer="Mark Wieringa" date="1996/04/15" tests="tTableRecord">
00049 // </reviewed>
00050 
00051 // <prerequisite>
00052 //# Classes you should understand before using this one.
00053 //   <li> <linkto class=TableRecord>TableRecord</linkto>
00054 //   <li> <linkto class=Table>Table</linkto>
00055 // </prerequisite>
00056 
00057 // <synopsis>
00058 // TableKeyword represents a record keyword field containing a table.
00059 // It is used by class TableRecord, which in its turn is meant to be
00060 // used by the Table class.
00061 // It serves the following purposes:
00062 // <ul>
00063 // <li> A table is only opened on demand, i.e. when the keyword
00064 //      is accessed for the first time. When opened, the function
00065 //      closeTable makes it possible to close a table when not
00066 //      needed anymore (provided the table is not used elsewhere).
00067 //      It will automatically be reopened when used again.
00068 // <li> A switch is maintained which indicates if the table
00069 //      should be opened as readonly or read/write.
00070 //      A table is opened as read/write when the switch is read/write and
00071 //      when the table is writable. Otherwise it is opened as readonly.
00072 //      When a parent table is read back, its TableKeyword's will be
00073 //      read back and the switch will be set to the access-mode
00074 //      (readonly or read/write) of the parent table.
00075 //      When a new table is inserted, the access-mode is taken from the table.
00076 // <li> When the parent table is reopened as read/write, the table in
00077 //      this object will also be reopened as read/write (if the table is
00078 //      writable).
00079 // <li> When a TableKeyword gets written, only the table name will be
00080 //      written. Reading it back will set the correct access-mode, while
00081 //      the table will not be opened until necessary.
00082 //      However, when reading a parent table back it is possible that it
00083 //      is done from a different directory than where it was created.
00084 //      Therefore the directory of the parent table is prepended to the
00085 //      TableKeyword subtable name. Similarly, when written it is stripped off.
00086 //      <br>E.g. parent table XX and subtable SUB are created in the working
00087 //      directory WD. Reading back is done from another directory by
00088 //      specifying WD/XX. WD will be prepended to SUB.
00089 // </ul>
00090 // </synopsis>
00091 
00092 // <motivation>
00093 // This class provides the extra functionality for keywords containing
00094 // tables. This is needed because tables are much more complex entities
00095 // than scalars or arrays.
00096 // </motivation>
00097 
00098 // <example>
00099 // <srcblock>
00100 // // Store a table in the keyword set.
00101 // void someFunc (const Table& subTable)
00102 // {
00103 //     // Open the table and get access to the table keyword set.
00104 //     Table table("table.data", Table::Update);
00105 //     TableRecord& keyset = table.rwKeywordSet();
00106 //     keyset.defineTable ("KeyTab", subTable);
00107 // }
00108 //
00109 // // Open the table and get the table from keyword KeyTab.
00110 // // It shows that this can be done in one statement.
00111 // Table table("table.data");
00112 // Table subTab = table.keywordSet().asTable ("KeyTab");
00113 // </srcblock>
00114 // </example>
00115 
00116 //# <todo asof="$DATE:$">
00117 //# A List of bugs, limitations, extensions or planned refinements.
00118 //# </todo>
00119 
00120 
00121 class TableKeyword
00122 {
00123 public:
00124     // Construct a TableKeyword with the given tableDescName.
00125     // When the tableDescName is empty the keyword is variable structured.
00126     // Otherwise it is fixed structured, meaning that only tables with a
00127     // description of that name can be assigned to this keyword.
00128     TableKeyword (const String& tableDescName);
00129 
00130     // Construct a TableKeyword from a Table.
00131     // <br>
00132     // When the tableDescName is empty the keyword is variable structured.
00133     // Otherwise it is fixed structured, meaning that only tables with a
00134     // description of that name can be assigned to this keyword.
00135     TableKeyword (const Table& table, const String& tableDescName);
00136 
00137     // Copy constructor (full copy semantics).
00138     TableKeyword (const TableKeyword& that);
00139 
00140     // Assignment (leaves tableDescName_p untouched).
00141     // This is only possible when both objects conform.
00142     // <group>
00143     TableKeyword& operator= (const TableKeyword& that);
00144     TableKeyword& operator= (const Table& table);
00145     // </group>
00146 
00147     ~TableKeyword();
00148 
00149     // Set the name of the table and the writable switch.
00150     // This is used when reading back a keyword.
00151     void set (const String& name, const TableAttr& parentAttr);
00152 
00153     // Set the keyword to read/write access.
00154     // If the table is already open, it will be reopened with read/write
00155     // access if the table is writable.
00156     void setRW();
00157 
00158     // Is the table in use in another process?
00159     // If <src>checkSubTables</src> is set, it is also checked if
00160     // a subtable is used in another process.
00161     Bool isMultiUsed (Bool checkSubTables) const;
00162 
00163     // Get the name of the table.
00164     const String& tableName() const;
00165 
00166     // Get the name of the table relative to parent table.
00167     // <group>
00168     String tableName (const String& parentName) const;
00169     String tableName (const TableAttr& parentAttr) const
00170       { return tableName (parentAttr.name()); }
00171     // </group>
00172 
00173     // Get the table.
00174     // It will be opened when necessary.
00175     Table table() const;
00176 
00177     // Get the table attributes.
00178     const TableAttr& tableAttributes() const
00179       { return attr_p; }
00180 
00181     // Set the table attributes.
00182     void setTableAttributes (const TableAttr& attr)
00183       { attr_p = attr; }
00184 
00185     // Close the table.
00186     void close() const;
00187 
00188     // Flush and optionally fsync the table.
00189     void flush (Bool fsync) const;
00190 
00191     // Rename the table if its path contains the old parent table name.
00192     void renameTable (const String& newParentName,
00193                       const String& oldParentName);
00194 
00195     // Test if the table in other conforms this table keyword.
00196     // It conforms when this description name is blank or matches the
00197     // table description name of the other.
00198     // <group>
00199     Bool conform (const TableKeyword& that) const;
00200     Bool conform (const Table& that) const;
00201     // </group>
00202 
00203     // Has the table a fixed description name?
00204     // It has when its description name is not empty.
00205     Bool isFixed() const;
00206 
00207 private:
00208     Table*    table_p;
00209     TableAttr attr_p;
00210     String    tableDescName_p;
00211 };
00212 
00213 
00214 
00215 inline const String& TableKeyword::tableName() const
00216 {
00217     return attr_p.name();
00218 }
00219 
00220 inline Bool TableKeyword::isFixed() const
00221 {
00222     return  (! tableDescName_p.empty());
00223 }
00224 
00225 
00226 } //# NAMESPACE CASA - END
00227 
00228 #endif