CMS 3D CMS Logo

RefCoreGet.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefCoreGet_h
2 #define DataFormats_Common_RefCoreGet_h
3 
4 /*----------------------------------------------------------------------
5 
6 RefCoreGet: Free function to get the pointer to a referenced product.
7 
8 ----------------------------------------------------------------------*/
9 
13 
14 #include <cassert>
15 #include <typeinfo>
16 
17 namespace edm {
18 
19  namespace refcore {
20  template <typename T>
21  inline
22  T const*
23  getProduct_(RefCore const& ref, const EDProductGetter* prodGetter) {
24  assert (!ref.isTransient());
25  WrapperBase const* product = ref.getProductPtr(typeid(T),prodGetter);
26  Wrapper<T> const* wrapper = static_cast<Wrapper<T> const*>(product);
27  if (wrapper == nullptr) {
28  ref.wrongTypeException(typeid(T), typeid(*product));
29  }
30  ref.setProductPtr(wrapper->product());
31  return wrapper->product();
32  }
33  }
34 
35  // Get the product using a RefCore from a RefProd.
36  // In this case the pointer cache in the RefCore
37  // is designed to hold a pointer to the container product.
38  template <typename T>
39  inline
40  T const*
41  getProduct(RefCore const& ref) {
42  T const* p = static_cast<T const*>(ref.productPtr());
43  if (p != nullptr) return p;
44  if (ref.isTransient()) {
46  }
47  auto productGetter = ref.productGetter();
48  if(nullptr == productGetter) {
49  p =static_cast<T const*>(ref.productPtr());
50  if(p != nullptr) {
51  //another thread updated the value since we checked
52  return p;
53  }
54  }
55  return refcore::getProduct_<T>(ref, productGetter);
56  }
57 
58  namespace refcore {
59  template <typename T>
60  inline
61  T const*
62  getProductWithCoreFromRef_(RefCore const& ref, EDProductGetter const* prodGetter) {
63  WrapperBase const* product = ref.getProductPtr(typeid(T), prodGetter);
64  Wrapper<T> const* wrapper = static_cast<Wrapper<T> const*>(product);
65  if (wrapper == nullptr) {
66  ref.wrongTypeException(typeid(T), typeid(*product));
67  }
68  return wrapper->product();
69  }
70  }
71 
72  // Get the product using a RefCore from a Ref.
73  // In this case the pointer cache in the RefCore
74  // is designed to hold a pointer to an element in the container product.
75  template <typename T>
76  inline
77  T const*
78  getProductWithCoreFromRef(RefCore const& ref, EDProductGetter const* prodGetter) {
79  if (ref.isTransient()) {
81  }
82  return refcore::getProductWithCoreFromRef_<T>(ref, prodGetter);
83  }
84 
85  namespace refcore {
86  template <typename T>
87  inline
88  T const*
89  tryToGetProductWithCoreFromRef_(RefCore const& ref, EDProductGetter const* prodGetter) {
90  WrapperBase const* product = ref.tryToGetProductPtr(typeid(T),prodGetter);
91  if(product == nullptr) {
92  return nullptr;
93  }
94  Wrapper<T> const* wrapper = static_cast<Wrapper<T> const*>(product);
95  return wrapper->product();
96  }
97  }
98 
99  // get the product using a RefCore from a Ref
100  // In this case the pointer cache in the RefCore
101  // is for a pointer to an element in the container product.
102  // In this case we try only, which means we do not throw
103  // if we fail. This gives the calling function the
104  // chance to look in thinned containers.
105  template <typename T>
106  inline
107  T const*
108  tryToGetProductWithCoreFromRef(RefCore const& ref, EDProductGetter const* prodGetter) {
109  if (ref.isTransient()) {
110  ref.nullPointerForTransientException(typeid(T));
111  }
112  return refcore::tryToGetProductWithCoreFromRef_<T>(ref, prodGetter);
113  }
114 
115  namespace refcore {
116  template <typename T>
117  inline
118  T const*
119  getThinnedProduct_(RefCore const& ref, unsigned int& thinnedKey, EDProductGetter const* prodGetter) {
120  WrapperBase const* product = ref.getThinnedProductPtr(typeid(T), thinnedKey, prodGetter);
121  Wrapper<T> const* wrapper = static_cast<Wrapper<T> const*>(product);
122  return wrapper->product();
123  }
124  }
125 
126  template <typename T>
127  inline
128  T const*
129  getThinnedProduct(RefCore const& ref, unsigned int& thinnedKey, EDProductGetter const* prodGetter) {
130  // The pointer to a thinned collection will never be cached
131  // T const* p = static_cast<T const*>(ref.productPtr());
132  // if (p != 0) return p;
133 
134  if (ref.isTransient()) {
135  ref.nullPointerForTransientException(typeid(T));
136  }
137  return refcore::getThinnedProduct_<T>(ref, thinnedKey, prodGetter);
138  }
139 }
140 #endif
T const * getProduct_(RefCore const &ref, const EDProductGetter *prodGetter)
Definition: RefCoreGet.h:23
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:58
WrapperBase const * getProductPtr(std::type_info const &type, EDProductGetter const *prodGetter) const
Definition: RefCore.cc:61
T const * getProduct(RefCore const &ref)
Definition: RefCoreGet.h:41
EDProductGetter const * productGetter(std::atomic< void const * > const &iCache)
T const * tryToGetProductWithCoreFromRef(RefCore const &ref, EDProductGetter const *prodGetter)
Definition: RefCoreGet.h:108
T const * tryToGetProductWithCoreFromRef_(RefCore const &ref, EDProductGetter const *prodGetter)
Definition: RefCoreGet.h:89
WrapperBase const * tryToGetProductPtr(std::type_info const &type, EDProductGetter const *prodGetter) const
Definition: RefCore.cc:97
T const * getThinnedProduct(RefCore const &ref, unsigned int &thinnedKey, EDProductGetter const *prodGetter)
Definition: RefCoreGet.h:129
WrapperBase const * getThinnedProductPtr(std::type_info const &type, unsigned int &thinnedKey, EDProductGetter const *prodGetter) const
Definition: RefCore.cc:115
void const * productPtr() const
Definition: RefCore.h:52
T const * product() const
Definition: Wrapper.h:35
T const * getProductWithCoreFromRef(RefCore const &ref, EDProductGetter const *prodGetter)
Definition: RefCoreGet.h:78
EDProductGetter const * productGetter() const
Definition: RefCore.h:84
bool isTransient() const
Definition: RefCore.h:106
T const * getProductWithCoreFromRef_(RefCore const &ref, EDProductGetter const *prodGetter)
Definition: RefCoreGet.h:62
HLT enums.
T const * getThinnedProduct_(RefCore const &ref, unsigned int &thinnedKey, EDProductGetter const *prodGetter)
Definition: RefCoreGet.h:119
long double T
void wrongTypeException(std::type_info const &expectedType, std::type_info const &actualType) const
Definition: RefCore.cc:151
void nullPointerForTransientException(std::type_info const &type) const
Definition: RefCore.cc:161
static HepMC::HEPEVT_Wrapper wrapper