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() = default;
50  ViewBase(ViewBase const&) = default;
51  ViewBase(ViewBase&&) = default;
52  ViewBase& operator=(ViewBase const&) = default;
53  ViewBase& operator=(ViewBase&&) = default;
54  virtual std::unique_ptr<ViewBase> doClone() const = 0;
55  void swap(ViewBase&) {} // Nothing to swap
56  };
57 
58  //------------------------------------------------------------------
73  //------------------------------------------------------------------
74 
75  template <typename T>
76  class View : public ViewBase {
77  typedef std::vector<T const*> seq_t;
78 
79  public:
80  typedef T const* pointer;
81  typedef T const* const_pointer;
82 
83  typedef T const& reference;
84  typedef T const& const_reference;
85 
86  typedef T value_type;
87 
88  typedef boost::indirect_iterator<typename seq_t::const_iterator> const_iterator;
89 
90  // This should be a typedef to seq_t::size_type but because this type is used as a template
91  // argument in a persistened class it must be stable for different architectures
92  typedef unsigned int size_type;
93  typedef typename seq_t::difference_type difference_type;
94 
95  typedef boost::indirect_iterator<typename seq_t::const_reverse_iterator> const_reverse_iterator;
96 
97  // Compiler-generated copy, and assignment each does the right
98  // thing.
99 
100  View();
101 
102  // This function is dangerous, and should only be called from the
103  // infrastructure code.
104  View(std::vector<void const*> const& pointers, FillViewHelperVector const& helpers, EDProductGetter const* getter);
105 
106  void swap(View& other);
107 
108  size_type capacity() const;
109 
110  // Most non-const member functions not present.
111  // No access to non-const contents provided.
112 
113  const_iterator begin() const;
114  const_iterator end() const;
115 
118 
119  size_type size() const;
120  size_type max_size() const;
121  bool empty() const;
126  std::vector<Ptr<value_type>> const& ptrs() const;
127 
128  const_reference front() const;
129  const_reference back() const;
130 
131  // No erase, because erase is required to return an *iterator*,
132  // not a *const_iterator*.
133 
134  // The following is for testing only.
135  static void fill_from_range(T* first, T* last, View& output);
136 
137  private:
139  std::vector<Ptr<value_type>> vPtrs_;
140  std::unique_ptr<ViewBase> doClone() const override;
141  };
142 
143  // Associated free functions (same as for std::vector)
144  template <typename T>
145  bool operator==(View<T> const&, View<T> const&);
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 
157  //------------------------------------------------------------------
158  // Implementation of View<T>
159  //------------------------------------------------------------------
160 
161  template <typename T>
162  inline View<T>::View() : items_(), vPtrs_() {}
163 
164  template <typename T>
165  View<T>::View(std::vector<void const*> const& pointers,
167  EDProductGetter const* getter)
168  : items_(), vPtrs_() {
169  size_type numElements = pointers.size();
170 
171  // If the two input vectors are not of the same size, there is a
172  // logic error in the framework code that called this.
173  // constructor.
174  assert(numElements == helpers.size());
175 
176  items_.reserve(numElements);
177  vPtrs_.reserve(numElements);
178  for (std::vector<void const*>::size_type i = 0; i < pointers.size(); ++i) {
179  void const* p = pointers[i];
180  auto const& h = helpers[i];
181  items_.push_back(static_cast<pointer>(p));
182  if (nullptr != p) {
183  vPtrs_.push_back(Ptr<T>(h.first, static_cast<T const*>(p), h.second));
184  } else if (getter != nullptr) {
185  vPtrs_.push_back(Ptr<T>(h.first, h.second, getter));
186  } else {
187  vPtrs_.push_back(Ptr<T>(h.first, nullptr, h.second));
188  }
189  }
190  }
191 
192  template <typename T>
193  inline void View<T>::swap(View& other) {
194  this->ViewBase::swap(other);
195  items_.swap(other.items_);
196  vPtrs_.swap(other.vPtrs_);
197  }
198 
199  template <typename T>
200  inline typename View<T>::size_type View<T>::capacity() const {
201  return items_.capacity();
202  }
203 
204  template <typename T>
205  inline typename View<T>::const_iterator View<T>::begin() const {
206  return items_.begin();
207  }
208 
209  template <typename T>
210  inline typename View<T>::const_iterator View<T>::end() const {
211  return items_.end();
212  }
213 
214  template <typename T>
215  inline typename View<T>::const_reverse_iterator View<T>::rbegin() const {
216  return items_.rbegin();
217  }
218 
219  template <typename T>
220  inline typename View<T>::const_reverse_iterator View<T>::rend() const {
221  return items_.rend();
222  }
223 
224  template <typename T>
225  inline typename View<T>::size_type View<T>::size() const {
226  return items_.size();
227  }
228 
229  template <typename T>
230  inline typename View<T>::size_type View<T>::max_size() const {
231  return items_.max_size();
232  }
233 
234  template <typename T>
235  inline bool View<T>::empty() const {
236  return items_.empty();
237  }
238 
239  template <typename T>
240  inline typename View<T>::const_reference View<T>::at(size_type pos) const {
241  return *items_.at(pos);
242  }
243 
244  template <typename T>
245  inline typename View<T>::const_reference View<T>::operator[](size_type pos) const {
246  return *items_[pos];
247  }
248 
249  template <typename T>
250  inline RefToBase<T> View<T>::refAt(size_type i) const {
251  //NOTE: considered creating a special BaseHolder for edm::Ptr.
252  // But the IndirectHolder and RefHolder would still be needed
253  // for other reasons. To reduce the number of dictionaries needed
254  // we avoid using a more efficient BaseHolder.
255  return RefToBase<T>(std::unique_ptr<reftobase::BaseHolder<T>>{new reftobase::IndirectHolder<T>{
256  std::unique_ptr<reftobase::RefHolder<edm::Ptr<T>>>{new reftobase::RefHolder<Ptr<T>>{ptrAt(i)}}}});
257  }
258 
259  template <typename T>
260  inline Ptr<T> View<T>::ptrAt(size_type i) const {
261  return vPtrs_[i];
262  }
263 
264  template <typename T>
265  inline std::vector<Ptr<T>> const& View<T>::ptrs() const {
266  return vPtrs_;
267  }
268 
269  template <typename T>
270  inline typename View<T>::const_reference View<T>::front() const {
271  return *items_.front();
272  }
273 
274  template <typename T>
275  inline typename View<T>::const_reference View<T>::back() const {
276  return *items_.back();
277  }
278 
279  // The following is for testing only.
280  template <typename T>
281  inline void View<T>::fill_from_range(T* first, T* last, View& output) {
282  output.items_.resize(std::distance(first, last));
283  for (typename View<T>::size_type i = 0; first != last; ++i, ++first)
284  output.items_[i] = first;
285  }
286 
287  template <typename T>
288  std::unique_ptr<ViewBase> View<T>::doClone() const {
289  return std::unique_ptr<ViewBase>{new View(*this)};
290  }
291 
292  template <typename T>
293  inline bool operator==(View<T> const& lhs, View<T> const& rhs) {
294  return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
295  }
296 
297  template <typename T>
298  inline bool operator!=(View<T> const& lhs, View<T> const& rhs) {
299  return !(lhs == rhs);
300  }
301 
302  template <typename T>
303  inline bool operator<(View<T> const& lhs, View<T> const& rhs) {
304  return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
305  }
306 
307  template <typename T>
308  inline bool operator<=(View<T> const& lhs, View<T> const& rhs) {
309  return !(rhs < lhs);
310  }
311 
312  template <typename T>
313  inline bool operator>(View<T> const& lhs, View<T> const& rhs) {
314  return rhs < lhs;
315  }
316 
317  template <typename T>
318  inline bool operator>=(View<T> const& lhs, View<T> const& rhs) {
319  return !(lhs < rhs);
320  }
321 
322  // Free swap function
323  template <typename T>
324  inline void swap(View<T>& lhs, View<T>& rhs) {
325  lhs.swap(rhs);
326  }
327 } // namespace edm
328 
329 #endif
size
Write out results.
constexpr bool operator==(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
unsigned int size_type
Definition: View.h:92
RefToBase< value_type > refAt(size_type i) const
Ptr< value_type > ptrAt(size_type i) const
std::vector< Ptr< value_type > > const & ptrs() const
const_reverse_iterator rbegin() const
bool empty() const
void swap(View< T > &lhs, View< T > &rhs)
Definition: View.h:324
std::vector< T const * > seq_t
Definition: View.h:77
T const * const_pointer
Definition: View.h:81
assert(be >=bs)
const_reference front() const
uint16_t size_type
seq_t::difference_type difference_type
Definition: View.h:93
std::unique_ptr< ViewBase > clone() const
Definition: View.cc:11
virtual std::unique_ptr< ViewBase > doClone() const =0
seq_t items_
Definition: View.h:138
bool equal(const T &first, const T &second)
Definition: Equal.h:32
size_type size() const
constexpr bool operator>=(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
const_reverse_iterator rend() const
T value_type
Definition: View.h:86
bool operator>=(View< T > const &, View< T > const &)
Definition: View.h:318
std::vector< Ptr< value_type > > vPtrs_
Definition: View.h:139
void swap(ViewBase &)
Definition: View.h:55
void swap(View &other)
T const & reference
Definition: View.h:83
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:313
const_reference operator[](size_type pos) const
constexpr bool operator!=(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
const_reference at(size_type pos) const
virtual ~ViewBase()
Definition: View.cc:9
ViewBase()=default
uint16_t *__restrict__ uint16_t const *__restrict__ uint32_t const *__restrict__ uint32_t *__restrict__ uint32_t const *__restrict__ int32_t *__restrict__ uint32_t numElements
ViewBase & operator=(ViewBase const &)=default
constexpr bool operator>(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
bool operator!=(View< T > const &, View< T > const &)
Definition: View.h:298
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:95
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:88
const_reference back() const
bool operator==(View< T > const &, View< T > const &)
Definition: View.h:293
size_type max_size() const
Definition: output.py:1
const_iterator begin() const
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
T first(std::pair< T, U > const &p)
T operator[](int i) const
long double T
T const & const_reference
Definition: View.h:84
const_iterator end() const
T const * pointer
Definition: View.h:80
size_type capacity() const
size d for d tracks hist hist capacity()
std::unique_ptr< ViewBase > doClone() const override
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector