MultiObject.h
Go to the documentation of this file.
1 #ifndef _utl_MultiObject_h_
2 #define _utl_MultiObject_h_
3 
4 
5 #include <utl/IteratorRange.h>
6 #include <boost/iterator/transform_iterator.hpp>
7 #include <map>
8 
9 
10 namespace utl {
11 
21  template<typename T>
22  class LabeledObject {
23 
24  public:
25  LabeledObject(const int label, T& obj) : fLabel(label), fObject(&obj) { }
26 
27  LabeledObject(const std::pair<int, T*>& pair)
28  : fLabel(pair.first), fObject(pair.second) { }
29 
31 
33  int GetLabel() const { return fLabel; }
34 
35  bool operator==(const LabeledObject& lobj) const
36  { return fLabel == lobj.fLabel && *fObject == *lobj.fObject; }
37 
38  bool operator!=(const LabeledObject& lobj) const
39  { return !operator==(lobj); }
40 
41  protected:
42  T& GetObject() { return *fObject; }
43  const T& GetObject() const { return *fObject; }
44 
45  private:
46  int fLabel = 0;
47  T* fObject = nullptr;
48 
49  };
50 
51 
52  namespace detail {
53  template<typename T, typename LabeledObjectType>
55  LabeledObjectType
56  operator()(const typename std::map<int, T*>::value_type& pair) const
57  { return LabeledObjectType(pair); }
58  };
59  }
60 
70  template<typename T, typename LabeledObjectType>
71  class MultiObject {
72 
73  public:
74  typedef typename std::map<int, T*> MultiObjectContainer;
76  typedef boost::transform_iterator<LabeledObjectFunctor,
77  typename MultiObjectContainer::iterator,
78  LabeledObjectType> Iterator;
79  typedef boost::transform_iterator<LabeledObjectFunctor,
80  typename MultiObjectContainer::const_iterator,
81  LabeledObjectType> ConstIterator;
82 
83  Iterator Begin() { return Iterator(fObjects.begin()); }
84  ConstIterator Begin() const { return ConstIterator(fObjects.begin()); }
85  Iterator End() { return Iterator(fObjects.end()); }
86  ConstIterator End() const { return ConstIterator(fObjects.end()); }
88 
89  unsigned int GetNLabels() const { return fObjects.size(); }
90 
91  bool HasLabel(const int label) const
92  { return fObjects.find(label) != fObjects.end(); }
93 
94  bool operator==(const MultiObject& mobj) const;
95 
96  bool operator!=(const MultiObject& mobj) const
97  { return !operator==(mobj); }
98 
99  explicit operator bool() const { return !fObjects.empty(); }
100 
101  void Clear();
102 
103  protected:
105 
106  MultiObject(const MultiObject& mobj) { *this = mobj; }
107 
108  MultiObject(MultiObject&& mobj) : fObjects(std::move(mobj.fObjects)) { }
109 
110  MultiObject& operator=(const MultiObject& mobj);
111 
112  MultiObject&
114  {
115  if (this != &mobj) {
116  Clear();
117  fObjects = std::move(mobj.fObjects);
118  }
119  return *this;
120  }
121 
122  ~MultiObject();
123 
124  T& GetObject(const int label = 0);
125  const T& GetObject(const int label = 0) const;
126 
128  void AddObject(const T& obj, const int label)
129  { AddObject(new T(obj), label); }
130 
132  void AddObject(T* const obj, const int label);
133 
134  void RemoveObject(const int label);
135 
136  private:
137  typedef typename MultiObjectContainer::iterator InternalIterator;
138  typedef typename MultiObjectContainer::const_iterator InternalConstIterator;
139 
141 
142  };
143 
144 }
145 
146 
147 #endif
constexpr double second
Definition: AugerUnits.h:145
boost::transform_iterator< LabeledObjectFunctor, typename MultiObjectContainer::iterator, LabeledObjectType > Iterator
Definition: MultiObject.h:78
LabeledObjectType operator()(const typename std::map< int, T * >::value_type &pair) const
Definition: MultiObject.h:56
#define OFFLINE_MAKE_BOTH_FRIEND_RANGES(_Iterator_, _ConstIterator_, _Class_)
MultiObjectContainer::const_iterator InternalConstIterator
Definition: MultiObject.h:138
ConstIterator End() const
Definition: MultiObject.h:86
LabeledObject(const std::pair< int, T * > &pair)
Definition: MultiObject.h:27
MultiObjectContainer::iterator InternalIterator
Definition: MultiObject.h:137
std::map< int, T * > MultiObjectContainer
Definition: MultiObject.h:74
Iterator Begin()
Definition: MultiObject.h:83
boost::transform_iterator< LabeledObjectFunctor, typename MultiObjectContainer::const_iterator, LabeledObjectType > ConstIterator
Definition: MultiObject.h:81
void RemoveObject(const int label)
bool operator==(const MultiObject &mobj) const
MultiObject(MultiObject &&mobj)
Definition: MultiObject.h:108
ConstIterator Begin() const
Definition: MultiObject.h:84
LabeledObject(const int label, T &obj)
Definition: MultiObject.h:25
int GetLabel() const
Return the label for the object.
Definition: MultiObject.h:33
MultiObject & operator=(MultiObject &&mobj)
Definition: MultiObject.h:113
bool operator!=(const LabeledObject &lobj) const
Definition: MultiObject.h:38
container for object and associated labels
Definition: MultiObject.h:71
MultiObject(const MultiObject &mobj)
Definition: MultiObject.h:106
bool operator==(const LabeledObject &lobj) const
Definition: MultiObject.h:35
bool operator!=(const MultiObject &mobj) const
Definition: MultiObject.h:96
utl::detail::LabeledObjectFunctor< T, LabeledObjectType > LabeledObjectFunctor
Definition: MultiObject.h:75
Iterator End()
Definition: MultiObject.h:85
unsigned int GetNLabels() const
Definition: MultiObject.h:89
const T & GetObject() const
Definition: MultiObject.h:43
MultiObject & operator=(const MultiObject &mobj)
Class for attaching a label to an object.
Definition: MultiObject.h:22
MultiObjectContainer fObjects
Definition: MultiObject.h:140
void AddObject(const T &obj, const int label)
add with deep copy of the object
Definition: MultiObject.h:128
T & GetObject(const int label=0)
bool HasLabel(const int label) const
Definition: MultiObject.h:91

, generated on Tue Sep 26 2023.