CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWConfigurationManager.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWConfigurationManager
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Sun Feb 24 14:42:32 EST 2008
11 //
12 
13 // system include files
14 #include <fstream>
15 #include <iostream>
16 #include <memory>
17 #include <stdexcept>
18 #include "TROOT.h"
19 
20 // user include files
26 
27 //
28 // constants, enums and typedefs
29 //
30 
31 //
32 // static data member definitions
33 //
34 
35 //
36 // constructors and destructor
37 //
39 {
40 }
41 
42 // FWConfigurationManager::FWConfigurationManager(const FWConfigurationManager& rhs)
43 // {
44 // // do actual copying here;
45 // }
46 
48 {
49 }
50 
51 //
52 // assignment operators
53 //
54 // const FWConfigurationManager& FWConfigurationManager::operator=(const FWConfigurationManager& rhs)
55 // {
56 // //An exception safe implementation is
57 // FWConfigurationManager temp(rhs);
58 // swap(rhs);
59 //
60 // return *this;
61 // }
62 
63 //
64 // member functions
65 //
66 void
68 {
69  assert(0!=iConf);
70  m_configurables[iName]=iConf;
71 }
72 
73 //
74 // const member functions
75 //
76 void
78 {
79  assert(0!=iConfig.keyValues());
80  for(FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin(),
81  itEnd = iConfig.keyValues()->end();
82  it != itEnd;
83  ++it) {
84  std::map<std::string,FWConfigurable*>::const_iterator itFound = m_configurables.find(it->first);
85  assert(itFound != m_configurables.end());
86  itFound->second->setFrom(it->second);
87  }
88 }
89 
90 void
92 {
94  for(std::map<std::string,FWConfigurable*>::const_iterator it = m_configurables.begin(),
95  itEnd = m_configurables.end();
96  it != itEnd;
97  ++it) {
98  it->second->addTo(config);
99  oConfig.addKeyValue(it->first, config, true);
100  }
101 }
102 
103 
104 void
106 {
107  try
108  {
109  std::ofstream file(iName.c_str());
110  if(not file) {
111  std::string message("unable to open file %s ", iName.c_str());
112  fflush(stdout);
113  message += iName;
114  throw std::runtime_error(message.c_str());
115  }
116  FWConfiguration top;
117  to(top);
118  fwLog(fwlog::kInfo) << "Writing to file "<< iName.c_str() << "...\n";
119  fflush(stdout);
120 
121  streamTo(file, top, "top");
122  }
123  catch (std::runtime_error &e) { std::cout << e.what() << std::endl; }
124 }
125 
126 void
128 {
129  Int_t error=0;
130  // Int_t value =
131  gROOT->LoadMacro( iName.c_str(), &error );
132  if(0 != error) {
133  std::string message("unable to load macro file ");
134  message += iName;
135  throw std::runtime_error(message.c_str());
136  }
137 
138  const std::string command("(Long_t)(fwConfig() )");
139 
140  error = 0;
141  Long_t lConfig = gROOT->ProcessLineFast(command.c_str(),
142  &error);
143 
144  {
145  //need to unload this macro so that we can load a new configuration
146  // which uses the same function name in the macro
147  Int_t error = 0;
148  gROOT->ProcessLineSync((std::string(".U ")+iName).c_str(), &error);
149  }
150  if(0 != error) {
151  std::string message("unable to properly parse configuration file ");
152  message += iName;
153  throw std::runtime_error(message.c_str());
154  }
155  std::auto_ptr<FWConfiguration> config( reinterpret_cast<FWConfiguration*>(lConfig) );
156 
157  setFrom( *config);
158 }
159 
160 void
161 debug_config_state_machine(const char *where, const std::string &tag, int state)
162 {
163 #ifdef FW_CONFIG_PARSER_DEBUG
164  static char *debug_states[] = {
165  "IN_BEGIN_DOCUMENT",
166  "IN_PUSHED_CONFIG",
167  "IN_POPPED_CONFIG",
168  "IN_BEGIN_STRING",
169  "IN_STORED_STRING"
170  };
171 
172  std::cerr << " " << where << " tag/data " << tag << "in state " << debug_states[state] << std::endl;
173 #endif
174 }
175 
200 {
201  enum STATES {
207  };
208 
209 public:
210  FWXMLConfigParser(istream &f)
211  : SimpleSAXParser(f),
213  m_first(0)
214  {}
215 
218  {
220  int version = 0;
221  for (size_t i = 0, e = attributes.size(); i != e; ++i)
222  {
223  Attribute &attr = attributes[i];
224  if (attr.key == "name")
225  name = attr.value;
226  else if (attr.key == "version")
227  {
228  char *endptr;
229  version = strtol(attr.value.c_str(), &endptr, 10);
230  if (endptr == attr.value.c_str())
231  throw ParserError("Version must be an integer.");
232  }
233  else
234  throw ParserError("Unexpected attribute " + attr.key);
235  }
236  m_configs.push_back(std::make_pair(name, new FWConfiguration(version)));
237  }
238 
239 
243  virtual void startElement(const std::string &tag, Attributes &attributes) override
244  {
245  debug_config_state_machine("start", tag, m_state);
246  if (m_state == IN_BEGIN_DOCUMENT)
247  {
248  if (tag != "config")
249  throw ParserError("Expecting toplevel <config> tag");
250  pushConfig(attributes);
251  m_first.reset(m_configs.back().second);
253  }
254  else if (m_state == IN_PUSHED_CONFIG)
255  {
256  if (tag == "config")
257  pushConfig(attributes);
258  else if (tag == "string")
260  else
261  throw ParserError("Unexpected element " + tag);
262  }
263  else if (m_state == IN_POPPED_CONFIG)
264  {
265  if (tag != "config")
266  throw ParserError("Unexpected element " + tag);
267  pushConfig(attributes);
269  }
270  else
271  throw ParserError("Wrong opening tag found " + tag);
272  }
273 
282  virtual void endElement(const std::string &tag) override
283  {
284  debug_config_state_machine("end", tag, m_state);
286  {
287  if (tag != "config")
288  throw ParserError("Wrong closing tag found " + tag);
289 
290  FWConfiguration *current = m_configs.back().second;
291  std::string key = m_configs.back().first;
292  m_configs.pop_back();
293  if (!m_configs.empty())
294  m_configs.back().second->addKeyValue(key, *current);
296  }
297  else if (m_state == IN_BEGIN_STRING && tag == "string")
298  {
299  m_configs.back().second->addValue("");
301  }
302  else if (m_state == IN_STORED_STRING && tag == "string")
304  else
305  throw ParserError("Wrong closing tag found " + tag);
306  }
307 
313  virtual void data(const std::string &data) override
314  {
315  debug_config_state_machine("data", data, m_state);
316  // We ignore whitespace but complain about any text which is not
317  // in the <string> tag.
318  if (m_state == IN_BEGIN_STRING)
319  {
320  m_configs.back().second->addValue(data);
322  }
323  else if (strspn(data.c_str(), " \t\n") != data.size())
324  throw ParserError("Unexpected text " + data);
325  }
326 
331  {
332  return m_first.get();
333  }
334 
335 private:
336  std::vector<std::pair<std::string, FWConfiguration *> > m_configs;
338  std::auto_ptr<FWConfiguration> m_first;
339  // unsigned int m_currentConfigVersion;
341 };
342 
349 void
351 {
352  std::ifstream f(iName.c_str());
353  if (f.peek() != (int) '<')
354  return readFromOldFile(iName);
355 
356  // Check that the syntax is correct.
357  SimpleSAXParser syntaxTest(f);
358  syntaxTest.parse();
359  f.close();
360 
361  // Read again, this time actually parse.
362  std::ifstream g(iName.c_str());
363  // Actually parse the results.
365  parser.parse();
366  setFrom(*parser.config());
367 }
368 
369 //
370 // static member functions
371 //
int i
Definition: DBlmapReader.cc:9
std::vector< Attribute > Attributes
void to(FWConfiguration &) const
std::auto_ptr< FWConfiguration > m_first
const KeyValues * keyValues() const
std::vector< std::pair< std::string, FWConfiguration * > > m_configs
void streamTo(std::ostream &, const FWConfiguration &, const std::string &name)
void writeToFile(const std::string &) const
void debug_config_state_machine(const char *where, const std::string &tag, int state)
void readFromOldFile(const std::string &) const
void readFromFile(const std::string &) const
FWConfiguration * config(void)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
virtual void startElement(const std::string &tag, Attributes &attributes) override
void pushConfig(Attributes &attributes)
void setFrom(const FWConfiguration &) const
list attributes
Definition: asciidump.py:415
double f[11][100]
virtual void endElement(const std::string &tag) override
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
virtual void data(const std::string &data) override
tuple attr
Definition: asciidump.py:432
#define fwLog(_level_)
Definition: fwLog.h:50
void add(const std::string &iName, FWConfigurable *)
does not take ownership
list key
Definition: combine.py:13
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, FWConfigurable * > m_configurables