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