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  requires std::is_base_of_v<T, U>
113  Ptr(Ptr<U> const& iOther)
114  : core_(iOther.id(),
115  (iOther.hasProductCache() ? static_cast<T const*>(iOther.get()) : static_cast<T const*>(nullptr)),
116  iOther.productGetter(),
117  iOther.isTransient()),
118  key_(iOther.key()) {
119  //make sure a race condition didn't happen where between the call to hasProductCache() and
120  // productGetter() the object was gotten
121  if (iOther.hasProductCache() and not hasProductCache()) {
122  core_.setProductPtr(static_cast<T const*>(iOther.get()));
123  }
124  }
125 
126  template <typename U>
127  requires std::is_base_of_v<U, T>
128  explicit Ptr(Ptr<U> const& iOther)
129  : core_(iOther.id(), dynamic_cast<T const*>(iOther.get()), nullptr, iOther.isTransient()), key_(iOther.key()) {}
130 
132  ~Ptr() {}
133 
135  T const& operator*() const;
136 
138  T const* operator->() const;
139 
141  T const* get() const { return isNull() ? nullptr : this->operator->(); }
142 
144  bool isNull() const { return !isNonnull(); }
145 
147  //bool isNonnull() const {return id().isValid(); }
148  bool isNonnull() const { return key_traits<key_type>::value != key_; }
150  bool operator!() const { return isNull(); }
151 
156  bool isAvailable() const;
157 
159  bool isTransient() const { return core_.isTransient(); }
160 
162  ProductID id() const { return core_.id(); }
163 
165  EDProductGetter const* productGetter() const { return core_.productGetter(); }
166 
167  key_type key() const { return key_; }
168 
169  bool hasProductCache() const { return nullptr != core_.productPtr(); }
170 
171  RefCore const& refCore() const { return core_; }
172  // ---------- member functions ---------------------------
173 
174  void const* product() const { return nullptr; }
175 
176  //Used by ROOT storage
178 
179  private:
180  template <typename C>
182 
183  void getData_(bool throwIfNotFound = true) const {
184  EDProductGetter const* getter = productGetter();
185  if (getter != nullptr) {
186  WrapperBase const* prod = getter->getIt(core_.id());
187  unsigned int iKey = key_;
188  if (prod == nullptr) {
189  auto optionalProd = getter->getThinnedProduct(core_.id(), key_);
190  if (not optionalProd.has_value()) {
191  if (throwIfNotFound) {
193  } else {
194  return;
195  }
196  }
197  std::tie(prod, iKey) = *optionalProd;
198  }
199  void const* ad = nullptr;
200  prod->setPtr(typeid(T), iKey, ad);
201  core_.setProductPtr(ad);
202  }
203  }
204  // ---------- member data --------------------------------
207  };
208 
209  template <typename T>
210  template <typename C>
211  T const* Ptr<T>::getItem_(C const* iProduct, key_type iKey) {
212  assert(iProduct != nullptr);
213  typename C::const_iterator it = iProduct->begin();
214  std::advance(it, iKey);
215  T const* address = detail::GetProduct<C>::address(it);
216  return address;
217  }
218 
220  template <typename T>
221  inline T const& Ptr<T>::operator*() const {
222  getData_();
223  return *reinterpret_cast<T const*>(core_.productPtr());
224  }
225 
227  template <typename T>
228  inline T const* Ptr<T>::operator->() const {
229  getData_();
230  return reinterpret_cast<T const*>(core_.productPtr());
231  }
232 
233  template <typename T>
234  inline bool Ptr<T>::isAvailable() const {
235  getData_(false);
236  return hasProductCache();
237  }
238 
239  template <typename T>
240  inline bool operator==(Ptr<T> const& lhs, Ptr<T> const& rhs) {
241  return lhs.refCore() == rhs.refCore() && lhs.key() == rhs.key();
242  }
243 
244  template <typename T>
245  inline bool operator!=(Ptr<T> const& lhs, Ptr<T> const& rhs) {
246  return !(lhs == rhs);
247  }
248 
249  template <typename T>
250  inline bool operator<(Ptr<T> const& lhs, Ptr<T> const& rhs) {
253  return (lhs.refCore() == rhs.refCore() ? lhs.key() < rhs.key() : lhs.refCore() < rhs.refCore());
254  }
255 } // namespace edm
256 
257 //The following is needed to get RefToBase to work with an edm::Ptr
258 //Handle specialization here
260 #include <vector>
261 
262 namespace edm {
263  template <typename T>
264  inline void fillView(std::vector<edm::Ptr<T> > const& obj,
265  ProductID const& id,
266  std::vector<void const*>& pointers,
268  pointers.reserve(obj.size());
269  helpers.reserve(obj.size());
270  for (auto const& p : obj) {
271  if (p.isAvailable()) {
272  pointers.push_back(p.get());
273  } else {
274  pointers.push_back(nullptr);
275  }
276  helpers.emplace_back(p.id(), p.key());
277  }
278  }
279 } // namespace edm
280 
281 #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:162
requires requires
Definition: RefToBase.h:238
Ptr(C const *iProduct, key_type iItemKey, bool=true)
Definition: Ptr.h:70
requires std::is_base_of_v< U, T > Ptr(Ptr< U > const &iOther)
Definition: Ptr.h:128
void getData_(bool throwIfNotFound=true) const
Definition: Ptr.h:183
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:228
bool isAvailable() const
Definition: Ptr.h:234
T const * getItem_(C const *product, key_type iKey)
Definition: Ptr.h:211
T value_type
Definition: Ptr.h:45
requires std::is_base_of_v< T, U > Ptr(Ptr< U > const &iOther)
Definition: Ptr.h:113
Ptr(TestHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:81
bool isNull() const
Checks for null.
Definition: Ptr.h:144
Ptr()
Definition: Ptr.h:109
void const * product() const
Definition: Ptr.h:174
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:148
key_type key_
Definition: Ptr.h:206
T const & operator*() const
Dereference operator.
Definition: Ptr.h:221
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:150
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:141
Ptr(ProductID const &productID, T const *item, key_type item_key)
Definition: Ptr.h:97
bool hasProductCache() const
Definition: Ptr.h:169
Ptr(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ptr.h:87
RefCore core_
Definition: Ptr.h:205
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:165
virtual std::optional< std::tuple< WrapperBase const *, unsigned int > > getThinnedProduct(ProductID const &, unsigned int key) const =0
RefCore const & refCore() const
Definition: Ptr.h:171
HLT enums.
key_type key() const
Definition: Ptr.h:167
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
long double T
~Ptr()
Destructor.
Definition: Ptr.h:132
bool isTransient() const
Checks if this Ptr is transient (i.e. not persistable).
Definition: Ptr.h:159
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector