CMS 3D CMS Logo

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