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  std::unique_ptr<ViewBase> clone() const;
42 
43  protected:
44  ViewBase();
45  ViewBase(ViewBase const&);
46  ViewBase& operator=(ViewBase const&);
47  virtual std::unique_ptr<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  FillViewHelperVector const& helpers,
99  EDProductGetter const* getter);
100 
101  virtual ~View();
102 
103  void swap(View& other);
104 
105  View& operator=(View const& rhs);
106 
107  size_type capacity() const;
108 
109  // Most non-const member functions not present.
110  // No access to non-const contents provided.
111 
112  const_iterator begin() const;
113  const_iterator end() const;
114 
117 
118  size_type size() const;
119  size_type max_size() const;
120  bool empty() const;
121  const_reference at(size_type pos) const;
125  std::vector<Ptr<value_type> > const& ptrs() const;
126 
127  const_reference front() const;
128  const_reference back() const;
129 
130  // No erase, because erase is required to return an *iterator*,
131  // not a *const_iterator*.
132 
133  // The following is for testing only.
134  static void fill_from_range(T* first, T* last, View& output);
135 
136  private:
138  std::vector<Ptr<value_type> > vPtrs_;
139  std::unique_ptr<ViewBase> doClone() const override;
140  };
141 
142  // Associated free functions (same as for std::vector)
143  template<typename T> bool operator==(View<T> const&, View<T> const&);
144  template<typename T> bool operator!=(View<T> const&, View<T> const&);
145  template<typename T> bool operator< (View<T> const&, View<T> const&);
146  template<typename T> bool operator<=(View<T> const&, View<T> const&);
147  template<typename T> bool operator> (View<T> const&, View<T> const&);
148  template<typename T> bool operator>=(View<T> const&, View<T> const&);
149 
150  //------------------------------------------------------------------
151  // Implementation of View<T>
152  //------------------------------------------------------------------
153 
154  template<typename T>
155  inline
156  View<T>::View() :
157  items_(),
158  vPtrs_() {
159  }
160 
161  template<typename T>
162  View<T>::View(std::vector<void const*> const& pointers,
163  FillViewHelperVector const& helpers,
164  EDProductGetter const* getter) :
165  items_(),
166  vPtrs_() {
167  size_type numElements = pointers.size();
168 
169  // If the two input vectors are not of the same size, there is a
170  // logic error in the framework code that called this.
171  // constructor.
172  assert(numElements == helpers.size());
173 
174  items_.reserve(numElements);
175  vPtrs_.reserve(numElements);
176  for(std::vector<void const*>::size_type i = 0; i < pointers.size(); ++i) {
177  void const* p = pointers[i];
178  auto const& h = helpers[i];
179  items_.push_back(static_cast<pointer>(p));
180  if(0!=p) {
181  vPtrs_.push_back(Ptr<T>(h.first, static_cast<T const*>(p), h.second));
182  } else if(getter != nullptr) {
183  vPtrs_.push_back(Ptr<T>(h.first, h.second, getter));
184  } else {
185  vPtrs_.push_back(Ptr<T>(h.first, nullptr, h.second));
186  }
187  }
188  }
189 
190  template<typename T>
191  View<T>::~View() {
192  }
193 
194  template<typename T>
195  inline
196  void
197  View<T>::swap(View& other) {
198  this->ViewBase::swap(other);
199  items_.swap(other.items_);
200  vPtrs_.swap(other.vPtrs_);
201  }
202 
203  template<typename T>
204  inline
205  typename View<T>::size_type
206  View<T>::capacity() const {
207  return items_.capacity();
208  }
209 
210  template<typename T>
211  inline
212  typename View<T>::const_iterator
213  View<T>::begin() const {
214  return items_.begin();
215  }
216 
217  template<typename T>
218  inline
219  typename View<T>::const_iterator
220  View<T>::end() const {
221  return items_.end();
222  }
223 
224  template<typename T>
225  inline
226  typename View<T>::const_reverse_iterator
227  View<T>::rbegin() const {
228  return items_.rbegin();
229  }
230 
231  template<typename T>
232  inline
233  typename View<T>::const_reverse_iterator
234  View<T>::rend() const {
235  return items_.rend();
236  }
237 
238  template<typename T>
239  inline
240  typename View<T>::size_type
241  View<T>::size() const {
242  return items_.size();
243  }
244 
245  template<typename T>
246  inline
247  typename View<T>::size_type
248  View<T>::max_size() const {
249  return items_.max_size();
250  }
251 
252  template<typename T>
253  inline
254  bool
255  View<T>::empty() const {
256  return items_.empty();
257  }
258 
259  template<typename T>
260  inline
261  typename View<T>::const_reference
262  View<T>::at(size_type pos) const {
263  return *items_.at(pos);
264  }
265 
266  template<typename T>
267  inline
268  typename View<T>::const_reference
269  View<T>::operator[](size_type pos) const {
270  return *items_[pos];
271  }
272 
273  template<typename T>
274  inline
276  View<T>::refAt(size_type i) const {
277  //NOTE: considered creating a special BaseHolder for edm::Ptr.
278  // But the IndirectHolder and RefHolder would still be needed
279  // for other reasons. To reduce the number of dictionaries needed
280  // we avoid using a more efficient BaseHolder.
281  return RefToBase<T>(std::unique_ptr<reftobase::BaseHolder<T>>{
282  new reftobase::IndirectHolder<T>{
283  std::unique_ptr<reftobase::RefHolder<edm::Ptr<T>>>{
284  new reftobase::RefHolder<Ptr<T>>{ptrAt(i)}
285  }
286  }
287  } );
288  }
289 
290  template<typename T>
291  inline
292  Ptr<T>
293  View<T>::ptrAt(size_type i) const {
294  return vPtrs_[i];
295  }
296 
297  template<typename T>
298  inline
299  std::vector<Ptr<T> > const&
300  View<T>::ptrs() const {
301  return vPtrs_;
302  }
303 
304 
305  template<typename T>
306  inline
307  typename View<T>::const_reference
308  View<T>::front() const {
309  return *items_.front();
310  }
311 
312  template<typename T>
313  inline
314  typename View<T>::const_reference
315  View<T>::back() const {
316  return *items_.back();
317  }
318 
319  // The following is for testing only.
320  template<typename T>
321  inline
322  void
323  View<T>::fill_from_range(T* first, T* last, View& output) {
324  output.items_.resize(std::distance(first, last));
325  for(typename View<T>::size_type i = 0; first != last; ++i, ++first)
326  output.items_[i] = first;
327  }
328 
329  template<typename T>
330  std::unique_ptr<ViewBase>
331  View<T>::doClone() const {
332  return std::unique_ptr<ViewBase>{new View(*this)};
333  }
334 
335  template<typename T>
336  inline
337  View<T>&
338  View<T>::operator=(View<T> const& rhs) {
339  View<T> temp(rhs);
340  this->swap(temp);
341  return *this;
342  }
343 
344  template<typename T>
345  inline
346  bool
347  operator==(View<T> const& lhs, View<T> const& rhs) {
348  return
349  lhs.size() == rhs.size() &&
350  std::equal(lhs.begin(), lhs.end(), rhs.begin());
351  }
352 
353  template<typename T>
354  inline
355  bool
356  operator!=(View<T> const& lhs, View<T> const& rhs) {
357  return !(lhs == rhs);
358  }
359 
360  template<typename T>
361  inline
362  bool
363  operator<(View<T> const& lhs, View<T> const& rhs) {
364  return std::lexicographical_compare(lhs.begin(), lhs.end(),
365  rhs.begin(), rhs.end());
366  }
367 
368  template<typename T>
369  inline
370  bool
371  operator<=(View<T> const& lhs, View<T> const& rhs) {
372  return !(rhs<lhs);
373  }
374 
375  template<typename T>
376  inline
377  bool operator> (View<T> const& lhs, View<T> const& rhs) {
378  return rhs<lhs;
379  }
380 
381  template<typename T>
382  inline
383  bool operator>=(View<T> const& lhs, View<T> const& rhs) {
384  return !(lhs<rhs);
385  }
386 
387  // Free swap function
388  template<typename T>
389  inline
390  void swap(View<T>& lhs, View<T>& rhs) {
391  lhs.swap(rhs);
392  }
393 }
394 
395 #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
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
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:71
T const * const_pointer
Definition: View.h:74
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:86
virtual std::unique_ptr< ViewBase > doClone() const =0
seq_t items_
Definition: View.h:137
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:383
const_iterator begin() const
View & operator=(View const &rhs)
std::vector< Ptr< value_type > > vPtrs_
Definition: View.h:138
virtual ~View()
void swap(ViewBase &)
Definition: View.h:48
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:76
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
bool empty() const
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:377
T operator[](int i) const
#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:88
#define begin
Definition: vmac.h:30
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
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
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
T const * pointer
Definition: View.h:73
tuple size
Write out results.
std::unique_ptr< ViewBase > doClone() const override