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 "boost/type_traits/is_base_of.hpp"
36 #include "boost/utility/enable_if.hpp"
37 
38 // forward declarations
39 namespace edm {
40  template<typename T>
41  class Ptr {
42  friend class PtrVectorBase;
43  public:
44 
45  typedef unsigned long key_type;
46  typedef T value_type;
47 
48  // General purpose constructor from handle.
49  template<typename C>
50  Ptr(Handle<C> const& handle, key_type itemKey, bool /*setNow*/ = true):
51  core_(handle.id(), getItem_(handle.product(), itemKey), 0, false), key_(itemKey) {}
52 
53  // General purpose constructor from orphan handle.
54  template<typename C>
55  Ptr(OrphanHandle<C> const& handle, key_type itemKey, bool /*setNow*/ = true):
56  core_(handle.id(), getItem_(handle.product(), itemKey), 0, false), key_(itemKey) {}
57 
58  // General purpose "constructor" from a Ref.
59  // Use the conversion function template:
60  // ptr = refToPtr(ref)
61  // defined in DataFormats/Common/interface/RefToPtr.h
62  // to construct a Ptr<T> from a Ref<C>, where T is C::value_type.
63 
64  // Constructors for ref to object that is not in an event.
65  // An exception will be thrown if an attempt is made to persistify
66  // any object containing this Ptr. Also, in the future work will
67  // be done to throw an exception if an attempt is made to put any object
68  // containing this Ptr into an event(or run or lumi).
69 
70  template<typename C>
71  Ptr(C const* iProduct, key_type iItemKey, bool /*setNow*/ = true):
72  core_(ProductID(), iProduct != 0 ? getItem_(iProduct,iItemKey) : 0, 0, true),
73  key_(iProduct != 0 ? iItemKey : key_traits<key_type>::value) {}
74 
75  Ptr(T const* item, key_type iItemKey):
76  core_(ProductID(), item, nullptr, true),
77  key_(item != nullptr ? iItemKey : key_traits<key_type>::value) {}
78 
79  // Constructor from test handle.
80  // An exception will be thrown if an attempt is made to persistify
81  // any object containing this Ptr.
82  template<typename C>
83  Ptr(TestHandle<C> const& handle, key_type itemKey, bool /*setNow*/ = true):
84  core_(handle.id(), getItem_(handle.product(), itemKey), 0, true), key_(itemKey) {}
85 
89  Ptr(ProductID const& productID, key_type itemKey, EDProductGetter const* prodGetter) :
90  core_(productID, 0, mustBeNonZero(prodGetter, "Ptr", productID), false), key_(itemKey) {
91  }
92 
100  Ptr(ProductID const& productID, T const* item, key_type item_key) :
101  core_(productID, item, 0, false),
102  key_(item_key) {
103  }
104 
105  Ptr(ProductID const& productID, T const* item, key_type item_key, bool transient) :
106  core_(productID, item, nullptr, transient),
107  key_(item_key) {
108  }
109 
114  explicit Ptr(ProductID const& iId) :
115  core_(iId, 0, 0, false),
116  key_(key_traits<key_type>::value)
117  { }
118 
119  Ptr():
120  core_(),
121  key_(key_traits<key_type>::value)
122  {}
123 
124  template<typename U>
125  Ptr(Ptr<U> const& iOther, typename boost::enable_if_c<boost::is_base_of<T, U>::value>::type * = 0):
126  core_(iOther.id(),
127  (iOther.hasProductCache() ? static_cast<T const*>(iOther.get()): static_cast<T const*>(0)),
128  iOther.productGetter(),
129  iOther.isTransient()),
130  key_(iOther.key()) {
131  //make sure a race condition didn't happen where between the call to hasProductCache() and
132  // productGetter() the object was gotten
133  if(iOther.hasProductCache() and not hasProductCache()) {
134  core_.setProductPtr(static_cast<T const*>(iOther.get()) );
135  }
136  }
137 
138  template<typename U>
139  explicit
140  Ptr(Ptr<U> const& iOther, typename boost::enable_if_c<boost::is_base_of<U, T>::value>::type * = 0):
141  core_(iOther.id(),
142  dynamic_cast<T const*>(iOther.get()),
143  0,
144  iOther.isTransient()),
145  key_(iOther.key()) {
146  }
147 
149  ~Ptr() {}
150 
152  T const&
153  operator*() const;
154 
156  T const*
157  operator->() const;
158 
160  T const* get() const {
161  return isNull() ? 0 : this->operator->();
162  }
163 
165  bool isNull() const {return !isNonnull(); }
166 
168  //bool isNonnull() const {return id().isValid(); }
169  bool isNonnull() const {return key_traits<key_type>::value != key_;}
171  bool operator!() const {return isNull();}
172 
175  bool isAvailable() const;
176 
178  bool isTransient() const {return core_.isTransient();}
179 
181  ProductID id() const {return core_.id();}
182 
185 
186  key_type key() const {return key_;}
187 
188  bool hasProductCache() const { return nullptr != core_.productPtr(); }
189 
190  RefCore const& refCore() const {return core_;}
191  // ---------- member functions ---------------------------
192 
193  void const* product() const {return 0;}
194 
195  //Used by ROOT storage
197 
198  private:
199 
200  template<typename C>
201  T const* getItem_(C const* product, key_type iKey);
202 
203  void getData_(bool throwIfNotFound = true) const {
204  EDProductGetter const* getter = productGetter();
205  if(getter != nullptr) {
206  WrapperBase const* prod = getter->getIt(core_.id());
207  unsigned int iKey = key_;
208  if(prod == nullptr) {
209  prod = getter->getThinnedProduct(core_.id(), iKey);
210  if(prod == nullptr) {
211  if(throwIfNotFound) {
213  } else {
214  return;
215  }
216  }
217  }
218  void const* ad = nullptr;
219  prod->setPtr(typeid(T), iKey, ad);
220  core_.setProductPtr(ad);
221  }
222  }
223  // ---------- member data --------------------------------
225  key_type key_;
226  };
227 
228  template<typename T>
229  template<typename C>
230  T const* Ptr<T>::getItem_(C const* iProduct, key_type iKey) {
231  assert (iProduct != 0);
232  typename C::const_iterator it = iProduct->begin();
233  std::advance(it,iKey);
234  T const* address = detail::GetProduct<C>::address(it);
235  return address;
236  }
237 
239  template<typename T>
240  inline
241  T const&
243  getData_();
244  return *reinterpret_cast<T const*>(core_.productPtr());
245  }
246 
248  template<typename T>
249  inline
250  T const*
252  getData_();
253  return reinterpret_cast<T const*>(core_.productPtr());
254  }
255 
256  template<typename T>
257  inline
258  bool
260  getData_(false);
261  return hasProductCache();
262  }
263 
264  template<typename T>
265  inline
266  bool
267  operator==(Ptr<T> const& lhs, Ptr<T> const& rhs) {
268  return lhs.refCore() == rhs.refCore() && lhs.key() == rhs.key();
269  }
270 
271  template<typename T>
272  inline
273  bool
274  operator!=(Ptr<T> const& lhs, Ptr<T> const& rhs) {
275  return !(lhs == rhs);
276  }
277 
278  template<typename T>
279  inline
280  bool
281  operator<(Ptr<T> const& lhs, Ptr<T> const& rhs) {
284  return (lhs.refCore() == rhs.refCore() ? lhs.key() < rhs.key() : lhs.refCore() < rhs.refCore());
285  }
286 }
287 
288 //The following is needed to get RefToBase to work with an edm::Ptr
289 //Handle specialization here
291 #include <vector>
292 
293 namespace edm {
294  template <typename T>
295  inline
296  void
297  fillView(std::vector<edm::Ptr<T> > const& obj,
298  ProductID const& id,
299  std::vector<void const*>& pointers,
301  pointers.reserve(obj.size());
302  helpers.reserve(obj.size());
303  for (auto const& p: obj) {
304  if(p.isAvailable()) {
305  pointers.push_back(p.get());
306  }else {
307  pointers.push_back(nullptr);
308  }
309  helpers.emplace_back(p.id(),p.key());
310  }
311  }
312 }
313 
314 #endif
void getData_(bool throwIfNotFound=true) const
Definition: Ptr.h:203
Ptr(Handle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:50
type
Definition: HCALResponse.h:21
EDProductGetter const * mustBeNonZero(EDProductGetter const *prodGetter, std::string refType, ProductID const &productID)
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:58
Ptr(Ptr< U > const &iOther, typename boost::enable_if_c< boost::is_base_of< T, U >::value >::type *=0)
Definition: Ptr.h:125
static const element_type * address(const iter &i)
Definition: GetProduct.h:33
key_type key() const
Definition: Ptr.h:186
void setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const
Definition: WrapperBase.cc:27
Ptr(C const *iProduct, key_type iItemKey, bool=true)
Definition: Ptr.h:71
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:160
bool isAvailable() const
Definition: Ptr.h:259
virtual WrapperBase const * getThinnedProduct(ProductID const &, unsigned int &key) const =0
T const * getItem_(C const *product, key_type iKey)
Definition: Ptr.h:230
void const * product() const
Definition: Ptr.h:193
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ptr.h:184
T const * operator->() const
Member dereference operator.
Definition: Ptr.h:251
unsigned long key_type
Definition: Ptr.h:45
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
#define nullptr
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
Ptr(OrphanHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:55
T value_type
Definition: Ptr.h:46
T const & operator*() const
Dereference operator.
Definition: Ptr.h:242
Ptr(TestHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:83
bool isTransient() const
Checks if this Ptr is transient (i.e. not persistable).
Definition: Ptr.h:178
Ptr()
Definition: Ptr.h:119
Ptr(T const *item, key_type iItemKey)
Definition: Ptr.h:75
key_type key_
Definition: Ptr.h:225
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
void const * productPtr() const
Definition: RefCore.h:52
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers)
bool isNull() const
Checks for null.
Definition: Ptr.h:165
RefCore const & refCore() const
Definition: Ptr.h:190
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:520
Definition: value.py:1
virtual WrapperBase const * getIt(ProductID const &) const =0
void productNotFoundException(std::type_info const &type) const
Definition: RefCore.cc:141
Ptr(ProductID const &productID, T const *item, key_type item_key, bool transient)
Definition: Ptr.h:105
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:169
Ptr(ProductID const &productID, T const *item, key_type item_key)
Definition: Ptr.h:100
bool hasProductCache() const
Definition: Ptr.h:188
Ptr(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ptr.h:89
RefCore core_
Definition: Ptr.h:224
Ptr(ProductID const &iId)
Definition: Ptr.h:114
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:181
EDProductGetter const * productGetter() const
Definition: RefCore.h:84
ProductID id() const
Definition: RefCore.h:49
Ptr(Ptr< U > const &iOther, typename boost::enable_if_c< boost::is_base_of< U, T >::value >::type *=0)
Definition: Ptr.h:140
bool isTransient() const
Definition: RefCore.h:106
HLT enums.
bool operator!() const
Checks for null.
Definition: Ptr.h:171
long double T
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
~Ptr()
Destructor.
Definition: Ptr.h:149