CMS 3D CMS Logo

Ptr.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_Ptr_h
2 #define DataFormats_Common_Ptr_h
3 // -*- C++ -*-
4 //
5 // Package: Common
6 // Class : Ptr
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Thu Oct 18 14:41:33 CEST 2007
19 //
20 
21 // user include files
33 
34 // system include files
35 #include <type_traits>
36 
37 // forward declarations
38 namespace edm {
39  template <typename T>
40  class Ptr {
41  friend class PtrVectorBase;
42 
43  public:
44  typedef unsigned long key_type;
45  typedef T value_type;
46 
47  // General purpose constructor from handle.
48  template <typename C>
49  Ptr(Handle<C> const& handle, key_type itemKey, bool /*setNow*/ = true)
50  : core_(handle.id(), getItem_(handle.product(), itemKey), nullptr, false), key_(itemKey) {}
51 
52  // General purpose constructor from orphan handle.
53  template <typename C>
54  Ptr(OrphanHandle<C> const& handle, key_type itemKey, bool /*setNow*/ = true)
55  : core_(handle.id(), getItem_(handle.product(), itemKey), nullptr, false), key_(itemKey) {}
56 
57  // General purpose "constructor" from a Ref.
58  // Use the conversion function template:
59  // ptr = refToPtr(ref)
60  // defined in DataFormats/Common/interface/RefToPtr.h
61  // to construct a Ptr<T> from a Ref<C>, where T is C::value_type.
62 
63  // Constructors for ref to object that is not in an event.
64  // An exception will be thrown if an attempt is made to persistify
65  // any object containing this Ptr. Also, in the future work will
66  // be done to throw an exception if an attempt is made to put any object
67  // containing this Ptr into an event(or run or lumi).
68 
69  template <typename C>
70  Ptr(C const* iProduct, key_type iItemKey, bool /*setNow*/ = true)
71  : core_(ProductID(), iProduct != nullptr ? getItem_(iProduct, iItemKey) : nullptr, nullptr, true),
72  key_(iProduct != nullptr ? iItemKey : key_traits<key_type>::value) {}
73 
74  Ptr(T const* item, key_type iItemKey)
75  : core_(ProductID(), item, nullptr, true), key_(item != nullptr ? iItemKey : key_traits<key_type>::value) {}
76 
77  // Constructor from test handle.
78  // An exception will be thrown if an attempt is made to persistify
79  // any object containing this Ptr.
80  template <typename C>
81  Ptr(TestHandle<C> const& handle, key_type itemKey, bool /*setNow*/ = true)
82  : core_(handle.id(), getItem_(handle.product(), itemKey), nullptr, true), key_(itemKey) {}
83 
87  Ptr(ProductID const& productID, key_type itemKey, EDProductGetter const* prodGetter)
88  : core_(productID, nullptr, mustBeNonZero(prodGetter, "Ptr", productID), false), key_(itemKey) {}
89 
97  Ptr(ProductID const& productID, T const* item, key_type item_key)
98  : core_(productID, item, nullptr, false), key_(item_key) {}
99 
100  Ptr(ProductID const& productID, T const* item, key_type item_key, bool transient)
101  : core_(productID, item, nullptr, transient), key_(item_key) {}
102 
107  explicit Ptr(ProductID const& iId) : core_(iId, nullptr, nullptr, false), key_(key_traits<key_type>::value) {}
108 
110 
111  template <typename U>
112  Ptr(Ptr<U> const& iOther, std::enable_if_t<std::is_base_of<T, U>::value>* = nullptr)
113  : core_(iOther.id(),
114  (iOther.hasProductCache() ? static_cast<T const*>(iOther.get()) : static_cast<T const*>(nullptr)),
115  iOther.productGetter(),
116  iOther.isTransient()),
117  key_(iOther.key()) {
118  //make sure a race condition didn't happen where between the call to hasProductCache() and
119  // productGetter() the object was gotten
120  if (iOther.hasProductCache() and not hasProductCache()) {
121  core_.setProductPtr(static_cast<T const*>(iOther.get()));
122  }
123  }
124 
125  template <typename U>
126  explicit Ptr(Ptr<U> const& iOther, std::enable_if_t<std::is_base_of<U, T>::value>* = nullptr)
127  : core_(iOther.id(), dynamic_cast<T const*>(iOther.get()), nullptr, iOther.isTransient()), key_(iOther.key()) {}
128 
130  ~Ptr() {}
131 
133  T const& operator*() const;
134 
136  T const* operator->() const;
137 
139  T const* get() const { return isNull() ? nullptr : this->operator->(); }
140 
142  bool isNull() const { return !isNonnull(); }
143 
145  //bool isNonnull() const {return id().isValid(); }
146  bool isNonnull() const { return key_traits<key_type>::value != key_; }
148  bool operator!() const { return isNull(); }
149 
154  bool isAvailable() const;
155 
157  bool isTransient() const { return core_.isTransient(); }
158 
160  ProductID id() const { return core_.id(); }
161 
163  EDProductGetter const* productGetter() const { return core_.productGetter(); }
164 
165  key_type key() const { return key_; }
166 
167  bool hasProductCache() const { return nullptr != core_.productPtr(); }
168 
169  RefCore const& refCore() const { return core_; }
170  // ---------- member functions ---------------------------
171 
172  void const* product() const { return nullptr; }
173 
174  //Used by ROOT storage
176 
177  private:
178  template <typename C>
180 
181  void getData_(bool throwIfNotFound = true) const {
182  EDProductGetter const* getter = productGetter();
183  if (getter != nullptr) {
184  WrapperBase const* prod = getter->getIt(core_.id());
185  unsigned int iKey = key_;
186  if (prod == nullptr) {
187  auto optionalProd = getter->getThinnedProduct(core_.id(), key_);
188  if (not optionalProd.has_value()) {
189  if (throwIfNotFound) {
191  } else {
192  return;
193  }
194  }
195  std::tie(prod, iKey) = *optionalProd;
196  }
197  void const* ad = nullptr;
198  prod->setPtr(typeid(T), iKey, ad);
199  core_.setProductPtr(ad);
200  }
201  }
202  // ---------- member data --------------------------------
205  };
206 
207  template <typename T>
208  template <typename C>
209  T const* Ptr<T>::getItem_(C const* iProduct, key_type iKey) {
210  assert(iProduct != nullptr);
211  typename C::const_iterator it = iProduct->begin();
212  std::advance(it, iKey);
213  T const* address = detail::GetProduct<C>::address(it);
214  return address;
215  }
216 
218  template <typename T>
219  inline T const& Ptr<T>::operator*() const {
220  getData_();
221  return *reinterpret_cast<T const*>(core_.productPtr());
222  }
223 
225  template <typename T>
226  inline T const* Ptr<T>::operator->() const {
227  getData_();
228  return reinterpret_cast<T const*>(core_.productPtr());
229  }
230 
231  template <typename T>
232  inline bool Ptr<T>::isAvailable() const {
233  getData_(false);
234  return hasProductCache();
235  }
236 
237  template <typename T>
238  inline bool operator==(Ptr<T> const& lhs, Ptr<T> const& rhs) {
239  return lhs.refCore() == rhs.refCore() && lhs.key() == rhs.key();
240  }
241 
242  template <typename T>
243  inline bool operator!=(Ptr<T> const& lhs, Ptr<T> const& rhs) {
244  return !(lhs == rhs);
245  }
246 
247  template <typename T>
248  inline bool operator<(Ptr<T> const& lhs, Ptr<T> const& rhs) {
251  return (lhs.refCore() == rhs.refCore() ? lhs.key() < rhs.key() : lhs.refCore() < rhs.refCore());
252  }
253 } // namespace edm
254 
255 //The following is needed to get RefToBase to work with an edm::Ptr
256 //Handle specialization here
258 #include <vector>
259 
260 namespace edm {
261  template <typename T>
262  inline void fillView(std::vector<edm::Ptr<T> > const& obj,
263  ProductID const& id,
264  std::vector<void const*>& pointers,
266  pointers.reserve(obj.size());
267  helpers.reserve(obj.size());
268  for (auto const& p : obj) {
269  if (p.isAvailable()) {
270  pointers.push_back(p.get());
271  } else {
272  pointers.push_back(nullptr);
273  }
274  helpers.emplace_back(p.id(), p.key());
275  }
276  }
277 } // namespace edm
278 
279 #endif
constexpr bool operator==(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
Ptr(Handle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:49
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const *> &pointers, FillViewHelperVector &helpers)
static const element_type * address(const iter &i)
Definition: GetProduct.h:36
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:160
Ptr(C const *iProduct, key_type iItemKey, bool=true)
Definition: Ptr.h:70
void getData_(bool throwIfNotFound=true) const
Definition: Ptr.h:181
ProductID id() const
Definition: RefCore.h:48
unsigned long key_type
Definition: Ptr.h:44
EDProductGetter const * mustBeNonZero(EDProductGetter const *prodGetter, std::string refType, ProductID const &productID)
#define CMS_CLASS_VERSION(_version_)
assert(be >=bs)
void const * productPtr() const
Definition: RefCore.h:51
Ptr(OrphanHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:54
T const * operator->() const
Member dereference operator.
Definition: Ptr.h:226
bool isAvailable() const
Definition: Ptr.h:232
T const * getItem_(C const *product, key_type iKey)
Definition: Ptr.h:209
T value_type
Definition: Ptr.h:45
Ptr(TestHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:81
bool isNull() const
Checks for null.
Definition: Ptr.h:142
Ptr()
Definition: Ptr.h:109
void const * product() const
Definition: Ptr.h:172
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:57
Ptr(T const *item, key_type iItemKey)
Definition: Ptr.h:74
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:146
key_type key_
Definition: Ptr.h:204
T const & operator*() const
Dereference operator.
Definition: Ptr.h:219
bool isTransient() const
Definition: RefCore.h:105
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
bool operator!() const
Checks for null.
Definition: Ptr.h:148
Definition: value.py:1
constexpr bool operator!=(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
virtual WrapperBase const * getIt(ProductID const &) const =0
Ptr(ProductID const &productID, T const *item, key_type item_key, bool transient)
Definition: Ptr.h:100
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:139
Ptr(ProductID const &productID, T const *item, key_type item_key)
Definition: Ptr.h:97
Ptr(Ptr< U > const &iOther, std::enable_if_t< std::is_base_of< U, T >::value > *=nullptr)
Definition: Ptr.h:126
bool hasProductCache() const
Definition: Ptr.h:167
Ptr(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ptr.h:87
RefCore core_
Definition: Ptr.h:203
Ptr(ProductID const &iId)
Definition: Ptr.h:107
void productNotFoundException(std::type_info const &type) const
Definition: RefCore.cc:125
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ptr.h:163
virtual std::optional< std::tuple< WrapperBase const *, unsigned int > > getThinnedProduct(ProductID const &, unsigned int key) const =0
RefCore const & refCore() const
Definition: Ptr.h:169
HLT enums.
key_type key() const
Definition: Ptr.h:165
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
long double T
~Ptr()
Destructor.
Definition: Ptr.h:130
Ptr(Ptr< U > const &iOther, std::enable_if_t< std::is_base_of< T, U >::value > *=nullptr)
Definition: Ptr.h:112
bool isTransient() const
Checks if this Ptr is transient (i.e. not persistable).
Definition: Ptr.h:157
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector