Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

fmwk/Config/Table.cxx

Go to the documentation of this file.
00001 
00002 // $Id: Table.cxx,v 1.1 2007/02/09 05:38:27 fmwk Exp $
00003 //
00005 //
00006 // messier@indiana.edu
00008 #include "Config/Table.h"
00009 #include "Config/Config.h"
00010 
00011 cfg::Table* cfg::Table::fInstance = 0;
00012 
00013 //......................................................................
00014 
00015 cfg::Table& cfg::Table::Instance() 
00016 {
00017 //======================================================================
00018 // Return the sole instance of this class
00019 //======================================================================
00020   if (fInstance == 0) fInstance = new cfg::Table;
00021   return (*fInstance);
00022 }
00023 
00024 //......................................................................
00025 
00026 cfg::Config* cfg::Table::GetConfig(const char* name, 
00027                                    const char* version) const 
00028 {
00029 //======================================================================
00030 // Find and return the configuration that matches the requested name
00031 // and version
00032 //======================================================================
00033   std::string n(name);
00034   std::string v(version);
00035   
00036   // Loop over configurations looking for one that matches
00037   ConfigList::iterator itr   (fConfigList.begin());
00038   ConfigList::iterator itrEnd(fConfigList.end());
00039   for (; itr!=itrEnd; ++itr) {
00040     cfg::Config* cfg = *itr;
00041     if (n == cfg->GetName() && v == cfg->GetVersion()) return cfg;
00042   }
00043 
00044   // Not found
00045   return 0;
00046 }
00047 
00048 //......................................................................
00049 
00050 void cfg::Table::GetObservers(const char*          name, 
00051                               const char*          version,
00052                               cfg::Table::ObsList& obslist) const
00053 {
00054 //======================================================================
00055 // Unpack the observers of configuration "name.version" to the list
00056 // obslist
00057 //======================================================================
00058   ObsTable::iterator otItr   (fObsTable.begin());
00059   ObsTable::iterator otItrEnd(fObsTable.end());
00060   for (; otItr!=otItrEnd; ++otItr) {
00061     cfg::Observer* obs    = otItr->first;
00062     NVPairList&  nvlist = otItr->second;
00063     
00064     NVPairList::iterator nvItr(nvlist.begin());
00065     NVPairList::iterator nvItrEnd(nvlist.end());
00066     for (; nvItr != nvItrEnd; ++nvItr) {
00067       std::string& n = nvItr->first;
00068       std::string& v = nvItr->second;
00069       
00070       if (n == name && v == version) obslist.push_back(obs);
00071       
00072     } // Loop on configuration name/version pairs
00073   } // Loop on all observers
00074 }
00075 
00076 //......................................................................
00077 
00078 void cfg::Table::Print(std::ostream& os) const 
00079 {
00080 //======================================================================
00081 // Print all the configurations and their watchers
00082 //======================================================================
00083   ConfigList::const_iterator itr(fConfigList.begin());
00084   ConfigList::const_iterator itrEnd(fConfigList.end());
00085   for (; itr!=itrEnd; ++itr) {
00086     cfg::Config* c = *itr;
00087     os << c->GetName() << "." << c->GetVersion() << "\t";
00088 
00089     os << "obs={ ";
00090     ObsList obslist;
00091     this->GetObservers(c->GetName(), c->GetVersion(), obslist);
00092     ObsList::const_iterator itr2(obslist.begin());
00093     ObsList::const_iterator itrEnd2(obslist.end());
00094     for (; itr2!=itrEnd2; ++itr2) {
00095       os << *itr2 << " ";
00096     }
00097     os << "}" << std::endl;
00098   }
00099 }
00100 
00101 //......................................................................
00102 
00103 void cfg::Table::AdoptConfig(cfg::Config* cfg) 
00104 {
00105 //======================================================================
00106 // Place the configuration into the list
00107 //======================================================================
00108   std::string n(cfg->GetName());
00109   std::string v(cfg->GetVersion());
00110   
00111   // Check if a configuration matching this name and version exists.
00112   // If yes, replace it
00113   ConfigList::iterator itr   (fConfigList.begin());
00114   ConfigList::iterator itrEnd(fConfigList.end());
00115   for (; itr!=itrEnd; ++itr) {
00116     cfg::Config* inlist = *itr;
00117     if (n == inlist->GetName() && v == inlist->GetVersion()) {
00118       // replace existing config with new
00119       delete inlist;
00120       *itr = cfg;
00121       return;
00122     }
00123   }
00124   
00125   // Insert the configuration into the list
00126   fConfigList.push_back(cfg);
00127 }
00128 
00129 //......................................................................
00130 
00131 void cfg::Table::SetWatch(const char*    config, 
00132                           const char*    version, 
00133                           cfg::Observer* obs) 
00134 {
00135 //======================================================================
00136 // Set a watch from observer obs to configuration with name
00137 // config.version
00138 //======================================================================
00139   NVPairList& nvlist = fObsTable[obs];
00140   
00141   NVPairList::iterator itr   (nvlist.begin());
00142   NVPairList::iterator itrEnd(nvlist.end());
00143   
00144   // Look if there is already a watch for this configuration. If yes,
00145   // update the version watched
00146   for (; itr!=itrEnd; ++itr) {
00147     if (itr->first == config) {
00148       itr->second = version;
00149       return;
00150     }
00151   }
00152   // Reach here if no watch was set for this config
00153   nvlist.push_back(NVPair(config,version));
00154 }
00155 
00156 //......................................................................
00157 
00158 void cfg::Table::RemoveWatch(const char* config, cfg::Observer* obs)
00159 {
00160 //======================================================================
00161 // Drop the watch set by observer obs to the configuration named
00162 // "config"
00163 //======================================================================
00164   // Get the list of configuration name/version pairs
00165   NVPairList& nvlist = fObsTable[obs];
00166   
00167   // Erase each entry which matches the config name
00168   NVPairList::iterator    itr(nvlist.begin());
00169   NVPairList::iterator itrEnd(nvlist.end());
00170   for (; itr!=itrEnd; ++itr) {
00171     if (itr->first == config) {
00172       nvlist.erase(itr);
00173       return;
00174     }
00175   }
00176 }
00177 
00178 //......................................................................
00179 
00180 void cfg::Table::RemoveAllWatches(cfg::Observer* obs) 
00181 {
00182   // Erase the configuration list for this observer
00183   NVPairList& nvplist = fObsTable[obs];
00184   nvplist.clear();
00185   
00186   fObsTable.erase(obs);
00187 }
00188 

Generated on Mon Nov 23 04:45:27 2009 for NOvA Offline by  doxygen 1.3.9.1