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 : public std::iterator<std::random_access_iterator_tag, Ptr<T> > {
52  public:
53  typedef Ptr<T> const reference; // otherwise boost::range does not work
54  // const, because this is a const_iterator
56  typedef typename std::iterator<std::random_access_iterator_tag, Ptr<T> >::difference_type difference_type;
57 
58  PtrVectorItr(std::vector<void const*>::const_iterator const& iItr, PtrVector<T> const* iBase)
59  : iter_(iItr), base_(iBase) {}
60 
61  Ptr<T> const operator*() const { return base_->fromItr(iter_); }
62 
63  Ptr<T> const operator[](difference_type n) const { // Otherwise the
64  return base_->fromItr(iter_ + n); // boost::range
65  } // doesn't have []
66 
67  PtrHolder<T> operator->() const { return PtrHolder<T>(this->operator*()); }
68 
69  iterator& operator++() {
70  ++iter_;
71  return *this;
72  }
74  --iter_;
75  return *this;
76  }
78  iter_ += n;
79  return *this;
80  }
82  iter_ -= n;
83  return *this;
84  }
85 
87  iterator it(*this);
88  ++iter_;
89  return it;
90  }
92  iterator it(*this);
93  --iter_;
94  return it;
95  }
97  iterator it(*this);
98  it.iter_ += n;
99  return it;
100  }
102  iterator it(*this);
103  it.iter_ -= n;
104  return it;
105  }
106 
107  difference_type operator-(iterator const& rhs) const { return this->iter_ - rhs.iter_; }
108 
109  bool operator==(iterator const& rhs) const { return this->iter_ == rhs.iter_; }
110  bool operator!=(iterator const& rhs) const { return this->iter_ != rhs.iter_; }
111  bool operator<(iterator const& rhs) const { return this->iter_ < rhs.iter_; }
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 
116  private:
117  std::vector<void const*>::const_iterator iter_;
119  };
120 
121  template <typename T>
122  class PtrVector : public PtrVectorBase {
123  public:
125  typedef PtrVectorItr<T> iterator; // make boost::sub_range happy (std allows this)
127  typedef T member_type;
128  typedef void collection_type;
129 
130  friend class PtrVectorItr<T>;
132  explicit PtrVector(ProductID const& iId) : PtrVectorBase(iId) {}
133  PtrVector(PtrVector<T> const& iOther) : PtrVectorBase(iOther) {}
134 
135  template <typename U>
136  PtrVector(PtrVector<U> const& iOther) : PtrVectorBase(iOther) {
137  static_assert(std::is_base_of<T, U>::value, "PtrVector being copied is not of compatible type");
138  }
139 
140  // ---------- const member functions ---------------------
141 
142  Ptr<T> operator[](unsigned long const iIndex) const { return this->makePtr<Ptr<T> >(iIndex); }
143 
144  const_iterator begin() const { return const_iterator(this->void_begin(), this); }
145 
146  const_iterator end() const { return const_iterator(this->void_end(), this); }
147  // ---------- member functions ---------------------------
148 
149  void push_back(Ptr<T> const& iPtr) {
150  this->push_back_base(
151  iPtr.refCore(), iPtr.key(), iPtr.hasProductCache() ? iPtr.operator->() : static_cast<void const*>(nullptr));
152  }
153 
154  template <typename U>
155  void push_back(Ptr<U> const& iPtr) {
156  //check that types are assignable
157  static_assert(std::is_base_of<T, U>::value,
158  "Ptr used in push_back can not be converted to type used by PtrVector.");
159  this->push_back_base(
160  iPtr.refCore(), iPtr.key(), iPtr.hasProductCache() ? iPtr.operator->() : static_cast<void const*>(nullptr));
161  }
162 
163  void swap(PtrVector& other) { this->PtrVectorBase::swap(other); }
164 
166  PtrVector temp(rhs);
167  this->swap(temp);
168  return *this;
169  }
170 
171  void fillView(std::vector<void const*>& pointers, FillViewHelperVector& helpers) const;
172 
173  //Used by ROOT storage
175 
176  private:
177  //PtrVector const& operator=(PtrVector const&); // stop default
178  std::type_info const& typeInfo() const override { return typeid(T); }
179 
180  // ---------- member data --------------------------------
181  Ptr<T> fromItr(std::vector<void const*>::const_iterator const& iItr) const { return this->makePtr<Ptr<T> >(iItr); }
182  };
183 
184  template <typename T>
185  void PtrVector<T>::fillView(std::vector<void const*>& pointers, FillViewHelperVector& helpers) const {
186  pointers.reserve(this->size());
187  for (const_iterator i = begin(), e = end(); i != e; ++i) {
188  Ptr<T> ref = *i;
189  T const* address = ref.isNull() ? nullptr : &*ref;
190  pointers.push_back(address);
191  helpers.push_back(FillViewHelperVector::value_type(ref.id(), ref.key()));
192  }
193  }
194 
195  template <typename T>
196  inline void fillView(PtrVector<T> const& obj,
197  ProductID const&,
198  std::vector<void const*>& pointers,
200  obj.fillView(pointers, helpers);
201  }
202 
203  template <typename T>
205  static bool const value = true;
206  };
207 
208  // Free swap function
209  template <typename T>
210  inline void swap(PtrVector<T>& lhs, PtrVector<T>& rhs) {
211  lhs.swap(rhs);
212  }
213 } // namespace edm
214 #endif
void push_back_base(RefCore const &core, key_type iKey, void const *iData)
size
Write out results.
iterator & operator--()
Definition: PtrVector.h:73
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:117
PtrHolder< T > operator->() const
Definition: PtrVector.h:67
void fillView(std::vector< void const *> &pointers, FillViewHelperVector &helpers) const
Definition: PtrVector.h:185
bool operator<(iterator const &rhs) const
Definition: PtrVector.h:111
void swap(PtrVector &other)
Definition: PtrVector.h:163
Ptr< T > const * operator->() const
Definition: PtrVector.h:44
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:160
bool operator>(iterator const &rhs) const
Definition: PtrVector.h:112
bool operator==(iterator const &rhs) const
Definition: PtrVector.h:109
Ptr< T > const & operator*() const
Definition: PtrVector.h:43
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:149
PtrVectorItr< T > const_iterator
Definition: PtrVector.h:124
void swap(PtrVectorBase &other)
swap
bool operator!=(iterator const &rhs) const
Definition: PtrVector.h:110
PtrVector< T > const * base_
Definition: PtrVector.h:118
iterator & operator-=(difference_type n)
Definition: PtrVector.h:81
#define CMS_CLASS_VERSION(_version_)
const_iterator end() const
Definition: PtrVector.h:146
iterator operator+(difference_type n) const
Definition: PtrVector.h:96
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
Ptr< T > value_type
Definition: PtrVector.h:126
std::type_info const & typeInfo() const override
Definition: PtrVector.h:178
bool isNull() const
Checks for null.
Definition: Ptr.h:142
Ptr< T > const reference
Definition: PtrVector.h:53
std::iterator< std::random_access_iterator_tag, Ptr< T > >::difference_type difference_type
Definition: PtrVector.h:56
Ptr< T > fromItr(std::vector< void const *>::const_iterator const &iItr) const
Definition: PtrVector.h:181
iterator operator-(difference_type n) const
Definition: PtrVector.h:101
Ptr< T > const operator*() const
Definition: PtrVector.h:61
iterator & operator++()
Definition: PtrVector.h:69
iterator operator--(int)
Definition: PtrVector.h:91
Ptr< T > operator[](unsigned long const iIndex) const
Definition: PtrVector.h:142
Definition: value.py:1
const_iterator begin() const
Definition: PtrVector.h:144
PtrVector(ProductID const &iId)
Definition: PtrVector.h:132
std::vector< void const * >::const_iterator void_end() const
iterator & operator+=(difference_type n)
Definition: PtrVector.h:77
bool operator>=(iterator const &rhs) const
Definition: PtrVector.h:114
void push_back(Ptr< U > const &iPtr)
Definition: PtrVector.h:155
Ptr< T > const operator[](difference_type n) const
Definition: PtrVector.h:63
PtrVectorItr(std::vector< void const *>::const_iterator const &iItr, PtrVector< T > const *iBase)
Definition: PtrVector.h:58
bool hasProductCache() const
Definition: Ptr.h:167
PtrVector & operator=(PtrVector const &rhs)
Definition: PtrVector.h:165
PtrHolder(Ptr< T > const &iPtr)
Definition: PtrVector.h:41
PtrVectorItr< T > iterator
Definition: PtrVector.h:125
bool operator<=(iterator const &rhs) const
Definition: PtrVector.h:113
PtrVectorItr< T > iterator
Definition: PtrVector.h:55
RefCore const & refCore() const
Definition: Ptr.h:169
HLT enums.
key_type key() const
Definition: Ptr.h:165
difference_type operator-(iterator const &rhs) const
Definition: PtrVector.h:107
void collection_type
Definition: PtrVector.h:128
std::vector< void const * >::const_iterator void_begin() const
long double T
PtrVector(PtrVector< T > const &iOther)
Definition: PtrVector.h:133
iterator operator++(int)
Definition: PtrVector.h:86
PtrVector(PtrVector< U > const &iOther)
Definition: PtrVector.h:136
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector