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  template<typename S>
144  void sort(S s);
145  void sort();
146 
147  void swap(OwnVector<T, P>& other) noexcept;
148 
149  void fillView(ProductID const& id,
150  std::vector<void const*>& pointers,
151  helper_vector& helpers) const;
152 
153  void setPtr(std::type_info const& toType,
154  unsigned long index,
155  void const*& ptr) const;
156 
157  void fillPtrVector(std::type_info const& toType,
158  std::vector<unsigned long> const& indices,
159  std::vector<void const*>& ptrs) const;
160 
161 
162  //Used by ROOT storage
164 
165  private:
166  void destroy() noexcept;
167  template<typename O>
168  struct Ordering {
169  Ordering(O const& c) : comp(c) { }
170  bool operator()(T const* t1, T const* t2) const {
171  return comp(*t1, *t2);
172  }
173  private:
174  O comp;
175  };
176  template<typename O>
177  static Ordering<O> ordering(O const& comp) {
178  return Ordering<O>(comp);
179  }
181  };
182 
183  template<typename T, typename P>
184  inline OwnVector<T, P>::OwnVector() : data_() {
185  }
186 
187  template<typename T, typename P>
188  inline OwnVector<T, P>::OwnVector(size_type n) : data_(n) {
189  }
190 
191  template<typename T, typename P>
192  inline OwnVector<T, P>::OwnVector(OwnVector<T, P> const& o) : data_(o.size()) {
193  size_type current = 0;
194  for (const_iterator i = o.begin(), e = o.end(); i != e; ++i,++current)
195  data_[current] = policy_type::clone(*i);
196  }
197 
198 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
199  template<typename T, typename P>
200  inline OwnVector<T, P>::OwnVector(OwnVector<T, P>&& o) noexcept{
201  data_.swap(o.data_);
202  }
203 #endif
204 
205  template<typename T, typename P>
206  inline OwnVector<T, P>::~OwnVector() noexcept {
207  destroy();
208  }
209 
210  template<typename T, typename P>
212  OwnVector<T,P> temp(o);
213  swap(temp);
214  return *this;
215  }
216 
217 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
218  template<typename T, typename P>
220  data_.swap(o.data_);
221  return *this;
222  }
223 #endif
224 
225 
226  template<typename T, typename P>
228  return iterator(data_.begin());
229  }
230 
231  template<typename T, typename P>
233  return iterator(data_.end());
234  }
235 
236  template<typename T, typename P>
238  return const_iterator(data_.begin());
239  }
240 
241  template<typename T, typename P>
243  return const_iterator(data_.end());
244  }
245 
246  template<typename T, typename P>
248  return data_.size();
249  }
250 
251  template<typename T, typename P>
252  inline bool OwnVector<T, P>::empty() const {
253  return data_.empty();
254  }
255 
256  template<typename T, typename P>
258  return *data_[n];
259  }
260 
261  template<typename T, typename P>
263  return *data_[n];
264  }
265 
266  template<typename T, typename P>
267  inline void OwnVector<T, P>::reserve(size_t n) {
268  data_.reserve(n);
269  }
270 
271  template<typename T, typename P>
272  template<typename D>
273  inline void OwnVector<T, P>::push_back(D*& d) {
274  // C++ does not yet support rvalue references, so d should only be
275  // able to bind to an lvalue.
276  // This should be called only for lvalues.
277  data_.push_back(d);
278  d = 0;
279  }
280 
281  template<typename T, typename P>
282  template<typename D>
283  inline void OwnVector<T, P>::push_back(D* const& d) {
284 
285  // C++ allows d to be bound to an lvalue or rvalue. But the other
286  // signature should be a better match for an lvalue (because it
287  // does not require an lvalue->rvalue conversion). Thus this
288  // signature should only be chosen for rvalues.
289  data_.push_back(d);
290  }
291 
292 
293  template<typename T, typename P>
294  template<typename D>
295  inline void OwnVector<T, P>::push_back(std::auto_ptr<D> d) {
296  data_.push_back(d.release());
297  }
298 
299 
300  template<typename T, typename P>
301  inline void OwnVector<T, P>::push_back(T const& d) {
302  data_.push_back(policy_type::clone(d));
303  }
304 
305 
306  template<typename T, typename P>
308  // We have to delete the pointed-to thing, before we squeeze it
309  // out of the vector...
310  delete data_.back();
311  data_.pop_back();
312  }
313 
314  template <typename T, typename P>
315  inline bool OwnVector<T, P>::is_back_safe() const {
316  return data_.back() != 0;
317  }
318 
319  template<typename T, typename P>
321  T* result = data_.back();
322  if (result == 0) {
324  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
325  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
326  "pointer at the end of the collection is not null before calling back()\n"
327  "if you wish to avoid this exception.\n"
328  "Consider using OwnVector::is_back_safe()\n");
329  }
330  return *data_.back();
331  }
332 
333  template<typename T, typename P>
335  T* result = data_.back();
336  if (result == 0) {
338  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
339  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
340  "pointer at the end of the collection is not null before calling back()\n"
341  "if you wish to avoid this exception.\n"
342  "Consider using OwnVector::is_back_safe()\n");
343  }
344  return *data_.back();
345  }
346 
347  template<typename T, typename P>
349  return *data_.front();
350  }
351 
352  template<typename T, typename P>
354  return *data_.front();
355  }
356 
357  template<typename T, typename P>
358  inline void OwnVector<T, P>::destroy() noexcept {
359  typename base::const_iterator b = data_.begin(), e = data_.end();
360  for(typename base::const_iterator i = b; i != e; ++ i)
361  delete * i;
362  }
363 
364  template<typename T, typename P>
365  inline typename OwnVector<T, P>::base const& OwnVector<T, P>::data() const {
366  return data_;
367  }
368 
369  template<typename T, typename P>
370  inline void OwnVector<T, P>::clear() {
371  destroy();
372  data_.clear();
373  }
374 
375  template<typename T, typename P>
377  delete * pos.i;
378  return iterator(data_.erase(pos.i));
379  }
380 
381  template<typename T, typename P>
383  typename base::iterator b = first.i, e = last.i;
384  for(typename base::iterator i = b; i != e; ++ i)
385  delete * i;
386  return iterator(data_.erase(b, e));
387  }
388 
389  template<typename T, typename P> template<typename S>
391  std::sort(data_.begin(), data_.end(), ordering(comp));
392  }
393 
394  template<typename T, typename P>
396  std::sort(data_.begin(), data_.end(), ordering(std::less<value_type>()));
397  }
398 
399  template<typename T, typename P>
400  inline void OwnVector<T, P>::swap(OwnVector<T, P>& other) noexcept {
401  data_.swap(other.data_);
402  }
403 
404  template<typename T, typename P>
406  std::vector<void const*>& pointers,
407  helper_vector& helpers) const {
408  typedef Ref<OwnVector> ref_type ;
409  typedef reftobase::RefHolder<ref_type> holder_type;
410 
411  size_type numElements = this->size();
412  pointers.reserve(numElements);
413  helpers.reserve(numElements);
414  size_type key = 0;
415  for(typename base::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i, ++key) {
416 
417  if (*i == 0) {
419  "In OwnVector::fillView() we have intercepted an attempt to put a null pointer\n"
420  "into a View and that is not allowed. It is probably an error that the null\n"
421  "pointer was in the OwnVector in the first place.\n");
422  }
423  else {
424  pointers.push_back(*i);
425  holder_type h(ref_type(id, *i, key,this));
426  helpers.push_back(&h);
427  }
428  }
429  }
430 
431  template<typename T, typename P>
432  inline void swap(OwnVector<T, P>& a, OwnVector<T, P>& b) noexcept {
433  a.swap(b);
434  }
435 
436  //----------------------------------------------------------------------
437  //
438  // Free function template to support creation of Views.
439 
440  template <typename T, typename P>
441  inline
442  void
444  ProductID const& id,
445  std::vector<void const*>& pointers,
446  helper_vector& helpers) {
447  obj.fillView(id, pointers, helpers);
448  }
449 
450 
451  template <typename T, typename P>
452  struct has_fillView<edm::OwnVector<T, P> > {
453  static bool const value = true;
454  };
455 
456 
457  // Free function templates to support the use of edm::Ptr.
458 
459  template <typename T, typename P>
460  inline
461  void
462  OwnVector<T,P>::setPtr(std::type_info const& toType,
463  unsigned long index,
464  void const*& ptr) const {
465  detail::reallySetPtr<OwnVector<T,P> >(*this, toType, index, ptr);
466  }
467 
468  template <typename T, typename P>
469  inline
470  void
472  std::type_info const& toType,
473  unsigned long index,
474  void const*& ptr) {
475  obj.setPtr(toType, index, ptr);
476  }
477 
478  template <typename T, typename P>
479  inline
480  void
481  OwnVector<T,P>::fillPtrVector(std::type_info const& toType,
482  std::vector<unsigned long> const& indices,
483  std::vector<void const*>& ptrs) const {
484  detail::reallyfillPtrVector(*this, toType, indices, ptrs);
485  }
486 
487 
488  template <typename T, typename P>
489  inline
490  void
492  std::type_info const& toType,
493  std::vector<unsigned long> const& indices,
494  std::vector<void const*>& ptrs) {
495  obj.fillPtrVector(toType, indices, ptrs);
496  }
497 
498 
499  template <typename T, typename P>
500  struct has_setPtr<edm::OwnVector<T,P> > {
501  static bool const value = true;
502  };
503 
504 
505 }
506 
507 
508 #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:320
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
~OwnVector() noexcept
Definition: OwnVector.h:206
reference operator[](size_type)
Definition: OwnVector.h:257
bool operator()(T const *t1, T const *t2) const
Definition: OwnVector.h:170
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:247
T const & operator*() const
Definition: OwnVector.h:62
iterator & operator-=(difference_type d)
Definition: OwnVector.h:95
#define P
iterator operator--(int)
Definition: OwnVector.h:83
bool operator!=(iterator const &ci) const
Definition: OwnVector.h:89
bool operator==(const_iterator const &ci) const
Definition: OwnVector.h:60
void swap(OwnVector< T, P > &other) noexcept
Definition: OwnVector.h:400
#define CMS_CLASS_VERSION(_version_)
OwnVector< T, P > & operator=(OwnVector< T, P > const &)
Definition: OwnVector.h:211
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:227
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
iterator & operator--()
Definition: OwnVector.h:82
bool operator==(iterator const &ci) const
Definition: OwnVector.h:88
void push_back(D *&d)
Definition: OwnVector.h:273
reference operator[](difference_type d) const
Definition: OwnVector.h:96
auto const T2 &decltype(t1.eta()) t2
Definition: deltaR.h:18
const_iterator(typename base::const_iterator const &it)
Definition: OwnVector.h:49
bool empty() const
Definition: OwnVector.h:252
base::const_iterator i
Definition: OwnVector.h:69
tuple result
Definition: query.py:137
void clear()
Definition: OwnVector.h:370
iterator erase(iterator pos)
Definition: OwnVector.h:376
void setPtr(std::type_info const &toType, unsigned long index, void const *&ptr) const
Definition: OwnVector.h:462
ptrdiff_t difference_type
Definition: OwnVector.h:76
void fillPtrVector(std::type_info const &toType, std::vector< unsigned long > const &indices, std::vector< void const * > &ptrs) const
Definition: OwnVector.h:481
iterator & operator++()
Definition: OwnVector.h:80
const_iterator operator++(int)
Definition: OwnVector.h:53
bool is_back_safe() const
Definition: OwnVector.h:315
#define noexcept
iterator end()
Definition: OwnVector.h:232
const_iterator(iterator const &it)
Definition: OwnVector.h:50
void destroy() noexcept
Definition: OwnVector.h:358
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
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
#define private
Definition: FWFileEntry.h:17
base const & data() const
Definition: OwnVector.h:365
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
tuple clone
Definition: statics.py:58
const_iterator & operator--()
Definition: OwnVector.h:54
static Ordering< O > ordering(O const &comp)
Definition: OwnVector.h:177
virtual void push_back(RefHolderBase const *r)=0
iterator operator+(difference_type n) const
Definition: OwnVector.h:85
void pop_back()
Definition: OwnVector.h:307
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
const_iterator operator+(difference_type n) const
Definition: OwnVector.h:57
reference front()
Definition: OwnVector.h:348
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150
tuple size
Write out results.
bool operator!=(const_iterator const &ci) const
Definition: OwnVector.h:61
void reserve(size_t)
Definition: OwnVector.h:267
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:405
base::iterator::iterator_category iterator_category
Definition: OwnVector.h:77
const_iterator operator-(difference_type n) const
Definition: OwnVector.h:58