VManager.h
Go to the documentation of this file.
1 #ifndef _det_VManager_h_
2 #define _det_VManager_h_
3 
4 #include <string>
5 #include <set>
6 #include <vector>
7 #include <list>
8 #include <map>
9 #include <boost/lexical_cast.hpp>
10 #include <utl/TabulatedFunction.h>
11 #include <utl/TabulatedFunctionComplexLgAmpPhase.h>
12 #include <utl/ErrorLogger.h>
13 #include <utl/AugerException.h>
14 #include <utl/Reader.h>
15 #include <utl/TypeId.h>
16 
17 
18 /* Use this macro for derived-manager GetData() methods that should never
19  be called. They will instantly issue an error message */
20 
21 #define VMANAGER_GETDATA_DENIED(_T_...) \
22  virtual \
23  Status \
24  GetData(_T_&, \
25  const std::string& componentProperty, \
26  const std::string& componentName, \
27  const VManager::IndexMap&) \
28  const \
29  { \
30  ERROR(NotImplementedMessage(#_T_, componentProperty, componentName)); \
31  return eNotFound; \
32  }
33 
34 
35 /* Use this macro for derived-manager GetData() methods that do not support
36  the specific parameter, but it is OK to check the rest of the managers in
37  the ManagerRegister chain. They will issue an error message in the
38  second run, when none of the other managers finds data, and the request
39  is repeated with IsReportingErrors() switched on. */
40 
41 #define VMANAGER_GETDATA_NOTFOUND(_T_...) \
42  virtual \
43  Status \
44  GetData(_T_&, \
45  const std::string& componentProperty, \
46  const std::string& componentName, \
47  const VManager::IndexMap&) \
48  const \
49  { \
50  if (IsReportingErrors()) \
51  ERROR(NotImplementedMessage(#_T_, componentProperty, componentName)); \
52  return eNotFound; \
53  }
54 
55 
56 /* Use this macro if you want to reroute call to some other method (usually a
57  template */
58 
59 #define VMANAGER_GETDATA_CALL(_GetData_, _T_...) \
60  Status GetData(_T_& returnData, \
61  const std::string& componentProperty, \
62  const std::string& componentName, \
63  const VManager::IndexMap& componentIndex) const \
64  { return _GetData_(returnData, componentProperty, componentName, componentIndex); }
65 
66 
67 /* Use this macro to declare the overriding method in derived managers. */
68 
69 #define VMANAGER_GETDATA_DECLARE(_T_...) \
70  Status GetData(_T_& returnData, \
71  const std::string& componentProperty, \
72  const std::string& componentName, \
73  const VManager::IndexMap& componentIndex) const;
74 
75 #define VMANAGER_GETDATA_HANDLE_DENIED \
76  virtual \
77  Status \
78  GenericGetData(VManager::Handle& returnData, \
79  const std::string& component, \
80  const std::string& property, \
81  const VManager::IndexMap&) \
82  const \
83  { \
84  ERROR(NotImplementedMessage(returnData.GetTypeIdName(), component, property)); \
85  return eNotFound; \
86  }
87 
88 
89 namespace det {
90 
115  class VManager {
116  public:
117  VManager() { }
118 
119  VManager(const std::string& name) : fName(name) { }
120 
121  virtual ~VManager() { }
122 
124 
127  enum Status {
130  };
131 
132  // component index map
133  typedef std::map<std::string, std::string> IndexMap;
134 
138  class Handle {
139  public:
140  template<typename T>
141  explicit Handle(T& t) : fData(&t), fTypeIdMangledName(utl::TypeId::MangledName<T>()) { }
142 
143  // this could be all compiler generated but is explicitly implemented in order to show intent
144  ~Handle() { }
145  Handle(const Handle& h) : fData(h.fData), fTypeIdMangledName(h.fTypeIdMangledName) { }
147  { fData = h.fData; fTypeIdMangledName = h.fTypeIdMangledName; return *this; }
148 
149  template<typename T>
150  typename boost::add_reference<typename boost::remove_const<T>::type>::type Get()
151  { Check<T>(); return *static_cast<typename boost::add_pointer<typename boost::remove_const<T>::type>::type>(fData); }
152 
153  template<typename T>
154  typename boost::add_reference<typename boost::add_const<T>::type>::type Get() const
155  { Check<T>(); return *static_cast<typename boost::add_pointer<typename boost::add_const<T>::type>::type>(fData); }
156 
157  std::string GetTypeIdName() const { return utl::TypeId::Demangle(fTypeIdMangledName); }
158 
159  const std::string& GetTypeIdMangledName() const { return fTypeIdMangledName; }
160 
161  template<typename T>
162  bool Is() const { return fTypeIdMangledName == utl::TypeId::MangledName<T>(); }
163 
164  private:
165  template<typename T>
166  void
167  Check()
168  const
169  {
170  if (!Is<T>()) {
171  std::ostringstream os;
172  os << "cannot Handle(" << GetTypeIdName() << ").Get<" << utl::TypeId::Name<T>() << ">()";
173  ERROR(os);
174  throw utl::DoesNotComputeException(os.str());
175  }
176  }
177 
178  void* fData = nullptr;
179  std::string fTypeIdMangledName;
180  };
181 
183 
212  virtual void Init(const std::string& configLink);
213 
214  bool IsInitialized() const { return fIsInitialized; }
215 
217 
231  void SetReportingErrors(const bool flag = true) { fReportingErrors = flag; }
232 
233 #define VMANAGER_ABSTRACT_GETDATA(_T_...) \
234  virtual Status GetData(_T_& returnData, \
235  const std::string& componentProperty, \
236  const std::string& componentName, \
237  const IndexMap& componentIndex) const = 0
238 
242  VMANAGER_ABSTRACT_GETDATA(std::string);
243  VMANAGER_ABSTRACT_GETDATA(std::vector<double>);
244  VMANAGER_ABSTRACT_GETDATA(std::vector<int>);
245  VMANAGER_ABSTRACT_GETDATA(std::vector<std::string>);
246  VMANAGER_ABSTRACT_GETDATA(std::vector<bool>);
247  VMANAGER_ABSTRACT_GETDATA(std::list<double>);
248  VMANAGER_ABSTRACT_GETDATA(std::list<int>);
249  VMANAGER_ABSTRACT_GETDATA(std::list<std::string>);
250  VMANAGER_ABSTRACT_GETDATA(std::list<std::pair<int, int>>);
252  VMANAGER_ABSTRACT_GETDATA(std::map<int, utl::TabulatedFunction>);
254  VMANAGER_ABSTRACT_GETDATA(std::map<std::string, double>);
255  VMANAGER_GETDATA_DENIED(std::vector<std::vector<int>>);
256  VMANAGER_GETDATA_DENIED(std::map<unsigned int, int>);
257 
258 #undef VMANAGER_ABSTRACT_GETDATA
259 
261 
274  Status
275  GetData(Handle& returnData,
276  const std::string& component,
277  const std::string& property,
278  const IndexMap& index = IndexMap())
279  const
280  {
281  return CheckedGetData(returnData, component, property, index);
282  }
283 
284  static std::string QueryInfoMessage(const Handle& returnData,
285  const std::string& component);
286 
287  static std::string QueryInfoMessage(const Handle& returnData,
288  const std::string& component,
289  const std::string& property,
290  const IndexMap& index);
291 
292  static
293  std::string
294  QueryInfoMessage(const std::string& componentProperty,
295  const std::string& componentName)
296  {
297  return std::string("('") + componentProperty + "', '" + componentName + "')";
298  }
299 
300  static std::string QueryInfoMessage(const std::string& componentProperty,
301  const std::string& componentName,
302  const IndexMap& componentIndex);
303 
304  template<typename T>
305  static
306  T
307  FindComponent(const std::string& componentName, const IndexMap& componentIndex)
308  {
309  const IndexMap::const_iterator it = componentIndex.find(componentName);
310  if (it == componentIndex.end()) {
311  std::ostringstream err;
312  err << "component " << componentName << " not found!";
313  ERROR(err);
314  throw utl::NonExistentComponentException(err.str());
315  }
316  return boost::lexical_cast<T>(it->second);
317  }
318 
319  template<typename T, typename U>
320  static
321  T
322  FindComponent(const std::string& componentName, const U& defaultValue,
323  const IndexMap& componentIndex)
324  {
325  const IndexMap::const_iterator it = componentIndex.find(componentName);
326  return (it == componentIndex.end()) ?
327  boost::lexical_cast<T>(defaultValue) : boost::lexical_cast<T>(it->second);
328  }
329 
330  const std::string& GetName() const { return fName; }
331 
332  void SetName(const std::string& name) { fName = name; }
333 
334  protected:
336  void FindConfig(const std::string& configLink);
337 
338  bool IsReportingErrors() const { return fReportingErrors; }
339 
340  std::string
341  NotImplementedMessage(const std::string& type,
342  const std::string& componentProperty,
343  const std::string& componentName) const;
344 
345  template<typename T>
346  void
347  AddAvailability(const std::string& component)
348  {
349  AddAvailability(utl::TypeId::Name<T>(), component);
350  }
351 
352  // Flag the Manager as initialized. (It is initialized if
353  // it successfully found its data source.)
354  mutable bool fIsInitialized = false;
355 
356  bool fReportingErrors = false;
357 
358  // Configuration file reader
359  // do not delete in dtor
361 
362  std::set<std::string> fAvailableComponents;
363 
364  private:
365  void AddAvailability(const std::string& typeIdName, const std::string& component)
366  { fAvailability[typeIdName].insert(component); }
367 
368  bool CanAnswer(const Handle& returnData, const std::string& component) const;
369 
370  Status
371  CheckedGetData(Handle& returnData,
372  const std::string& component,
373  const std::string& property,
374  const IndexMap& index)
375  const
376  {
377  if (CanAnswer(returnData, component)) {
378  const Status status =
379  GenericGetData(returnData, component, property, index);
380  if (status == eFound)
381  return eFound;
382  else if (IsReportingErrors())
383  WARNING(std::string("Manager ") + GetName() + " did not find " +
384  QueryInfoMessage(returnData, component));
385  }
386  return eNotFound;
387  }
388 
404  virtual
405  Status
406  GenericGetData(Handle& returnData,
407  const std::string& component,
408  const std::string& property,
409  const IndexMap& index) const = 0;
410 
411  std::string fName;
412 
413  typedef std::set<std::string> ComponentSet;
414  typedef std::map<std::string, ComponentSet> AvailabilityMap;
416 
417  };
418 
419 }
420 
421 
422 #endif
void SetName(const std::string &name)
Definition: VManager.h:332
void FindConfig(const std::string &configLink)
Locate the configuration file in CentralConfig and make a Reader for it.
Definition: VManager.cc:25
#define VMANAGER_ABSTRACT_GETDATA(_T_...)
Definition: VManager.h:233
std::string fTypeIdMangledName
Definition: VManager.h:179
Class to hold collection (x,y) points and provide interpolation between them.
Handle & operator=(const Handle &h)
Definition: VManager.h:146
bool Is() const
Definition: VManager.h:162
string Demangle(const string &mangledName)
Definition: TypeId.cc:16
Base class for exceptions trying to access non-existing components.
bool IsInitialized() const
Definition: VManager.h:214
Interface for detector managers.
Definition: VManager.h:115
void AddAvailability(const std::string &component)
Definition: VManager.h:347
#define U
virtual void Init(const std::string &configLink)
Manager Initialization. configLink is the CentralConfig hook for the configuration file...
Definition: VManager.cc:16
bool fReportingErrors
Definition: VManager.h:356
std::set< std::string > fAvailableComponents
Definition: VManager.h:362
void AddAvailability(const std::string &typeIdName, const std::string &component)
Definition: VManager.h:365
Class representing a document branch.
Definition: Branch.h:107
std::string GetTypeIdName() const
Definition: VManager.h:157
Class to hold collection (x,y) points and provide interpolation between them, where y are complex num...
static std::string QueryInfoMessage(const Handle &returnData, const std::string &component)
Definition: VManager.cc:108
void Check(const Iterator &i, const Iterator &e, const int id)
#define VMANAGER_GETDATA_DENIED(_T_...)
Definition: VManager.h:21
VManager(const std::string &name)
Definition: VManager.h:119
const std::string & GetName() const
Definition: VManager.h:330
virtual ~VManager()
Definition: VManager.h:121
Handle(const Handle &h)
Definition: VManager.h:145
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
static T FindComponent(const std::string &componentName, const U &defaultValue, const IndexMap &componentIndex)
Definition: VManager.h:322
Base class for inconsistency/illogicality exceptions.
bool CanAnswer(const Handle &returnData, const std::string &component) const
Definition: VManager.cc:78
std::set< std::string > ComponentSet
Definition: VManager.h:413
boost::add_reference< typename boost::add_const< T >::type >::type Get() const
Definition: VManager.h:154
std::string fName
Definition: VManager.h:411
std::string NotImplementedMessage(const std::string &type, const std::string &componentProperty, const std::string &componentName) const
Definition: VManager.cc:47
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
const std::string & GetTypeIdMangledName() const
Definition: VManager.h:159
Status GetData(Handle &returnData, const std::string &component, const std::string &property, const IndexMap &index=IndexMap()) const
new data accessor
Definition: VManager.h:275
bool IsReportingErrors() const
Definition: VManager.h:338
utl::Branch fBranch
Definition: VManager.h:360
AvailabilityMap fAvailability
Definition: VManager.h:415
boost::add_reference< typename boost::remove_const< T >::type >::type Get()
Definition: VManager.h:150
std::string MangledName()
Definition: TypeId.h:44
void SetReportingErrors(const bool flag=true)
Generic data accessors.
Definition: VManager.h:231
std::map< std::string, ComponentSet > AvailabilityMap
Definition: VManager.h:414
static std::string QueryInfoMessage(const std::string &componentProperty, const std::string &componentName)
Definition: VManager.h:294
static T FindComponent(const std::string &componentName, const IndexMap &componentIndex)
Definition: VManager.h:307
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Status CheckedGetData(Handle &returnData, const std::string &component, const std::string &property, const IndexMap &index) const
Definition: VManager.h:371
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127
bool fIsInitialized
Definition: VManager.h:354
virtual Status GenericGetData(Handle &returnData, const std::string &component, const std::string &property, const IndexMap &index) const =0

, generated on Tue Sep 26 2023.