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 #ifndef __GCCXML__
57  virtual bool isMergeable_() const GCC11_OVERRIDE;
58  virtual bool mergeProduct_(WrapperBase const* newProduct) GCC11_OVERRIDE;
59  virtual bool hasIsProductEqual_() const GCC11_OVERRIDE;
60  virtual bool isProductEqual_(WrapperBase const* newProduct) const GCC11_OVERRIDE;
61 #endif
62 
63  virtual void do_fillView(ProductID const& id,
64  std::vector<void const*>& pointers,
65  helper_vector_ptr& helpers) const GCC11_OVERRIDE;
66  virtual void do_setPtr(std::type_info const& iToType,
67  unsigned long iIndex,
68  void const*& oPtr) const GCC11_OVERRIDE;
69  virtual void do_fillPtrVector(std::type_info const& iToType,
70  std::vector<unsigned long> const& iIndices,
71  std::vector<void const*>& oPtr) const GCC11_OVERRIDE;
72 
73  private:
74  // We wish to disallow copy construction and assignment.
75  // We make the copy constructor and assignment operator private.
76  Wrapper(Wrapper<T> const& rh); // disallow copy construction
77  Wrapper<T>& operator=(Wrapper<T> const&); // disallow assignment
78 
79  bool present;
80  // T const obj;
81  T obj;
82  };
83 
84 } //namespace edm
85 
87 
88 namespace edm {
89 
90  template <typename T>
91  struct DoFillView {
92  void operator()(T const& obj,
93  ProductID const& id,
94  std::vector<void const*>& pointers,
95  helper_vector_ptr & helpers) const;
96  };
97 
98  template <typename T>
99  struct DoNotFillView {
100  void operator()(T const&,
101  ProductID const&,
102  std::vector<void const*>&,
103  helper_vector_ptr&) const {
105  "The product type ",
106  typeid(T).name(),
107  "\ndoes not support Views\n");
108  }
109  };
110 
111  template <typename T>
112  inline
114  std::vector<void const*>& pointers,
115  helper_vector_ptr& helpers) const {
118  DoNotFillView<T> >::type maybe_filler;
119  maybe_filler(obj, id, pointers, helpers);
120  }
121 
122 
123  template <typename T>
124  struct DoSetPtr {
125  void operator()(T const& obj,
126  std::type_info const& iToType,
127  unsigned long iIndex,
128  void const*& oPtr) const;
129  void operator()(T const& obj,
130  std::type_info const& iToType,
131  std::vector<unsigned long> const& iIndex,
132  std::vector<void const*>& oPtr) const;
133  };
134 
135  template <typename T>
136  struct DoNotSetPtr {
137  void operator()(T const& /*obj*/,
138  std::type_info const& /*iToType*/,
139  unsigned long /*iIndex*/,
140  void const*& /*oPtr*/) const {
142  "The product type ",
143  typeid(T).name(),
144  "\ndoes not support edm::Ptr\n");
145  }
146  void operator()(T const& /*obj*/,
147  std::type_info const& /*iToType*/,
148  std::vector<unsigned long> const& /*iIndexes*/,
149  std::vector<void const*>& /*oPtrs*/) const {
151  "The product type ",
152  typeid(T).name(),
153  "\ndoes not support edm::PtrVector\n");
154  }
155  };
156 
157  template <typename T>
158  inline
159  void Wrapper<T>::do_setPtr(std::type_info const& iToType,
160  unsigned long iIndex,
161  void const*& oPtr) const {
163  DoSetPtr<T>,
164  DoNotSetPtr<T> >::type maybe_filler;
165  maybe_filler(this->obj, iToType, iIndex, oPtr);
166  }
167 
168  template <typename T>
169  void Wrapper<T>::do_fillPtrVector(std::type_info const& iToType,
170  std::vector<unsigned long> const& iIndices,
171  std::vector<void const*>& oPtr) const {
173  DoSetPtr<T>,
174  DoNotSetPtr<T> >::type maybe_filler;
175  maybe_filler(this->obj, iToType, iIndices, oPtr);
176  }
177 
178  // This is an attempt to optimize for speed, by avoiding the copying
179  // of large objects of type T. In this initial version, we assume
180  // that for any class having a 'swap' member function should call
181  // 'swap' rather than copying the object.
182 
183  template <typename T>
184  struct DoSwap {
185  void operator()(T& a, T& b) { a.swap(b); }
186  };
187 
188  template <typename T>
189  struct DoAssign {
190  void operator()(T& a, T& b) { a = b; }
191  };
192 
193 #ifndef __GCCXML__
194  template <typename T>
195  struct IsMergeable {
196  bool operator()(T const&) const { return true; }
197  };
198 
199  template <typename T>
200  struct IsNotMergeable {
201  bool operator()(T const&) const { return false; }
202  };
203 
204  template <typename T>
205  struct DoMergeProduct {
206  bool operator()(T& a, T const& b) { return a.mergeProduct(b); }
207  };
208 
209  template <typename T>
211  bool operator()(T&, T const&) { return true; }
212  };
213 
214  template <typename T>
216  bool operator()(T const&) const { return true; }
217  };
218 
219  template <typename T>
221  bool operator()(T const&) const { return false; }
222  };
223 
224  template <typename T>
226  bool operator()(T const& a, T const& b) const { return a.isProductEqual(b); }
227  };
228 
229  template <typename T>
231  bool operator()(T const&, T const&) const { return true; }
232  };
233 #endif
234 
235  //------------------------------------------------------------
236  // Metafunction support for compile-time selection of code used in
237  // Wrapper constructor
238  //
239 
240  namespace detail {
241  typedef char (& no_tag)[1]; // type indicating FALSE
242  typedef char (& yes_tag)[2]; // type indicating TRUE
243 
244  // Definitions for the following struct and function templates are
245  // not needed; we only require the declarations.
246  template <typename T, void (T::*)(T&)> struct swap_function;
247  template <typename T> no_tag has_swap_helper(...);
248  template <typename T> yes_tag has_swap_helper(swap_function<T, &T::swap> * dummy);
249 
250  template<typename T>
252  static bool const value =
253  sizeof(has_swap_helper<T>(0)) == sizeof(yes_tag);
254  };
255 
256 #ifndef __GCCXML__
257  template <typename T, bool (T::*)(T const&)> struct mergeProduct_function;
258  template <typename T> no_tag has_mergeProduct_helper(...);
260 
261  template<typename T>
263  static bool const value =
264  sizeof(has_mergeProduct_helper<T>(0)) == sizeof(yes_tag);
265  };
266 
267  template <typename T, bool (T::*)(T const&) const> struct isProductEqual_function;
268  template <typename T> no_tag has_isProductEqual_helper(...);
270 
271  template<typename T>
273  static bool const value =
274  sizeof(has_isProductEqual_helper<T>(0)) == sizeof(yes_tag);
275  };
276 #endif
277  }
278 
279 #ifndef __GCCXML__
280  template <typename T>
281  Wrapper<T>::Wrapper(std::unique_ptr<T> ptr) :
282  WrapperBase(),
283  present(ptr.get() != 0),
284  obj() {
285  if (present) {
286  // The following will call swap if T has such a function,
287  // and use assignment if T has no such function.
289  DoSwap<T>,
290  DoAssign<T> >::type swap_or_assign;
291  swap_or_assign(obj, *ptr);
292  }
293  }
294 
295  template <typename T>
297  WrapperBase(),
298  present(ptr != 0),
299  obj() {
300  std::unique_ptr<T> temp(ptr);
301  if (present) {
302  // The following will call swap if T has such a function,
303  // and use assignment if T has no such function.
305  DoSwap<T>,
306  DoAssign<T> >::type swap_or_assign;
307  swap_or_assign(obj, *ptr);
308  }
309 
310  }
311 #endif
312 
313 #ifndef __GCCXML__
314  template <typename T>
318  IsNotMergeable<T> >::type is_mergeable;
319  return is_mergeable(obj);
320  }
321 
322  template <typename T>
323  bool Wrapper<T>::mergeProduct_(WrapperBase const* newProduct) {
324  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
325  assert(wrappedNewProduct != nullptr);
328  DoNotMergeProduct<T> >::type merge_product;
329  return merge_product(obj, wrappedNewProduct->obj);
330  }
331 
332  template <typename T>
336  DoNotHasIsProductEqual<T> >::type has_is_equal;
337  return has_is_equal(obj);
338  }
339 
340  template <typename T>
341  bool Wrapper<T>::isProductEqual_(WrapperBase const* newProduct) const {
342  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
343  assert(wrappedNewProduct != nullptr);
346  DoNotIsProductEqual<T> >::type is_equal;
347  return is_equal(obj, wrappedNewProduct->obj);
348  }
349 #endif
350 }
351 
354 
355 namespace edm {
356  namespace helpers {
357  template<typename T>
358  struct ViewFiller {
359  static void fill(T const& obj,
360  ProductID const& id,
361  std::vector<void const*>& pointers,
362  helper_vector_ptr & helpers) {
364  typedef Ref<T> ref;
367  // fillView is the name of an overload set; each concrete
368  // collection T should supply a fillView function, in the same
369  // namespace at that in which T is defined, or in the 'edm'
370  // namespace.
371  fillView(obj, id, pointers, * helpers);
372  assert(pointers.size() == helpers->size());
373  }
374  };
375 
376  template<typename T>
378  static void fill(RefToBaseVector<T> const& obj,
379  ProductID const&,
380  std::vector<void const*>& pointers,
381  helper_vector_ptr & helpers) {
382  std::auto_ptr<helper_vector> h = obj.vectorHolder();
383  if(h.get() != 0) {
384  pointers.reserve(h->size());
385  // NOTE: the following implementation has unusual signature!
386  fillView(obj, pointers);
387  helpers = helper_vector_ptr(h.release());
388  }
389  }
390  };
391 
392  template<typename T>
393  struct ViewFiller<PtrVector<T> > {
394  static void fill(PtrVector<T> const& obj,
395  ProductID const&,
396  std::vector<void const*>& pointers,
397  helper_vector_ptr & helpers) {
398  std::auto_ptr<helper_vector> h(new reftobase::RefVectorHolder<PtrVector<T> >(obj));
399  if(h.get() != 0) {
400  pointers.reserve(obj.size());
401  // NOTE: the following implementation has unusual signature!
402  fillView(obj, pointers);
403  helpers = helper_vector_ptr(h.release());
404  }
405  }
406  };
407 
408  template<typename T>
409  struct PtrSetter {
410  static void set(T const& obj,
411  std::type_info const& iToType,
412  unsigned long iIndex,
413  void const*& oPtr) {
414  // setPtr is the name of an overload set; each concrete
415  // collection T should supply a fillView function, in the same
416  // namespace at that in which T is defined, or in the 'edm'
417  // namespace.
418  setPtr(obj, iToType, iIndex, oPtr);
419  }
420 
421  static void fill(T const& obj,
422  std::type_info const& iToType,
423  std::vector<unsigned long> const& iIndex,
424  std::vector<void const*>& oPtr) {
425  // fillPtrVector is the name of an overload set; each concrete
426  // collection T should supply a fillPtrVector function, in the same
427  // namespace at that in which T is defined, or in the 'edm'
428  // namespace.
429  fillPtrVector(obj, iToType, iIndex, oPtr);
430  }
431  };
432  }
433 
434 #ifndef __GCCXML__
435  template <typename T>
437  ProductID const& id,
438  std::vector<void const*>& pointers,
439  helper_vector_ptr& helpers) const {
440  helpers::ViewFiller<T>::fill(obj, id, pointers, helpers);
441  }
442 #endif
443 
444  template <typename T>
446  std::type_info const& iToType,
447  unsigned long iIndex,
448  void const*& oPtr) const {
449  helpers::PtrSetter<T>::set(obj, iToType, iIndex, oPtr);
450  }
451 
452  template <typename T>
454  std::type_info const& iToType,
455  std::vector<unsigned long> const& iIndices,
456  std::vector<void const*>& oPtr) const {
457  helpers::PtrSetter<T>::fill(obj, iToType, iIndices, oPtr);
458  }
459 
460 }
461 
465 
466 #endif
void operator()(T &a, T &b)
Definition: Wrapper.h:185
type
Definition: HCALResponse.h:21
static void fill(PtrVector< T > const &obj, ProductID const &, std::vector< void const * > &pointers, helper_vector_ptr &helpers)
Definition: Wrapper.h:394
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:73
char(& yes_tag)[2]
Definition: Wrapper.h:242
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:410
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 bool isProductEqual_(WrapperBase const *newProduct) const
Definition: Wrapper.h:341
virtual bool isMergeable_() const
Definition: Wrapper.h:315
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:323
virtual bool hasIsProductEqual_() const
Definition: Wrapper.h:333
void operator()(T const &, ProductID const &, std::vector< void const * > &, helper_vector_ptr &) const
Definition: Wrapper.h:100
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:216
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:421
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:190
static bool const value
Definition: Wrapper.h:252
bool present
Definition: Wrapper.h:79
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:113
bool operator()(T const &) const
Definition: Wrapper.h:221
static void fill(RefToBaseVector< T > const &obj, ProductID const &, std::vector< void const * > &pointers, helper_vector_ptr &helpers)
Definition: Wrapper.h:378
std::auto_ptr< reftobase::RefVectorHolderBase > vectorHolder() const
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:359
no_tag has_swap_helper(...)
bool operator()(T const &) const
Definition: Wrapper.h:201
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:241
T const * product() const
Definition: Wrapper.h:38
double b
Definition: hdecay.h:120
bool operator()(T &a, T const &b)
Definition: Wrapper.h:206
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:169
#define private
Definition: FWFileEntry.h:17
void operator()(T const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers) const
Definition: Wrapper.h:436
void operator()(T const &, std::type_info const &, std::vector< unsigned long > const &, std::vector< void const * > &) const
Definition: Wrapper.h:146
void operator()(T const &, std::type_info const &, unsigned long, void const *&) const
Definition: Wrapper.h:137
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:445
volatile std::atomic< bool > shutdown_flag false
bool operator()(T const &) const
Definition: Wrapper.h:196
bool operator()(T const &, T const &) const
Definition: Wrapper.h:231
virtual void do_setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const
Definition: Wrapper.h:159
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:226
static std::type_info const & typeInfo()
Definition: Wrapper.h:43
bool operator()(T &, T const &)
Definition: Wrapper.h:211