ManagerRegister.h
Go to the documentation of this file.
1 #ifndef _sdet_ManagerRegister_h_
2 #define _sdet_ManagerRegister_h_
3 
4 #include <list>
5 #include <sstream>
6 #include <string>
7 #include <map>
8 #include <boost/iterator/indirect_iterator.hpp>
9 #include <det/VManager.h>
10 #include <utl/ErrorLogger.h>
11 
12 
13 namespace utl {
14  class Reader;
15  class Branch;
16 }
17 
18 namespace det {
19 
20  class VManagerBuilder;
21 
22 
43  class ManagerRegister : public det::VManager {
44 
45  private:
46  typedef std::vector<VManager*> ManagerCollection;
47  typedef ManagerCollection::const_iterator InternalManagerIterator;
48 
49  public:
50  ManagerRegister(const std::string& name)
51  : VManager(name) { }
52 
53  virtual ~ManagerRegister() { }
54 
55  // The individual managers are automatically initialized during configuration
56  virtual void Init(const std::string&) { }
57 
58  // NOTE ON CONFIGURE METHODS: These methods were introduced in anticipation
59  // of providing a way to reconfigure the detector managers on-the-fly. It
60  // has not yet (as of 22/6/03) been decided to what extent this reconfiguration
61  // cabability will be provided. We may allow reconfiguration at any time,
62  // only during construction of the Detector, or never -- depending on which
63  // of these 3 approches is chosen to be be the best, the Configure methods
64  // below may be modified accordingly. eg. if only configured upon detector
65  // construction, should the Configure methods be public (if so, users could
66  // reconfigure the detector) ? It may be okay to
67  // leave them as public since users are not supposed to interact directly
68  // with the ManagerRegister anyway.
69 
71 
81  void Configure(const utl::Branch branch);
82 
84 
90  void Configure(const std::ostringstream& configInstructions);
91 
93  typedef boost::indirect_iterator<InternalManagerIterator,
95 
98  { return ManagerIterator(fConfiguredManagerList.begin()); }
99 
102  { return ManagerIterator(fConfiguredManagerList.end()); }
103 
104  /*
105  * RFG 20091007 Originally this method was non-const, tough
106  * it should be (and can be), so we made it const; the real reason
107  * for this is to be able to introduce a non-const version (on the
108  * return type also, of course).
109  */
111  const VManager& GetManager(const std::string& managerName) const;
112 
114  /*
115  RFG 20091007
116 
117  Caveat Emptor: Since the VManager is returning non-const, take care
118  to use it properly. For instance, it may not be possible (in the
119  sense right behaviour) to modify the name of the VManager (incidentally
120  it is right because the look-up by name is peformed here in a linear search
121  over the managers themselves, instead of performing a look-up on an
122  associative structure -like std::map - having a copy of the (original)
123  name as key).
124 
125  At last, let's state this: non-const methods from VManager itself
126  shouldn't be called, and instead call (though casting) whatever method
127  a particular manager provides.
128 
129  This ended being used in MdOverrideManagerAG::MdOverrideManager, so as to
130  be able to call its det::XXMLManager::NextConfiguration. Of course,
131  the field modified in that method could be made mutable, but I'd that's
132  cheating because the field is no cache-like or so.
133  */
134  VManager& GetManager(const std::string& managerName);
135 
136  // data getters implementing VManager interface
137 
138 #define MANAGERREGISTER_GETDATA(_T_...) \
139  Status GetData(_T_& returnData, \
140  const std::string& componentProperty, \
141  const std::string& componentName, \
142  const IndexMap& componentIndex) const \
143  { return GetManagerData(returnData, componentProperty, componentName, componentIndex); }
144 
147  //MANAGERREGISTER_GETDATA(unsigned long long int)
149  MANAGERREGISTER_GETDATA(std::vector<double>)
150  MANAGERREGISTER_GETDATA(std::vector<int>)
151  MANAGERREGISTER_GETDATA(std::vector<std::string>)
152  MANAGERREGISTER_GETDATA(std::vector<bool>)
153  MANAGERREGISTER_GETDATA(std::list<double>)
154  MANAGERREGISTER_GETDATA(std::list<int>)
155  MANAGERREGISTER_GETDATA(std::list<std::string>)
156  MANAGERREGISTER_GETDATA(utl::TabulatedFunction)
157  MANAGERREGISTER_GETDATA(utl::TabulatedFunctionComplexLgAmpPhase)
158  MANAGERREGISTER_GETDATA(std::list<std::pair<int, int> >)
159  MANAGERREGISTER_GETDATA(std::map<int, utl::TabulatedFunction>)
160  MANAGERREGISTER_GETDATA(std::vector<std::vector<int> >)
161  MANAGERREGISTER_GETDATA(std::map<std::string, double>)
162 #undef MANAGERREGISTER_GETDATA
163 
164  template<typename T>
165  Status
166  GetData(T& returnData,
167  const std::string& component,
168  const std::string& property,
169  const IndexMap& componentIndex = IndexMap())
170  const
171  {
172  Handle handle(returnData);
173  return GenericGetData(handle, component, property, componentIndex);
174  }
175 
176  template<typename T>
177  Status
178  GetDataOrThrow(T& returnData,
179  const std::string& component,
180  const std::string& property,
181  const IndexMap& index = IndexMap())
182  const
183  {
184  Handle handle(returnData);
185  const Status status = GenericGetData(handle, component, property, index);
186  if (status == VManager::eNotFound)
187  NotFoundAndThrow(handle, component, property, index);
188  return status;
189  }
190 
191  private:
192  void RegisterBuilder(const std::string& name, VManagerBuilder& builder);
193 
194  typedef std::map<std::string, VManagerBuilder*> ManagerBuilderMap;
196 
198 
199  void InterpretXML(const utl::Branch& topBranch);
200 
201  std::string GetConfiguredManagerListString() const;
202 
203  template<typename T>
204  Status
205  GetManagerData(T& returnData,
206  const std::string& componentProperty,
207  const std::string& componentName,
208  const IndexMap& componentIndex)
209  const
210  {
211  bool seenOne = false;
212  for (ManagerIterator it = ManagersBegin(); it != ManagersEnd(); ++it) {
213  seenOne = true;
214  const Status status =
215  it->GetData(returnData, componentProperty, componentName, componentIndex);
216  if (status == eFound)
217  return eFound;
218  }
219  // try again with reporting errors
220  if (FindComponent<bool>("reportErrors", true, componentIndex)) {
221  if (seenOne) {
222  std::ostringstream warn;
223  warn << "None of the managers (" << GetConfiguredManagerListString()
224  << ") in " << GetName() << " found "
225  << QueryInfoMessage(componentProperty, componentName, componentIndex)
226  << " data. Trying to figure out the reason why...";
227  WARNING(warn);
228  for (ManagerIterator it = ManagersBegin(); it != ManagersEnd(); ++it) {
229  it->SetReportingErrors(true);
230  it->GetData(returnData, componentProperty, componentName, componentIndex);
231  it->SetReportingErrors(false);
232  }
233  } else
234  WARNING(std::string("No managers in ") + GetName() + " to get " +
235  QueryInfoMessage(componentProperty, componentName) + " data.");
236  }
237 
238  return eNotFound;
239  }
240 
241  virtual
242  Status
243  GenericGetData(Handle& returnData,
244  const std::string& component,
245  const std::string& property,
246  const IndexMap& componentIndex)
247  const
248  {
249  bool seenOne = false;
250  for (ManagerIterator it = ManagersBegin(); it != ManagersEnd(); ++it) {
251  seenOne = true;
252  const Status status =
253  it->GetData(returnData, component, property, componentIndex);
254  if (status == eFound)
255  return eFound;
256  }
257  // try again with reporting errors
258  if (FindComponent<bool>("reportErrors", true, componentIndex)) {
259  if (seenOne) {
260  std::ostringstream warn;
261  warn << "None of the managers (" << GetConfiguredManagerListString()
262  << ") in " << GetName() << " found "
263  << QueryInfoMessage(returnData, component, property, componentIndex)
264  << " data. Trying to figure out the reason why...";
265  WARNING(warn);
266  for (ManagerIterator it = ManagersBegin(); it != ManagersEnd(); ++it) {
267  it->SetReportingErrors(true);
268  it->GetData(returnData, component, property, componentIndex);
269  it->SetReportingErrors(false);
270  }
271  } else
272  WARNING(std::string("No managers in ") + GetName() + " to get " +
273  QueryInfoMessage(returnData, component, property, componentIndex) +
274  " data.");
275  }
276 
277  return eNotFound;
278  }
279 
280  static void NotFoundAndThrow(const Handle& handle,
281  const std::string& component, const std::string& property,
282  const IndexMap& index);
283 
285 
286  };
287 
288 
289 } // namespace det
290 
291 
292 #endif // det_ManagerRegister_h_
293 
294 // Configure (x)emacs for this file ...
295 // Local Variables:
296 // mode: c++
297 // End:
Status GetManagerData(T &returnData, const std::string &componentProperty, const std::string &componentName, const IndexMap &componentIndex) const
virtual Status GenericGetData(Handle &returnData, const std::string &component, const std::string &property, const IndexMap &componentIndex) const
Status GetData(T &returnData, const std::string &component, const std::string &property, const IndexMap &componentIndex=IndexMap()) const
static void NotFoundAndThrow(const Handle &handle, const std::string &component, const std::string &property, const IndexMap &index)
void Configure(const utl::Branch branch)
Configure the ManagerRegister from a Branch.
Status GetDataOrThrow(T &returnData, const std::string &component, const std::string &property, const IndexMap &index=IndexMap()) const
ManagerIterator ManagersBegin() const
Iterator pointing to first available manager.
ManagerBuilderMap fManagerBuilderRegister
Interface for detector managers.
Definition: VManager.h:115
ManagerCollection fConfiguredManagerList
Class representing a document branch.
Definition: Branch.h:107
boost::indirect_iterator< InternalManagerIterator, VManager & > ManagerIterator
Iterator over configured managers.
void RegisterBuilder(const std::string &name, VManagerBuilder &builder)
static std::string QueryInfoMessage(const Handle &returnData, const std::string &component)
Definition: VManager.cc:108
ManagerCollection::const_iterator InternalManagerIterator
std::map< std::string, VManagerBuilder * > ManagerBuilderMap
#define MANAGERREGISTER_GETDATA(_T_...)
const std::string & GetName() const
Definition: VManager.h:330
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
const VManager & GetManager(const std::string &managerName) const
Get a specific manager by name.
virtual void Init(const std::string &)
Manager Initialization. configLink is the CentralConfig hook for the configuration file...
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
Register for detector description managers.
std::vector< VManager * > ManagerCollection
ManagerRegister(const std::string &name)
ManagerIterator ManagersEnd() const
Iterator pointing one past the last available manager.
std::string GetConfiguredManagerListString() const
void InterpretXML(const utl::Branch &topBranch)
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127

, generated on Tue Sep 26 2023.