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 
152  bool isAvailable() const;
153 
155  bool isTransient() const { return core_.isTransient(); }
156 
158  ProductID id() const { return core_.id(); }
159 
161  EDProductGetter const* productGetter() const { return core_.productGetter(); }
162 
163  key_type key() const { return key_; }
164 
165  bool hasProductCache() const { return nullptr != core_.productPtr(); }
166 
167  RefCore const& refCore() const { return core_; }
168  // ---------- member functions ---------------------------
169 
170  void const* product() const { return nullptr; }
171 
172  //Used by ROOT storage
174 
175  private:
176  template <typename C>
178 
179  void getData_(bool throwIfNotFound = true) const {
180  EDProductGetter const* getter = productGetter();
181  if (getter != nullptr) {
182  WrapperBase const* prod = getter->getIt(core_.id());
183  unsigned int iKey = key_;
184  if (prod == nullptr) {
185  auto optionalProd = getter->getThinnedProduct(core_.id(), key_);
186  if (not optionalProd.has_value()) {
187  if (throwIfNotFound) {
189  } else {
190  return;
191  }
192  }
193  std::tie(prod, iKey) = *optionalProd;
194  }
195  void const* ad = nullptr;
196  prod->setPtr(typeid(T), iKey, ad);
197  core_.setProductPtr(ad);
198  }
199  }
200  // ---------- member data --------------------------------
203  };
204 
205  template <typename T>
206  template <typename C>
207  T const* Ptr<T>::getItem_(C const* iProduct, key_type iKey) {
208  assert(iProduct != nullptr);
209  typename C::const_iterator it = iProduct->begin();
210  std::advance(it, iKey);
211  T const* address = detail::GetProduct<C>::address(it);
212  return address;
213  }
214 
216  template <typename T>
217  inline T const& Ptr<T>::operator*() const {
218  getData_();
219  return *reinterpret_cast<T const*>(core_.productPtr());
220  }
221 
223  template <typename T>
224  inline T const* Ptr<T>::operator->() const {
225  getData_();
226  return reinterpret_cast<T const*>(core_.productPtr());
227  }
228 
229  template <typename T>
230  inline bool Ptr<T>::isAvailable() const {
231  getData_(false);
232  return hasProductCache();
233  }
234 
235  template <typename T>
236  inline bool operator==(Ptr<T> const& lhs, Ptr<T> const& rhs) {
237  return lhs.refCore() == rhs.refCore() && lhs.key() == rhs.key();
238  }
239 
240  template <typename T>
241  inline bool operator!=(Ptr<T> const& lhs, Ptr<T> const& rhs) {
242  return !(lhs == rhs);
243  }
244 
245  template <typename T>
246  inline bool operator<(Ptr<T> const& lhs, Ptr<T> const& rhs) {
249  return (lhs.refCore() == rhs.refCore() ? lhs.key() < rhs.key() : lhs.refCore() < rhs.refCore());
250  }
251 } // namespace edm
252 
253 //The following is needed to get RefToBase to work with an edm::Ptr
254 //Handle specialization here
256 #include <vector>
257 
258 namespace edm {
259  template <typename T>
260  inline void fillView(std::vector<edm::Ptr<T> > const& obj,
261  ProductID const& id,
262  std::vector<void const*>& pointers,
264  pointers.reserve(obj.size());
265  helpers.reserve(obj.size());
266  for (auto const& p : obj) {
267  if (p.isAvailable()) {
268  pointers.push_back(p.get());
269  } else {
270  pointers.push_back(nullptr);
271  }
272  helpers.emplace_back(p.id(), p.key());
273  }
274  }
275 } // namespace edm
276 
277 #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:158
Ptr(C const *iProduct, key_type iItemKey, bool=true)
Definition: Ptr.h:70
void getData_(bool throwIfNotFound=true) const
Definition: Ptr.h:179
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:224
bool isAvailable() const
Definition: Ptr.h:230
T const * getItem_(C const *product, key_type iKey)
Definition: Ptr.h:207
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:170
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:202
T const & operator*() const
Dereference operator.
Definition: Ptr.h:217
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:165
Ptr(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ptr.h:87
RefCore core_
Definition: Ptr.h:201
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:161
virtual std::optional< std::tuple< WrapperBase const *, unsigned int > > getThinnedProduct(ProductID const &, unsigned int key) const =0
RefCore const & refCore() const
Definition: Ptr.h:167
HLT enums.
key_type key() const
Definition: Ptr.h:163
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:155
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector