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 
18 
19 #include "boost/mpl/if.hpp"
20 
21 #include <algorithm>
22 #include <memory>
23 #include <string>
24 #include <typeinfo>
25 
26 namespace edm {
27  template <typename T> class WrapperInterface;
28  template <typename T>
29  class Wrapper {
30  public:
31  typedef T value_type;
32  typedef T wrapped_type; // used with Reflex to identify Wrappers
33  Wrapper() : present(false), obj() {}
34  explicit Wrapper(std::auto_ptr<T> ptr);
35  ~Wrapper() {}
36  T const* product() const {return (present ? &obj : 0);}
37  T const* operator->() const {return product();}
38 
39  //these are used by FWLite
40  static std::type_info const& productTypeInfo() { return typeid(T);}
41  static std::type_info const& typeInfo() { return typeid(Wrapper<T>);}
42 
45  Wrapper(T*);
46 
47  static
49 
50  void fillView(ProductID const& id,
51  std::vector<void const*>& pointers,
52  helper_vector_ptr& helpers) const;
53 
54  void setPtr(std::type_info const& iToType,
55  unsigned long iIndex,
56  void const*& oPtr) const;
57 
58  void fillPtrVector(std::type_info const& iToType,
59  std::vector<unsigned long> const& iIndicies,
60  std::vector<void const*>& oPtr) const;
61 
62  std::type_info const& dynamicTypeInfo() const {return dynamicTypeInfo_();}
63 
64  bool isPresent() const {return present;}
65  std::type_info const& dynamicTypeInfo_() const {return typeid(T);}
66 #ifndef __REFLEX__
67  bool isMergeable() const;
68 
69  bool mergeProduct(Wrapper<T> const* wrappedNewProduct);
70 
71  bool hasIsProductEqual() const;
72 
73  bool isProductEqual(Wrapper<T> const* wrappedNewProduct) const;
74 #endif
75 
76  private:
77  // We wish to disallow copy construction and assignment.
78  // We make the copy constructor and assignment operator private.
79  Wrapper(Wrapper<T> const& rh); // disallow copy construction
80  Wrapper<T>& operator=(Wrapper<T> const&); // disallow assignment
81 
82  bool present;
83  // T const obj;
84  T obj;
85  };
86 
87 } //namespace edm
88 
90 
91 namespace edm {
92 
93  template <typename T>
94  struct DoFillView {
95  void operator()(T const& obj,
96  ProductID const& id,
97  std::vector<void const*>& pointers,
98  helper_vector_ptr & helpers) const;
99  };
100 
101  template <typename T>
102  struct DoNotFillView {
103  void operator()(T const&,
104  ProductID const&,
105  std::vector<void const*>&,
106  helper_vector_ptr&) const {
108  "The product type ",
109  typeid(T).name(),
110  "\ndoes not support Views\n");
111  }
112  };
113 
114  template <typename T>
115  inline
117  std::vector<void const*>& pointers,
118  helper_vector_ptr& helpers) const {
119  // This should never be called with non-empty arguments, or an
120  // invalid ID; any attempt to do so is an indication of a coding error.
121  assert(id.isValid());
122  assert(pointers.empty());
123  assert(helpers.get() == 0);
126  DoNotFillView<T> >::type maybe_filler;
127  maybe_filler(obj, id, pointers, helpers);
128  }
129 
130 
131  template <typename T>
132  struct DoSetPtr {
133  void operator()(T const& obj,
134  std::type_info const& iToType,
135  unsigned long iIndex,
136  void const*& oPtr) const;
137  void operator()(T const& obj,
138  std::type_info const& iToType,
139  std::vector<unsigned long> const& iIndex,
140  std::vector<void const*>& oPtr) const;
141  };
142 
143  template <typename T>
144  struct DoNotSetPtr {
145  void operator()(T const& /*obj*/,
146  std::type_info const& /*iToType*/,
147  unsigned long /*iIndex*/,
148  void const*& /*oPtr*/) const {
150  "The product type ",
151  typeid(T).name(),
152  "\ndoes not support edm::Ptr\n");
153  }
154  void operator()(T const& /*obj*/,
155  std::type_info const& /*iToType*/,
156  std::vector<unsigned long> const& /*iIndexes*/,
157  std::vector<void const*>& /*oPtrs*/) const {
159  "The product type ",
160  typeid(T).name(),
161  "\ndoes not support edm::PtrVector\n");
162  }
163  };
164 
165  template <typename T>
166  inline
167  void Wrapper<T>::setPtr(std::type_info const& iToType,
168  unsigned long iIndex,
169  void const*& oPtr) const {
171  DoSetPtr<T>,
172  DoNotSetPtr<T> >::type maybe_filler;
173  maybe_filler(this->obj, iToType, iIndex, oPtr);
174  }
175 
176  template <typename T>
177  void Wrapper<T>::fillPtrVector(std::type_info const& iToType,
178  std::vector<unsigned long> const& iIndices,
179  std::vector<void const*>& oPtr) const {
181  DoSetPtr<T>,
182  DoNotSetPtr<T> >::type maybe_filler;
183  maybe_filler(this->obj, iToType, iIndices, oPtr);
184  }
185 
186  // This is an attempt to optimize for speed, by avoiding the copying
187  // of large objects of type T. In this initial version, we assume
188  // that for any class having a 'swap' member function should call
189  // 'swap' rather than copying the object.
190 
191  template <typename T>
192  struct DoSwap {
193  void operator()(T& a, T& b) { a.swap(b); }
194  };
195 
196  template <typename T>
197  struct DoAssign {
198  void operator()(T& a, T& b) { a = b; }
199  };
200 
201 #ifndef __REFLEX__
202  template <typename T>
203  struct IsMergeable {
204  bool operator()(T const&) const { return true; }
205  };
206 
207  template <typename T>
208  struct IsNotMergeable {
209  bool operator()(T const&) const { return false; }
210  };
211 
212  template <typename T>
213  struct DoMergeProduct {
214  bool operator()(T& a, T const& b) { return a.mergeProduct(b); }
215  };
216 
217  template <typename T>
219  bool operator()(T&, T const&) { return true; }
220  };
221 
222  template <typename T>
224  bool operator()(T const&) const { return true; }
225  };
226 
227  template <typename T>
229  bool operator()(T const&) const { return false; }
230  };
231 
232  template <typename T>
234  bool operator()(T const& a, T const& b) const { return a.isProductEqual(b); }
235  };
236 
237  template <typename T>
239  bool operator()(T const&, T const&) const { return true; }
240  };
241 #endif
242 
243  //------------------------------------------------------------
244  // Metafunction support for compile-time selection of code used in
245  // Wrapper constructor
246  //
247 
248  namespace detail {
249  typedef char (& no_tag)[1]; // type indicating FALSE
250  typedef char (& yes_tag)[2]; // type indicating TRUE
251 
252  // Definitions for the following struct and function templates are
253  // not needed; we only require the declarations.
254  template <typename T, void (T::*)(T&)> struct swap_function;
255  template <typename T> no_tag has_swap_helper(...);
256  template <typename T> yes_tag has_swap_helper(swap_function<T, &T::swap> * dummy);
257 
258  template<typename T>
260  static bool const value =
261  sizeof(has_swap_helper<T>(0)) == sizeof(yes_tag);
262  };
263 
264 #ifndef __REFLEX__
265  template <typename T, bool (T::*)(T const&)> struct mergeProduct_function;
266  template <typename T> no_tag has_mergeProduct_helper(...);
268 
269  template<typename T>
271  static bool const value =
272  sizeof(has_mergeProduct_helper<T>(0)) == sizeof(yes_tag);
273  };
274 
275  template <typename T, bool (T::*)(T const&) const> struct isProductEqual_function;
276  template <typename T> no_tag has_isProductEqual_helper(...);
278 
279  template<typename T>
281  static bool const value =
282  sizeof(has_isProductEqual_helper<T>(0)) == sizeof(yes_tag);
283  };
284 #endif
285  }
286 
287  template <typename T>
288  Wrapper<T>::Wrapper(std::auto_ptr<T> ptr) :
289  present(ptr.get() != 0),
290  obj() {
291  if (present) {
292  // The following will call swap if T has such a function,
293  // and use assignment if T has no such function.
295  DoSwap<T>,
296  DoAssign<T> >::type swap_or_assign;
297  swap_or_assign(obj, *ptr);
298  }
299  }
300 
301  template <typename T>
303  present(ptr != 0),
304  obj() {
305  std::auto_ptr<T> temp(ptr);
306  if (present) {
307  // The following will call swap if T has such a function,
308  // and use assignment if T has no such function.
310  DoSwap<T>,
311  DoAssign<T> >::type swap_or_assign;
312  swap_or_assign(obj, *ptr);
313  }
314 
315  }
316 
317 #ifndef __REFLEX__
318  template <typename T>
319  bool Wrapper<T>::isMergeable() const {
322  IsNotMergeable<T> >::type is_mergeable;
323  return is_mergeable(obj);
324  }
325 
326  template <typename T>
327  bool Wrapper<T>::mergeProduct(Wrapper<T> const* wrappedNewProduct) {
330  DoNotMergeProduct<T> >::type merge_product;
331  return merge_product(obj, wrappedNewProduct->obj);
332  }
333 
334  template <typename T>
338  DoNotHasIsProductEqual<T> >::type has_is_equal;
339  return has_is_equal(obj);
340  }
341 
342  template <typename T>
343  bool Wrapper<T>::isProductEqual(Wrapper<T> const* wrappedNewProduct) const {
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);
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);
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  template <typename T>
436  ProductID const& id,
437  std::vector<void const*>& pointers,
438  helper_vector_ptr& helpers) const {
439  helpers::ViewFiller<T>::fill(obj, id, pointers, helpers);
440  }
441 
442  template <typename T>
444  std::type_info const& iToType,
445  unsigned long iIndex,
446  void const*& oPtr) const {
447  helpers::PtrSetter<T>::set(obj, iToType, iIndex, oPtr);
448  }
449 
450  template <typename T>
452  std::type_info const& iToType,
453  std::vector<unsigned long> const& iIndices,
454  std::vector<void const*>& oPtr) const {
455  helpers::PtrSetter<T>::fill(obj, iToType, iIndices, oPtr);
456  }
457 
458 }
459 
463 
464 #ifndef __REFLEX__
466 namespace edm {
467  template <typename T>
468  WrapperInterface<T> const*
471  return &instance;
472  }
473 }
474 
475 #endif
476 #endif
void operator()(T &a, T &b)
Definition: Wrapper.h:193
type
Definition: HCALResponse.h:22
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:37
bool isMergeable() const
Definition: Wrapper.h:319
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:73
char(& yes_tag)[2]
Definition: Wrapper.h:250
boost::shared_ptr< reftobase::RefVectorHolderBase > helper_vector_ptr
Definition: EDProductfwd.h:46
bool isPresent() const
Definition: Wrapper.h:64
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
static PFTauRenderPlugin instance
void fillView(ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers) const
Definition: Wrapper.h:116
#define dso_export
Definition: Visibility.h:11
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:97
std::type_info const & dynamicTypeInfo_() const
Definition: Wrapper.h:65
static std::type_info const & productTypeInfo()
Definition: Wrapper.h:40
bool isProductEqual(Wrapper< T > const *wrappedNewProduct) const
Definition: Wrapper.h:343
bool hasIsProductEqual() const
Definition: Wrapper.h:335
void operator()(T const &, ProductID const &, std::vector< void const * > &, helper_vector_ptr &) const
Definition: Wrapper.h:103
void setPtr(std::vector< T, A > const &obj, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: setPtr.h:84
bool operator()(T const &) const
Definition: Wrapper.h:224
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
no_tag has_isProductEqual_helper(...)
void operator()(T &a, T &b)
Definition: Wrapper.h:198
static bool const value
Definition: Wrapper.h:260
static WrapperInterface< T > const * getInterface()
Definition: Wrapper.h:469
bool present
Definition: Wrapper.h:82
bool operator()(T const &) const
Definition: Wrapper.h:229
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
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:209
T wrapped_type
Definition: Wrapper.h:32
char(& no_tag)[1]
Definition: Wrapper.h:249
std::type_info const & dynamicTypeInfo() const
Definition: Wrapper.h:62
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
T const * product() const
Definition: Wrapper.h:36
double b
Definition: hdecay.h:120
bool operator()(T &a, T const &b)
Definition: Wrapper.h:214
string const
Definition: compareJSON.py:14
void operator()(T const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers) const
Definition: Wrapper.h:435
void operator()(T const &, std::type_info const &, std::vector< unsigned long > const &, std::vector< void const * > &) const
Definition: Wrapper.h:154
void operator()(T const &, std::type_info const &, unsigned long, void const *&) const
Definition: Wrapper.h:145
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:443
bool mergeProduct(Wrapper< T > const *wrappedNewProduct)
Definition: Wrapper.h:327
bool operator()(T const &) const
Definition: Wrapper.h:204
void setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const
Definition: Wrapper.h:167
bool operator()(T const &, T const &) const
Definition: Wrapper.h:239
void fillPtrVector(std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr) const
Definition: Wrapper.h:177
long double T
Wrapper< T > & operator=(Wrapper< T > const &)
T get(const Candidate &c)
Definition: component.h:56
no_tag has_mergeProduct_helper(...)
bool operator()(T const &a, T const &b) const
Definition: Wrapper.h:234
static std::type_info const & typeInfo()
Definition: Wrapper.h:41
bool operator()(T &, T const &)
Definition: Wrapper.h:219