LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
Wrapper.h
Go to the documentation of this file.
1 #ifndef canvas_Persistency_Common_Wrapper_h
2 #define canvas_Persistency_Common_Wrapper_h
3 // vim: set sw=2:
4 
5 // =====================================================================
6 // Wrapper: A class template that inherits from art::EDProduct, thus
7 // providing the representation needed for providing products
8 // of type T. Each instantiation also includes:
9 // - a Boolean value corresponding to the presence of the
10 // product in the file
11 // - the RangeSet corresponding to the set of events
12 // processed in creating the product.
13 // =====================================================================
14 
18 #include "cetlib/metaprogramming.h"
19 #include "cetlib_except/demangle.h"
20 
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 namespace art {
26  template <typename T>
27  class Wrapper;
28 
29  // Implementation detail declarations.
30  namespace detail {
31 
32  using cet::enable_if_function_exists_t;
33 
34  // has_size_member
35  template <typename T, typename = void>
36  struct has_size_member : std::false_type {
37  };
38 
39  template <typename T>
41  T,
42  enable_if_function_exists_t<size_t (T::*)() const, &T::size>>
43  : std::true_type {
44  };
45 
46  // has_makePartner_member
47  template <typename T, typename = void>
48  struct has_makePartner_member : std::false_type {
49  };
50 
51  template <typename T>
53  T,
54  enable_if_function_exists_t<std::unique_ptr<EDProduct> (T::*)(
55  std::type_info const&) const,
56  &T::makePartner>> : std::true_type {
57  };
58  }
59 
61  struct productSize;
62 
63  template <typename T>
64  struct DoMakePartner;
65 
66  template <typename T>
68 
69  template <typename T>
70  struct DoSetPtr;
71 
72  template <typename T>
73  struct DoNotSetPtr;
74 }
75 
77 // Definition of art::Wrapper<T>
78 template <typename T>
79 class art::Wrapper : public art::EDProduct {
80 public:
81  Wrapper() = default;
82 
83  explicit Wrapper(std::unique_ptr<T> ptr);
84  virtual ~Wrapper() = default;
85 
86  T const* product() const;
87  T const* operator->() const;
88 
89  // MUST UPDATE WHEN CLASS IS CHANGED!
90  static short
92  {
93  return 11;
94  }
95 
96 private:
97  void fillView(std::vector<void const*>& view) const override;
98 
99  std::string productSize() const override;
100  void do_combine(EDProduct* product) override;
101 
102  void do_setRangeSetID(unsigned) override;
103  unsigned do_getRangeSetID() const override;
104 
105  std::unique_ptr<EDProduct> do_makePartner(
106  std::type_info const& wanted_type) const override;
107 
108  bool
109  isPresent_() const override
110  {
111  return present;
112  }
113  std::type_info const* typeInfo_() const override;
114 
115  void do_setPtr(std::type_info const& toType,
116  unsigned long index,
117  void const*& ptr) const override;
118 
119  void do_getElementAddresses(std::type_info const& toType,
120  std::vector<unsigned long> const& indices,
121  std::vector<void const*>& ptr) const override;
122 
123  T&& refOrThrow(T* ptr);
124 
125  bool present{false};
126  unsigned rangeSetID{-1u};
127  T obj{};
128 
129 }; // Wrapper<>
130 
132 // Implementation details.
133 
138 
139 #include "boost/lexical_cast.hpp"
142 #include <memory>
143 #include <type_traits>
144 
145 #include <deque>
146 #include <list>
147 #include <set>
148 #include <string>
149 #include <typeinfo>
150 #include <vector>
151 
153 // Wrapper member functions.
154 template <typename T>
155 art::Wrapper<T>::Wrapper(std::unique_ptr<T> ptr)
156  : present{ptr.get() != 0}, rangeSetID{-1u}, obj(refOrThrow(ptr.get()))
157 {}
158 
159 template <typename T>
160 T const*
162 {
163  return present ? &obj : nullptr;
164 }
165 
166 template <typename T>
168 {
169  return product();
170 }
171 
172 template <typename T>
173 std::type_info const*
175 {
176  return SupportsView<T>::type_id();
177 }
178 
179 template <typename T>
180 void
181 art::Wrapper<T>::fillView(std::vector<void const*>& view) const
182 {
184 }
185 
186 template <typename T>
187 std::string
189 {
190  return art::productSize<T>()(obj);
191 }
192 
193 template <typename T>
194 void
196 {
197  if (!p->isPresent())
198  return;
199 
200  auto wp = static_cast<Wrapper<T>*>(p);
202 
203  // The presence for the combined product is 'true', if we get this
204  // far.
205  present = true;
206 }
207 
208 template <typename T>
209 void
211 {
212  rangeSetID = id;
213 }
214 
215 template <typename T>
216 unsigned
218 {
219  return rangeSetID;
220 }
221 
222 template <typename T>
223 std::unique_ptr<art::EDProduct>
224 art::Wrapper<T>::do_makePartner(std::type_info const& wanted_wrapper) const
225 {
226  std::unique_ptr<art::EDProduct> retval;
230  maybe_maker;
231  retval = maybe_maker(obj, wanted_wrapper);
232  return retval;
233 }
234 
235 template <typename T>
236 inline void
237 art::Wrapper<T>::do_setPtr(std::type_info const& toType,
238  unsigned long index,
239  void const*& ptr) const
240 {
242  maybe_filler;
243  maybe_filler(this->obj, toType, index, ptr);
244 }
245 
246 template <typename T>
247 inline void
249  std::type_info const& toType,
250  std::vector<unsigned long> const& indices,
251  std::vector<void const*>& ptrs) const
252 {
254  maybe_filler;
255  maybe_filler(this->obj, toType, indices, ptrs);
256 }
257 
258 template <typename T>
259 inline T&&
261 {
262  if (ptr) {
263  return std::move(*ptr);
264  } else {
266  << "Attempt to construct " << cet::demangle_symbol(typeid(*this).name())
267  << " from nullptr.\n";
268  }
269 }
270 
272 // Metafunction support for compile-time selection of code used in
273 // Wrapper implementation.
274 
275 namespace art {
276 
277  template <typename T>
278  struct productSize<T, true> {
279  std::string
280  operator()(T const& obj) const
281  {
282  return boost::lexical_cast<std::string>(obj.size());
283  }
284  };
285 
286  template <typename T>
287  struct productSize<T, false> {
288  std::string
289  operator()(T const&) const
290  {
291  return "-";
292  }
293  };
294 
295  template <class E>
296  struct productSize<std::vector<E>, false>
297  : public productSize<std::vector<E>, true> {
298  };
299 
300  template <class E>
301  struct productSize<std::list<E>, false>
302  : public productSize<std::list<E>, true> {
303  };
304 
305  template <class E>
306  struct productSize<std::deque<E>, false>
307  : public productSize<std::deque<E>, true> {
308  };
309 
310  template <class E>
311  struct productSize<std::set<E>, false>
312  : public productSize<std::set<E>, true> {
313  };
314 
315  template <class E>
316  struct productSize<PtrVector<E>, false>
317  : public productSize<PtrVector<E>, true> {
318  };
319 
320  template <class E>
321  struct productSize<cet::map_vector<E>, false>
322  : public productSize<cet::map_vector<E>, true> {
323  };
324 
325  template <typename T>
326  struct DoMakePartner {
327  std::unique_ptr<EDProduct>
328  operator()(T const& obj, std::type_info const& wanted_wrapper_type) const
329  {
330  return obj.makePartner(wanted_wrapper_type);
331  }
332  };
333 
334  template <typename T>
335  struct DoNotMakePartner {
336  std::unique_ptr<EDProduct>
337  operator()(T const&, std::type_info const&) const
338  {
339  throw Exception(errors::LogicError, "makePartner")
340  << "Attempted to make partner of a product ("
341  << cet::demangle_symbol(typeid(T).name())
342  << ") that does not know how!\n"
343  << "Please report to the ART framework developers.\n";
344  }
345  };
346 
347  template <typename T>
348  struct DoSetPtr {
349  void operator()(T const& obj,
350  std::type_info const& toType,
351  unsigned long index,
352  void const*& ptr) const;
353  void operator()(T const& obj,
354  std::type_info const& toType,
355  std::vector<unsigned long> const& index,
356  std::vector<void const*>& ptrs) const;
357  };
358 
359  template <typename T>
360  struct DoNotSetPtr {
361  void
362  operator()(T const&,
363  std::type_info const&,
364  unsigned long,
365  void const*&) const
366  {
368  << "The product type " << cet::demangle_symbol(typeid(T).name())
369  << "\ndoes not support art::Ptr\n";
370  }
371 
372  void
373  operator()(T const&,
374  std::type_info const&,
375  std::vector<unsigned long> const&,
376  std::vector<void const*>&) const
377  {
379  << "The product type " << cet::demangle_symbol(typeid(T).name())
380  << "\ndoes not support art::PtrVector\n";
381  }
382  };
383 
384  template <typename T>
385  void
387  std::type_info const& toType,
388  unsigned long const index,
389  void const*& ptr) const
390  {
391  // setPtr is the name of an overload set; each concrete collection
392  // T should supply a setPtr function, in the same namespace at
393  // that in which T is defined, or in the 'art' namespace.
394  setPtr(obj, toType, index, ptr);
395  }
396 
397  template <typename T>
398  void
400  std::type_info const& toType,
401  std::vector<unsigned long> const& indices,
402  std::vector<void const*>& ptr) const
403  {
404  // getElementAddresses is the name of an overload set; each
405  // concrete collection T should supply a getElementAddresses
406  // function, in the same namespace at that in which T is
407  // defined, or in the 'art' namespace.
408  getElementAddresses(obj, toType, indices, ptr);
409  }
410 }
411 
412 #endif /* canvas_Persistency_Common_Wrapper_h */
413 
414 // Local Variables:
415 // mode: c++
416 // End:
unsigned do_getRangeSetID() const override
Definition: Wrapper.h:217
T const * operator->() const
Definition: Wrapper.h:167
void do_setPtr(std::type_info const &toType, unsigned long index, void const *&ptr) const override
Definition: Wrapper.h:237
Float_t E
Definition: plot.C:23
Wrapper()=default
std::unique_ptr< EDProduct > do_makePartner(std::type_info const &wanted_type) const override
Definition: Wrapper.h:224
void operator()(T const &, std::type_info const &, std::vector< unsigned long > const &, std::vector< void const * > &) const
Definition: Wrapper.h:373
STL namespace.
void do_getElementAddresses(std::type_info const &toType, std::vector< unsigned long > const &indices, std::vector< void const * > &ptr) const override
Definition: Wrapper.h:248
static std::type_info const * type_id()
Definition: traits.h:92
void do_combine(EDProduct *product) override
Definition: Wrapper.h:195
std::type_info const * typeInfo_() const override
Definition: Wrapper.h:174
bool isPresent_() const override
Definition: Wrapper.h:109
std::unique_ptr< EDProduct > operator()(T const &obj, std::type_info const &wanted_wrapper_type) const
Definition: Wrapper.h:328
static short Class_Version()
Definition: Wrapper.h:91
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
std::string operator()(T const &) const
Definition: Wrapper.h:289
T const * product() const
Definition: Wrapper.h:161
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
std::string operator()(T const &obj) const
Definition: Wrapper.h:280
unsigned rangeSetID
Definition: Wrapper.h:126
std::unique_ptr< EDProduct > operator()(T const &, std::type_info const &) const
Definition: Wrapper.h:337
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void getElementAddresses(std::type_info const &toType, std::vector< unsigned long > const &indices, std::vector< void const * > &ptr) const
Definition: EDProduct.cc:23
std::string value(boost::any const &)
static void fill(T const &, std::vector< void const * > &)
Definition: traits.h:147
bool present
Definition: Wrapper.h:125
void operator()(T const &, std::type_info const &, unsigned long, void const *&) const
Definition: Wrapper.h:362
void operator()(T const &obj, std::type_info const &toType, unsigned long index, void const *&ptr) const
Definition: Wrapper.h:386
HLT enums.
bool isPresent() const
Definition: EDProduct.h:28
void fillView(std::vector< void const * > &view) const override
Definition: Wrapper.h:181
T && refOrThrow(T *ptr)
Definition: Wrapper.h:260
static void aggregate(T &, T const &)
Definition: aggregate.h:57
ProductStatus present()
Definition: ProductStatus.h:16
std::string productSize() const override
Definition: Wrapper.h:188
void setPtr(std::type_info const &toType, unsigned long index, void const *&ptr) const
Definition: EDProduct.cc:15
void do_setRangeSetID(unsigned) override
Definition: Wrapper.h:210