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 
32  template<typename... Args>
33  explicit Wrapper( Emplace, Args&&... );
34  ~Wrapper() override {}
35  T const* product() const {return (present ? &obj : nullptr);}
36  T const* operator->() const {return product();}
37 
38  T& bareProduct() { return obj;}
39 
40  //these are used by FWLite
41  static std::type_info const& productTypeInfo() {return typeid(T);}
42  static std::type_info const& typeInfo() {return typeid(Wrapper<T>);}
43 
44  // the constructor takes ownership of T*
45  Wrapper(T*);
46 
47  //Used by ROOT storage
49 
50 private:
51  bool isPresent_() const override {return present;}
52  std::type_info const& dynamicTypeInfo_() const override {return typeid(T);}
53  std::type_info const& wrappedTypeInfo_() const override {return typeid(Wrapper<T>);}
54 
55  std::type_info const& valueTypeInfo_() const override;
56  std::type_info const& memberTypeInfo_() const override;
57  bool isMergeable_() const override;
58  bool mergeProduct_(WrapperBase const* newProduct) override;
59  bool hasIsProductEqual_() const override;
60  bool isProductEqual_(WrapperBase const* newProduct) const override;
61  bool hasSwap_() const override;
62  void swapProduct_(WrapperBase* newProduct) override;
63 
64  void do_fillView(ProductID const& id,
65  std::vector<void const*>& pointers,
66  FillViewHelperVector& helpers) const override;
67  void do_setPtr(std::type_info const& iToType,
68  unsigned long iIndex,
69  void const*& oPtr) const override;
70  void do_fillPtrVector(std::type_info const& iToType,
71  std::vector<unsigned long> const& iIndices,
72  std::vector<void const*>& oPtr) const override;
73 
74  std::shared_ptr<soa::TableExaminerBase> tableExaminer_() const override;
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) = delete; // disallow copy construction
80  Wrapper<T>& operator=(Wrapper<T> const&) = delete; // disallow assignment
81 
82  bool present;
83  T obj;
84  };
85 
86  template<typename T>
87  Wrapper<T>::Wrapper(std::unique_ptr<T> ptr) :
88  WrapperBase(),
89  present(ptr.get() != nullptr),
90  obj() {
91  if (present) {
92  obj = std::move(*ptr);
93  }
94  }
95 
96  template<typename T>
97  template<typename... Args>
99  WrapperBase(),
100  present(true),
101  obj(std::forward<Args>(args)...) {
102  }
103 
104  template<typename T>
106  WrapperBase(),
107  present(ptr != 0),
108  obj() {
109  std::unique_ptr<T> temp(ptr);
110  if (present) {
111  obj = std::move(*ptr);
112  }
113  }
114 
115  template<typename T>
116  inline
117  std::type_info const& Wrapper<T>::valueTypeInfo_() const {
118  return detail::getValueType<T>()();
119  }
120 
121  template<typename T>
122  inline
123  std::type_info const& Wrapper<T>::memberTypeInfo_() const {
124  return detail::getMemberType<T>()();
125  }
126 
127  template<typename T>
128  inline
131  }
132 
133  template<typename T>
134  inline
135  bool Wrapper<T>::mergeProduct_(WrapperBase const* newProduct) {
136  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
137  assert(wrappedNewProduct != nullptr);
138  return detail::doMergeProduct<T>()(obj, wrappedNewProduct->obj);
139  }
140 
141  template<typename T>
142  inline
145  }
146 
147  template<typename T>
148  inline
149  bool Wrapper<T>::isProductEqual_(WrapperBase const* newProduct) const {
150  Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
151  assert(wrappedNewProduct != nullptr);
152  return detail::doIsProductEqual<T>()(obj, wrappedNewProduct->obj);
153  }
154 
155  template<typename T>
156  inline
157  bool Wrapper<T>::hasSwap_() const {
159  }
160 
161  template<typename T>
162  inline
164  Wrapper<T>* wrappedNewProduct = dynamic_cast<Wrapper<T>*>(newProduct);
165  assert(wrappedNewProduct != nullptr);
166  detail::doSwapProduct<T>()(obj, wrappedNewProduct->obj);
167  }
168 
169  namespace soa {
170  template<class T>
172  static std::shared_ptr<edm::soa::TableExaminerBase> make(void const*) {
173  return std::shared_ptr<edm::soa::TableExaminerBase>{};
174  }
175  };
176  }
177  template <typename T>
178  inline
179  std::shared_ptr<edm::soa::TableExaminerBase> Wrapper<T>::tableExaminer_() const {
181  }
182 
183 }
184 
185 #include "DataFormats/Common/interface/WrapperView.icc"
186 
187 #endif
T value_type
Definition: Wrapper.h:27
T const * operator->() const
Definition: Wrapper.h:36
bool isProductEqual_(WrapperBase const *newProduct) const override
Definition: Wrapper.h:149
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:172
std::type_info const & valueTypeInfo_() const override
Definition: Wrapper.h:117
std::type_info const & wrappedTypeInfo_() const override
Definition: Wrapper.h:53
#define nullptr
static std::type_info const & productTypeInfo()
Definition: Wrapper.h:41
#define CMS_CLASS_VERSION(_version_)
bool hasSwap_() const override
Definition: Wrapper.h:157
std::type_info const & dynamicTypeInfo_() const override
Definition: Wrapper.h:52
bool isPresent_() const override
Definition: Wrapper.h:51
T & bareProduct()
Definition: Wrapper.h:38
std::shared_ptr< soa::TableExaminerBase > tableExaminer_() const override
Definition: Wrapper.h:179
bool hasIsProductEqual_() const override
Definition: Wrapper.h:143
bool present
Definition: Wrapper.h:82
~Wrapper() override
Definition: Wrapper.h:34
void do_fillView(ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers) const override
bool isMergeable_() const override
Definition: Wrapper.h:129
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 & get(Event const &event, InputTag const &tag)(false)
Definition: Event.h:658
T const * product() const
Definition: Wrapper.h:35
void swapProduct_(WrapperBase *newProduct) override
Definition: Wrapper.h:163
std::type_info const & memberTypeInfo_() const override
Definition: Wrapper.h:123
bool mergeProduct_(WrapperBase const *newProduct) override
Definition: Wrapper.h:135
HLT enums.
Wrapper< T > & operator=(Wrapper< T > const &)=delete
long double T
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
def move(src, dest)
Definition: eostools.py:511
static std::type_info const & typeInfo()
Definition: Wrapper.h:42