CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OwnVector.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_OwnVector_h
2 #define DataFormats_Common_OwnVector_h
3 
10 
11 #if defined CMS_USE_DEBUGGING_ALLOCATOR
13 #endif
15 
16 #include <algorithm>
17 #include <functional>
18 #include <typeinfo>
19 #include <vector>
21 
22 namespace edm {
23  class ProductID;
24  template <typename T, typename P = ClonePolicy<T> >
25  class OwnVector {
26  private:
27 #if defined(CMS_USE_DEBUGGING_ALLOCATOR)
28  typedef std::vector<T*, debugging_allocator<T> > base;
29 #else
30  typedef std::vector<T*> base;
31 #endif
32 
33  public:
34  typedef typename base::size_type size_type;
35  typedef T value_type;
36  typedef T* pointer;
37  typedef T& reference;
38  typedef T const& const_reference;
39  typedef P policy_type;
40 
41  class iterator;
43  public:
44  typedef T value_type;
45  typedef T* pointer;
46  typedef T const& reference;
47  typedef ptrdiff_t difference_type;
48  typedef typename base::const_iterator::iterator_category iterator_category;
49  const_iterator(typename base::const_iterator const& it) : i(it) { }
50  const_iterator(iterator const& it) : i(it.i) { }
52  const_iterator& operator++() { ++i; return *this; }
53  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
54  const_iterator& operator--() { --i; return *this; }
55  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
56  difference_type operator-(const_iterator const& o) const { return i - o.i; }
59  bool operator<(const_iterator const& o) const { return i < o.i; }
60  bool operator==(const_iterator const& ci) const { return i == ci.i; }
61  bool operator!=(const_iterator const& ci) const { return i != ci.i; }
62  T const& operator *() const { return **i; }
63  // operator T const*() const { return & **i; }
64  T const* operator->() const { return & (operator*()); }
65  const_iterator & operator +=(difference_type d) { i += d; return *this; }
66  const_iterator & operator -=(difference_type d) { i -= d; return *this; }
67  reference operator[](difference_type d) const { return *const_iterator(i+d); } // for boost::iterator_range []
68  private:
69  typename base::const_iterator i;
70  };
71  class iterator {
72  public:
73  typedef T value_type;
74  typedef T * pointer;
75  typedef T & reference;
76  typedef ptrdiff_t difference_type;
77  typedef typename base::iterator::iterator_category iterator_category;
78  iterator(typename base::iterator const& it) : i(it) { }
79  iterator() {}
80  iterator& operator++() { ++i; return *this; }
81  iterator operator++(int) { iterator ci = *this; ++i; return ci; }
82  iterator& operator--() { --i; return *this; }
83  iterator operator--(int) { iterator ci = *this; --i; return ci; }
84  difference_type operator-(iterator const& o) const { return i - o.i; }
85  iterator operator+(difference_type n) const { return iterator(i + n); }
86  iterator operator-(difference_type n) const { return iterator(i - n); }
87  bool operator<(iterator const& o) const { return i < o.i; }
88  bool operator==(iterator const& ci) const { return i == ci.i; }
89  bool operator!=(iterator const& ci) const { return i != ci.i; }
90  T & operator *() const { return **i; }
91  // operator T *() const { return & **i; }
92  //T *& get() { return *i; }
93  T * operator->() const { return & (operator*()); }
94  iterator & operator +=(difference_type d) { i += d; return *this; }
95  iterator & operator -=(difference_type d) { i -= d; return *this; }
96  reference operator[](difference_type d) const { return *iterator(i+d); } // for boost::iterator_range []
97  private:
98  typename base::iterator i;
99  friend class const_iterator;
100  friend class OwnVector<T, P>;
101  };
102 
103 
104  OwnVector();
106  OwnVector(OwnVector const&);
107 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
109 #endif
110 
112 
113  iterator begin();
114  iterator end();
115  const_iterator begin() const;
116  const_iterator end() const;
117  size_type size() const;
118  bool empty() const;
120  const_reference operator[](size_type) const;
121 
122  OwnVector<T, P>& operator=(OwnVector<T, P> const&);
123 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
125 #endif
126 
127 
128  void reserve(size_t);
129  template <typename D> void push_back(D*& d);
130  template <typename D> void push_back(D* const& d);
131  template <typename D> void push_back(std::auto_ptr<D> d);
132  void push_back(T const& valueToCopy);
133  bool is_back_safe() const;
134  void pop_back();
135  reference back();
136  const_reference back() const;
137  reference front();
138  const_reference front() const;
139  base const& data() const;
140  void clear();
141  iterator erase(iterator pos);
143  void reverse() { std::reverse(data_.begin(),data_.end());}
144  template<typename S>
145  void sort(S s);
146  void sort();
147 
148  void swap(OwnVector<T, P>& other) noexcept;
149 
150  void fillView(ProductID const& id,
151  std::vector<void const*>& pointers,
152  helper_vector& helpers) const;
153 
154  void setPtr(std::type_info const& toType,
155  unsigned long index,
156  void const*& ptr) const;
157 
158  void fillPtrVector(std::type_info const& toType,
159  std::vector<unsigned long> const& indices,
160  std::vector<void const*>& ptrs) const;
161 
162 
163  //Used by ROOT storage
165 
166  private:
167  void destroy() noexcept;
168  template<typename O>
169  struct Ordering {
170  Ordering(O const& c) : comp(c) { }
171  bool operator()(T const* t1, T const* t2) const {
172  return comp(*t1, *t2);
173  }
174  private:
175  O comp;
176  };
177  template<typename O>
178  static Ordering<O> ordering(O const& comp) {
179  return Ordering<O>(comp);
180  }
182  };
183 
184  template<typename T, typename P>
185  inline OwnVector<T, P>::OwnVector() : data_() {
186  }
187 
188  template<typename T, typename P>
189  inline OwnVector<T, P>::OwnVector(size_type n) : data_(n) {
190  }
191 
192  template<typename T, typename P>
193  inline OwnVector<T, P>::OwnVector(OwnVector<T, P> const& o) : data_(o.size()) {
194  size_type current = 0;
195  for (const_iterator i = o.begin(), e = o.end(); i != e; ++i,++current)
196  data_[current] = policy_type::clone(*i);
197  }
198 
199 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
200  template<typename T, typename P>
201  inline OwnVector<T, P>::OwnVector(OwnVector<T, P>&& o) noexcept{
202  data_.swap(o.data_);
203  }
204 #endif
205 
206  template<typename T, typename P>
207  inline OwnVector<T, P>::~OwnVector() noexcept {
208  destroy();
209  }
210 
211  template<typename T, typename P>
213  OwnVector<T,P> temp(o);
214  swap(temp);
215  return *this;
216  }
217 
218 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
219  template<typename T, typename P>
221  data_.swap(o.data_);
222  return *this;
223  }
224 #endif
225 
226 
227  template<typename T, typename P>
229  return iterator(data_.begin());
230  }
231 
232  template<typename T, typename P>
234  return iterator(data_.end());
235  }
236 
237  template<typename T, typename P>
239  return const_iterator(data_.begin());
240  }
241 
242  template<typename T, typename P>
244  return const_iterator(data_.end());
245  }
246 
247  template<typename T, typename P>
249  return data_.size();
250  }
251 
252  template<typename T, typename P>
253  inline bool OwnVector<T, P>::empty() const {
254  return data_.empty();
255  }
256 
257  template<typename T, typename P>
259  return *data_[n];
260  }
261 
262  template<typename T, typename P>
264  return *data_[n];
265  }
266 
267  template<typename T, typename P>
268  inline void OwnVector<T, P>::reserve(size_t n) {
269  data_.reserve(n);
270  }
271 
272  template<typename T, typename P>
273  template<typename D>
274  inline void OwnVector<T, P>::push_back(D*& d) {
275  // C++ does not yet support rvalue references, so d should only be
276  // able to bind to an lvalue.
277  // This should be called only for lvalues.
278  data_.push_back(d);
279  d = 0;
280  }
281 
282  template<typename T, typename P>
283  template<typename D>
284  inline void OwnVector<T, P>::push_back(D* const& d) {
285 
286  // C++ allows d to be bound to an lvalue or rvalue. But the other
287  // signature should be a better match for an lvalue (because it
288  // does not require an lvalue->rvalue conversion). Thus this
289  // signature should only be chosen for rvalues.
290  data_.push_back(d);
291  }
292 
293 
294  template<typename T, typename P>
295  template<typename D>
296  inline void OwnVector<T, P>::push_back(std::auto_ptr<D> d) {
297  data_.push_back(d.release());
298  }
299 
300 
301  template<typename T, typename P>
302  inline void OwnVector<T, P>::push_back(T const& d) {
303  data_.push_back(policy_type::clone(d));
304  }
305 
306 
307  template<typename T, typename P>
309  // We have to delete the pointed-to thing, before we squeeze it
310  // out of the vector...
311  delete data_.back();
312  data_.pop_back();
313  }
314 
315  template <typename T, typename P>
316  inline bool OwnVector<T, P>::is_back_safe() const {
317  return data_.back() != 0;
318  }
319 
320  template<typename T, typename P>
322  T* result = data_.back();
323  if (result == 0) {
325  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
326  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
327  "pointer at the end of the collection is not null before calling back()\n"
328  "if you wish to avoid this exception.\n"
329  "Consider using OwnVector::is_back_safe()\n");
330  }
331  return *data_.back();
332  }
333 
334  template<typename T, typename P>
336  T* result = data_.back();
337  if (result == 0) {
339  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
340  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
341  "pointer at the end of the collection is not null before calling back()\n"
342  "if you wish to avoid this exception.\n"
343  "Consider using OwnVector::is_back_safe()\n");
344  }
345  return *data_.back();
346  }
347 
348  template<typename T, typename P>
350  return *data_.front();
351  }
352 
353  template<typename T, typename P>
355  return *data_.front();
356  }
357 
358  template<typename T, typename P>
359  inline void OwnVector<T, P>::destroy() noexcept {
360  typename base::const_iterator b = data_.begin(), e = data_.end();
361  for(typename base::const_iterator i = b; i != e; ++ i)
362  delete * i;
363  }
364 
365  template<typename T, typename P>
366  inline typename OwnVector<T, P>::base const& OwnVector<T, P>::data() const {
367  return data_;
368  }
369 
370  template<typename T, typename P>
371  inline void OwnVector<T, P>::clear() {
372  destroy();
373  data_.clear();
374  }
375 
376  template<typename T, typename P>
378  delete * pos.i;
379  return iterator(data_.erase(pos.i));
380  }
381 
382  template<typename T, typename P>
384  typename base::iterator b = first.i, e = last.i;
385  for(typename base::iterator i = b; i != e; ++ i)
386  delete * i;
387  return iterator(data_.erase(b, e));
388  }
389 
390  template<typename T, typename P> template<typename S>
392  std::sort(data_.begin(), data_.end(), ordering(comp));
393  }
394 
395  template<typename T, typename P>
397  std::sort(data_.begin(), data_.end(), ordering(std::less<value_type>()));
398  }
399 
400  template<typename T, typename P>
401  inline void OwnVector<T, P>::swap(OwnVector<T, P>& other) noexcept {
402  data_.swap(other.data_);
403  }
404 
405  template<typename T, typename P>
407  std::vector<void const*>& pointers,
408  helper_vector& helpers) const {
409  typedef Ref<OwnVector> ref_type ;
410  typedef reftobase::RefHolder<ref_type> holder_type;
411 
412  size_type numElements = this->size();
413  pointers.reserve(numElements);
414  helpers.reserve(numElements);
415  size_type key = 0;
416  for(typename base::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i, ++key) {
417 
418  if (*i == 0) {
420  "In OwnVector::fillView() we have intercepted an attempt to put a null pointer\n"
421  "into a View and that is not allowed. It is probably an error that the null\n"
422  "pointer was in the OwnVector in the first place.\n");
423  }
424  else {
425  pointers.push_back(*i);
426  holder_type h(ref_type(id, *i, key,this));
427  helpers.push_back(&h);
428  }
429  }
430  }
431 
432  template<typename T, typename P>
433  inline void swap(OwnVector<T, P>& a, OwnVector<T, P>& b) noexcept {
434  a.swap(b);
435  }
436 
437  //----------------------------------------------------------------------
438  //
439  // Free function template to support creation of Views.
440 
441  template <typename T, typename P>
442  inline
443  void
445  ProductID const& id,
446  std::vector<void const*>& pointers,
447  helper_vector& helpers) {
448  obj.fillView(id, pointers, helpers);
449  }
450 
451 
452  template <typename T, typename P>
453  struct has_fillView<edm::OwnVector<T, P> > {
454  static bool const value = true;
455  };
456 
457 
458  // Free function templates to support the use of edm::Ptr.
459 
460  template <typename T, typename P>
461  inline
462  void
463  OwnVector<T,P>::setPtr(std::type_info const& toType,
464  unsigned long index,
465  void const*& ptr) const {
466  detail::reallySetPtr<OwnVector<T,P> >(*this, toType, index, ptr);
467  }
468 
469  template <typename T, typename P>
470  inline
471  void
473  std::type_info const& toType,
474  unsigned long index,
475  void const*& ptr) {
476  obj.setPtr(toType, index, ptr);
477  }
478 
479  template <typename T, typename P>
480  inline
481  void
482  OwnVector<T,P>::fillPtrVector(std::type_info const& toType,
483  std::vector<unsigned long> const& indices,
484  std::vector<void const*>& ptrs) const {
485  detail::reallyfillPtrVector(*this, toType, indices, ptrs);
486  }
487 
488 
489  template <typename T, typename P>
490  inline
491  void
493  std::type_info const& toType,
494  std::vector<unsigned long> const& indices,
495  std::vector<void const*>& ptrs) {
496  obj.fillPtrVector(toType, indices, ptrs);
497  }
498 
499 
500  template <typename T, typename P>
501  struct has_setPtr<edm::OwnVector<T,P> > {
502  static bool const value = true;
503  };
504 
505 
506 }
507 
508 
509 #endif
T & operator*() const
Definition: OwnVector.h:90
difference_type operator-(iterator const &o) const
Definition: OwnVector.h:84
base::iterator i
Definition: OwnVector.h:98
int i
Definition: DBlmapReader.cc:9
static bool const value
Definition: traits.h:173
T const * operator->() const
Definition: OwnVector.h:64
reference back()
Definition: OwnVector.h:321
std::vector< T * > base
Definition: OwnVector.h:30
iterator operator-(difference_type n) const
Definition: OwnVector.h:86
base::const_iterator::iterator_category iterator_category
Definition: OwnVector.h:48
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector &helpers)
const_iterator & operator++()
Definition: OwnVector.h:52
reference operator[](size_type)
Definition: OwnVector.h:258
bool operator()(T const *t1, T const *t2) const
Definition: OwnVector.h:171
void fillPtrVector(std::vector< T, A > const &obj, std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr)
Definition: fillPtrVector.h:88
iterator & operator+=(difference_type d)
Definition: OwnVector.h:94
size_type size() const
Definition: OwnVector.h:248
T const & operator*() const
Definition: OwnVector.h:62
#define noexcept
iterator & operator-=(difference_type d)
Definition: OwnVector.h:95
#define P
iterator operator--(int)
Definition: OwnVector.h:83
void destroy()
Definition: OwnVector.h:359
bool operator!=(iterator const &ci) const
Definition: OwnVector.h:89
bool operator==(const_iterator const &ci) const
Definition: OwnVector.h:60
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
OwnVector< T, P > & operator=(OwnVector< T, P > const &)
Definition: OwnVector.h:212
uint16_t size_type
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
void setPtr(std::vector< T, A > const &obj, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: setPtr.h:75
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
void reallyfillPtrVector(COLLECTION const &coll, std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr)
Definition: fillPtrVector.h:38
const_iterator operator--(int)
Definition: OwnVector.h:55
iterator begin()
Definition: OwnVector.h:228
list ordering
Definition: config.py:7
bool operator<(const_iterator const &o) const
Definition: OwnVector.h:59
reference operator[](difference_type d) const
Definition: OwnVector.h:67
bool operator<(iterator const &o) const
Definition: OwnVector.h:87
void reverse()
Definition: OwnVector.h:143
iterator & operator--()
Definition: OwnVector.h:82
bool operator==(iterator const &ci) const
Definition: OwnVector.h:88
void push_back(D *&d)
Definition: OwnVector.h:274
reference operator[](difference_type d) const
Definition: OwnVector.h:96
const_iterator(typename base::const_iterator const &it)
Definition: OwnVector.h:49
bool empty() const
Definition: OwnVector.h:253
base::const_iterator i
Definition: OwnVector.h:69
tuple result
Definition: query.py:137
void clear()
Definition: OwnVector.h:371
iterator erase(iterator pos)
Definition: OwnVector.h:377
void setPtr(std::type_info const &toType, unsigned long index, void const *&ptr) const
Definition: OwnVector.h:463
ptrdiff_t difference_type
Definition: OwnVector.h:76
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void fillPtrVector(std::type_info const &toType, std::vector< unsigned long > const &indices, std::vector< void const * > &ptrs) const
Definition: OwnVector.h:482
iterator & operator++()
Definition: OwnVector.h:80
const_iterator operator++(int)
Definition: OwnVector.h:53
bool is_back_safe() const
Definition: OwnVector.h:316
iterator end()
Definition: OwnVector.h:233
const_iterator(iterator const &it)
Definition: OwnVector.h:50
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150
T const & const_reference
Definition: OwnVector.h:38
base::size_type size_type
Definition: OwnVector.h:34
double b
Definition: hdecay.h:120
string const
Definition: compareJSON.py:14
virtual void reserve(size_type n)=0
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
double S(const TLorentzVector &, const TLorentzVector &)
Definition: Particle.cc:99
#define private
Definition: FWFileEntry.h:17
base const & data() const
Definition: OwnVector.h:366
list key
Definition: combine.py:13
iterator operator++(int)
Definition: OwnVector.h:81
double a
Definition: hdecay.h:121
iterator(typename base::iterator const &it)
Definition: OwnVector.h:78
difference_type operator-(const_iterator const &o) const
Definition: OwnVector.h:56
const_iterator & operator--()
Definition: OwnVector.h:54
static Ordering< O > ordering(O const &comp)
Definition: OwnVector.h:178
virtual void push_back(RefHolderBase const *r)=0
iterator operator+(difference_type n) const
Definition: OwnVector.h:85
void pop_back()
Definition: OwnVector.h:308
const_iterator & operator-=(difference_type d)
Definition: OwnVector.h:66
T first(std::pair< T, U > const &p)
T * operator->() const
Definition: OwnVector.h:93
long double T
void swap(OwnVector< T, P > &other)
Definition: OwnVector.h:401
const_iterator operator+(difference_type n) const
Definition: OwnVector.h:57
reference front()
Definition: OwnVector.h:349
tuple size
Write out results.
bool operator!=(const_iterator const &ci) const
Definition: OwnVector.h:61
void reserve(size_t)
Definition: OwnVector.h:268
static bool const value
Definition: traits.h:124
def template
Definition: svgfig.py:520
const_iterator & operator+=(difference_type d)
Definition: OwnVector.h:65
void fillView(ProductID const &id, std::vector< void const * > &pointers, helper_vector &helpers) const
Definition: OwnVector.h:406
base::iterator::iterator_category iterator_category
Definition: OwnVector.h:77
const_iterator operator-(difference_type n) const
Definition: OwnVector.h:58