Event/ComponentGroup.h
Go to the documentation of this file.
1 #ifndef _evt_ComponentGroup_h_
2 #define _evt_ComponentGroup_h_
3 
4 #include <utl/ErrorLogger.h>
5 #include <utl/AugerException.h>
6 
7 #include <sstream>
8 #include <utility>
9 
10 #include <boost/ptr_container/ptr_map.hpp>
11 #include <boost/iterator/transform_iterator.hpp>
12 
13 
14 namespace evt {
15 
19  template<class Iterator>
20  void
21  Check(const Iterator& i, const Iterator& e, const int id)
22  {
23  if (i == e) {
24  std::ostringstream error;
25  // XXX See Make.
26  error << "Id " << id << " not present.";
27  ERROR(error);
28  throw utl::NonExistentComponentException(error.str());
29  }
30  }
31 
32 
79  template<class Component>
81  private:
85  /*template< class R >
86  struct SecondResolver {
87  typedef R result_type;
88  template<class Pair>
89  R operator()(const Pair& p) const { return *p.second; }
90  };*/
91  template<class RetType, class ArgType>
92  struct SecondResolver {
93  typedef RetType result_type;
94  typedef ArgType argument_type;
95  result_type operator()(const argument_type& p) const { return *p.second; }
96  };
108  typedef boost::ptr_map<int, Component> InternalComponentCollection;
109  // Some More aliases (in const and non-const flavor for some cases)
110  typedef typename InternalComponentCollection::key_type KeyType;
111  typedef typename InternalComponentCollection::mapped_type MappedType;
112  typedef const Component* /* No nested typedef for this...*/ ConstMappedType;
113  typedef typename InternalComponentCollection::mapped_reference MappedReference;
114  typedef typename InternalComponentCollection::const_mapped_reference ConstMappedReference;
115  typedef typename InternalComponentCollection::const_reference ConstReference;
116  typedef typename InternalComponentCollection::reference Reference;
117  typedef typename InternalComponentCollection::const_iterator PairConstIterator;
118  typedef typename InternalComponentCollection::iterator PairIterator;
119  // Alternative typedef SecondResolver<ConstMappedReference> ConstComponentResolver;
120  // Alternative typedef SecondResolver<MappedReference> ComponentResolver;
121  typedef SecondResolver<ConstMappedReference, ConstReference> ConstComponentResolver;
122  typedef SecondResolver<MappedReference, Reference> ComponentResolver;
133  static constexpr const ComponentResolver kResolver = ComponentResolver();
134  public:
139  typedef boost::transform_iterator<ConstComponentResolver, PairConstIterator>
145  typedef boost::transform_iterator<ComponentResolver, PairIterator>
150  const Component&
151  Get(const int id)
152  const
153  {
154  const PairConstIterator i = fComponents.find(id);
156  Check(i, e, id);
157  return *i->second;
158  }
162  Component&
163  Get(const int id)
164  {
165  const PairIterator i = fComponents.find(id);
166  const PairIterator e = fComponents.end();
167  Check(i, e, id);
168  return *i->second;
169  }
173  void
174  Make(/*const*/ int id)
175  {
176  // id-constructor
177  Component* const c = new Component(id);
178  /*
179  * The id needs to be non-const due to the needs
180  * of the object being called.
181  * http://www.boost.org/doc/libs/1_38_0/libs/ptr_container/doc/tutorial.html
182  * "Note that must create the key as an lvalue (due to exception-safety issues)..."
183  */
184  if (!fComponents.insert(id, c).second) {
185  /*
186  * boost::ptr_map_adapter::insert_impl
187  * takes care of destroying if insertion
188  * was not performed at last.
189  * delete c;
190  */
191  std::ostringstream error;
192  // XXX Put some component label: add a typedef to the Component type?
193  error << id << " already present.";
194  WARNING(error);
195  }
196  }
200  bool Has(const int id) const { return fComponents.find(id) != fComponents.end(); }
201 
205  // XXX Why this isn't unsigned (taken from sdet::SEvent::GetNumberOfStations and others)?
206  int GetNumberOf() const { return fComponents.size(); }
207 
209  { return boost::make_transform_iterator(fComponents.begin(), kResolver); }
210 
212  { return boost::make_transform_iterator(fComponents.end(), kResolver); }
213 
215  { return boost::make_transform_iterator(fComponents.begin(), kConstResolver); }
216 
218  { return boost::make_transform_iterator(fComponents.end(), kConstResolver); }
219 
220  private:
222  };
223 
224 }
225 
226 
227 #endif
ComponentIterator Begin()
ComponentConstIterator Begin() const
result_type operator()(const argument_type &p) const
const Component * ConstMappedType
boost::transform_iterator< ConstComponentResolver, PairConstIterator > ComponentConstIterator
Alias for constant iterator over contained components.
boost::transform_iterator< ComponentResolver, PairIterator > ComponentIterator
Alias for non-constant iterator over contained components.
boost::ptr_map< int, Component > InternalComponentCollection
InternalComponentCollection::key_type KeyType
void Make(int id)
Construct by id.
SecondResolver< MappedReference, Reference > ComponentResolver
static constexpr const ConstComponentResolver kConstResolver
Instance of the (stateless) helper class (stored on a class for the containing class) for const compo...
Base class for exceptions trying to access non-existing components.
InternalComponentCollection::mapped_reference MappedReference
bool Has(const int id) const
Query existence.
InternalComponentCollection::const_iterator PairConstIterator
int GetNumberOf() const
Query quantity.
InternalComponentCollection::reference Reference
const PairConstIterator e
SecondResolver< ConstMappedReference, ConstReference > ConstComponentResolver
void Check(const Iterator &i, const Iterator &e, const int id)
InternalComponentCollection::mapped_type MappedType
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
static constexpr const ComponentResolver kResolver
Non-const resolving.
Common class for groups of components of the Event hierarchy.
InternalComponentCollection::const_reference ConstReference
ComponentConstIterator End() const
InternalComponentCollection fComponents
ComponentIterator End()
InternalComponentCollection::iterator PairIterator
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Component & Get(const int id)
Retrieve by id.
InternalComponentCollection::const_mapped_reference ConstMappedReference

, generated on Tue Sep 26 2023.