DetectorComponent.h
Go to the documentation of this file.
1 #ifndef _det_DetectorComponent_h_
2 #define _det_DetectorComponent_h_
3 
4 #include <det/VManager.h>
5 #include <det/Detector.h>
6 
7 #include <utl/ErrorLogger.h>
8 #include <utl/AugerException.h>
9 #include <utl/Validated.h>
10 #include <utl/VValidated.h>
11 #include <utl/ShadowPtr.h>
12 
13 #include <sstream>
14 #include <vector>
15 
16 #include <boost/utility.hpp>
17 
18 
19 namespace det {
20 
21  class Detector;
22 
23 
44  template<class C>
51  DetectorUserData(const std::string& /* mngrData */)
52  { WARNING("Call to unspecialized DetectorUserData::DetectorUserData(const std::string&)."); }
53  };
54 
55 
82  template<class C, class ManagerProvider>
83  class DetectorComponent : boost::noncopyable {
84  // Aliases for container of managed fields.
99  typedef std::vector<utl::VValidated*> FieldsContainer;
100 
101  public:
105  int GetId() const { return fId; }
106 
111  const VManager::IndexMap& GetIdsMap() const { return fIdsMap; }
112 
122  std::string
123  GetIdMessage()
124  const
125  {
126  std::ostringstream o;
127  AddIdMessage(o);
128  return o.str();
129  }
130 
134  const DetectorUserData<C>&
135  GetUserData()
136  const
137  {
138  if (!fUserData) {
139  /*
140  * XXX The user data the information has to
141  * be stored as string in the managers:
142  * there's no much point in making the managers and
143  * branches to know the structure of the user data
144  * because the idea is precisely to have something
145  * customized with an unknown structure (until the
146  * user provided an specization). Of course, some
147  * refactoring could be put in the manager or branch
148  * struture so as to call a specialized-user-data aware
149  * method that could perform the loading given a branch.
150  * By now, no changes in there and take the data to be
151  * a string: which in the end is not something to bad
152  * because at last what is read from the XML (and in the
153  * end the only difference could be if the datum is
154  * structured in several deeper branches).
155  */
156  // Temporal variable though which the data is loaded from the
157  // managers.
158  std::string dataMngr;
159  // Returns the same object (or throws).
160  GetDataNoWrap(dataMngr, "userData");
161  // Once read, create the actual type datum, ...
162  DetectorUserData<C>* const t = new DetectorUserData<C>();
163  // call the static method that loads the data
164  // (which can be "overrided" in a particular specialization) ...
165  DetectorUserData<C>::Load(t, dataMngr);
166  // and then make the field store the datum.
167  fUserData = t;
168  }
169  return *fUserData;
170  }
171 
172  protected:
190  template<typename T, template<typename> class P>
191  T& GetData(P<T>& d, const std::string& p) const { return GetDataUnwrap<T>(d, p); }
192 
199  template<typename T1, typename T2, template<typename,typename> class P>
200  T1&
201  GetData(P<T1, T2>& d, const std::string& p)
202  const
203  {
204  // In utl::ShadowPtr the 1st the is still the datum data-type, the 2nd
205  // is the policy.
206  return GetDataUnwrap<T1>(d, p);
207  }
208 
212  template<typename T>
213  T&
214  GetData(utl::Validated<T>& d, const std::string& p)
215  const
216  {
217  if (! d.IsValid()) {
218  GetDataNoWrap(d.Get(), p);
219  d.SetValid();
220  }
221  return d.Get();
222  }
223 
231  template<typename T>
232  T& GetData(T& d, const std::string& p) const { return GetDataNoWrap(d, p); }
233 
247  void
248  AddIdMessage(std::ostringstream& s)
249  const
250  {
251  typedef det::VManager::IndexMap::const_iterator It;
252  // Put the self id...
253  s << "id=" << GetId() << " [";
254  // and then the id map.
255  // TODO There's no definite order.
256  for (It i = GetIdsMap().begin(), e = GetIdsMap().end(); i != e; ++i)
257  s << " " << i->first << "=" << i->second;
258  s << " ]";
259  }
260 
268  DetectorComponent(int i, const VManager::IndexMap& parentMap)
269  : fId(i), fIdsMap(parentMap) { Init(); }
270 
276  DetectorComponent(const int i) : fId(i) { Init(); }
277 
278  virtual ~DetectorComponent() { }
279 
286  virtual
287  void
288  Update(const bool invalidateData, const bool /*invalidateComponents*/)
289  {
290  if (invalidateData)
291  for (FieldsContainer::iterator i = fFields.begin(), e = fFields.end(); i != e; ++i)
292  (*i)->SetValid(false);
293  }
294 
298  void Register(utl::VValidated& v) { fFields.push_back(&v); }
299 
300  private:
311  template<typename T>
312  T&
313  GetDataNoWrap(T& d, const std::string& p)
314  const
315  {
316  // A datum needs to be retrieved.
317  const VManager& mngr = ManagerProvider::GetInstance();
318  // Pass a reference to the datum.
319  const VManager::Status f = mngr.GetData(d, p, C::kComponentName, fIdsMap);
321  std::ostringstream err;
322  err << "Did not find the requested component: '"
323  << p << "' in " << GetIdMessage() << ".";
324  ERROR(err);
325  throw utl::NonExistentComponentException(err.str());
326  }
327  //DEBUGLOG(BuildLogMessage(d, p, std::string(C::kComponentName)/*, fIdsMap*/));
328  return d;
329  }
330 
337  template<typename R, typename T>
338  R&
339  GetDataUnwrap(T& d , const std::string& p)
340  const
341  {
342  if (!d) {
343  // Heap allocate the datum ...
344  R* const t = new R();
345  // and pass the pointer to d in order to get it handled by it.
346  d = t;
347  // Call the non-wrapped version, with a reference (thanks to *)
348  // to the datum.
349  return GetDataNoWrap(*t, p);
350  }
351  return *d;
352  }
353 
357  template<typename T>
358  //static
359  std::string
360  BuildLogMessage(const T& d, const std::string& p, const std::string& c/*, const VManager::IndexMap& m*/)
361  {
362  std::ostringstream s;
363  s << c << ", " << p << " in " << GetIdMessage() << " with data= " << d;
364  return s.str();
365  }
366 
370  template<typename T>
371  //static
372  std::string
373  BuildLogMessage(const std::list<T>& d, const std::string& p, const std::string& c/*, const VManager::IndexMap& m*/)
374  {
375  // Fake datum, it's an string indicating the list list values (don't want to overload << for lists).
376  std::ostringstream s;
377  s << '{';
378  for (typename std::list<T>::const_iterator i = d.begin(); i != d.end(); ++i)
379  s << (*i) << ' '; // Not any delimiters between positions.
380  s << '}';
381  return BuildLogMessage(s.str(), p, c/*, m*/);
382  }
383 
391  void
393  {
394  // Add this component's id to map that
395  // comes as a copy from this component's
396  // parent
397  std::ostringstream idStr;
398  idStr << GetId();
399  // Add to the map, using the template argument
400  // to obtain the key.
401  fIdsMap[C::kComponentId] = idStr.str();
402  }
403 
405  int fId;
406 
415 
420 
430 };
431 
432 }
433 
434 #endif
pointer with built-in initialization, deletion, deep copying
Definition: ShadowPtr.h:163
int fId
Id of the component.
utl::ShadowPtr< DetectorUserData< C > > fUserData
User customized detector component level data.
std::string BuildLogMessage(const std::list< T > &d, const std::string &p, const std::string &c)
Overload for list: don&#39;t show the list.
Minimum validation handling methods.
Definition: VValidated.h:22
void Update(std::vector< double > &init, const std::vector< double > &res)
Definition: Util.h:100
Base class for exceptions trying to access non-existing components.
Interface for detector managers.
Definition: VManager.h:115
const VManager::IndexMap & GetIdsMap() const
The id identifying this component within its detector hierarhy.
void Register(utl::VValidated &v)
Register the field so as to allow handling it.
T & GetData(P< T > &d, const std::string &p) const
Common utility function for configuration.
Wrapper class for initially unset data.
Definition: ResponseMap.h:17
virtual Status GetData(double &returnData, const std::string &componentProperty, const std::string &componentName, const IndexMap &componentIndex) const =0
constexpr double s
Definition: AugerUnits.h:163
bool IsValid() const
Definition: Validated.h:64
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
DetectorUserData(const std::string &)
Construct the user data according to an string read from configuration.
std::vector< utl::VValidated * > FieldsContainer
Container for those fields (from the deriving classes) meant to be managed by this object...
void Init()
Common object initialization method.
Base class for detector components.
VManager::IndexMap fIdsMap
Identifies this component within its hierarchy.
const VManager::Status f
T & GetData(T &d, const std::string &p) const
Common utility function for configuration.
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
void SetValid(const bool valid=true)
Definition: Validated.h:66
User custom data base structure.
int GetId() const
The id of this component.
std::string BuildLogMessage(const T &d, const std::string &p, const std::string &c)
All the requested info in a line.
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127
FieldsContainer fFields
Keep pointers to the detector-date fields of the particular (deriving) instance.

, generated on Tue Sep 26 2023.