Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | 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
std::string AsXML () 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 63 of file Config.h.


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 27 of file Config.cxx.

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

cfg::Config::~Config  ) 
 

Definition at line 38 of file Config.cxx.

References fEditable, and RemoveParam().

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


Member Function Documentation

void cfg::Config::AdoptParam Param p  ) 
 

Definition at line 198 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().

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

std::string cfg::Config::AsXML  )  const
 

Write the configuration as an XML formatted string

Definition at line 291 of file Config.cxx.

References fName, fParam, fVersion, cfg::Param::GetComment(), cfg::Param::GetName(), and cfg::Param::XMLTag().

Referenced by main().

00292 {
00293   // I'm not too excited about the need for "setprecision(8)" below,
00294   // but without it you seem to either lose precision with doubles and
00295   // floats or to gain bogus digits. The number 8 is fairly fine tuned
00296   // which is why I'm not excited -- I'd rather have something more
00297   // robust. Such is life I suppose.
00298   std::ostringstream xml;
00299   xml << "<config name=\"" << this->fName 
00300       << "\" version=\"" << this->fVersion << "\">\n";
00301   ParamMap::const_iterator itr(fParam.begin());
00302   ParamMap::const_iterator itrEnd(fParam.end());
00303   for (; itr!=itrEnd; ++itr) {
00304     const cfg::Param* p = itr->second;
00305     xml << "  <param name=\"" << p->GetName() << "\">\n"
00306         << "    <" << p->XMLTag() << "> "
00307         << std::setprecision(8) << (*p)
00308         << " </" << p->XMLTag() << ">\n";
00309     xml << "    " << p->GetComment() << "\n";
00310     xml << "  </param>\n";
00311   }
00312   xml << "</config>\n";
00313   return xml.str();
00314 }

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

Definition at line 245 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().

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

bool cfg::Config::EditOK  )  const
 

Definition at line 138 of file Config.cxx.

References fEditable, and GetVersion().

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

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

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

Definition at line 42 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(), TestObserver::Update(), and evd::Display3D::Update().

00042 { return fName.c_str(); }

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

Definition at line 50 of file Config.cxx.

References fSource.

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

00050 { return fSource.c_str(); }

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

Definition at line 46 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().

00046 { return fVersion.c_str(); }

bool cfg::Config::IsEditable  )  const
 

Definition at line 54 of file Config.cxx.

00054 { return fEditable; }

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

Definition at line 75 of file Config.cxx.

References fName, fParam, and fVersion.

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

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

Definition at line 58 of file Config.cxx.

References fParam.

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

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

Definition at line 117 of file Config.cxx.

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

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

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

Definition at line 95 of file Config.cxx.

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

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

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

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

Definition at line 64 of file Config.h.

00064 { return fParam; }

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

Definition at line 219 of file Config.cxx.

References EditOK(), and fParam.

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

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

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

Definition at line 23 of file Config.cxx.

References gsAllEditOK.

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

00023 { gsAllEditOK = true; }

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

Definition at line 159 of file Config.cxx.

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

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

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

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

Definition at line 194 of file Config.cxx.

References fSource.

00194 { fSource = filename; }


Member Data Documentation

bool cfg::Config::fEditable [private]
 

Allow edits to this config?

Definition at line 67 of file Config.h.

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

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

Name of configuration.

Definition at line 68 of file Config.h.

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

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

Collection of parameters.

Definition at line 71 of file Config.h.

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

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

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

Definition at line 70 of file Config.h.

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

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

Version tag of configuration.

Definition at line 69 of file Config.h.

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


The documentation for this class was generated from the following files:
Generated on Sat Nov 21 04:45:35 2009 for NOvA Offline by  doxygen 1.3.9.1