LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
View.h
Go to the documentation of this file.
1 #ifndef art_Framework_Principal_View_h
2 #define art_Framework_Principal_View_h
3 
4 // ====================================================================
5 // The class template View<T> provides a means to obtain pointers (of
6 // type T const*) into an arbitrary collection in an Event.
7 //
8 // A View<T> is *valid* if it refers to a product in an Event. Default
9 // constructed Views are not valid. A valid View may still contain an
10 // empty vector; this means that either the referenced collection was
11 // empty, or that the View's vector was emptied after the View was
12 // created.
13 //
14 // While View<T> is not a persistent class, one can fill a
15 // PtrVector<T> from a View<T>, as long as no new pointers have been
16 // added to the View<T>.
17 // ====================================================================
18 
21 #include "cetlib/container_algorithms.h"
22 
23 template <class T>
24 class art::View {
25 public:
26  using collection_type = std::vector<T const*>;
27  using value_type = typename collection_type::value_type;
30  using size_type = typename collection_type::size_type;
31 
32  View() = default;
34  vals()
35  {
36  return vals_;
37  }
38  collection_type const&
39  vals() const
40  {
41  return vals_;
42  }
43 
44  // return true if this view has been populated, and false if it
45  // has not.
46  bool
47  isValid() const
48  {
49  return prod_ != nullptr;
50  }
51 
52  ProductID
53  id() const
54  {
55  return id_;
56  }
57 
58  // Fill the given PtrVector<T> to refer to the same elements as the
59  // View does. It is only safe to call this if isValid() is true.
60  void fill(PtrVector<T>& pv) const;
61 
62  // Conversion operators
63  operator collection_type&() { return vals_; }
64  operator collection_type const&() const { return vals_; }
65 
66  iterator
68  {
69  return vals_.begin();
70  }
71  iterator
72  end()
73  {
74  return vals_.end();
75  }
77  begin() const
78  {
79  return vals_.begin();
80  }
82  end() const
83  {
84  return vals_.end();
85  }
87  cbegin() const
88  {
89  return vals_.cbegin();
90  }
92  cend() const
93  {
94  return vals_.cend();
95  }
96 
97  size_type
98  size() const
99  {
100  return vals_.size();
101  }
102 
103 private:
104  collection_type vals_{}; // we do not own the pointed-to elements
106  EDProduct const* prod_{nullptr}; // we do not own the product
107 
108  friend class DataViewImpl;
109  void set_innards(ProductID const& id, EDProduct const* p);
110 };
111 
112 template <class T>
113 void
115 {
116  std::vector<void const*> addresses;
117  prod_->fillView(addresses);
118 
119  std::size_t i{};
120  for (auto a : addresses) {
121  if (cet::search_all(vals_, a)) {
122  auto p = reinterpret_cast<T const*>(a);
123  pv.push_back(Ptr<T>{id_, const_cast<T*>(p), i});
124  ++i;
125  }
126  }
127 }
128 
129 template <class T>
130 inline void
132 {
133  id_ = id;
134  prod_ = p;
135 }
136 
137 #endif /* art_Framework_Principal_View_h */
138 
139 // Local Variables:
140 // mode: c++
141 // End:
iterator end()
Definition: View.h:72
iterator begin()
Definition: View.h:67
EDProduct const * prod_
Definition: View.h:106
virtual void fillView(std::vector< void const * > &) const
Definition: EDProduct.h:36
intermediate_table::iterator iterator
typename collection_type::const_iterator const_iterator
Definition: View.h:28
const_iterator cbegin() const
Definition: View.h:87
collection_type vals_
Definition: View.h:104
bool isValid() const
Definition: View.h:47
void fill(PtrVector< T > &pv) const
Definition: View.h:114
size_type size() const
Definition: View.h:98
View()=default
ProductID id_
Definition: View.h:105
intermediate_table::const_iterator const_iterator
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
std::vector< T const * > collection_type
Definition: View.h:26
collection_type const & vals() const
Definition: View.h:39
Definition: fwd.h:47
typename collection_type::iterator iterator
Definition: View.h:29
const_iterator end() const
Definition: View.h:82
const_iterator cend() const
Definition: View.h:92
void set_innards(ProductID const &id, EDProduct const *p)
Definition: View.h:131
const_iterator begin() const
Definition: View.h:77
typename collection_type::value_type value_type
Definition: View.h:27
Definition: fwd.h:25
collection_type & vals()
Definition: View.h:34
typename collection_type::size_type size_type
Definition: View.h:30
ProductID id() const
Definition: View.h:53