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  template <typename T>
74  class View : public ViewBase {
75  typedef std::vector<T const*> seq_t;
76 
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, FillViewHelperVector const& helpers, EDProductGetter const* getter);
103 
104  ~View() override;
105 
106  void swap(View& other);
107 
108  View& operator=(View const& rhs);
109 
110  size_type capacity() const;
111 
112  // Most non-const member functions not present.
113  // No access to non-const contents provided.
114 
115  const_iterator begin() const;
116  const_iterator end() const;
117 
118  const_reverse_iterator rbegin() const;
119  const_reverse_iterator rend() const;
120 
121  size_type size() const;
122  size_type max_size() const;
123  bool empty() const;
124  const_reference at(size_type pos) const;
125  const_reference operator[](size_type pos) const;
126  RefToBase<value_type> refAt(size_type i) const;
127  Ptr<value_type> ptrAt(size_type i) const;
128  std::vector<Ptr<value_type>> const& ptrs() const;
129 
130  const_reference front() const;
131  const_reference back() 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  private:
140  seq_t items_;
141  std::vector<Ptr<value_type>> vPtrs_;
142  std::unique_ptr<ViewBase> doClone() const override;
143  };
144 
145  // Associated free functions (same as for std::vector)
146  template <typename T>
147  bool operator==(View<T> const&, View<T> const&);
148  template <typename T>
149  bool operator!=(View<T> const&, View<T> const&);
150  template <typename T>
151  bool operator<(View<T> const&, View<T> const&);
152  template <typename T>
153  bool operator<=(View<T> const&, View<T> const&);
154  template <typename T>
155  bool operator>(View<T> const&, View<T> const&);
156  template <typename T>
157  bool operator>=(View<T> const&, View<T> const&);
158 
159  //------------------------------------------------------------------
160  // Implementation of View<T>
161  //------------------------------------------------------------------
162 
163  template <typename T>
164  inline View<T>::View() : items_(), vPtrs_() {}
165 
166  template <typename T>
167  View<T>::View(std::vector<void const*> const& pointers,
169  EDProductGetter const* getter)
170  : items_(), vPtrs_() {
171  size_type numElements = pointers.size();
172 
173  // If the two input vectors are not of the same size, there is a
174  // logic error in the framework code that called this.
175  // constructor.
176  assert(numElements == helpers.size());
177 
178  items_.reserve(numElements);
179  vPtrs_.reserve(numElements);
180  for (std::vector<void const*>::size_type i = 0; i < pointers.size(); ++i) {
181  void const* p = pointers[i];
182  auto const& h = helpers[i];
183  items_.push_back(static_cast<pointer>(p));
184  if (nullptr != p) {
185  vPtrs_.push_back(Ptr<T>(h.first, static_cast<T const*>(p), h.second));
186  } else if (getter != nullptr) {
187  vPtrs_.push_back(Ptr<T>(h.first, h.second, getter));
188  } else {
189  vPtrs_.push_back(Ptr<T>(h.first, nullptr, h.second));
190  }
191  }
192  }
193 
194  template <typename T>
195  View<T>::~View() {}
196 
197  template <typename T>
198  inline void View<T>::swap(View& other) {
199  this->ViewBase::swap(other);
200  items_.swap(other.items_);
201  vPtrs_.swap(other.vPtrs_);
202  }
203 
204  template <typename T>
205  inline typename View<T>::size_type View<T>::capacity() const {
206  return items_.capacity();
207  }
208 
209  template <typename T>
210  inline typename View<T>::const_iterator View<T>::begin() const {
211  return items_.begin();
212  }
213 
214  template <typename T>
215  inline typename View<T>::const_iterator View<T>::end() const {
216  return items_.end();
217  }
218 
219  template <typename T>
220  inline typename View<T>::const_reverse_iterator View<T>::rbegin() const {
221  return items_.rbegin();
222  }
223 
224  template <typename T>
225  inline typename View<T>::const_reverse_iterator View<T>::rend() const {
226  return items_.rend();
227  }
228 
229  template <typename T>
230  inline typename View<T>::size_type View<T>::size() const {
231  return items_.size();
232  }
233 
234  template <typename T>
235  inline typename View<T>::size_type View<T>::max_size() const {
236  return items_.max_size();
237  }
238 
239  template <typename T>
240  inline bool View<T>::empty() const {
241  return items_.empty();
242  }
243 
244  template <typename T>
245  inline typename View<T>::const_reference View<T>::at(size_type pos) const {
246  return *items_.at(pos);
247  }
248 
249  template <typename T>
250  inline typename View<T>::const_reference View<T>::operator[](size_type pos) const {
251  return *items_[pos];
252  }
253 
254  template <typename T>
255  inline RefToBase<T> View<T>::refAt(size_type i) const {
256  //NOTE: considered creating a special BaseHolder for edm::Ptr.
257  // But the IndirectHolder and RefHolder would still be needed
258  // for other reasons. To reduce the number of dictionaries needed
259  // we avoid using a more efficient BaseHolder.
260  return RefToBase<T>(std::unique_ptr<reftobase::BaseHolder<T>>{new reftobase::IndirectHolder<T>{
261  std::unique_ptr<reftobase::RefHolder<edm::Ptr<T>>>{new reftobase::RefHolder<Ptr<T>>{ptrAt(i)}}}});
262  }
263 
264  template <typename T>
265  inline Ptr<T> View<T>::ptrAt(size_type i) const {
266  return vPtrs_[i];
267  }
268 
269  template <typename T>
270  inline std::vector<Ptr<T>> const& View<T>::ptrs() const {
271  return vPtrs_;
272  }
273 
274  template <typename T>
275  inline typename View<T>::const_reference View<T>::front() const {
276  return *items_.front();
277  }
278 
279  template <typename T>
280  inline typename View<T>::const_reference View<T>::back() const {
281  return *items_.back();
282  }
283 
284  // The following is for testing only.
285  template <typename T>
286  inline void View<T>::fill_from_range(T* first, T* last, View& output) {
287  output.items_.resize(std::distance(first, last));
288  for (typename View<T>::size_type i = 0; first != last; ++i, ++first)
289  output.items_[i] = first;
290  }
291 
292  template <typename T>
293  std::unique_ptr<ViewBase> View<T>::doClone() const {
294  return std::unique_ptr<ViewBase>{new View(*this)};
295  }
296 
297  template <typename T>
298  inline View<T>& View<T>::operator=(View<T> const& rhs) {
299  View<T> temp(rhs);
300  this->swap(temp);
301  return *this;
302  }
303 
304  template <typename T>
305  inline bool operator==(View<T> const& lhs, View<T> const& rhs) {
306  return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
307  }
308 
309  template <typename T>
310  inline bool operator!=(View<T> const& lhs, View<T> const& rhs) {
311  return !(lhs == rhs);
312  }
313 
314  template <typename T>
315  inline bool operator<(View<T> const& lhs, View<T> const& rhs) {
316  return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
317  }
318 
319  template <typename T>
320  inline bool operator<=(View<T> const& lhs, View<T> const& rhs) {
321  return !(rhs < lhs);
322  }
323 
324  template <typename T>
325  inline bool operator>(View<T> const& lhs, View<T> const& rhs) {
326  return rhs < lhs;
327  }
328 
329  template <typename T>
330  inline bool operator>=(View<T> const& lhs, View<T> const& rhs) {
331  return !(lhs < rhs);
332  }
333 
334  // Free swap function
335  template <typename T>
336  inline void swap(View<T>& lhs, View<T>& rhs) {
337  lhs.swap(rhs);
338  }
339 } // namespace edm
340 
341 #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:336
std::vector< T const * > seq_t
Definition: View.h:75
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:140
bool equal(const T &first, const T &second)
Definition: Equal.h:32
T value_type
Definition: View.h:84
bool operator>=(View< T > const &, View< T > const &)
Definition: View.h:330
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
std::vector< Ptr< value_type > > vPtrs_
Definition: View.h:141
void swap(ViewBase &)
Definition: View.h:53
std::unique_ptr< ViewBase > clone() const
Definition: View.cc:11
T const & reference
Definition: View.h:81
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:325
T operator[](int i) const
#define end
Definition: vmac.h:39
const_reverse_iterator rend() const
ViewBase()
Definition: View.cc:21
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
virtual ~ViewBase()
Definition: View.cc:9
size_type capacity() const
ViewBase & operator=(ViewBase const &)
bool operator!=(View< T > const &, View< T > const &)
Definition: View.h:310
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:305
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
T const * pointer
Definition: View.h:78
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector