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 the dictionary 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 
43  // the constructor takes ownership of T*
44  Wrapper(T*);
45 
46  static
48 
49  void fillView(ProductID const& id,
50  std::vector<void const*>& pointers,
51  helper_vector_ptr& helpers) const;
52 
53  void setPtr(std::type_info const& iToType,
54  unsigned long iIndex,
55  void const*& oPtr) const;
56 
57  void fillPtrVector(std::type_info const& iToType,
58  std::vector<unsigned long> const& iIndicies,
59  std::vector<void const*>& oPtr) const;
60 
61  std::type_info const& dynamicTypeInfo() const {return dynamicTypeInfo_();}
62 
63  bool isPresent() const {return present;}
64  std::type_info const& dynamicTypeInfo_() const {return typeid(T);}
65 #ifndef __GCCXML__
66  bool isMergeable() const;
67 
68  bool mergeProduct(Wrapper<T> const* wrappedNewProduct);
69 
70  bool hasIsProductEqual() const;
71 
72  bool isProductEqual(Wrapper<T> const* wrappedNewProduct) const;
73 #endif
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 {
118  // This should never be called with non-empty arguments, or an
119  // invalid ID; any attempt to do so is an indication of a coding error.
120  assert(id.isValid());
121  assert(pointers.empty());
122  assert(helpers.get() == 0);
125  DoNotFillView<T> >::type maybe_filler;
126  maybe_filler(obj, id, pointers, helpers);
127  }
128 
129 
130  template <typename T>
131  struct DoSetPtr {
132  void operator()(T const& obj,
133  std::type_info const& iToType,
134  unsigned long iIndex,
135  void const*& oPtr) const;
136  void operator()(T const& obj,
137  std::type_info const& iToType,
138  std::vector<unsigned long> const& iIndex,
139  std::vector<void const*>& oPtr) const;
140  };
141 
142  template <typename T>
143  struct DoNotSetPtr {
144  void operator()(T const& /*obj*/,
145  std::type_info const& /*iToType*/,
146  unsigned long /*iIndex*/,
147  void const*& /*oPtr*/) const {
149  "The product type ",
150  typeid(T).name(),
151  "\ndoes not support edm::Ptr\n");
152  }
153  void operator()(T const& /*obj*/,
154  std::type_info const& /*iToType*/,
155  std::vector<unsigned long> const& /*iIndexes*/,
156  std::vector<void const*>& /*oPtrs*/) const {
158  "The product type ",
159  typeid(T).name(),
160  "\ndoes not support edm::PtrVector\n");
161  }
162  };
163 
164  template <typename T>
165  inline
166  void Wrapper<T>::setPtr(std::type_info const& iToType,
167  unsigned long iIndex,
168  void const*& oPtr) const {
170  DoSetPtr<T>,
171  DoNotSetPtr<T> >::type maybe_filler;
172  maybe_filler(this->obj, iToType, iIndex, oPtr);
173  }
174 
175  template <typename T>
176  void Wrapper<T>::fillPtrVector(std::type_info const& iToType,
177  std::vector<unsigned long> const& iIndices,
178  std::vector<void const*>& oPtr) const {
180  DoSetPtr<T>,
181  DoNotSetPtr<T> >::type maybe_filler;
182  maybe_filler(this->obj, iToType, iIndices, oPtr);
183  }
184 
185  // This is an attempt to optimize for speed, by avoiding the copying
186  // of large objects of type T. In this initial version, we assume
187  // that for any class having a 'swap' member function should call
188  // 'swap' rather than copying the object.
189 
190  template <typename T>
191  struct DoSwap {
192  void operator()(T& a, T& b) { a.swap(b); }
193  };
194 
195  template <typename T>
196  struct DoAssign {
197  void operator()(T& a, T& b) { a = b; }
198  };
199 
200 #ifndef __GCCXML__
201  template <typename T>
202  struct IsMergeable {
203  bool operator()(T const&) const { return true; }
204  };
205 
206  template <typename T>
207  struct IsNotMergeable {
208  bool operator()(T const&) const { return false; }
209  };
210 
211  template <typename T>
212  struct DoMergeProduct {
213  bool operator()(T& a, T const& b) { return a.mergeProduct(b); }
214  };
215 
216  template <typename T>
218  bool operator()(T&, T const&) { return true; }
219  };
220 
221  template <typename T>
223  bool operator()(T const&) const { return true; }
224  };
225 
226  template <typename T>
228  bool operator()(T const&) const { return false; }
229  };
230 
231  template <typename T>
233  bool operator()(T const& a, T const& b) const { return a.isProductEqual(b); }
234  };
235 
236  template <typename T>
238  bool operator()(T const&, T const&) const { return true; }
239  };
240 #endif
241 
242  //------------------------------------------------------------
243  // Metafunction support for compile-time selection of code used in
244  // Wrapper constructor
245  //
246 
247  namespace detail {
248  typedef char (& no_tag)[1]; // type indicating FALSE
249  typedef char (& yes_tag)[2]; // type indicating TRUE
250 
251  // Definitions for the following struct and function templates are
252  // not needed; we only require the declarations.
253  template <typename T, void (T::*)(T&)> struct swap_function;
254  template <typename T> no_tag has_swap_helper(...);
255  template <typename T> yes_tag has_swap_helper(swap_function<T, &T::swap> * dummy);
256 
257  template<typename T>
259  static bool const value =
260  sizeof(has_swap_helper<T>(0)) == sizeof(yes_tag);
261  };
262 
263 #ifndef __GCCXML__
264  template <typename T, bool (T::*)(T const&)> struct mergeProduct_function;
265  template <typename T> no_tag has_mergeProduct_helper(...);
267 
268  template<typename T>
270  static bool const value =
271  sizeof(has_mergeProduct_helper<T>(0)) == sizeof(yes_tag);
272  };
273 
274  template <typename T, bool (T::*)(T const&) const> struct isProductEqual_function;
275  template <typename T> no_tag has_isProductEqual_helper(...);
277 
278  template<typename T>
280  static bool const value =
281  sizeof(has_isProductEqual_helper<T>(0)) == sizeof(yes_tag);
282  };
283 #endif
284  }
285 
286  template <typename T>
287  Wrapper<T>::Wrapper(std::auto_ptr<T> ptr) :
288  present(ptr.get() != 0),
289  obj() {
290  if (present) {
291  // The following will call swap if T has such a function,
292  // and use assignment if T has no such function.
294  DoSwap<T>,
295  DoAssign<T> >::type swap_or_assign;
296  swap_or_assign(obj, *ptr);
297  }
298  }
299 
300  template <typename T>
302  present(ptr != 0),
303  obj() {
304  std::auto_ptr<T> temp(ptr);
305  if (present) {
306  // The following will call swap if T has such a function,
307  // and use assignment if T has no such function.
309  DoSwap<T>,
310  DoAssign<T> >::type swap_or_assign;
311  swap_or_assign(obj, *ptr);
312  }
313 
314  }
315 
316 #ifndef __GCCXML__
317  template <typename T>
318  bool Wrapper<T>::isMergeable() const {
321  IsNotMergeable<T> >::type is_mergeable;
322  return is_mergeable(obj);
323  }
324 
325  template <typename T>
326  bool Wrapper<T>::mergeProduct(Wrapper<T> const* wrappedNewProduct) {
329  DoNotMergeProduct<T> >::type merge_product;
330  return merge_product(obj, wrappedNewProduct->obj);
331  }
332 
333  template <typename T>
337  DoNotHasIsProductEqual<T> >::type has_is_equal;
338  return has_is_equal(obj);
339  }
340 
341  template <typename T>
342  bool Wrapper<T>::isProductEqual(Wrapper<T> const* wrappedNewProduct) const {
345  DoNotIsProductEqual<T> >::type is_equal;
346  return is_equal(obj, wrappedNewProduct->obj);
347  }
348 #endif
349 }
350 
353 
354 namespace edm {
355  namespace helpers {
356  template<typename T>
357  struct ViewFiller {
358  static void fill(T const& obj,
359  ProductID const& id,
360  std::vector<void const*>& pointers,
361  helper_vector_ptr & helpers) {
363  typedef Ref<T> ref;
366  // fillView is the name of an overload set; each concrete
367  // collection T should supply a fillView function, in the same
368  // namespace at that in which T is defined, or in the 'edm'
369  // namespace.
370  fillView(obj, id, pointers, * helpers);
371  assert(pointers.size() == helpers->size());
372  }
373  };
374 
375  template<typename T>
377  static void fill(RefToBaseVector<T> const& obj,
378  ProductID const&,
379  std::vector<void const*>& pointers,
380  helper_vector_ptr & helpers) {
381  std::auto_ptr<helper_vector> h = obj.vectorHolder();
382  if(h.get() != 0) {
383  pointers.reserve(h->size());
384  // NOTE: the following implementation has unusual signature!
385  fillView(obj, pointers);
386  helpers = helper_vector_ptr(h);
387  }
388  }
389  };
390 
391  template<typename T>
392  struct ViewFiller<PtrVector<T> > {
393  static void fill(PtrVector<T> const& obj,
394  ProductID const&,
395  std::vector<void const*>& pointers,
396  helper_vector_ptr & helpers) {
397  std::auto_ptr<helper_vector> h(new reftobase::RefVectorHolder<PtrVector<T> >(obj));
398  if(h.get() != 0) {
399  pointers.reserve(obj.size());
400  // NOTE: the following implementation has unusual signature!
401  fillView(obj, pointers);
402  helpers = helper_vector_ptr(h);
403  }
404  }
405  };
406 
407  template<typename T>
408  struct PtrSetter {
409  static void set(T const& obj,
410  std::type_info const& iToType,
411  unsigned long iIndex,
412  void const*& oPtr) {
413  // setPtr is the name of an overload set; each concrete
414  // collection T should supply a fillView function, in the same
415  // namespace at that in which T is defined, or in the 'edm'
416  // namespace.
417  setPtr(obj, iToType, iIndex, oPtr);
418  }
419 
420  static void fill(T const& obj,
421  std::type_info const& iToType,
422  std::vector<unsigned long> const& iIndex,
423  std::vector<void const*>& oPtr) {
424  // fillPtrVector is the name of an overload set; each concrete
425  // collection T should supply a fillPtrVector function, in the same
426  // namespace at that in which T is defined, or in the 'edm'
427  // namespace.
428  fillPtrVector(obj, iToType, iIndex, oPtr);
429  }
430  };
431  }
432 
433  template <typename T>
435  ProductID const& id,
436  std::vector<void const*>& pointers,
437  helper_vector_ptr& helpers) const {
438  helpers::ViewFiller<T>::fill(obj, id, pointers, helpers);
439  }
440 
441  template <typename T>
443  std::type_info const& iToType,
444  unsigned long iIndex,
445  void const*& oPtr) const {
446  helpers::PtrSetter<T>::set(obj, iToType, iIndex, oPtr);
447  }
448 
449  template <typename T>
451  std::type_info const& iToType,
452  std::vector<unsigned long> const& iIndices,
453  std::vector<void const*>& oPtr) const {
454  helpers::PtrSetter<T>::fill(obj, iToType, iIndices, oPtr);
455  }
456 
457 }
458 
462 
463 #ifndef __GCCXML__
465 namespace edm {
466  template <typename T>
467  WrapperInterface<T> const*
470  return &instance;
471  }
472 }
473 
474 #endif
475 #endif
void operator()(T &a, T &b)
Definition: Wrapper.h:192
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:393
T value_type
Definition: Wrapper.h:31
T const * operator->() const
Definition: Wrapper.h:37
bool isMergeable() const
Definition: Wrapper.h:318
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:73
char(& yes_tag)[2]
Definition: Wrapper.h:249
boost::shared_ptr< reftobase::RefVectorHolderBase > helper_vector_ptr
Definition: EDProductfwd.h:46
bool isPresent() const
Definition: Wrapper.h:63
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:409
static PFTauRenderPlugin instance
void fillView(ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers) const
Definition: Wrapper.h:115
#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:88
std::type_info const & dynamicTypeInfo_() const
Definition: Wrapper.h:64
static std::type_info const & productTypeInfo()
Definition: Wrapper.h:40
bool isProductEqual(Wrapper< T > const *wrappedNewProduct) const
Definition: Wrapper.h:342
bool hasIsProductEqual() const
Definition: Wrapper.h:334
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:223
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:420
no_tag has_isProductEqual_helper(...)
void operator()(T &a, T &b)
Definition: Wrapper.h:197
static bool const value
Definition: Wrapper.h:259
bool present
Definition: Wrapper.h:81
bool operator()(T const &) const
Definition: Wrapper.h:228
static void fill(RefToBaseVector< T > const &obj, ProductID const &, std::vector< void const * > &pointers, helper_vector_ptr &helpers)
Definition: Wrapper.h:377
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:358
no_tag has_swap_helper(...)
bool operator()(T const &) const
Definition: Wrapper.h:208
T wrapped_type
Definition: Wrapper.h:32
char(& no_tag)[1]
Definition: Wrapper.h:248
std::type_info const & dynamicTypeInfo() const
Definition: Wrapper.h:61
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:213
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:434
void operator()(T const &, std::type_info const &, std::vector< unsigned long > const &, std::vector< void const * > &) const
Definition: Wrapper.h:153
void operator()(T const &, std::type_info const &, unsigned long, void const *&) const
Definition: Wrapper.h:144
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:442
bool mergeProduct(Wrapper< T > const *wrappedNewProduct)
Definition: Wrapper.h:326
bool operator()(T const &) const
Definition: Wrapper.h:203
void setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const
Definition: Wrapper.h:166
bool operator()(T const &, T const &) const
Definition: Wrapper.h:238
void fillPtrVector(std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr) const
Definition: Wrapper.h:176
long double T
static WrapperInterface< T > const * getInterface() dso_export
Definition: Wrapper.h:468
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:233
static std::type_info const & typeInfo()
Definition: Wrapper.h:41
bool operator()(T &, T const &)
Definition: Wrapper.h:218