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 #include "boost/iterator/indirect_iterator.hpp"
23 
24 #include <vector>
25 #include <memory>
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 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
42  std::unique_ptr<ViewBase> clone() const;
43 #endif
44 
45  protected:
46  ViewBase();
47  ViewBase(ViewBase const&);
48  ViewBase& operator=(ViewBase const&);
49 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
50  virtual std::unique_ptr<ViewBase> doClone() const = 0;
51 #endif
52  void swap(ViewBase&) {} // Nothing to swap
53  };
54 
55  //------------------------------------------------------------------
70  //------------------------------------------------------------------
71 
72 
73  template<typename T>
74  class View : public ViewBase {
75  typedef std::vector<T const*> seq_t;
76  public:
77  typedef T const* pointer;
78  typedef T const* const_pointer;
79 
80  typedef T const& reference;
81  typedef T const& const_reference;
82 
83  typedef T value_type;
84 
85  typedef boost::indirect_iterator<typename seq_t::const_iterator> const_iterator;
86 
87  // This should be a typedef to seq_t::size_type but because this type is used as a template
88  // argument in a persistened class it must be stable for different architectures
89  typedef unsigned int size_type;
90  typedef typename seq_t::difference_type difference_type;
91 
92  typedef boost::indirect_iterator<typename seq_t::const_reverse_iterator> const_reverse_iterator;
93 
94  // Compiler-generated copy, and assignment each does the right
95  // thing.
96 
97  View();
98 
99  // This function is dangerous, and should only be called from the
100  // infrastructure code.
101  View(std::vector<void const*> const& pointers,
102  FillViewHelperVector const& helpers,
103  EDProductGetter const* getter);
104 
105  virtual ~View();
106 
107  void swap(View& other);
108 
109  View& operator=(View const& rhs);
110 
111  size_type capacity() const;
112 
113  // Most non-const member functions not present.
114  // No access to non-const contents provided.
115 
116  const_iterator begin() const;
117  const_iterator end() const;
118 
121 
122  size_type size() const;
123  size_type max_size() const;
124  bool empty() const;
125  const_reference at(size_type pos) const;
129  std::vector<Ptr<value_type> > const& ptrs() const;
130 
131  const_reference front() const;
132  const_reference back() const;
133 
134  // No erase, because erase is required to return an *iterator*,
135  // not a *const_iterator*.
136 
137  // The following is for testing only.
138  static void fill_from_range(T* first, T* last, View& output);
139 
140  private:
142  std::vector<Ptr<value_type> > vPtrs_;
143 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
144  std::unique_ptr<ViewBase> doClone() const override;
145 #endif
146  };
147 
148  // Associated free functions (same as for std::vector)
149  template<typename T> bool operator==(View<T> const&, View<T> const&);
150  template<typename T> bool operator!=(View<T> const&, View<T> const&);
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 
156  //------------------------------------------------------------------
157  // Implementation of View<T>
158  //------------------------------------------------------------------
159 
160  template<typename T>
161  inline
162  View<T>::View() :
163  items_(),
164  vPtrs_() {
165  }
166 
167 #ifndef __GCCXML__
168  template<typename T>
169  View<T>::View(std::vector<void const*> const& pointers,
170  FillViewHelperVector const& helpers,
171  EDProductGetter const* getter) :
172  items_(),
173  vPtrs_() {
174  size_type numElements = pointers.size();
175 
176  // If the two input vectors are not of the same size, there is a
177  // logic error in the framework code that called this.
178  // constructor.
179  assert(numElements == helpers.size());
180 
181  items_.reserve(numElements);
182  vPtrs_.reserve(numElements);
183  for(std::vector<void const*>::size_type i = 0; i < pointers.size(); ++i) {
184  void const* p = pointers[i];
185  auto const& h = helpers[i];
186  items_.push_back(static_cast<pointer>(p));
187  if(0!=p) {
188  vPtrs_.push_back(Ptr<T>(h.first, static_cast<T const*>(p), h.second));
189  } else if(getter != nullptr) {
190  vPtrs_.push_back(Ptr<T>(h.first, h.second, getter));
191  } else {
192  vPtrs_.push_back(Ptr<T>(h.first, nullptr, h.second));
193  }
194  }
195  }
196 #endif
197 
198  template<typename T>
199  View<T>::~View() {
200  }
201 
202  template<typename T>
203  inline
204  void
205  View<T>::swap(View& other) {
206  this->ViewBase::swap(other);
207  items_.swap(other.items_);
208  vPtrs_.swap(other.vPtrs_);
209  }
210 
211  template<typename T>
212  inline
213  typename View<T>::size_type
214  View<T>::capacity() const {
215  return items_.capacity();
216  }
217 
218  template<typename T>
219  inline
220  typename View<T>::const_iterator
221  View<T>::begin() const {
222  return items_.begin();
223  }
224 
225  template<typename T>
226  inline
227  typename View<T>::const_iterator
228  View<T>::end() const {
229  return items_.end();
230  }
231 
232  template<typename T>
233  inline
234  typename View<T>::const_reverse_iterator
235  View<T>::rbegin() const {
236  return items_.rbegin();
237  }
238 
239  template<typename T>
240  inline
241  typename View<T>::const_reverse_iterator
242  View<T>::rend() const {
243  return items_.rend();
244  }
245 
246  template<typename T>
247  inline
248  typename View<T>::size_type
249  View<T>::size() const {
250  return items_.size();
251  }
252 
253  template<typename T>
254  inline
255  typename View<T>::size_type
256  View<T>::max_size() const {
257  return items_.max_size();
258  }
259 
260  template<typename T>
261  inline
262  bool
263  View<T>::empty() const {
264  return items_.empty();
265  }
266 
267  template<typename T>
268  inline
269  typename View<T>::const_reference
270  View<T>::at(size_type pos) const {
271  return *items_.at(pos);
272  }
273 
274  template<typename T>
275  inline
276  typename View<T>::const_reference
277  View<T>::operator[](size_type pos) const {
278  return *items_[pos];
279  }
280 
281 #ifndef __GCCXML__
282  template<typename T>
283  inline
285  View<T>::refAt(size_type i) const {
286  //NOTE: considered creating a special BaseHolder for edm::Ptr.
287  // But the IndirectHolder and RefHolder would still be needed
288  // for other reasons. To reduce the number of dictionaries needed
289  // we avoid using a more efficient BaseHolder.
290  return RefToBase<T>(std::unique_ptr<reftobase::BaseHolder<T>>{
291  new reftobase::IndirectHolder<T>{
292  std::unique_ptr<reftobase::RefHolder<edm::Ptr<T>>>{
293  new reftobase::RefHolder<Ptr<T>>{ptrAt(i)}
294  }
295  }
296  } );
297  }
298 #endif
299 
300  template<typename T>
301  inline
302  Ptr<T>
303  View<T>::ptrAt(size_type i) const {
304  return vPtrs_[i];
305  }
306 
307  template<typename T>
308  inline
309  std::vector<Ptr<T> > const&
310  View<T>::ptrs() const {
311  return vPtrs_;
312  }
313 
314 
315  template<typename T>
316  inline
317  typename View<T>::const_reference
318  View<T>::front() const {
319  return *items_.front();
320  }
321 
322  template<typename T>
323  inline
324  typename View<T>::const_reference
325  View<T>::back() const {
326  return *items_.back();
327  }
328 
329  // The following is for testing only.
330  template<typename T>
331  inline
332  void
333  View<T>::fill_from_range(T* first, T* last, View& output) {
334  output.items_.resize(std::distance(first, last));
335  for(typename View<T>::size_type i = 0; first != last; ++i, ++first)
336  output.items_[i] = first;
337  }
338 
339 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
340  template<typename T>
341  std::unique_ptr<ViewBase>
342  View<T>::doClone() const {
343  return std::unique_ptr<ViewBase>{new View(*this)};
344  }
345 #endif
346 
347  template<typename T>
348  inline
349  View<T>&
350  View<T>::operator=(View<T> const& rhs) {
351  View<T> temp(rhs);
352  this->swap(temp);
353  return *this;
354  }
355 
356  template<typename T>
357  inline
358  bool
359  operator==(View<T> const& lhs, View<T> const& rhs) {
360  return
361  lhs.size() == rhs.size() &&
362  std::equal(lhs.begin(), lhs.end(), rhs.begin());
363  }
364 
365  template<typename T>
366  inline
367  bool
368  operator!=(View<T> const& lhs, View<T> const& rhs) {
369  return !(lhs == rhs);
370  }
371 
372  template<typename T>
373  inline
374  bool
375  operator<(View<T> const& lhs, View<T> const& rhs) {
376  return std::lexicographical_compare(lhs.begin(), lhs.end(),
377  rhs.begin(), rhs.end());
378  }
379 
380  template<typename T>
381  inline
382  bool
383  operator<=(View<T> const& lhs, View<T> const& rhs) {
384  return !(rhs<lhs);
385  }
386 
387  template<typename T>
388  inline
389  bool operator> (View<T> const& lhs, View<T> const& rhs) {
390  return rhs<lhs;
391  }
392 
393  template<typename T>
394  inline
395  bool operator>=(View<T> const& lhs, View<T> const& rhs) {
396  return !(lhs<rhs);
397  }
398 
399  // Free swap function
400  template<typename T>
401  inline
402  void swap(View<T>& lhs, View<T>& rhs) {
403  lhs.swap(rhs);
404  }
405 }
406 
407 #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:89
int i
Definition: DBlmapReader.cc:9
Ptr< value_type > ptrAt(size_type i) const
const_reference back() const
assert(m_qm.get())
std::vector< T const * > seq_t
Definition: View.h:75
T const * const_pointer
Definition: View.h:78
bool operator>(l1t::Jet &a, l1t::Jet &b)
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:90
virtual std::unique_ptr< ViewBase > doClone() const =0
seq_t items_
Definition: View.h:141
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:83
bool operator>=(View< T > const &, View< T > const &)
Definition: View.h:395
const_iterator begin() const
View & operator=(View const &rhs)
std::vector< Ptr< value_type > > vPtrs_
Definition: View.h:142
virtual ~View()
void swap(ViewBase &)
Definition: View.h:52
std::unique_ptr< ViewBase > clone() const
Definition: View.cc:13
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:80
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
bool empty() const
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:389
T operator[](int i) const
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
#define end
Definition: vmac.h:37
const_reverse_iterator rend() const
ViewBase()
Definition: View.cc:20
virtual ~ViewBase()
Definition: View.cc:10
size_type capacity() const
ViewBase & operator=(ViewBase const &)
std::vector< Ptr< value_type > > const & ptrs() const
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:92
#define begin
Definition: vmac.h:30
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:85
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:81
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
T const * pointer
Definition: View.h:77
tuple size
Write out results.
list at
Definition: asciidump.py:428
std::unique_ptr< ViewBase > doClone() const override