CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Wrapper.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_Wrapper_h
2 #define DataFormats_Common_Wrapper_h
3 
4 /*----------------------------------------------------------------------
5 
6 Wrapper: A template wrapper around EDProducts to hold the product ID.
7 
8 ----------------------------------------------------------------------*/
9 
20 #include "boost/mpl/if.hpp"
21 
22 #include <algorithm>
23 #include <memory>
24 #include <string>
25 #include <typeinfo>
26 
27 namespace edm {
28  template <typename T>
29  class Wrapper : public WrapperBase {
30  public:
31  typedef T value_type;
32  typedef T wrapped_type; // used with the dictionary to identify Wrappers
34 #ifndef __GCCXML__
35  explicit Wrapper(std::unique_ptr<T> ptr);
36 #endif
37  virtual ~Wrapper() {}
38  T const* product() const {return (present ? &obj : 0);}
39  T const* operator->() const {return product();}
40 
41  //these are used by FWLite
42  static std::type_info const& productTypeInfo() {return typeid(T);}
43  static std::type_info const& typeInfo() {return typeid(Wrapper<T>);}
44 
45  // the constructor takes ownership of T*
46  Wrapper(T*);
47 
48  //Used by ROOT storage
50 
51 private:
52  virtual bool isPresent_() const GCC11_OVERRIDE {return present;}
53  virtual std::type_info const& dynamicTypeInfo_() const GCC11_OVERRIDE {return typeid(T);}
54  virtual std::type_info const& wrappedTypeInfo_() const GCC11_OVERRIDE {return typeid(Wrapper<T>);}
55 
56  virtual std::type_info const& valueTypeInfo_() const GCC11_OVERRIDE;
57  virtual std::type_info const& memberTypeInfo_() const GCC11_OVERRIDE;
58 #ifndef __GCCXML__
59  virtual bool isMergeable_() const GCC11_OVERRIDE;
60  virtual bool mergeProduct_(WrapperBase const* newProduct) GCC11_OVERRIDE;
61  virtual bool hasIsProductEqual_() const GCC11_OVERRIDE;
62  virtual bool isProductEqual_(WrapperBase const* newProduct) const GCC11_OVERRIDE;
63 #endif
64 
65  virtual void do_fillView(ProductID const& id,
66  std::vector<void const*>& pointers,
67  helper_vector_ptr& helpers) const GCC11_OVERRIDE;
68  virtual void do_setPtr(std::type_info const& iToType,
69  unsigned long iIndex,
70  void const*& oPtr) const GCC11_OVERRIDE;
71  virtual void do_fillPtrVector(std::type_info const& iToType,
72  std::vector<unsigned long> const& iIndices,
73  std::vector<void const*>& oPtr) const GCC11_OVERRIDE;
74 
75  private:
76  // We wish to disallow copy construction and assignment.
77  // We make the copy constructor and assignment operator private.
78  Wrapper(Wrapper<T> const& rh); // disallow copy construction
79  Wrapper<T>& operator=(Wrapper<T> const&); // disallow assignment
80 
81  bool present;
82  // T const obj;
83  T obj;
84  };
85 
86 } //namespace edm
87 
89 
90 namespace edm {
91 
92  template <typename T>
93  struct DoFillView {
94  void operator()(T const& obj,
95  ProductID const& id,
96  std::vector<void const*>& pointers,
97  helper_vector_ptr & helpers) const;
98  };
99 
100  template <typename T>
101  struct DoNotFillView {
102  void operator()(T const&,
103  ProductID const&,
104  std::vector<void const*>&,
105  helper_vector_ptr&) const {
107  "The product type ",
108  typeid(T).name(),
109  "\ndoes not support Views\n");
110  }
111  };
112 
113  template <typename T>
114  inline
116  std::vector<void const*>& pointers,
117  helper_vector_ptr& helpers) const {
120  DoNotFillView<T> >::type maybe_filler;
121  maybe_filler(obj, id, pointers, helpers);
122  }
123 
124 
125  template <typename T>
126  struct DoSetPtr {
127  void operator()(T const& obj,
128  std::type_info const& iToType,
129  unsigned long iIndex,
130  void const*& oPtr) const;
131  void operator()(T const& obj,
132  std::type_info const& iToType,
133  std::vector<unsigned long> const& iIndex,
134  std::vector<void const*>& oPtr) const;
135  };
136 
137  template <typename T>
138  struct DoNotSetPtr {
139  void operator()(T const& /*obj*/,
140  std::type_info const& /*iToType*/,
141  unsigned long /*iIndex*/,
142  void const*& /*oPtr*/) const {
144  "The product type ",
145  typeid(T).name(),
146  "\ndoes not support edm::Ptr\n");
147  }
148  void operator()(T const& /*obj*/,
149  std::type_info const& /*iToType*/,
150  std::vector<unsigned long> const& /*iIndexes*/,
151  std::vector<void const*>& /*oPtrs*/) const {
153  "The product type ",
154  typeid(T).name(),
155  "\ndoes not support edm::PtrVector\n");
156  }
157  };
158 
159  template <typename T>
160  inline
161  void Wrapper<T>::do_setPtr(std::type_info const& iToType,
162  unsigned long iIndex,
163  void const*& oPtr) const {
165  DoSetPtr<T>,
166  DoNotSetPtr<T> >::type maybe_filler;
167  maybe_filler(this->obj, iToType, iIndex, oPtr);
168  }
169 
170  template <typename T>
171  void Wrapper<T>::do_fillPtrVector(std::type_info const& iToType,
172  std::vector<unsigned long> const& iIndices,
173  std::vector<void const*>& oPtr) const {
175  DoSetPtr<T>,
176  DoNotSetPtr<T> >::type maybe_filler;
177  maybe_filler(this->obj, iToType, iIndices, oPtr);
178  }
179 
180  // This is an attempt to optimize for speed, by avoiding the copying
181  // of large objects of type T. In this initial version, we assume
182  // that for any class having a 'swap' member function should call
183  // 'swap' rather than copying the object.
184 
185  template <typename T>
186  struct DoSwap {
187  void operator()(T& a, T& b) { a.swap(b); }
188  };
189 
190  template <typename T>
191  struct DoAssign {
192  void operator()(T& a, T& b) { a = b; }
193  };
194 
195 #ifndef __GCCXML__
196  template <typename T>
197  struct IsMergeable {
198  bool operator()(T const&) const { return true; }
199  };
200 
201  template <typename T>
202  struct IsNotMergeable {
203  bool operator()(T const&) const { return false; }
204  };
205 
206  template <typename T>
207  struct DoMergeProduct {
208  bool operator()(T& a, T const& b) { return a.mergeProduct(b); }
209  };
210 
211  template <typename T>
213  bool operator()(T&, T const&) { return true; }
214  };
215 
216  template <typename T>
218  bool operator()(T const&) const { return true; }
219  };
220 
221  template <typename T>
223  bool operator()(T const&) const { return false; }
224  };
225 
226  template <typename T>
228  bool operator()(T const& a, T const& b) const { return a.isProductEqual(b); }
229  };
230 
231  template <typename T>
233  bool operator()(T const&, T const&) const { return true; }
234  };
235 #endif
236 
237  //------------------------------------------------------------
238  // Metafunction support for compile-time selection of code used in
239  // Wrapper constructor
240  //
241 
242  namespace detail {
243  typedef char (& no_tag)[1]; // type indicating FALSE
244  typedef char (& yes_tag)[2]; // type indicating TRUE
245 
246  // Definitions for the following struct and function templates are
247  // not needed; we only require the declarations.
248  template <typename T, void (T::*)(T&)> struct swap_function;
249  template <typename T> no_tag has_swap_helper(...);
250  template <typename T> yes_tag has_swap_helper(swap_function<T, &T::swap> * dummy);
251 
252  template<typename T>
254  static bool const value =
255  sizeof(has_swap_helper<T>(0)) == sizeof(yes_tag);
256  };
257 
258 #ifndef __GCCXML__
259 
260  template <typename T> static yes_tag& has_value_type(typename T::value_type*);
261  template <typename T> static no_tag& has_value_type(...);
262  template <typename T> struct has_typedef_value_type {
263  static const bool value = sizeof(has_value_type<T>(nullptr)) == sizeof(yes_tag);
264  };
266  template <typename T> struct getValueType<T, true> {
267  std::type_info const& operator()() {
268  return typeid(typename T::value_type);
269  }
270  };
271  template <typename T> struct getValueType<T, false> {
272  std::type_info const& operator()() {
273  return typeid(void);
274  }
275  };
276 
277  template <typename T> static yes_tag& has_member_type(typename T::member_type*);
278  template <typename T> static no_tag& has_member_type(...);
279  template <typename T> struct has_typedef_member_type {
280  static const bool value = sizeof(has_member_type<T>(nullptr)) == sizeof(yes_tag);
281  };
283  template <typename T> struct getMemberType<T, true> {
284  std::type_info const& operator()() {
285  return typeid(typename T::member_type);
286  }
287  };
288  template <typename T> struct getMemberType<T, false> {
289  std::type_info const& operator()() {
290  return typeid(void);
291  }
292  };
293 
294  template <typename T, bool (T::*)(T const&)> struct mergeProduct_function;
295  template <typename T> no_tag has_mergeProduct_helper(...);
297 
298  template<typename T>
300  static bool const value =
301  sizeof(has_mergeProduct_helper<T>(0)) == sizeof(yes_tag);
302  };
303 
304  template <typename T, bool (T::*)(T const&) const> struct isProductEqual_function;
305  template <typename T> no_tag has_isProductEqual_helper(...);
307 
308  template<typename T>
310  static bool const value =
311  sizeof(has_isProductEqual_helper<T>(0)) == sizeof(yes_tag);
312  };
313 #endif
314  }
315 
316 #ifndef __GCCXML__
317  template <typename T>
318  Wrapper<T>::Wrapper(std::unique_ptr<T> ptr) :
319  WrapperBase(),
320  present(ptr.get() != 0),
321  obj() {
322  if (present) {
323  // The following will call swap if T has such a function,
324  // and use assignment if T has no such function.
326  DoSwap<T>,
327  DoAssign<T> >::type swap_or_assign;
328  swap_or_assign(obj, *ptr);
329  }
330  }
331 
332  template <typename T>
334  WrapperBase(),
335  present(ptr != 0),
336  obj() {
337  std::unique_ptr<T> temp(ptr);
338  if (present) {
339  // The following will call swap if T has such a function,
340  // and use assignment if T has no such function.
342  DoSwap<T>,
343  DoAssign<T> >::type swap_or_assign;
344  swap_or_assign(obj, *ptr);
345  }
346 
347  }
348 #endif
349 
350 #ifndef __GCCXML__
351  template <typename T>
352  std::type_info const& Wrapper<T>::valueTypeInfo_() const {
353  return detail::getValueType<T>()();
354  }
355 
356  template <typename T>
357  std::type_info const& Wrapper<T>::memberTypeInfo_() const {
358  return detail::getMemberType<T>()();
359  }
360 #endif
361 
362 #ifndef __GCCXML__
363  template <typename T>
367  IsNotMergeable<T> >::type is_mergeable;
368  return is_mergeable(obj);
369  }
370 
371  template <typename T>
372  bool Wrapper<T>::mergeProduct_(WrapperBase const* newProduct) {
373  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
374  assert(wrappedNewProduct != nullptr);
377  DoNotMergeProduct<T> >::type merge_product;
378  return merge_product(obj, wrappedNewProduct->obj);
379  }
380 
381  template <typename T>
385  DoNotHasIsProductEqual<T> >::type has_is_equal;
386  return has_is_equal(obj);
387  }
388 
389  template <typename T>
390  bool Wrapper<T>::isProductEqual_(WrapperBase const* newProduct) const {
391  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
392  assert(wrappedNewProduct != nullptr);
395  DoNotIsProductEqual<T> >::type is_equal;
396  return is_equal(obj, wrappedNewProduct->obj);
397  }
398 #endif
399 }
400 
403 
404 namespace edm {
405  namespace helpers {
406  template<typename T>
407  struct ViewFiller {
408  static void fill(T const& obj,
409  ProductID const& id,
410  std::vector<void const*>& pointers,
411  helper_vector_ptr & helpers) {
413  typedef Ref<T> ref;
416  // fillView is the name of an overload set; each concrete
417  // collection T should supply a fillView function, in the same
418  // namespace at that in which T is defined, or in the 'edm'
419  // namespace.
420  fillView(obj, id, pointers, * helpers);
421  assert(pointers.size() == helpers->size());
422  }
423  };
424 
425  template<typename T>
427  static void fill(RefToBaseVector<T> const& obj,
428  ProductID const&,
429  std::vector<void const*>& pointers,
430  helper_vector_ptr & helpers) {
431  std::auto_ptr<helper_vector> h = obj.vectorHolder();
432  if(h.get() != 0) {
433  pointers.reserve(h->size());
434  // NOTE: the following implementation has unusual signature!
435  fillView(obj, pointers);
436  helpers = helper_vector_ptr(h.release());
437  }
438  }
439  };
440 
441  template<typename T>
442  struct ViewFiller<PtrVector<T> > {
443  static void fill(PtrVector<T> const& obj,
444  ProductID const&,
445  std::vector<void const*>& pointers,
446  helper_vector_ptr & helpers) {
447  std::auto_ptr<helper_vector> h(new reftobase::RefVectorHolder<PtrVector<T> >(obj));
448  if(h.get() != 0) {
449  pointers.reserve(obj.size());
450  // NOTE: the following implementation has unusual signature!
451  fillView(obj, pointers);
452  helpers = helper_vector_ptr(h.release());
453  }
454  }
455  };
456 
457  template<typename T>
458  struct PtrSetter {
459  static void set(T const& obj,
460  std::type_info const& iToType,
461  unsigned long iIndex,
462  void const*& oPtr) {
463  // setPtr is the name of an overload set; each concrete
464  // collection T should supply a fillView function, in the same
465  // namespace at that in which T is defined, or in the 'edm'
466  // namespace.
467  setPtr(obj, iToType, iIndex, oPtr);
468  }
469 
470  static void fill(T const& obj,
471  std::type_info const& iToType,
472  std::vector<unsigned long> const& iIndex,
473  std::vector<void const*>& oPtr) {
474  // fillPtrVector is the name of an overload set; each concrete
475  // collection T should supply a fillPtrVector function, in the same
476  // namespace at that in which T is defined, or in the 'edm'
477  // namespace.
478  fillPtrVector(obj, iToType, iIndex, oPtr);
479  }
480  };
481  }
482 
483 #ifndef __GCCXML__
484  template <typename T>
486  ProductID const& id,
487  std::vector<void const*>& pointers,
488  helper_vector_ptr& helpers) const {
489  helpers::ViewFiller<T>::fill(obj, id, pointers, helpers);
490  }
491 #endif
492 
493  template <typename T>
495  std::type_info const& iToType,
496  unsigned long iIndex,
497  void const*& oPtr) const {
498  helpers::PtrSetter<T>::set(obj, iToType, iIndex, oPtr);
499  }
500 
501  template <typename T>
503  std::type_info const& iToType,
504  std::vector<unsigned long> const& iIndices,
505  std::vector<void const*>& oPtr) const {
506  helpers::PtrSetter<T>::fill(obj, iToType, iIndices, oPtr);
507  }
508 
509 }
510 
514 
515 #endif
void operator()(T &a, T &b)
Definition: Wrapper.h:187
type
Definition: HCALResponse.h:21
virtual std::type_info const & valueTypeInfo_() const
Definition: Wrapper.h:352
static void fill(PtrVector< T > const &obj, ProductID const &, std::vector< void const * > &pointers, helper_vector_ptr &helpers)
Definition: Wrapper.h:443
T value_type
Definition: Wrapper.h:31
T const * operator->() const
Definition: Wrapper.h:39
#define GCC11_OVERRIDE
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:71
char(& yes_tag)[2]
Definition: Wrapper.h:244
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector &helpers)
static void set(T const &obj, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: Wrapper.h:459
std::type_info const & operator()()
Definition: Wrapper.h:289
void fillPtrVector(std::vector< T, A > const &obj, std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr)
Definition: fillPtrVector.h:88
virtual std::type_info const & memberTypeInfo_() const
Definition: Wrapper.h:357
virtual bool isProductEqual_(WrapperBase const *newProduct) const
Definition: Wrapper.h:390
virtual bool isMergeable_() const
Definition: Wrapper.h:364
static std::type_info const & productTypeInfo()
Definition: Wrapper.h:42
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
virtual bool mergeProduct_(WrapperBase const *newProduct)
Definition: Wrapper.h:372
virtual bool hasIsProductEqual_() const
Definition: Wrapper.h:382
void operator()(T const &, ProductID const &, std::vector< void const * > &, helper_vector_ptr &) const
Definition: Wrapper.h:102
void setPtr(std::vector< T, A > const &obj, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: setPtr.h:75
bool operator()(T const &) const
Definition: Wrapper.h:218
std::type_info const & operator()()
Definition: Wrapper.h:272
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
static void fill(T const &obj, std::type_info const &iToType, std::vector< unsigned long > const &iIndex, std::vector< void const * > &oPtr)
Definition: Wrapper.h:470
virtual std::type_info const & dynamicTypeInfo_() const
Definition: Wrapper.h:53
no_tag has_isProductEqual_helper(...)
void operator()(T &a, T &b)
Definition: Wrapper.h:192
static bool const value
Definition: Wrapper.h:254
bool present
Definition: Wrapper.h:81
virtual bool isPresent_() const
Definition: Wrapper.h:52
virtual ~Wrapper()
Definition: Wrapper.h:37
virtual void do_fillView(ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers) const
Definition: Wrapper.h:115
bool operator()(T const &) const
Definition: Wrapper.h:223
static void fill(RefToBaseVector< T > const &obj, ProductID const &, std::vector< void const * > &pointers, helper_vector_ptr &helpers)
Definition: Wrapper.h:427
std::auto_ptr< reftobase::RefVectorHolderBase > vectorHolder() const
std::type_info const & operator()()
Definition: Wrapper.h:284
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
static void fill(T const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers)
Definition: Wrapper.h:408
no_tag has_swap_helper(...)
bool operator()(T const &) const
Definition: Wrapper.h:203
Container::value_type value_type
T wrapped_type
Definition: Wrapper.h:32
std::shared_ptr< reftobase::RefVectorHolderBase > helper_vector_ptr
Definition: EDProductfwd.h:47
char(& no_tag)[1]
Definition: Wrapper.h:243
T const * product() const
Definition: Wrapper.h:38
double b
Definition: hdecay.h:120
bool operator()(T &a, T const &b)
Definition: Wrapper.h:208
virtual std::type_info const & wrappedTypeInfo_() const
Definition: Wrapper.h:54
string const
Definition: compareJSON.py:14
virtual void do_fillPtrVector(std::type_info const &iToType, std::vector< unsigned long > const &iIndices, std::vector< void const * > &oPtr) const
Definition: Wrapper.h:171
#define private
Definition: FWFileEntry.h:17
static yes_tag & has_member_type(typename T::member_type *)
void operator()(T const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers) const
Definition: Wrapper.h:485
void operator()(T const &, std::type_info const &, std::vector< unsigned long > const &, std::vector< void const * > &) const
Definition: Wrapper.h:148
void operator()(T const &, std::type_info const &, unsigned long, void const *&) const
Definition: Wrapper.h:139
double a
Definition: hdecay.h:121
void operator()(T const &obj, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const
Definition: Wrapper.h:494
std::type_info const & operator()()
Definition: Wrapper.h:267
static yes_tag & has_value_type(typename T::value_type *)
volatile std::atomic< bool > shutdown_flag false
bool operator()(T const &) const
Definition: Wrapper.h:198
bool operator()(T const &, T const &) const
Definition: Wrapper.h:233
virtual void do_setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const
Definition: Wrapper.h:161
long double T
Wrapper< T > & operator=(Wrapper< T > const &)
T get(const Candidate &c)
Definition: component.h:55
no_tag has_mergeProduct_helper(...)
bool operator()(T const &a, T const &b) const
Definition: Wrapper.h:228
static std::type_info const & typeInfo()
Definition: Wrapper.h:43
bool operator()(T &, T const &)
Definition: Wrapper.h:213