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
edm::FillViewHelperVector
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
Definition: FillViewHelperVector.h:30
edm::Ptr::key_
key_type key_
Definition: Ptr.h:202
edm::RefCore
Definition: RefCore.h:21
Handle.h
edm::EDProductGetter::getThinnedProduct
virtual std::optional< std::tuple< WrapperBase const *, unsigned int > > getThinnedProduct(ProductID const &, unsigned int key) const =0
edm::Ptr::operator*
T const & operator*() const
Dereference operator.
Definition: Ptr.h:217
edm::Ptr::core_
RefCore core_
Definition: Ptr.h:201
edm::RefCore::setProductPtr
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:57
funct::false
false
Definition: Factorize.h:29
edm::Ptr::hasProductCache
bool hasProductCache() const
Definition: Ptr.h:165
edm::Ptr::operator!
bool operator!() const
Checks for null.
Definition: Ptr.h:148
patZpeak.handle
handle
Definition: patZpeak.py:23
edm::RefCore::productNotFoundException
void productNotFoundException(std::type_info const &type) const
Definition: RefCore.cc:125
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::Ptr::refCore
RefCore const & refCore() const
Definition: Ptr.h:167
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
edm::Ptr::Ptr
Ptr(ProductID const &productID, T const *item, key_type item_key)
Definition: Ptr.h:97
edm::Ptr::Ptr
Ptr(Ptr< U > const &iOther, std::enable_if_t< std::is_base_of< U, T >::value > *=nullptr)
Definition: Ptr.h:126
edm::Ptr::productGetter
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ptr.h:161
edm::Ptr::Ptr
Ptr(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ptr.h:87
cms::cuda::assert
assert(be >=bs)
edm::Ptr::get
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:139
edm::Ptr::Ptr
Ptr(ProductID const &productID, T const *item, key_type item_key, bool transient)
Definition: Ptr.h:100
edm::EDProductGetter::getIt
virtual WrapperBase const * getIt(ProductID const &) const =0
edm::Ptr::Ptr
Ptr(ProductID const &iId)
Definition: Ptr.h:107
edm::Ptr::key
key_type key() const
Definition: Ptr.h:163
watchdog.const
const
Definition: watchdog.py:83
edm::Handle
Definition: AssociativeIterator.h:50
GetProduct.h
edm::mustBeNonZero
EDProductGetter const * mustBeNonZero(EDProductGetter const *prodGetter, std::string refType, ProductID const &productID)
Definition: EDProductGetter.cc:65
edm::RefCore::productGetter
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
edm::EDProductGetter
Definition: EDProductGetter.h:41
FillViewHelperVector.h
edm::operator!=
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
Definition: debugging_allocator.h:75
edm::operator==
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
Definition: debugging_allocator.h:72
OrphanHandle.h
dumpMFGeometry_cfg.prod
prod
Definition: dumpMFGeometry_cfg.py:24
edm::operator<
bool operator<(DetSet< T > const &x, DetSet< T > const &y)
Definition: DetSet.h:89
ProductID.h
CMS_CLASS_VERSION
#define CMS_CLASS_VERSION(_version_)
Definition: CMS_CLASS_VERSION.h:30
edm::Ptr::Ptr
Ptr(Handle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:49
edm::key_traits
Definition: traits.h:32
edm::Ptr::~Ptr
~Ptr()
Destructor.
Definition: Ptr.h:130
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
WrapperBase.h
edm::TestHandle
Definition: TestHandle.h:29
funct::true
true
Definition: Factorize.h:173
edm::RefCore::isTransient
bool isTransient() const
Definition: RefCore.h:105
edm::Ptr::Ptr
Ptr(Ptr< U > const &iOther, std::enable_if_t< std::is_base_of< T, U >::value > *=nullptr)
Definition: Ptr.h:112
edm::detail::GetProduct::address
static const element_type * address(const iter &i)
Definition: GetProduct.h:36
edm::Ptr::id
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:158
edm::Ptr::isTransient
bool isTransient() const
Checks if this Ptr is transient (i.e. not persistable).
Definition: Ptr.h:155
helpers
Definition: makeCompositeCandidate.h:8
value
Definition: value.py:1
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::WrapperBase
Definition: WrapperBase.h:23
edm::RefCore::id
ProductID id() const
Definition: RefCore.h:48
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
svgfig.template
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
edm::Ptr::operator->
T const * operator->() const
Member dereference operator.
Definition: Ptr.h:224
edm::Ptr::Ptr
Ptr(C const *iProduct, key_type iItemKey, bool=true)
Definition: Ptr.h:70
edm::Ptr
Definition: AssociationVector.h:31
edm::RefCore::productPtr
void const * productPtr() const
Definition: RefCore.h:51
edm::Ptr::Ptr
Ptr(TestHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:81
CMS_CLASS_VERSION.h
edm::OrphanHandle
Definition: EDProductfwd.h:39
gen::C
C
Definition: PomwigHadronizer.cc:78
edm::Ptr::getItem_
T const * getItem_(C const *product, key_type iKey)
Definition: Ptr.h:207
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
edm::Ptr::product
void const * product() const
Definition: Ptr.h:170
edm::Ptr::value_type
T value_type
Definition: Ptr.h:45
edm::Ptr::getData_
void getData_(bool throwIfNotFound=true) const
Definition: Ptr.h:179
edm::Ptr::isNonnull
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:146
edm::Ptr::Ptr
Ptr(OrphanHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:54
EDProductGetter.h
edm::Ptr::isAvailable
bool isAvailable() const
Definition: Ptr.h:230
edm::Ptr::key_type
unsigned long key_type
Definition: Ptr.h:44
HolderToVectorTrait_Ptr_specialization.h
traits.h
edm::Ptr::Ptr
Ptr()
Definition: Ptr.h:109
RefCore.h
edm::Ptr::Ptr
Ptr(T const *item, key_type iItemKey)
Definition: Ptr.h:74
edm::PtrVectorBase
Definition: PtrVectorBase.h:32
edm::fillView
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers)
Definition: AssociationVector.h:290
TestHandle.h
edm::Ptr::isNull
bool isNull() const
Checks for null.
Definition: Ptr.h:142
edm::ProductID
Definition: ProductID.h:27