CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
View.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_View_h
2 #define DataFormats_Common_View_h
3 // -*- C++ -*-
4 //
5 // Package: Framework
6 // Class : View
7 //
13 //
14 // Original Author:
15 // Created: Mon Dec 18 09:48:30 CST 2006
16 //
17 
22 
23 #include "boost/iterator/indirect_iterator.hpp"
24 
25 #include <vector>
26 
27 namespace edm {
28 
29  //------------------------------------------------------------------
30  // Class ViewBase
31  //
32  // ViewBase is an abstract base class. It exists only so that we
33  // make invoke View<T> destructors polymorphically, and copy them
34  // using clone().
35  //
36  //------------------------------------------------------------------
37 
38  class ViewBase {
39  public:
40  virtual ~ViewBase();
41  ViewBase* clone() const;
42 
43  protected:
44  ViewBase();
45  ViewBase(ViewBase const&);
46  ViewBase& operator=(ViewBase const&);
47  virtual ViewBase* doClone() const = 0;
48  void swap(ViewBase&) {} // Nothing to swap
49  };
50 
51  //------------------------------------------------------------------
66  //------------------------------------------------------------------
67 
68 
69  template<typename T>
70  class View : public ViewBase {
71  typedef std::vector<T const*> seq_t;
72  public:
73  typedef T const* pointer;
74  typedef T const* const_pointer;
75 
76  typedef T const& reference;
77  typedef T const& const_reference;
78 
79  typedef T value_type;
80 
81  typedef boost::indirect_iterator<typename seq_t::const_iterator> const_iterator;
82 
83  // This should be a typedef to seq_t::size_type but because this type is used as a template
84  // argument in a persistened class it must be stable for different architectures
85  typedef unsigned int size_type;
86  typedef typename seq_t::difference_type difference_type;
87 
88  typedef boost::indirect_iterator<typename seq_t::const_reverse_iterator> const_reverse_iterator;
89 
90  // Compiler-generated copy, and assignment each does the right
91  // thing.
92 
93  View();
94 
95  // This function is dangerous, and should only be called from the
96  // infrastructure code.
97  View(std::vector<void const*> const& pointers,
98  helper_vector_ptr const& helpers);
99 
100  virtual ~View();
101 
102  void swap(View& other);
103 
104  View& operator=(View const& rhs);
105 
106  size_type capacity() const;
107 
108  // Most non-const member functions not present.
109  // No access to non-const contents provided.
110 
111  const_iterator begin() const;
112  const_iterator end() const;
113 
116 
117  size_type size() const;
118  size_type max_size() const;
119  bool empty() const;
120  const_reference at(size_type pos) const;
124  std::vector<Ptr<value_type> > const& ptrs() const;
125 
126  const_reference front() const;
127  const_reference back() const;
128  void pop_back();
129  ProductID id() const;
130  EDProductGetter const* productGetter() const;
131 
132  // No erase, because erase is required to return an *iterator*,
133  // not a *const_iterator*.
134 
135  // The following is for testing only.
136  static void fill_from_range(T* first, T* last, View& output);
137 
138  void const* product() const {
139  return refs_.product();
140  }
141 
142  private:
146  mutable std::vector<Ptr<value_type> > vPtrs_;
147  ViewBase* doClone() const;
148  };
149 
150  // Associated free functions (same as for std::vector)
151  template<typename T> bool operator==(View<T> const&, View<T> const&);
152  template<typename T> bool operator!=(View<T> const&, View<T> const&);
153  template<typename T> bool operator< (View<T> const&, View<T> const&);
154  template<typename T> bool operator<=(View<T> const&, View<T> const&);
155  template<typename T> bool operator> (View<T> const&, View<T> const&);
156  template<typename T> bool operator>=(View<T> const&, View<T> const&);
157 
158  //------------------------------------------------------------------
159  // Implementation of View<T>
160  //------------------------------------------------------------------
161 
162  template<typename T>
163  inline
164  View<T>::View() :
165  items_(),
166  refs_(),
167  ptrs_() {
168  }
169 
170  template<typename T>
171  View<T>::View(std::vector<void const*> const& pointers,
172  helper_vector_ptr const& helpers) :
173  items_(),
174  refs_(),
175  ptrs_() {
176  size_type numElements = pointers.size();
177 
178  // If the two input vectors are not of the same size, there is a
179  // logic error in the framework code that called this.
180  // constructor.
181  if(helpers.get() != 0) {
182  assert(numElements == helpers->size());
183 
184  items_.reserve(numElements);
185  ptrs_.reserve(refs_.size());
186  for(std::vector<void const*>::size_type i = 0; i < pointers.size(); ++i) {
187  void const* p = pointers[i];
188  items_.push_back(static_cast<pointer>(p));
189  if(0!=p) {
190  ptrs_.push_back(Ptr<T>(helpers->id(), static_cast<T const*>(p), helpers->keyForIndex(i)));
191  } else if(helpers->productGetter() != 0) {
192  ptrs_.push_back(Ptr<T>(helpers->id(), helpers->keyForIndex(i), helpers->productGetter()));
193  } else {
194  ptrs_.push_back(Ptr<T>(helpers->id(), 0, helpers->keyForIndex(i)));
195  }
196  }
197  RefToBaseVector<T> temp(helpers);
198  refs_.swap(temp);
199  }
200  }
201 
202  template<typename T>
203  View<T>::~View() {
204  }
205 
206  template<typename T>
207  inline
208  void
209  View<T>::swap(View& other) {
210  this->ViewBase::swap(other);
211  items_.swap(other.items_);
212  refs_.swap(other.refs_);
213  ptrs_.swap(other.ptrs_);
214  }
215 
216  template<typename T>
217  inline
218  typename View<T>::size_type
219  View<T>::capacity() const {
220  return items_.capacity();
221  }
222 
223  template<typename T>
224  inline
225  typename View<T>::const_iterator
226  View<T>::begin() const {
227  return items_.begin();
228  }
229 
230  template<typename T>
231  inline
232  typename View<T>::const_iterator
233  View<T>::end() const {
234  return items_.end();
235  }
236 
237  template<typename T>
238  inline
239  typename View<T>::const_reverse_iterator
240  View<T>::rbegin() const {
241  return items_.rbegin();
242  }
243 
244  template<typename T>
245  inline
246  typename View<T>::const_reverse_iterator
247  View<T>::rend() const {
248  return items_.rend();
249  }
250 
251  template<typename T>
252  inline
253  typename View<T>::size_type
254  View<T>::size() const {
255  return items_.size();
256  }
257 
258  template<typename T>
259  inline
260  typename View<T>::size_type
261  View<T>::max_size() const {
262  return items_.max_size();
263  }
264 
265  template<typename T>
266  inline
267  bool
268  View<T>::empty() const {
269  return items_.empty();
270  }
271 
272  template<typename T>
273  inline
274  typename View<T>::const_reference
275  View<T>::at(size_type pos) const {
276  return *items_.at(pos);
277  }
278 
279  template<typename T>
280  inline
281  typename View<T>::const_reference
282  View<T>::operator[](size_type pos) const {
283  return *items_[pos];
284  }
285 
286  template<typename T>
287  inline
289  View<T>::refAt(size_type i) const {
290  return refs_[i];
291  }
292 
293  template<typename T>
294  inline
295  Ptr<T>
296  View<T>::ptrAt(size_type i) const {
297  RefToBase<T> ref = refAt(i);
298  return Ptr<T>(ref.id(), (ref.isAvailable() ? ref.get(): 0), ref.key());
299  }
300 
301  template<typename T>
302  inline
303  std::vector<Ptr<T> > const&
304  View<T>::ptrs() const {
305  //vPtrs_.insert(ptrs_.begin(),ptrs_.end());
306  if(vPtrs_.size()!=ptrs_.size()) {
307  vPtrs_.reserve(ptrs_.size());
308  std::copy(ptrs_.begin(),ptrs_.end(),std::back_inserter(vPtrs_));
309  }
310  return vPtrs_;
311  }
312 
313 
314  template<typename T>
315  inline
316  typename View<T>::const_reference
317  View<T>::front() const {
318  return *items_.front();
319  }
320 
321  template<typename T>
322  inline
323  typename View<T>::const_reference
324  View<T>::back() const {
325  return *items_.back();
326  }
327 
328  template<typename T>
329  inline
330  void
331  View<T>::pop_back() {
332  items_.pop_back();
333  }
334 
335  template<typename T>
336  inline
337  ProductID
338  View<T>::id() const {
339  return refs_.id();
340  }
341  template<typename T>
342  inline
343  EDProductGetter const*
344  View<T>::productGetter() const {
345  return refs_.productGetter();
346  }
347 
348  // The following is for testing only.
349  template<typename T>
350  inline
351  void
352  View<T>::fill_from_range(T* first, T* last, View& output) {
353  output.items_.resize(std::distance(first, last));
354  for(typename View<T>::size_type i = 0; first != last; ++i, ++first)
355  output.items_[i] = first;
356  }
357 
358  template<typename T>
359  ViewBase*
360  View<T>::doClone() const {
361  return new View(*this);
362  }
363 
364  template<typename T>
365  inline
366  View<T>&
367  View<T>::operator=(View<T> const& rhs) {
368  View<T> temp(rhs);
369  this->swap(temp);
370  return *this;
371  }
372 
373  template<typename T>
374  inline
375  bool
376  operator==(View<T> const& lhs, View<T> const& rhs) {
377  return
378  lhs.size() == rhs.size() &&
379  std::equal(lhs.begin(), lhs.end(), rhs.begin());
380  }
381 
382  template<typename T>
383  inline
384  bool
385  operator!=(View<T> const& lhs, View<T> const& rhs) {
386  return !(lhs == rhs);
387  }
388 
389  template<typename T>
390  inline
391  bool
392  operator<(View<T> const& lhs, View<T> const& rhs) {
393  return std::lexicographical_compare(lhs.begin(), lhs.end(),
394  rhs.begin(), rhs.end());
395  }
396 
397  template<typename T>
398  inline
399  bool
400  operator<=(View<T> const& lhs, View<T> const& rhs) {
401  return !(rhs<lhs);
402  }
403 
404  template<typename T>
405  inline
406  bool operator> (View<T> const& lhs, View<T> const& rhs) {
407  return rhs<lhs;
408  }
409 
410  template<typename T>
411  inline
412  bool operator>=(View<T> const& lhs, View<T> const& rhs) {
413  return !(lhs<rhs);
414  }
415 
416  // Free swap function
417  template<typename T>
418  inline
419  void swap(View<T>& lhs, View<T>& rhs) {
420  lhs.swap(rhs);
421  }
422 }
423 
424 #endif
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
const_reverse_iterator rbegin() const
unsigned int size_type
Definition: View.h:85
int i
Definition: DBlmapReader.cc:9
EDProductGetter const * productGetter() const
RefToBaseVector< T > refs_
Definition: View.h:144
virtual ViewBase * doClone() const =0
Ptr< value_type > ptrAt(size_type i) const
const_reference back() const
std::vector< T const * > seq_t
Definition: View.h:71
T const * const_pointer
Definition: View.h:74
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
size_type size() const
size_type max_size() const
uint16_t size_type
seq_t::difference_type difference_type
Definition: View.h:86
seq_t items_
Definition: View.h:143
void pop_back()
bool equal(const T &first, const T &second)
Definition: Equal.h:34
RefToBase< value_type > refAt(size_type i) const
T value_type
Definition: View.h:79
bool operator>=(View< T > const &, View< T > const &)
Definition: View.h:412
const_iterator begin() const
View & operator=(View const &rhs)
std::vector< Ptr< value_type > > vPtrs_
Definition: View.h:146
virtual ~View()
void swap(ViewBase &)
Definition: View.h:48
const_reference operator[](size_type pos) const
void swap(View &other)
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
T const & reference
Definition: View.h:76
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
bool empty() const
bool operator>(l1t::Jet &a, l1t::Jet &b)
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:406
T operator[](int i) const
#define end
Definition: vmac.h:37
const_reverse_iterator rend() const
bool first
Definition: L1TdeRCT.cc:75
ViewBase()
Definition: View.cc:20
PtrVector< T > ptrs_
Definition: View.h:145
virtual ~ViewBase()
Definition: View.cc:10
size_type capacity() const
ViewBase & operator=(ViewBase const &)
std::shared_ptr< reftobase::RefVectorHolderBase > helper_vector_ptr
Definition: EDProductfwd.h:47
std::vector< Ptr< value_type > > const & ptrs() const
ViewBase * clone() const
Definition: View.cc:13
string const
Definition: compareJSON.py:14
static void fill_from_range(T *first, T *last, View &output)
boost::indirect_iterator< typename seq_t::const_reverse_iterator > const_reverse_iterator
Definition: View.h:88
#define begin
Definition: vmac.h:30
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
void const * product() const
Definition: View.h:138
ProductID id() const
const_reference at(size_type pos) const
const_iterator end() const
const_reference front() const
T first(std::pair< T, U > const &p)
long double T
T const & const_reference
Definition: View.h:77
T const * pointer
Definition: View.h:73
tuple size
Write out results.
ViewBase * doClone() const
list at
Definition: asciidump.py:428