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 T member_type;
110  typedef void collection_type;
111 
112  friend class PtrVectorItr<T>;
114  explicit PtrVector(ProductID const& iId) : PtrVectorBase(iId) {}
115  PtrVector(PtrVector<T> const& iOther): PtrVectorBase(iOther) {}
116 
117  template <typename U>
118  PtrVector(PtrVector<U> const& iOther): PtrVectorBase(iOther) {
119  BOOST_STATIC_ASSERT( (boost::is_base_of<T, U>::value) );
120  }
121 
122  // ---------- const member functions ---------------------
123 
124  Ptr<T> operator[](unsigned long const iIndex ) const {
125  return this->makePtr<Ptr<T> >(iIndex);
126  }
127 
129  return const_iterator(this->void_begin(),
130  this);
131  }
132 
133  const_iterator end() const {
134  return const_iterator(this->void_end(),
135  this);
136  }
137  // ---------- member functions ---------------------------
138 
139  void push_back(Ptr<T> const& iPtr) {
140  this->push_back_base(iPtr.refCore(),
141  iPtr.key(),
142  iPtr.hasProductCache() ? iPtr.operator->() : static_cast<void const*>(0));
143  }
144 
145  template<typename U>
146  void push_back(Ptr<U> const& iPtr) {
147  //check that types are assignable
148  BOOST_STATIC_ASSERT( (boost::is_base_of<T, U>::value) );
149  this->push_back_base(iPtr.refCore(),
150  iPtr.key(),
151  iPtr.hasProductCache() ? iPtr.operator->() : static_cast<void const*>(0));
152  }
153 
154  void swap(PtrVector& other) {
155  this->PtrVectorBase::swap(other);
156  }
157 
159  PtrVector temp(rhs);
160  this->swap(temp);
161  return *this;
162  }
163 
164  void fillView(std::vector<void const*>& pointers) const;
165 
166  //Used by ROOT storage
168 
169  private:
170 
171  //PtrVector const& operator=(PtrVector const&); // stop default
172  std::type_info const& typeInfo() const {return typeid(T);}
173 
174  // ---------- member data --------------------------------
175  Ptr<T> fromItr(std::vector<void const*>::const_iterator const& iItr) const {
176  return this->makePtr<Ptr<T> >(iItr);
177  }
178 
179  };
180 
181  template <typename T>
182  void
183  PtrVector<T>::fillView(std::vector<void const*>& pointers) const {
184  pointers.reserve(this->size());
185  for (const_iterator i = begin(), e = end(); i != e; ++i) {
186  Ptr<T> ref = *i;
187  T const* address = ref.isNull() ? 0 : &*ref;
188  pointers.push_back(address);
189  }
190  }
191 
192  // NOTE: the following implementation has unusual signature!
193  template <typename T>
194  inline void fillView(PtrVector<T> const& obj,
195  std::vector<void const*>& pointers) {
196  obj.fillView(pointers);
197  }
198 
199  template <typename T>
201  static bool const value = true;
202  };
203 
204  // Free swap function
205  template <typename T>
206  inline
207  void
209  lhs.swap(rhs);
210  }
211 }
212 #endif
void push_back_base(RefCore const &core, key_type iKey, void const *iData)
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:175
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:154
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
key_type key() const
Definition: Ptr.h:169
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:139
PtrVectorItr< T > const_iterator
Definition: PtrVector.h:106
bool operator<=(iterator const &rhs) const
Definition: PtrVector.h:93
void swap(PtrVectorBase &other)
swap
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:172
PtrVector< T > const * base_
Definition: PtrVector.h:98
iterator & operator-=(difference_type n)
Definition: PtrVector.h:80
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
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:128
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
bool isNull() const
Checks for null.
Definition: Ptr.h:148
RefCore const & refCore() const
Definition: Ptr.h:173
const_iterator end() const
Definition: PtrVector.h:133
#define end
Definition: vmac.h:37
PtrVector(ProductID const &iId)
Definition: PtrVector.h:114
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:146
PtrVector & operator=(PtrVector const &rhs)
Definition: PtrVector.h:158
PtrHolder(Ptr< T > const &iPtr)
Definition: PtrVector.h:39
bool hasProductCache() const
Definition: Ptr.h:171
void fillView(std::vector< void const * > &pointers) const
Definition: PtrVector.h:183
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
#define begin
Definition: vmac.h:30
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:124
bool operator<(iterator const &rhs) const
Definition: PtrVector.h:91
void collection_type
Definition: PtrVector.h:110
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:115
iterator operator++(int)
Definition: PtrVector.h:82
static bool const value
Definition: traits.h:124
PtrVector(PtrVector< U > const &iOther)
Definition: PtrVector.h:118
bool operator!=(iterator const &rhs) const
Definition: PtrVector.h:90