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