/* * File: gfTableManger.h * Header and Implementation for the gfTablemanager class. * * This class will manage the gfTables, it will ensure that only * 1 table of any type is in memory at any given time * The table manager will hold a list of all the tables. * The GF2M objects will ask for pointers to the these tables * If the tables don't exist, the manager will create the tables * If the tables do exist, the table manager will return a pointer to them * * Copyright 1996-2004 The MathWorks, Inc. * $Revision: 1.3.4.3 $ $Date: 2004/07/28 04:10:16 $ */ #ifndef _GFTABLEMANAGER_HEADER_ #define _GFTABLEMANAGER_HEADER_ /* disable msvc's warning */ #ifdef WIN32 #pragma warning (disable: 4786) #endif #include #include using namespace std; #include "gfTables.h" typedef map , gfTables *> gfTableMap; template class gfTableManager { public: static gfTableManager * Instance(); gfTables * getTables(int M, int Prim_Poly); static void RemoveInstance(); ~gfTableManager(); // non-virtual protected: gfTableManager(); gfTableManager(const gfTableManager&); gfTableManager & operator= (const gfTableManager &); private: static gfTableManager * theInstance; static int numInstances; gfTableMap tableMap; }; template gfTableManager *gfTableManager::theInstance = 0; template int gfTableManager::numInstances = 0; template gfTableManager *gfTableManager::Instance() { numInstances++; if(theInstance == 0) { theInstance = new gfTableManager; } return theInstance; } template gfTableManager::gfTableManager() { theInstance = 0; // just to be sure upon reconstruction of this class } template gfTableManager::~gfTableManager() { /* destroy all the tables (in two steps: delete and clear, see Josuttis, page 205) */ //erase the entire map. if(--numInstances == 0) { // First: delete the table entries that the gfTables pointers are pointing to for( gfTableMap::iterator tblMapItr = tableMap.begin(); tblMapItr != tableMap.end() ; tblMapItr++ ) { delete tblMapItr->second; } // Second: clear out the collection. This is necessary map maintainance tableMap.clear(); // reset this instance theInstance = 0; } } template gfTables * gfTableManager::getTables(int M, int Prim_Poly) { /* find the index that has the contains the M, prim_poly pair we're looking for */ gfTableMap::iterator Index = tableMap.find( make_pair(M, Prim_Poly) ); /* if Index == tableMap.end, then the tables we want don't exist yet * create them and return a pointer to them */ if( Index == tableMap.end()) { gfTables * newTable = new gfTables(M, Prim_Poly); tableMap.insert( gfTableMap::value_type ( make_pair(M,Prim_Poly), newTable)); return newTable; } else return tableMap[ make_pair(M,Prim_Poly)]; } template void gfTableManager::RemoveInstance() { if(numInstances==1) { delete theInstance; } else numInstances--; } #endif