CMS 3D CMS Logo

PtrVector.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_PtrVector_h
2 #define DataFormats_Common_PtrVector_h
3 // -*- C++ -*-
4 //
5 // Package: Common
6 // Class : PtrVector
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Wed Oct 24 15:26:50 EDT 2007
19 //
20 
21 // user include files
26 
27 // system include files
28 #include <type_traits>
29 #include <typeinfo>
30 #include <vector>
31 
32 // forward declarations
33 namespace edm {
34  class ProductID;
35  template <typename T>
36  class PtrVector;
37 
38  template <typename T>
39  class PtrHolder {
40  public:
41  PtrHolder(Ptr<T> const& iPtr) : ptr_(iPtr) {}
42 
43  Ptr<T> const& operator*() const { return ptr_; }
44  Ptr<T> const* operator->() const { return &ptr_; }
45 
46  private:
48  };
49 
50  template <typename T>
51  class PtrVectorItr {
52  public:
53  using iterator_category = std::random_access_iterator_tag;
54  using value_type = Ptr<T>;
55  using pointer = Ptr<T>*;
56  using reference = Ptr<T> const; // otherwise boost::range does not work
57  // const, because this is a const_iterator
59  using difference_type = std::ptrdiff_t;
60 
61  PtrVectorItr(std::vector<void const*>::const_iterator const& iItr, PtrVector<T> const* iBase)
62  : iter_(iItr), base_(iBase) {}
63 
64  Ptr<T> const operator*() const { return base_->fromItr(iter_); }
65 
66  Ptr<T> const operator[](difference_type n) const { // Otherwise the
67  return base_->fromItr(iter_ + n); // boost::range
68  } // doesn't have []
69 
70  PtrHolder<T> operator->() const { return PtrHolder<T>(this->operator*()); }
71 
73  ++iter_;
74  return *this;
75  }
77  --iter_;
78  return *this;
79  }
81  iter_ += n;
82  return *this;
83  }
85  iter_ -= n;
86  return *this;
87  }
88 
90  iterator it(*this);
91  ++iter_;
92  return it;
93  }
95  iterator it(*this);
96  --iter_;
97  return it;
98  }
100  iterator it(*this);
101  it.iter_ += n;
102  return it;
103  }
105  iterator it(*this);
106  it.iter_ -= n;
107  return it;
108  }
109 
110  difference_type operator-(iterator const& rhs) const { return this->iter_ - rhs.iter_; }
111 
112  bool operator==(iterator const& rhs) const { return this->iter_ == rhs.iter_; }
113  bool operator!=(iterator const& rhs) const { return this->iter_ != rhs.iter_; }
114  bool operator<(iterator const& rhs) const { return this->iter_ < rhs.iter_; }
115  bool operator>(iterator const& rhs) const { return this->iter_ > rhs.iter_; }
116  bool operator<=(iterator const& rhs) const { return this->iter_ <= rhs.iter_; }
117  bool operator>=(iterator const& rhs) const { return this->iter_ >= rhs.iter_; }
118 
119  private:
120  std::vector<void const*>::const_iterator iter_;
122  };
123 
124  template <typename T>
125  class PtrVector : public PtrVectorBase {
126  public:
128  using iterator = PtrVectorItr<T>; // make boost::sub_range happy (std allows this)
130  using member_type = T;
132 
133  friend class PtrVectorItr<T>;
135  explicit PtrVector(ProductID const& iId) : PtrVectorBase(iId) {}
136  PtrVector(PtrVector<T> const& iOther) : PtrVectorBase(iOther) {}
137 
138  template <typename U>
139  PtrVector(PtrVector<U> const& iOther) : PtrVectorBase(iOther) {
140  static_assert(std::is_base_of<T, U>::value, "PtrVector being copied is not of compatible type");
141  }
142 
143  // ---------- const member functions ---------------------
144 
145  Ptr<T> operator[](unsigned long const iIndex) const { return this->makePtr<Ptr<T> >(iIndex); }
146 
147  const_iterator begin() const { return const_iterator(this->void_begin(), this); }
148 
149  const_iterator end() const { return const_iterator(this->void_end(), this); }
150  // ---------- member functions ---------------------------
151 
152  void push_back(Ptr<T> const& iPtr) {
153  this->push_back_base(
154  iPtr.refCore(), iPtr.key(), iPtr.hasProductCache() ? iPtr.operator->() : static_cast<void const*>(nullptr));
155  }
156 
157  template <typename U>
158  void push_back(Ptr<U> const& iPtr) {
159  //check that types are assignable
160  static_assert(std::is_base_of<T, U>::value,
161  "Ptr used in push_back can not be converted to type used by PtrVector.");
162  this->push_back_base(
163  iPtr.refCore(), iPtr.key(), iPtr.hasProductCache() ? iPtr.operator->() : static_cast<void const*>(nullptr));
164  }
165 
166  void swap(PtrVector& other) { this->PtrVectorBase::swap(other); }
167 
169  PtrVector temp(rhs);
170  this->swap(temp);
171  return *this;
172  }
173 
174  void fillView(std::vector<void const*>& pointers, FillViewHelperVector& helpers) const;
175 
176  //Used by ROOT storage
178 
179  private:
180  //PtrVector const& operator=(PtrVector const&); // stop default
181  std::type_info const& typeInfo() const override { return typeid(T); }
182 
183  // ---------- member data --------------------------------
184  Ptr<T> fromItr(std::vector<void const*>::const_iterator const& iItr) const { return this->makePtr<Ptr<T> >(iItr); }
185  };
186 
187  template <typename T>
188  void PtrVector<T>::fillView(std::vector<void const*>& pointers, FillViewHelperVector& helpers) const {
189  pointers.reserve(this->size());
190  for (const_iterator i = begin(), e = end(); i != e; ++i) {
191  Ptr<T> ref = *i;
192  T const* address = ref.isNull() ? nullptr : &*ref;
193  pointers.push_back(address);
194  helpers.push_back(FillViewHelperVector::value_type(ref.id(), ref.key()));
195  }
196  }
197 
198  template <typename T>
199  inline void fillView(PtrVector<T> const& obj,
200  ProductID const&,
201  std::vector<void const*>& pointers,
203  obj.fillView(pointers, helpers);
204  }
205 
206  template <typename T>
208  static bool const value = true;
209  };
210 
211  // Free swap function
212  template <typename T>
213  inline void swap(PtrVector<T>& lhs, PtrVector<T>& rhs) {
214  lhs.swap(rhs);
215  }
216 } // namespace edm
217 #endif
void push_back_base(RefCore const &core, key_type iKey, void const *iData)
size
Write out results.
iterator & operator--()
Definition: PtrVector.h:76
Ptr< T > ptr_
Definition: PtrVector.h:47
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const *> &pointers, FillViewHelperVector &helpers)
std::vector< void const * >::const_iterator iter_
Definition: PtrVector.h:120
PtrHolder< T > operator->() const
Definition: PtrVector.h:70
void fillView(std::vector< void const *> &pointers, FillViewHelperVector &helpers) const
Definition: PtrVector.h:188
PtrVectorItr< T > const_iterator
Definition: PtrVector.h:127
bool operator<(iterator const &rhs) const
Definition: PtrVector.h:114
void swap(PtrVector &other)
Definition: PtrVector.h:166
Ptr< T > const * operator->() const
Definition: PtrVector.h:44
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:162
bool operator>(iterator const &rhs) const
Definition: PtrVector.h:115
bool operator==(iterator const &rhs) const
Definition: PtrVector.h:112
Ptr< T > const & operator*() const
Definition: PtrVector.h:43
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:152
void swap(PtrVectorBase &other)
swap
bool operator!=(iterator const &rhs) const
Definition: PtrVector.h:113
PtrVector< T > const * base_
Definition: PtrVector.h:121
iterator & operator-=(difference_type n)
Definition: PtrVector.h:84
#define CMS_CLASS_VERSION(_version_)
const_iterator end() const
Definition: PtrVector.h:149
iterator operator+(difference_type n) const
Definition: PtrVector.h:99
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
std::ptrdiff_t difference_type
Definition: PtrVector.h:59
std::type_info const & typeInfo() const override
Definition: PtrVector.h:181
bool isNull() const
Checks for null.
Definition: Ptr.h:144
Ptr< T > fromItr(std::vector< void const *>::const_iterator const &iItr) const
Definition: PtrVector.h:184
iterator operator-(difference_type n) const
Definition: PtrVector.h:104
Ptr< T > const operator*() const
Definition: PtrVector.h:64
iterator & operator++()
Definition: PtrVector.h:72
iterator operator--(int)
Definition: PtrVector.h:94
Ptr< T > operator[](unsigned long const iIndex) const
Definition: PtrVector.h:145
std::random_access_iterator_tag iterator_category
Definition: PtrVector.h:53
Definition: value.py:1
const_iterator begin() const
Definition: PtrVector.h:147
PtrVector(ProductID const &iId)
Definition: PtrVector.h:135
std::vector< void const * >::const_iterator void_end() const
iterator & operator+=(difference_type n)
Definition: PtrVector.h:80
bool operator>=(iterator const &rhs) const
Definition: PtrVector.h:117
void push_back(Ptr< U > const &iPtr)
Definition: PtrVector.h:158
Ptr< T > const operator[](difference_type n) const
Definition: PtrVector.h:66
PtrVectorItr(std::vector< void const *>::const_iterator const &iItr, PtrVector< T > const *iBase)
Definition: PtrVector.h:61
bool hasProductCache() const
Definition: Ptr.h:169
PtrVector & operator=(PtrVector const &rhs)
Definition: PtrVector.h:168
PtrHolder(Ptr< T > const &iPtr)
Definition: PtrVector.h:41
bool operator<=(iterator const &rhs) const
Definition: PtrVector.h:116
RefCore const & refCore() const
Definition: Ptr.h:171
HLT enums.
key_type key() const
Definition: Ptr.h:167
difference_type operator-(iterator const &rhs) const
Definition: PtrVector.h:110
std::vector< void const * >::const_iterator void_begin() const
long double T
PtrVector(PtrVector< T > const &iOther)
Definition: PtrVector.h:136
iterator operator++(int)
Definition: PtrVector.h:89
PtrVector(PtrVector< U > const &iOther)
Definition: PtrVector.h:139
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector