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  RefToBaseVector<T> const& refVector() const { return refs_; }
125  PtrVector<T> const& ptrVector() const { return ptrs_; }
126 
127  const_reference front() const;
128  const_reference back() const;
129  void pop_back();
130  ProductID id() const;
131  EDProductGetter const* productGetter() const;
132 
133  // No erase, because erase is required to return an *iterator*,
134  // not a *const_iterator*.
135 
136  // The following is for testing only.
137  static void fill_from_range(T* first, T* last, View& output);
138 
139  void const* product() const {
140  return refs_.product();
141  }
142 
143  private:
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  typename View<T>::const_reference
304  View<T>::front() const {
305  return *items_.front();
306  }
307 
308  template<typename T>
309  inline
310  typename View<T>::const_reference
311  View<T>::back() const {
312  return *items_.back();
313  }
314 
315  template<typename T>
316  inline
317  void
318  View<T>::pop_back() {
319  items_.pop_back();
320  }
321 
322  template<typename T>
323  inline
324  ProductID
325  View<T>::id() const {
326  return refs_.id();
327  }
328  template<typename T>
329  inline
330  EDProductGetter const*
331  View<T>::productGetter() const {
332  return refs_.productGetter();
333  }
334 
335  // The following is for testing only.
336  template<typename T>
337  inline
338  void
339  View<T>::fill_from_range(T* first, T* last, View& output) {
340  output.items_.resize(std::distance(first, last));
341  for(typename View<T>::size_type i = 0; first != last; ++i, ++first)
342  output.items_[i] = first;
343  }
344 
345  template<typename T>
346  ViewBase*
347  View<T>::doClone() const {
348  return new View(*this);
349  }
350 
351  template<typename T>
352  inline
353  View<T>&
354  View<T>::operator=(View<T> const& rhs) {
355  View<T> temp(rhs);
356  this->swap(temp);
357  return *this;
358  }
359 
360  template<typename T>
361  inline
362  bool
363  operator==(View<T> const& lhs, View<T> const& rhs) {
364  return
365  lhs.size() == rhs.size() &&
366  std::equal(lhs.begin(), lhs.end(), rhs.begin());
367  }
368 
369  template<typename T>
370  inline
371  bool
372  operator!=(View<T> const& lhs, View<T> const& rhs) {
373  return !(lhs == rhs);
374  }
375 
376  template<typename T>
377  inline
378  bool
379  operator<(View<T> const& lhs, View<T> const& rhs) {
380  return std::lexicographical_compare(lhs.begin(), lhs.end(),
381  rhs.begin(), rhs.end());
382  }
383 
384  template<typename T>
385  inline
386  bool
387  operator<=(View<T> const& lhs, View<T> const& rhs) {
388  return !(rhs<lhs);
389  }
390 
391  template<typename T>
392  inline
393  bool operator> (View<T> const& lhs, View<T> const& rhs) {
394  return rhs<lhs;
395  }
396 
397  template<typename T>
398  inline
399  bool operator>=(View<T> const& lhs, View<T> const& rhs) {
400  return !(lhs<rhs);
401  }
402 
403  // Free swap function
404  template<typename T>
405  inline
406  void swap(View<T>& lhs, View<T>& rhs) {
407  lhs.swap(rhs);
408  }
409 }
410 
411 #endif
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
T value_type
Definition: View.h:79
int i
Definition: DBlmapReader.cc:9
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
virtual ~View()
boost::shared_ptr< reftobase::RefVectorHolderBase > helper_vector_ptr
Definition: EDProductfwd.h:46
const_reference front() const
const_reverse_iterator rbegin() const
virtual ViewBase * doClone() const =0
bool empty() const
View & operator=(View const &rhs)
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
size_type capacity() const
ProductID id() const
bool operator==(const CaloTower &t1, const CaloTower &t2)
Definition: CaloTower.h:209
const_reverse_iterator rend() const
RefToBase< value_type > refAt(size_type i) const
T const * pointer
Definition: View.h:73
T const & const_reference
Definition: View.h:77
uint16_t size_type
ViewBase * doClone() const
seq_t::difference_type difference_type
Definition: View.h:86
bool operator>=(View< T > const &, View< T > const &)
Definition: View.h:399
Ptr< value_type > ptrAt(size_type i) const
RefToBaseVector< T > refs_
Definition: View.h:145
static void fill_from_range(T *first, T *last, View &output)
size_type max_size() const
void swap(ViewBase &)
Definition: View.h:48
T const * const_pointer
Definition: View.h:74
boost::indirect_iterator< typename seq_t::const_reverse_iterator > const_reverse_iterator
Definition: View.h:88
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:393
unsigned int size_type
Definition: View.h:85
void const * product() const
Definition: View.h:139
T operator[](int i) const
#define end
Definition: vmac.h:37
void swap(View &other)
bool first
Definition: L1TdeRCT.cc:79
ViewBase()
Definition: View.cc:20
const_reference back() const
virtual ~ViewBase()
Definition: View.cc:10
ViewBase & operator=(ViewBase const &)
PtrVector< T > ptrs_
Definition: View.h:146
ViewBase * clone() const
Definition: View.cc:13
const_reference operator[](size_type pos) const
size_type size() const
string const
Definition: compareJSON.py:14
const_reference at(size_type pos) const
#define begin
Definition: vmac.h:30
std::vector< T const * > seq_t
Definition: View.h:71
const_iterator begin() const
const_iterator end() const
PtrVector< T > const & ptrVector() const
Definition: View.h:125
T first(std::pair< T, U > const &p)
long double T
void pop_back()
EDProductGetter const * productGetter() const
tuple size
Write out results.
seq_t items_
Definition: View.h:144
RefToBaseVector< T > const & refVector() const
Definition: View.h:124
list at
Definition: asciidump.py:428
T const & reference
Definition: View.h:76