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 
120 
121  size_type size() const;
122  size_type max_size() const;
123  bool empty() 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:
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
edm::FillViewHelperVector
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
Definition: FillViewHelperVector.h:30
edm::View::begin
const_iterator begin() const
edm::View::seq_t
std::vector< T const * > seq_t
Definition: View.h:75
edm::View::doClone
std::unique_ptr< ViewBase > doClone() const override
mps_fire.i
i
Definition: mps_fire.py:428
edm::View::const_reference
T const & const_reference
Definition: View.h:82
edm::View::empty
bool empty() const
sistrip::View
View
Definition: ConstantsForView.h:26
View< T >
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
edm::View::refAt
RefToBase< value_type > refAt(size_type i) const
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::View::ptrs
std::vector< Ptr< value_type > > const & ptrs() const
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
edm::View::difference_type
seq_t::difference_type difference_type
Definition: View.h:91
edm::swap
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
pos
Definition: PixelAliasList.h:18
edm::View::rbegin
const_reverse_iterator rbegin() const
edm::ViewBase::swap
void swap(ViewBase &)
Definition: View.h:53
cms::cuda::assert
assert(be >=bs)
edm::View::View
View()
edm::View::const_pointer
T const * const_pointer
Definition: View.h:79
edm::View::~View
~View() override
edm::View::items_
seq_t items_
Definition: View.h:140
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
edm::View::rend
const_reverse_iterator rend() const
edm::first
T first(std::pair< T, U > const &p)
Definition: ParameterSet.cc:217
edm::EDProductGetter
Definition: EDProductGetter.h:41
edm::View::value_type
T value_type
Definition: View.h:84
dqmdumpme.last
last
Definition: dqmdumpme.py:56
edm::operator!=
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
Definition: debugging_allocator.h:75
edm::ViewBase::operator=
ViewBase & operator=(ViewBase const &)
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
edm::ViewBase::doClone
virtual std::unique_ptr< ViewBase > doClone() const =0
edm::View::vPtrs_
std::vector< Ptr< value_type > > vPtrs_
Definition: View.h:141
edm::operator==
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
Definition: debugging_allocator.h:72
edm::operator<
bool operator<(DetSet< T > const &x, DetSet< T > const &y)
Definition: DetSet.h:89
edm::ViewBase::clone
std::unique_ptr< ViewBase > clone() const
Definition: View.cc:11
edm::View::front
const_reference front() const
trackingPlots.other
other
Definition: trackingPlots.py:1467
h
RefToBase.h
edm::operator<=
bool operator<=(View< T > const &, View< T > const &)
Definition: View.h:320
edm::operator>
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:325
edm::View::size
size_type size() const
edm::View
Definition: CaloClusterFwd.h:14
edm::View::at
const_reference at(size_type pos) const
EDProductGetter
helpers
Definition: makeCompositeCandidate.h:8
edm::View::operator=
View & operator=(View const &rhs)
Ptr.h
RefToBase
edm::View::operator[]
const_reference operator[](size_type pos) const
edm::View::swap
void swap(View &other)
edm::View::reference
T const & reference
Definition: View.h:81
edm::View::back
const_reference back() const
edm::View::fill_from_range
static void fill_from_range(T *first, T *last, View &output)
edm::View::const_reverse_iterator
boost::indirect_iterator< typename seq_t::const_reverse_iterator > const_reverse_iterator
Definition: View.h:93
edm::Ptr< value_type >
edm::View::max_size
size_type max_size() const
cms::cuda::device::unique_ptr
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
Definition: device_unique_ptr.h:33
T
long double T
Definition: Basic3DVectorLD.h:48
RefHolder_.h
edm::operator>=
bool operator>=(View< T > const &, View< T > const &)
Definition: View.h:330
edm::RefToBase
Definition: AssociativeIterator.h:54
edm::View::const_iterator
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
edm::ViewBase::ViewBase
ViewBase()
Definition: View.cc:21
edm::ViewBase
Definition: View.h:43
edm::View::end
const_iterator end() const
edm::View::pointer
T const * pointer
Definition: View.h:78
edm::View::capacity
size_type capacity() const
HLT_FULL_cff.distance
distance
Definition: HLT_FULL_cff.py:7733
IndirectHolder.h
cond::serialization::equal
bool equal(const T &first, const T &second)
Definition: Equal.h:32
edm::View::ptrAt
Ptr< value_type > ptrAt(size_type i) const
edm::ViewBase::~ViewBase
virtual ~ViewBase()
Definition: View.cc:9
edm::View::size_type
unsigned int size_type
Definition: View.h:90