CMS 3D CMS Logo

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  ~Wrapper() override {}
32  T const* product() const {return (present ? &obj : nullptr);}
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  bool isPresent_() const override {return present;}
47  std::type_info const& dynamicTypeInfo_() const override {return typeid(T);}
48  std::type_info const& wrappedTypeInfo_() const override {return typeid(Wrapper<T>);}
49 
50  std::type_info const& valueTypeInfo_() const override;
51  std::type_info const& memberTypeInfo_() const override;
52  bool isMergeable_() const override;
53  bool mergeProduct_(WrapperBase const* newProduct) override;
54  bool hasIsProductEqual_() const override;
55  bool isProductEqual_(WrapperBase const* newProduct) const override;
56 
57  void do_fillView(ProductID const& id,
58  std::vector<void const*>& pointers,
59  FillViewHelperVector& helpers) const override;
60  void do_setPtr(std::type_info const& iToType,
61  unsigned long iIndex,
62  void const*& oPtr) const override;
63  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  std::shared_ptr<soa::TableExaminerBase> tableExaminer_() const override;
68 
69  private:
70  // We wish to disallow copy construction and assignment.
71  // We make the copy constructor and assignment operator private.
72  Wrapper(Wrapper<T> const& rh) = delete; // disallow copy construction
73  Wrapper<T>& operator=(Wrapper<T> const&) = delete; // disallow assignment
74 
75  bool present;
76  T obj;
77  };
78 
79  template<typename T>
80  inline
81  void swap_or_assign(T& a, T& b) {
83  }
84 
85  template<typename T>
86  Wrapper<T>::Wrapper(std::unique_ptr<T> ptr) :
87  WrapperBase(),
88  present(ptr.get() != 0),
89  obj() {
90  if (present) {
91  // The following will call swap if T has such a function,
92  // and use assignment if T has no such function.
93  swap_or_assign(obj, *ptr);
94  }
95  }
96 
97  template<typename T>
99  WrapperBase(),
100  present(ptr != 0),
101  obj() {
102  std::unique_ptr<T> temp(ptr);
103  if (present) {
104  // The following will call swap if T has such a function,
105  // and use assignment if T has no such function.
106  swap_or_assign(obj, *ptr);
107  }
108  }
109 
110  template<typename T>
111  inline
112  std::type_info const& Wrapper<T>::valueTypeInfo_() const {
113  return detail::getValueType<T>()();
114  }
115 
116  template<typename T>
117  inline
118  std::type_info const& Wrapper<T>::memberTypeInfo_() const {
119  return detail::getMemberType<T>()();
120  }
121 
122  template<typename T>
123  inline
126  }
127 
128  template<typename T>
129  inline
130  bool Wrapper<T>::mergeProduct_(WrapperBase const* newProduct) {
131  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
132  assert(wrappedNewProduct != nullptr);
133  return detail::doMergeProduct<T>()(obj, wrappedNewProduct->obj);
134  }
135 
136  template<typename T>
137  inline
140  }
141 
142  template<typename T>
143  inline
144  bool Wrapper<T>::isProductEqual_(WrapperBase const* newProduct) const {
145  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
146  assert(wrappedNewProduct != nullptr);
147  return detail::doIsProductEqual<T>()(obj, wrappedNewProduct->obj);
148  }
149 
150  namespace soa {
151  template<class T>
153  static std::shared_ptr<edm::soa::TableExaminerBase> make(void const*) {
154  return std::shared_ptr<edm::soa::TableExaminerBase>{};
155  }
156  };
157  }
158  template <typename T>
159  inline
160  std::shared_ptr<edm::soa::TableExaminerBase> Wrapper<T>::tableExaminer_() const {
162  }
163 
164 }
165 
166 #include "DataFormats/Common/interface/WrapperView.icc"
167 
168 #endif
T value_type
Definition: Wrapper.h:27
T const * operator->() const
Definition: Wrapper.h:33
bool isProductEqual_(WrapperBase const *newProduct) const override
Definition: Wrapper.h:144
void do_fillPtrVector(std::type_info const &iToType, std::vector< unsigned long > const &iIndices, std::vector< void const * > &oPtr) const override
static std::shared_ptr< edm::soa::TableExaminerBase > make(void const *)
Definition: Wrapper.h:153
std::type_info const & valueTypeInfo_() const override
Definition: Wrapper.h:112
std::type_info const & wrappedTypeInfo_() const override
Definition: Wrapper.h:48
void swap_or_assign(T &a, T &b)
Definition: Wrapper.h:81
static std::type_info const & productTypeInfo()
Definition: Wrapper.h:36
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
std::type_info const & dynamicTypeInfo_() const override
Definition: Wrapper.h:47
bool isPresent_() const override
Definition: Wrapper.h:46
std::shared_ptr< soa::TableExaminerBase > tableExaminer_() const override
Definition: Wrapper.h:160
bool hasIsProductEqual_() const override
Definition: Wrapper.h:138
bool present
Definition: Wrapper.h:75
~Wrapper() override
Definition: Wrapper.h:31
void do_fillView(ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers) const override
bool isMergeable_() const override
Definition: Wrapper.h:124
T wrapped_type
Definition: Wrapper.h:28
void do_setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const override
T const * product() const
Definition: Wrapper.h:32
double b
Definition: hdecay.h:120
std::type_info const & memberTypeInfo_() const override
Definition: Wrapper.h:118
bool mergeProduct_(WrapperBase const *newProduct) override
Definition: Wrapper.h:130
HLT enums.
double a
Definition: hdecay.h:121
Wrapper< T > & operator=(Wrapper< T > const &)=delete
long double T
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
T get(const Candidate &c)
Definition: component.h:55
static std::type_info const & typeInfo()
Definition: Wrapper.h:37