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

cfg::Config Class Reference

A collection of configuration parameters. More...

#include <Config.h>

List of all members.

Public Types

typedef std::map< std::string,
Param * > 
ParamMap

Public Member Functions

const char * GetName () const
const char * GetVersion () const
const char * GetSource () const
const Paramoperator() (const char *pname) const
const Paramoperator() (const std::string &pname) const
bool IsEditable () const
 Config (const char *name, const char *version, const char *source)
 ~Config ()
void AdoptParam (Param *p)
void RemoveParam (const char *which="*")
ParamPar (const char *pname)
ParamPar (const std::string &pname)
bool EditOK () const
void SetReadOnly (int flag=1)
void SetSource (const char *filename)
ParamMapParMap ()

Static Public Member Functions

void SetEditAllOK ()
void Copy (Config &dest, const Config &src, const char *destVersion=0, const char *destSource=0)

Private Attributes

bool fEditable
 Allow edits to this config?

std::string fName
 Name of configuration.

std::string fVersion
 Version tag of configuration.

std::string fSource
 Source of config. data (file name eg.).

ParamMap fParam
 Collection of parameters.


Detailed Description

A collection of configuration parameters.

Configurations are known by their name and version. The source where the data used to fill them comes from (eg. XML file name, database possibly) is recorded.

In general, configurations are created and seldom modified. Modification of configurations is something of a tricky business as clients of the configuration require notification of the changes but probably don't want then piecemeal (parameter-by-parameter). For that reason the routines which allow modification of the configuration data are defines as protected and access to them is only allowed via the cfg::ConfigEditor class.

Definition at line 28 of file Config.h.


Member Typedef Documentation

typedef std::map<std::string, Param*> cfg::Config::ParamMap
 

Definition at line 62 of file Config.h.

Referenced by ParMap().


Constructor & Destructor Documentation

cfg::Config::Config const char *  name,
const char *  version,
const char *  source
 

These methods should *only* be used by cfg::ConfigBuilders and cfg::ConfigEditors.

Todo:
Find some way to enforce usage only by cfg::ConfigBuilders

Definition at line 25 of file Config.cxx.

00027                                         : 
00028   fEditable( true    ),
00029   fName    ( name    ),
00030   fVersion ( version ),
00031   fSource  ( source  )
00032 { }

cfg::Config::~Config  ) 
 

Definition at line 36 of file Config.cxx.

References fEditable, and RemoveParam().

00036 { fEditable = true; this->RemoveParam("*"); }


Member Function Documentation

void cfg::Config::AdoptParam Param p  ) 
 

Definition at line 196 of file Config.cxx.

References EditOK(), fParam, and cfg::Param::GetName().

Referenced by cfg::configBuilder::Build(), TestBuilder::BuildCfg1(), TestBuilder::BuildCfg2(), Copy(), evdb::CfgEdit::EditConfig(), and main().

00197 {
00198 //======================================================================
00199 // Insert the parameter p into a configuration. If the parameter
00200 // already exists in the configuration then replace it. Ownership of
00201 // the parameter stored at p is transferred to the configuration.
00202 //======================================================================
00203   this->EditOK();
00204   
00205   std::string s = p->GetName();
00206   
00207   // Check the parameter set for one of this name. If one is found
00208   // replace it
00209   cfg::Param* param = fParam[s];
00210   if (param==0) delete param;
00211   
00212   fParam[s] = p;
00213 }

void cfg::Config::Copy Config dest,
const Config src,
const char *  destVersion = 0,
const char *  destSource = 0
[static]
 

Definition at line 243 of file Config.cxx.

References AdoptParam(), EditOK(), fName, fParam, fSource, fVersion, and RemoveParam().

Referenced by cfg::configBuilder::Build(), cfg::ConfigEditor::Cancel(), cfg::ConfigEditor::Edit(), and cfg::ConfigEditor::Reset().

00247 {
00248 //======================================================================
00249 // Copy one configuration to another
00250 //   dest - configuration to copy to
00251 //   src  - configuration to copy from
00252 //   destVersion - version tag appied to dest (default appends '.copy')
00253 //   destSource  - source tag appied to dest (default appends '(copy)' )
00254 //======================================================================
00255   dest.EditOK();
00256 
00257   dest.fName = src.fName;
00258   if (destVersion==0) { 
00259     dest.fVersion  = src.fVersion;
00260     dest.fVersion += ".copy";
00261   }
00262   else {
00263     dest.fVersion = destVersion;
00264   }
00265   if (destSource==0) {
00266     dest.fSource  = src.fSource;
00267     dest.fSource += "(copy)";
00268   }
00269   else {
00270     dest.fSource = destSource;
00271   }
00272 
00273   // Wipe the parameter map clean and build a new one using the cfg::Param
00274   // copy constructor on each element in the source map
00275   dest.RemoveParam("*");
00276   cfg::Config::ParamMap::const_iterator    itr(src.fParam.begin());
00277   cfg::Config::ParamMap::const_iterator itrEnd(src.fParam.end());
00278   for (; itr!=itrEnd; ++itr) {
00279     cfg::Param* p = new cfg::Param(*itr->second);
00280     dest.AdoptParam(p);
00281   }
00282 }

bool cfg::Config::EditOK  )  const
 

Definition at line 136 of file Config.cxx.

References fEditable, GetVersion(), and gsAllEditOK.

Referenced by AdoptParam(), Copy(), cfg::ConfigEditor::Edit(), Par(), RemoveParam(), and SetReadOnly().

00137 {
00138 //======================================================================
00139 // Check if it is OK to edit this configuration. Throws exception is
00140 // edit is not allowed
00141 //======================================================================
00142   if (gsAllEditOK) return true;
00143   if (fEditable==false) {
00144     std::ostringstream os;
00145     os << "Attempt to get read access to read-only config "
00146        << "." << this->GetVersion();
00147     throw cfg::Exception(cfg::Exception::kConfigRO,
00148                          os.str().c_str(),
00149                          __FILE__,
00150                          __LINE__);
00151   }
00152   return fEditable;
00153 }

const char * cfg::Config::GetName  )  const
 

Definition at line 40 of file Config.cxx.

References fName.

Referenced by cfg::Table::AdoptConfig(), cfg::ConfigEditor::Apply(), evdb::CfgEdit::CfgEdit(), cfg::ConfigEditor::Edit(), cfg::Table::GetConfig(), main(), cfg::Table::Print(), cfg::ConfigBuilder::Publish(), TestObs::Update(), and TestObserver::Update().

00040 { return fName.c_str(); }

const char * cfg::Config::GetSource  )  const
 

Definition at line 48 of file Config.cxx.

References fSource.

Referenced by cfg::ConfigEditor::Cancel(), evdb::CfgEdit::CfgEdit(), cfg::ConfigEditor::Edit(), main(), and cfg::ConfigEditor::Reset().

00048 { return fSource.c_str(); }

const char * cfg::Config::GetVersion  )  const
 

Definition at line 44 of file Config.cxx.

References fVersion.

Referenced by cfg::Table::AdoptConfig(), cfg::ConfigEditor::Apply(), cfg::ConfigEditor::Cancel(), evdb::CfgEdit::CfgEdit(), cfg::ConfigEditor::Edit(), EditOK(), cfg::Table::GetConfig(), main(), cfg::Table::Print(), cfg::ConfigBuilder::Publish(), cfg::ConfigEditor::Reset(), TestObs::Update(), and TestObserver::Update().

00044 { return fVersion.c_str(); }

bool cfg::Config::IsEditable  )  const
 

Definition at line 52 of file Config.cxx.

References fEditable.

00052 { return fEditable; }

const cfg::Param & cfg::Config::operator() const std::string &  pname  )  const
 

Definition at line 73 of file Config.cxx.

References fName, fParam, and fVersion.

00074 {
00075 //======================================================================
00076 // Allows read-only access to parameters
00077 //======================================================================
00078   cfg::Config::ParamMap::iterator itr = fParam.find(pname);
00079   if (itr==fParam.end()) {
00080     std::ostringstream os;
00081     os << "Parameter " << pname    << " not found in configuration " 
00082        << fName << ":" << fVersion << std::endl;
00083     throw cfg::Exception(cfg::Exception::kParamNotFound,
00084                          os.str().c_str(),
00085                          __FILE__,
00086                          __LINE__);
00087   }
00088   return (*itr->second);
00089 }

const cfg::Param & cfg::Config::operator() const char *  pname  )  const
 

Definition at line 56 of file Config.cxx.

References fParam.

00057 {
00058 //======================================================================
00059 // Allows read-only access to parameters
00060 //======================================================================
00061   cfg::Config::ParamMap::iterator itr = fParam.find(pname);
00062   if (itr==fParam.end()) {
00063     throw cfg::Exception(cfg::Exception::kParamNotFound,
00064                          pname, 
00065                          __FILE__,
00066                          __LINE__);
00067   }
00068   return (*itr->second);
00069 }

cfg::Param & cfg::Config::Par const std::string &  pname  ) 
 

Definition at line 115 of file Config.cxx.

References EditOK(), fName, fParam, and fVersion.

00116 {
00117 //======================================================================
00118 // Allows non-const access to parameters
00119 //======================================================================
00120   this->EditOK();
00121   cfg::Config::ParamMap::iterator itr = fParam.find(pname);
00122   if (itr==fParam.end()) {
00123     std::ostringstream os;
00124     os << "Parameter " << pname    << " not found in configuration " 
00125        << fName << ":" << fVersion << std::endl;
00126     throw cfg::Exception(cfg::Exception::kParamNotFound,
00127                          os.str().c_str(),
00128                          __FILE__,
00129                          __LINE__);
00130   }
00131   return (*itr->second);
00132 }

cfg::Param & cfg::Config::Par const char *  pname  ) 
 

Definition at line 93 of file Config.cxx.

References EditOK(), fName, fParam, and fVersion.

Referenced by cfg::configBuilder::Build(), and TestEditor::EditConfig().

00094 {
00095 //======================================================================
00096 // Allows non-const access to parameters
00097 //======================================================================
00098   this->EditOK();
00099   std::string p(pname);
00100   cfg::Config::ParamMap::iterator itr = fParam.find(p);
00101   if (itr==fParam.end()) {
00102     std::ostringstream os;
00103     os << "Parameter " << pname    << " not found in configuration " 
00104        << fName << ":" << fVersion << std::endl;
00105     throw cfg::Exception(cfg::Exception::kParamNotFound,
00106                          os.str().c_str(),
00107                          __FILE__,
00108                          __LINE__);
00109   }
00110   return (*itr->second);
00111 }

ParamMap& cfg::Config::ParMap  )  [inline]
 

Definition at line 63 of file Config.h.

References fParam, and ParamMap.

Referenced by evdb::ParamFrame::ParamFrame().

00063 { return fParam; }

void cfg::Config::RemoveParam const char *  which = "*"  ) 
 

Definition at line 217 of file Config.cxx.

References EditOK(), and fParam.

Referenced by Copy(), and ~Config().

00218 {
00219 //======================================================================
00220 // Remove a selected parameter from the configuration. "*" removes all
00221 // parameters
00222 //======================================================================
00223   this->EditOK();
00224 
00225   std::string s(which);
00226   // If which is "*" remove all parameters
00227   if (s=="*") {
00228     cfg::Config::ParamMap::iterator    itr(fParam.begin());
00229     cfg::Config::ParamMap::iterator itrEnd(fParam.end());
00230     for (; itr!=itrEnd; ++itr) {
00231       if (itr->second) { delete itr->second; itr->second = 0; }
00232     }
00233     return;
00234   }
00235   
00236   // Otherwise just remove the named set
00237   cfg::Param* param = fParam[s];
00238   if (param==0) { delete param; fParam[s] = 0; }
00239 }

void cfg::Config::SetEditAllOK  )  [static]
 

Definition at line 21 of file Config.cxx.

References gsAllEditOK.

Referenced by evdb::JobMenu::OpenJobXML().

00021 { gsAllEditOK = true; }

void cfg::Config::SetReadOnly int  flag = 1  ) 
 

Definition at line 157 of file Config.cxx.

References EditOK(), fEditable, fVersion, gsNoDefaultEdits, and gsNoEdits.

Referenced by cfg::configBuilder::Build(), and cfg::ConfigBuilder::Publish().

00158 {
00159 //======================================================================
00160 // Set the read-only status for this configuration.
00161 //   flag =  0 - allow read/write access
00162 //   flag =  1 - read only access
00163 //   flag = -1 - determine read/write accees based on R/W policies for
00164 //               configurations
00165 //======================================================================
00166   if (flag == 1 && fEditable == false) return; // RO already
00167   
00168   // First make sure we are allowed to change the state. Once set read
00169   // only the configuration is always read only
00170   this->EditOK();
00171 
00172   if (flag ==  0) { fEditable = true;  return; } // RW
00173   if (flag ==  1) { fEditable = false; return; } // RO
00174 
00175   // Apply policy to determine RW status
00176   
00177   // Edits turned off globally
00178   if (gsNoEdits == true) { fEditable = false; return; } // RO
00179   
00180   // Edits turned off globally for default configurations
00181   if ( (gsNoDefaultEdits == true) && (this->fVersion == "default") ) {
00182     fEditable = false; // RO
00183     return;
00184   }
00185   
00186   // Edit allowed
00187   fEditable = true; // RW
00188 }

void cfg::Config::SetSource const char *  filename  ) 
 

Definition at line 192 of file Config.cxx.

References fSource.

00192 { fSource = filename; }


Member Data Documentation

bool cfg::Config::fEditable [private]
 

Allow edits to this config?

Definition at line 66 of file Config.h.

Referenced by EditOK(), IsEditable(), SetReadOnly(), and ~Config().

std::string cfg::Config::fName [private]
 

Name of configuration.

Definition at line 67 of file Config.h.

Referenced by Copy(), GetName(), operator()(), and Par().

ParamMap cfg::Config::fParam [mutable, private]
 

Collection of parameters.

Definition at line 70 of file Config.h.

Referenced by AdoptParam(), Copy(), operator()(), Par(), ParMap(), and RemoveParam().

std::string cfg::Config::fSource [private]
 

Source of config. data (file name eg.).

Definition at line 69 of file Config.h.

Referenced by Copy(), GetSource(), and SetSource().

std::string cfg::Config::fVersion [private]
 

Version tag of configuration.

Definition at line 68 of file Config.h.

Referenced by Copy(), GetVersion(), operator()(), Par(), and SetReadOnly().


The documentation for this class was generated from the following files:
Generated on Fri Jul 25 02:05:52 2008 for NOvA Offline by doxygen 1.3.5