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