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
edm::FillViewHelperVector
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
Definition: FillViewHelperVector.h:30
edm::PtrVectorItr::operator+=
iterator & operator+=(difference_type n)
Definition: PtrVector.h:77
mps_fire.i
i
Definition: mps_fire.py:355
edm::PtrVectorItr::PtrVectorItr
PtrVectorItr(std::vector< void const * >::const_iterator const &iItr, PtrVector< T > const *iBase)
Definition: PtrVector.h:58
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
edm::PtrVectorItr::operator-
iterator operator-(difference_type n) const
Definition: PtrVector.h:101
edm::PtrVectorItr::operator++
iterator & operator++()
Definition: PtrVector.h:69
edm::Ptr::hasProductCache
bool hasProductCache() const
Definition: Ptr.h:165
edm::PtrVectorBase::push_back_base
void push_back_base(RefCore const &core, key_type iKey, void const *iData)
Definition: PtrVectorBase.cc:62
edm::PtrVector::value_type
Ptr< T > value_type
Definition: PtrVector.h:126
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::Ptr::refCore
RefCore const & refCore() const
Definition: Ptr.h:167
edm::PtrVector::fillView
void fillView(std::vector< void const * > &pointers, FillViewHelperVector &helpers) const
Definition: PtrVector.h:185
edm::PtrVectorItr::operator+
iterator operator+(difference_type n) const
Definition: PtrVector.h:96
edm::swap
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
edm::PtrVector::begin
const_iterator begin() const
Definition: PtrVector.h:144
edm::PtrVectorItr
Definition: PtrVector.h:51
edm::PtrHolder::PtrHolder
PtrHolder(Ptr< T > const &iPtr)
Definition: PtrVector.h:41
edm::PtrVectorItr::operator--
iterator & operator--()
Definition: PtrVector.h:73
edm::PtrVector::operator[]
Ptr< T > operator[](unsigned long const iIndex) const
Definition: PtrVector.h:142
edm::Ptr::key
key_type key() const
Definition: Ptr.h:163
watchdog.const
const
Definition: watchdog.py:83
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
edm::PtrVector::member_type
T member_type
Definition: PtrVector.h:127
edm::PtrVectorItr::operator->
PtrHolder< T > operator->() const
Definition: PtrVector.h:67
end
#define end
Definition: vmac.h:39
edm::PtrVector::fromItr
Ptr< T > fromItr(std::vector< void const * >::const_iterator const &iItr) const
Definition: PtrVector.h:181
edm::PtrVector::PtrVector
PtrVector()
Definition: PtrVector.h:131
edm::PtrVector::PtrVector
PtrVector(ProductID const &iId)
Definition: PtrVector.h:132
FillViewHelperVector.h
edm::PtrVectorItr::operator>=
bool operator>=(iterator const &rhs) const
Definition: PtrVector.h:114
edm::PtrVector::operator=
PtrVector & operator=(PtrVector const &rhs)
Definition: PtrVector.h:165
edm::PtrVector
Definition: PtrVector.h:36
edm::PtrVectorItr::operator>
bool operator>(iterator const &rhs) const
Definition: PtrVector.h:112
edm::PtrVector::PtrVector
PtrVector(PtrVector< U > const &iOther)
Definition: PtrVector.h:136
edm::PtrVector::iterator
PtrVectorItr< T > iterator
Definition: PtrVector.h:125
trackingPlots.other
other
Definition: trackingPlots.py:1465
CMS_CLASS_VERSION
#define CMS_CLASS_VERSION(_version_)
Definition: CMS_CLASS_VERSION.h:30
edm::PtrVectorItr::operator<=
bool operator<=(iterator const &rhs) const
Definition: PtrVector.h:113
edm::PtrVectorItr::operator-
difference_type operator-(iterator const &rhs) const
Definition: PtrVector.h:107
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
edm::PtrVector::const_iterator
PtrVectorItr< T > const_iterator
Definition: PtrVector.h:124
edm::PtrVector::push_back
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:149
edm::PtrVector::collection_type
void collection_type
Definition: PtrVector.h:128
edm::PtrVector::swap
void swap(PtrVector &other)
Definition: PtrVector.h:163
edm::Ptr::id
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:158
edm::PtrHolder::operator->
Ptr< T > const * operator->() const
Definition: PtrVector.h:44
edm::PtrHolder::ptr_
Ptr< T > ptr_
Definition: PtrVector.h:47
helpers
Definition: makeCompositeCandidate.h:8
edm::PtrVectorItr::operator!=
bool operator!=(iterator const &rhs) const
Definition: PtrVector.h:110
edm::PtrVectorItr::operator[]
const Ptr< T > operator[](difference_type n) const
Definition: PtrVector.h:63
PtrVectorBase.h
edm::PtrVector::PtrVector
PtrVector(PtrVector< T > const &iOther)
Definition: PtrVector.h:133
Ptr.h
edm::PtrHolder::operator*
Ptr< T > const & operator*() const
Definition: PtrVector.h:43
value
Definition: value.py:1
edm::PtrVector::end
const_iterator end() const
Definition: PtrVector.h:146
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
edm::PtrVectorBase::void_end
std::vector< void const * >::const_iterator void_end() const
Definition: PtrVectorBase.h:121
edm::Ptr
Definition: AssociationVector.h:31
edm::PtrVectorItr::operator--
iterator operator--(int)
Definition: PtrVector.h:91
std
Definition: JetResolutionObject.h:76
CMS_CLASS_VERSION.h
edm::PtrVectorItr::operator*
const Ptr< T > operator*() const
Definition: PtrVector.h:61
edm::PtrVectorItr::iter_
std::vector< void const * >::const_iterator iter_
Definition: PtrVector.h:117
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
edm::PtrHolder
Definition: PtrVector.h:39
edm::PtrVectorBase::void_begin
std::vector< void const * >::const_iterator void_begin() const
Definition: PtrVectorBase.h:114
edm::PtrVectorItr::operator<
bool operator<(iterator const &rhs) const
Definition: PtrVector.h:111
edm::PtrVectorItr::reference
const Ptr< T > reference
Definition: PtrVector.h:53
edm::PtrVectorItr::operator==
bool operator==(iterator const &rhs) const
Definition: PtrVector.h:109
edm::PtrVector::push_back
void push_back(Ptr< U > const &iPtr)
Definition: PtrVector.h:155
edm::PtrVectorItr::iterator
PtrVectorItr< T > iterator
Definition: PtrVector.h:55
edm::PtrVectorItr::operator-=
iterator & operator-=(difference_type n)
Definition: PtrVector.h:81
edm::PtrVectorBase
Definition: PtrVectorBase.h:32
edm::fillView
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers)
Definition: AssociationVector.h:290
edm::PtrVector::typeInfo
std::type_info const & typeInfo() const override
Definition: PtrVector.h:178
edm::PtrVectorItr::operator++
iterator operator++(int)
Definition: PtrVector.h:86
edm::PtrVectorItr::difference_type
std::iterator< std::random_access_iterator_tag, Ptr< T > >::difference_type difference_type
Definition: PtrVector.h:56
edm::Ptr::isNull
bool isNull() const
Checks for null.
Definition: Ptr.h:142
begin
#define begin
Definition: vmac.h:32
edm::PtrVectorItr::base_
PtrVector< T > const * base_
Definition: PtrVector.h:118
edm::has_fillView
Definition: traits.h:114
edm::ProductID
Definition: ProductID.h:27
edm::PtrVectorBase::swap
void swap(PtrVectorBase &other)
swap
Definition: PtrVectorBase.cc:56
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37