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 
10 #include <algorithm>
11 #include <memory>
12 #include <string>
13 #include <typeinfo>
14 #include <vector>
15 #include <list>
16 #include <deque>
17 #include <set>
18 
19 #include "boost/mpl/if.hpp"
25 
26 namespace edm {
27 
28  template <typename T>
29  class Wrapper : public EDProduct {
30  public:
31  typedef T value_type;
32  typedef T wrapped_type; // used with Reflex to identify Wrappers
34  explicit Wrapper(std::auto_ptr<T> ptr);
35  virtual ~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  private:
48  virtual bool isPresent_() const {return present;}
49  virtual std::type_info const& dynamicTypeInfo_() const { return typeid(T);}
50 #ifndef __REFLEX__
51  virtual bool isMergeable_() const;
52  virtual bool mergeProduct_(EDProduct const* newProduct);
53  virtual bool hasIsProductEqual_() const;
54  virtual bool isProductEqual_(EDProduct const* newProduct) const;
55 #endif
56  virtual void do_fillView(ProductID const& id,
57  std::vector<void const*>& pointers,
58  helper_vector_ptr& helpers) const;
59  virtual void do_setPtr(std::type_info const& iToType,
60  unsigned long iIndex,
61  void const*& oPtr) const;
62  virtual void do_fillPtrVector(std::type_info const& iToType,
63  std::vector<unsigned long> const& iIndices,
64  std::vector<void const*>& oPtr) const;
65 
66  // We wish to disallow copy construction and assignment.
67  // We make the copy constructor and assignment operator private.
68  Wrapper(Wrapper<T> const& rh); // disallow copy construction
69  Wrapper<T> & operator=(Wrapper<T> const&); // disallow assignment
70 
71  bool present;
72  // T const obj;
73  T obj;
74  };
75 
76 } //namespace edm
77 
79 
80 namespace edm {
81 
82  template <typename T>
83  struct DoFillView {
84  void operator()(T const& obj,
85  ProductID const& id,
86  std::vector<void const*>& pointers,
87  helper_vector_ptr & helpers) const;
88  };
89 
90  template <typename T>
91  struct DoNotFillView {
92  void operator()(T const&,
93  ProductID const&,
94  std::vector<void const*>&,
95  helper_vector_ptr&) const {
97  "The product type ",
98  typeid(T).name(),
99  "\ndoes not support Views\n");
100  }
101  };
102 
103  template <typename T>
104  inline
106  std::vector<void const*>& pointers,
107  helper_vector_ptr& helpers) const {
110  DoNotFillView<T> >::type maybe_filler;
111  maybe_filler(obj, id, pointers, helpers);
112  }
113 
114 
115  template <typename T>
116  struct DoSetPtr {
117  void operator()(T const& obj,
118  std::type_info const& iToType,
119  unsigned long iIndex,
120  void const*& oPtr) const;
121  void operator()(T const& obj,
122  std::type_info const& iToType,
123  std::vector<unsigned long> const& iIndex,
124  std::vector<void const*>& oPtr) const;
125  };
126 
127  template <typename T>
128  struct DoNotSetPtr {
129  void operator()(T const&,
130  std::type_info const&,
131  unsigned long,
132  void const*& oPtr) const {
134  "The product type ",
135  typeid(T).name(),
136  "\ndoes not support edm::Ptr\n");
137  }
138  void operator()(T const& obj,
139  std::type_info const& iToType,
140  std::vector<unsigned long> const& iIndex,
141  std::vector<void const*>& oPtr) const {
143  "The product type ",
144  typeid(T).name(),
145  "\ndoes not support edm::PtrVector\n");
146  }
147  };
148 
149  template <typename T>
150  inline
151  void Wrapper<T>::do_setPtr(std::type_info const& iToType,
152  unsigned long iIndex,
153  void const*& oPtr) const {
155  DoSetPtr<T>,
156  DoNotSetPtr<T> >::type maybe_filler;
157  maybe_filler(this->obj,iToType,iIndex,oPtr);
158  }
159 
160  template <typename T>
161  void Wrapper<T>::do_fillPtrVector(std::type_info const& iToType,
162  std::vector<unsigned long> const& iIndices,
163  std::vector<void const*>& oPtr) const {
165  DoSetPtr<T>,
166  DoNotSetPtr<T> >::type maybe_filler;
167  maybe_filler(this->obj,iToType,iIndices,oPtr);
168  }
169 
170  // This is an attempt to optimize for speed, by avoiding the copying
171  // of large objects of type T. In this initial version, we assume
172  // that for any class having a 'swap' member function should call
173  // 'swap' rather than copying the object.
174 
175  template <typename T>
176  struct DoSwap {
177  void operator()(T& a, T& b) { a.swap(b); }
178  };
179 
180  template <typename T>
181  struct DoAssign {
182  void operator()(T& a, T& b) { a = b; }
183  };
184 
185 #ifndef __REFLEX__
186  template <typename T>
187  struct IsMergeable {
188  bool operator()(T const& a) const { return true; }
189  };
190 
191  template <typename T>
192  struct IsNotMergeable {
193  bool operator()(T const& a) const { return false; }
194  };
195 
196  template <typename T>
197  struct DoMergeProduct {
198  bool operator()(T& a, T const& b) { return a.mergeProduct(b); }
199  };
200 
201  template <typename T>
203  bool operator()(T& a, T const& b) { return true; }
204  };
205 
206  template <typename T>
208  bool operator()(T const& a) const { return true; }
209  };
210 
211  template <typename T>
213  bool operator()(T const& a) const { return false; }
214  };
215 
216  template <typename T>
218  bool operator()(T const& a, T const& b) const { return a.isProductEqual(b); }
219  };
220 
221  template <typename T>
223  bool operator()(T const& a, T const& b) const { return true; }
224  };
225 #endif
226 
227  //------------------------------------------------------------
228  // Metafunction support for compile-time selection of code used in
229  // Wrapper constructor
230  //
231 
232  namespace detail {
233  typedef char (& no_tag)[1]; // type indicating FALSE
234  typedef char (& yes_tag)[2]; // type indicating TRUE
235 
236  // Definitions for the following struct and function templates are
237  // not needed; we only require the declarations.
238  template <typename T, void (T::*)(T&)> struct swap_function;
239  template <typename T> no_tag has_swap_helper(...);
240  template <typename T> yes_tag has_swap_helper(swap_function<T, &T::swap> * dummy);
241 
242  template<typename T>
244  static bool const value =
245  sizeof(has_swap_helper<T>(0)) == sizeof(yes_tag);
246  };
247 
248 #ifndef __REFLEX__
249  template <typename T, bool (T::*)(T const&)> struct mergeProduct_function;
250  template <typename T> no_tag has_mergeProduct_helper(...);
252 
253  template<typename T>
255  static bool const value =
256  sizeof(has_mergeProduct_helper<T>(0)) == sizeof(yes_tag);
257  };
258 
259  template <typename T, bool (T::*)(T const&) const> struct isProductEqual_function;
260  template <typename T> no_tag has_isProductEqual_helper(...);
262 
263  template<typename T>
265  static bool const value =
266  sizeof(has_isProductEqual_helper<T>(0)) == sizeof(yes_tag);
267  };
268 #endif
269  }
270 
271  template <typename T>
272  Wrapper<T>::Wrapper(std::auto_ptr<T> ptr) :
273  EDProduct(),
274  present(ptr.get() != 0),
275  obj() {
276  if (present) {
277  // The following will call swap if T has such a function,
278  // and use assignment if T has no such function.
280  DoSwap<T>,
281  DoAssign<T> >::type swap_or_assign;
282  swap_or_assign(obj, *ptr);
283  }
284  }
285 
286  template <typename T>
288  EDProduct(),
289  present(ptr != 0),
290  obj() {
291  std::auto_ptr<T> temp(ptr);
292  if (present) {
293  // The following will call swap if T has such a function,
294  // and use assignment if T has no such function.
296  DoSwap<T>,
297  DoAssign<T> >::type swap_or_assign;
298  swap_or_assign(obj, *ptr);
299  }
300 
301  }
302 
303 #ifndef __REFLEX__
304  template <typename T>
308  IsNotMergeable<T> >::type is_mergeable;
309  return is_mergeable(obj);
310  }
311 
312  template <typename T>
313  bool Wrapper<T>::mergeProduct_(EDProduct const* newProduct) {
314  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
315  if (wrappedNewProduct == 0) return false;
318  DoNotMergeProduct<T> >::type merge_product;
319  return merge_product(obj, wrappedNewProduct->obj);
320  }
321 
322  template <typename T>
326  DoNotHasIsProductEqual<T> >::type has_is_equal;
327  return has_is_equal(obj);
328  }
329 
330  template <typename T>
331  bool Wrapper<T>::isProductEqual_(EDProduct const* newProduct) const {
332  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
333  if (wrappedNewProduct == 0) return false;
336  DoNotIsProductEqual<T> >::type is_equal;
337  return is_equal(obj, wrappedNewProduct->obj);
338  }
339 #endif
340 }
341 
344 
345 namespace edm {
346  namespace helpers {
347  template<typename T>
348  struct ViewFiller {
349  static void fill(T const& obj,
350  ProductID const& id,
351  std::vector<void const*>& pointers,
352  helper_vector_ptr & helpers) {
354  typedef Ref<T> ref;
357  // fillView is the name of an overload set; each concrete
358  // collection T should supply a fillView function, in the same
359  // namespace at that in which T is defined, or in the 'edm'
360  // namespace.
361  fillView(obj, id, pointers, * helpers);
362  assert(pointers.size() == helpers->size());
363  }
364  };
365 
366  template<typename T>
368  static void fill(RefToBaseVector<T> const& obj,
369  ProductID const& id,
370  std::vector<void const*>& pointers,
371  helper_vector_ptr & helpers) {
372  std::auto_ptr<helper_vector> h = obj.vectorHolder();
373  if(h.get() != 0) {
374  pointers.reserve(h->size());
375  // NOTE: the following implementation has unusual signature!
376  fillView(obj, pointers);
377  helpers = helper_vector_ptr(h);
378  }
379  }
380  };
381 
382  template<typename T>
383  struct ViewFiller<PtrVector<T> > {
384  static void fill(PtrVector<T> const& obj,
385  ProductID const& id,
386  std::vector<void const*>& pointers,
387  helper_vector_ptr & helpers) {
388  std::auto_ptr<helper_vector> h(new reftobase::RefVectorHolder<PtrVector<T> >(obj));
389  if(h.get() != 0) {
390  pointers.reserve(obj.size());
391  // NOTE: the following implementation has unusual signature!
392  fillView(obj, pointers);
393  helpers = helper_vector_ptr(h);
394  }
395  }
396  };
397 
398  template<typename T>
399  struct PtrSetter {
400  static void set(T const& obj,
401  std::type_info const& iToType,
402  unsigned long iIndex,
403  void const*& oPtr) {
404  // setPtr is the name of an overload set; each concrete
405  // collection T should supply a fillView function, in the same
406  // namespace at that in which T is defined, or in the 'edm'
407  // namespace.
408  setPtr(obj, iToType, iIndex, oPtr);
409  }
410 
411  static void fill(T const& obj,
412  std::type_info const& iToType,
413  std::vector<unsigned long> const& iIndex,
414  std::vector<void const*>& oPtr) {
415  // fillPtrVector is the name of an overload set; each concrete
416  // collection T should supply a fillPtrVector function, in the same
417  // namespace at that in which T is defined, or in the 'edm'
418  // namespace.
419  fillPtrVector(obj, iToType, iIndex, oPtr);
420  }
421  };
422  }
423 
424  template <typename T>
426  ProductID const& id,
427  std::vector<void const*>& pointers,
428  helper_vector_ptr& helpers) const {
429  helpers::ViewFiller<T>::fill(obj, id, pointers, helpers);
430  }
431 
432  template <typename T>
434  std::type_info const& iToType,
435  unsigned long iIndex,
436  void const*& oPtr) const {
437  helpers::PtrSetter<T>::set(obj, iToType, iIndex, oPtr);
438  }
439 
440  template <typename T>
442  std::type_info const& iToType,
443  std::vector<unsigned long> const& iIndices,
444  std::vector<void const*>& oPtr) const {
445  helpers::PtrSetter<T>::fill(obj, iToType, iIndices, oPtr);
446  }
447 
448 }
449 
453 
454 #endif
void operator()(T &a, T &b)
Definition: Wrapper.h:177
type
Definition: HCALResponse.h:22
T value_type
Definition: Wrapper.h:31
T const * operator->() const
Definition: Wrapper.h:37
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:72
char(& yes_tag)[2]
Definition: Wrapper.h:234
bool operator()(T const &a) const
Definition: Wrapper.h:188
void operator()(T const &, std::type_info const &, unsigned long, void const *&oPtr) const
Definition: Wrapper.h:129
bool operator()(T const &a) const
Definition: Wrapper.h:213
virtual bool mergeProduct_(EDProduct const *newProduct)
Definition: Wrapper.h:313
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:400
bool operator()(T &a, T const &b)
Definition: Wrapper.h:203
virtual bool isMergeable_() const
Definition: Wrapper.h:305
static std::type_info const & productTypeInfo()
Definition: Wrapper.h:40
bool operator()(T const &a) const
Definition: Wrapper.h:193
void operator()(T const &obj, std::type_info const &iToType, std::vector< unsigned long > const &iIndex, std::vector< void const * > &oPtr) const
Definition: Wrapper.h:138
bool operator()(T const &a, T const &b) const
Definition: Wrapper.h:223
virtual bool hasIsProductEqual_() const
Definition: Wrapper.h:323
static void fill(RefToBaseVector< T > const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers)
Definition: Wrapper.h:368
void operator()(T const &, ProductID const &, std::vector< void const * > &, helper_vector_ptr &) const
Definition: Wrapper.h:92
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:81
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:411
virtual std::type_info const & dynamicTypeInfo_() const
Definition: Wrapper.h:49
no_tag has_isProductEqual_helper(...)
void operator()(T &a, T &b)
Definition: Wrapper.h:182
tuple obj
Example code starts here #.
Definition: VarParsing.py:655
static bool const value
Definition: Wrapper.h:244
bool operator()(T const &a) const
Definition: Wrapper.h:208
bool present
Definition: Wrapper.h:71
virtual bool isPresent_() const
Definition: Wrapper.h:48
virtual ~Wrapper()
Definition: Wrapper.h:35
virtual void do_fillView(ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers) const
Definition: Wrapper.h:105
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:349
no_tag has_swap_helper(...)
void setPtr(OwnVector< T, P > const &obj, std::type_info const &toType, unsigned long index, void const *&ptr)
Definition: OwnVector.h:506
T wrapped_type
Definition: Wrapper.h:32
boost::shared_ptr< reftobase::RefVectorHolderBase > helper_vector_ptr
Definition: EDProductfwd.h:46
ProductStatus present()
Definition: ProductStatus.h:17
char(& no_tag)[1]
Definition: Wrapper.h:233
T const * product() const
Definition: Wrapper.h:36
double b
Definition: hdecay.h:120
bool operator()(T &a, T const &b)
Definition: Wrapper.h:198
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:161
void operator()(T const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers) const
Definition: Wrapper.h:425
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:433
static void fill(PtrVector< T > const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector_ptr &helpers)
Definition: Wrapper.h:384
virtual bool isProductEqual_(EDProduct const *newProduct) const
Definition: Wrapper.h:331
void fillPtrVector(std::vector< T, A > const &obj, const std::type_info &iToType, const std::vector< unsigned long > &iIndicies, std::vector< void const * > &oPtr)
Definition: fillPtrVector.h:95
virtual void do_setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const
Definition: Wrapper.h:151
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
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:218
static std::type_info const & typeInfo()
Definition: Wrapper.h:41