casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TableCache.h
Go to the documentation of this file.
00001 //# TableCache.h: Cache of open tables
00002 //# Copyright (C) 1994,1995,1997,1999
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: TableCache.h 21040 2011-04-07 13:26:55Z gervandiepen $
00027 
00028 #ifndef TABLES_TABLECACHE_H
00029 #define TABLES_TABLECACHE_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/Containers/SimOrdMap.h>
00034 #include <casa/OS/Mutex.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 //# Forward Declarations
00039 class PlainTable;
00040 
00041 
00042 // <summary>
00043 // Cache of open tables
00044 // </summary>
00045 
00046 // <use visibility=local>
00047 
00048 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00049 // </reviewed>
00050 
00051 // <prerequisite>
00052 //# Classes you should understand before using this one.
00053 // </prerequisite>
00054 
00055 // <etymology>
00056 // TableCache represents a cache of open tables.
00057 // </etymology>
00058 
00059 // <synopsis> 
00060 // A TableCache object keeps track of the tables which have already
00061 // been opened in a program. It maps the name of a table to its
00062 // PlainTable object.
00063 // In principle only one TableCache object (statically defined in
00064 // class PlainTable) exists in a process.
00065 // The cache is used to prevent a table from being opened more than
00066 // once, which is not only a waste of space, but more importantly,
00067 // may give rise to synchronization problems.
00068 // Synchronization between the same table in multiple processes must
00069 // be done by a locking mechanism.
00070 //
00071 // TableCache is used by class Table and PlainTable.
00072 // Before opening a table, Table will first look in the cache.
00073 // Newly opened or created tables will be added to the cache.
00074 // When a table is actually closed, it will be removed from the cache.
00075 // </synopsis> 
00076 
00077 // <motivation>
00078 // When a RefTable is read back, it will also read back the table it
00079 // references. However, that table may have been opened before and
00080 // it is bad to have a table open more than once in the same program.
00081 // The TableCache class catches this and will not reopen the table.
00082 // </motivation>
00083 
00084 // <todo asof="$DATE:$">
00085 //# A List of bugs, limitations, extensions or planned refinements.
00086 //   <li> Currently only PlainTables are taken into account.
00087 //          Maybe RefTables should be too.
00088 // </todo>
00089 
00090 
00091 class TableCache
00092 {
00093 public:
00094 
00095     // Construct an empty cache of open tables.
00096     TableCache();
00097 
00098     ~TableCache();
00099 
00100     // Try to find a table with the given name in the cache.
00101     // Return a pointer to a table if found (thus if already open).
00102     // Return a zero pointer if not found.
00103     PlainTable* operator() (const String& tableName) const;
00104 
00105     // Try to find a table at the given index in the cache.
00106     // Return a pointer to a table if found (thus if already open).
00107     // Return a zero pointer if not found.
00108     PlainTable* operator() (uInt index) const;
00109 
00110     // Return the number of open tables in the cache.
00111     uInt ntable() const;
00112 
00113     // Add an open table to the cache.
00114     void define (const String& tableName, PlainTable*);
00115 
00116     // Remove an open table.
00117     void remove (const String& tableName);
00118 
00119     // Rename an open table.
00120     // If oldName is not in the cache, nothing will be done.
00121     void rename (const String& newName, const String& oldName);
00122 
00123 private:
00124     // The copy constructor is forbidden.
00125     TableCache (const TableCache&);
00126     // The assignment operator is forbidden.
00127     TableCache& operator= (const TableCache&);
00128 
00129     //# void* iso. PlainTable* is used in the map declaration
00130     //# to reduce the number of template instantiations.
00131     //# The .cc file will use (fully safe) casts.
00132     SimpleOrderedMap<String,void*> tableMap_p;
00133     //# A mutex to synchronize access to the cache.
00134     mutable Mutex itsMutex;
00135 };
00136 
00137 
00138 
00139 } //# NAMESPACE CASA - END
00140 
00141 #endif