CMS 3D CMS Logo

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