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 
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <memory>
20 #include <string>
21 #include <typeinfo>
22 
23 namespace edm {
24  template<typename T>
25  class Wrapper : public WrapperBase {
26  public:
27  typedef T value_type;
28  typedef T wrapped_type; // used with the dictionary to identify Wrappers
30  explicit Wrapper(std::unique_ptr<T> ptr);
31  virtual ~Wrapper() {}
32  T const* product() const {return (present ? &obj : 0);}
33  T const* operator->() const {return product();}
34 
35  //these are used by FWLite
36  static std::type_info const& productTypeInfo() {return typeid(T);}
37  static std::type_info const& typeInfo() {return typeid(Wrapper<T>);}
38 
39  // the constructor takes ownership of T*
40  Wrapper(T*);
41 
42  //Used by ROOT storage
44 
45 private:
46  virtual bool isPresent_() const override {return present;}
47  virtual std::type_info const& dynamicTypeInfo_() const override {return typeid(T);}
48  virtual std::type_info const& wrappedTypeInfo_() const override {return typeid(Wrapper<T>);}
49 
50  virtual std::type_info const& valueTypeInfo_() const override;
51  virtual std::type_info const& memberTypeInfo_() const override;
52  virtual bool isMergeable_() const override;
53  virtual bool mergeProduct_(WrapperBase const* newProduct) override;
54  virtual bool hasIsProductEqual_() const override;
55  virtual bool isProductEqual_(WrapperBase const* newProduct) const override;
56 
57  virtual void do_fillView(ProductID const& id,
58  std::vector<void const*>& pointers,
59  FillViewHelperVector& helpers) const override;
60  virtual void do_setPtr(std::type_info const& iToType,
61  unsigned long iIndex,
62  void const*& oPtr) const override;
63  virtual void do_fillPtrVector(std::type_info const& iToType,
64  std::vector<unsigned long> const& iIndices,
65  std::vector<void const*>& oPtr) const override;
66 
67  private:
68  // We wish to disallow copy construction and assignment.
69  // We make the copy constructor and assignment operator private.
70  Wrapper(Wrapper<T> const& rh); // disallow copy construction
71  Wrapper<T>& operator=(Wrapper<T> const&); // disallow assignment
72 
73  bool present;
74  T obj;
75  };
76 
77  template<typename T>
78  inline
79  void swap_or_assign(T& a, T& b) {
81  }
82 
83  template<typename T>
84  Wrapper<T>::Wrapper(std::unique_ptr<T> ptr) :
85  WrapperBase(),
86  present(ptr.get() != 0),
87  obj() {
88  if (present) {
89  // The following will call swap if T has such a function,
90  // and use assignment if T has no such function.
91  swap_or_assign(obj, *ptr);
92  }
93  }
94 
95  template<typename T>
97  WrapperBase(),
98  present(ptr != 0),
99  obj() {
100  std::unique_ptr<T> temp(ptr);
101  if (present) {
102  // The following will call swap if T has such a function,
103  // and use assignment if T has no such function.
104  swap_or_assign(obj, *ptr);
105  }
106  }
107 
108  template<typename T>
109  inline
110  std::type_info const& Wrapper<T>::valueTypeInfo_() const {
111  return detail::getValueType<T>()();
112  }
113 
114  template<typename T>
115  inline
116  std::type_info const& Wrapper<T>::memberTypeInfo_() const {
117  return detail::getMemberType<T>()();
118  }
119 
120  template<typename T>
121  inline
124  }
125 
126  template<typename T>
127  inline
128  bool Wrapper<T>::mergeProduct_(WrapperBase const* newProduct) {
129  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
130  assert(wrappedNewProduct != nullptr);
131  return detail::doMergeProduct<T>()(obj, wrappedNewProduct->obj);
132  }
133 
134  template<typename T>
135  inline
138  }
139 
140  template<typename T>
141  inline
142  bool Wrapper<T>::isProductEqual_(WrapperBase const* newProduct) const {
143  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
144  assert(wrappedNewProduct != nullptr);
145  return detail::doIsProductEqual<T>()(obj, wrappedNewProduct->obj);
146  }
147 }
148 
149 #include "DataFormats/Common/interface/WrapperView.icc"
150 
151 #endif
T value_type
Definition: Wrapper.h:27
T const * operator->() const
Definition: Wrapper.h:33
virtual void do_fillPtrVector(std::type_info const &iToType, std::vector< unsigned long > const &iIndices, std::vector< void const * > &oPtr) const override
virtual std::type_info const & wrappedTypeInfo_() const override
Definition: Wrapper.h:48
assert(m_qm.get())
void swap_or_assign(T &a, T &b)
Definition: Wrapper.h:79
static std::type_info const & productTypeInfo()
Definition: Wrapper.h:36
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
virtual bool hasIsProductEqual_() const override
Definition: Wrapper.h:136
virtual std::type_info const & valueTypeInfo_() const override
Definition: Wrapper.h:110
virtual bool isProductEqual_(WrapperBase const *newProduct) const override
Definition: Wrapper.h:142
bool present
Definition: Wrapper.h:73
virtual ~Wrapper()
Definition: Wrapper.h:31
virtual std::type_info const & dynamicTypeInfo_() const override
Definition: Wrapper.h:47
virtual bool isPresent_() const override
Definition: Wrapper.h:46
T wrapped_type
Definition: Wrapper.h:28
T const * product() const
Definition: Wrapper.h:32
virtual void do_setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const override
double b
Definition: hdecay.h:120
string const
Definition: compareJSON.py:14
virtual bool mergeProduct_(WrapperBase const *newProduct) override
Definition: Wrapper.h:128
double a
Definition: hdecay.h:121
virtual bool isMergeable_() const override
Definition: Wrapper.h:122
virtual std::type_info const & memberTypeInfo_() const override
Definition: Wrapper.h:116
volatile std::atomic< bool > shutdown_flag false
long double T
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
Wrapper< T > & operator=(Wrapper< T > const &)
T get(const Candidate &c)
Definition: component.h:55
static std::type_info const & typeInfo()
Definition: Wrapper.h:37
virtual void do_fillView(ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers) const override