CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
31 
32 // system include files
33 #include "boost/type_traits/is_base_of.hpp"
34 #include "boost/utility/enable_if.hpp"
35 
36 // forward declarations
37 namespace edm {
38  template<typename T>
39  class Ptr {
40  friend class PtrVectorBase;
41  public:
42 
43  typedef unsigned long key_type;
44  typedef T value_type;
45 
46  // General purpose constructor from handle.
47  template<typename C>
48  Ptr(Handle<C> const& handle, key_type itemKey, bool /*setNow*/ = true):
49  core_(handle.id(), getItem_(handle.product(), itemKey), 0, false), key_(itemKey) {}
50 
51  // General purpose constructor from orphan handle.
52  template<typename C>
53  Ptr(OrphanHandle<C> const& handle, key_type itemKey, bool /*setNow*/ = true):
54  core_(handle.id(), getItem_(handle.product(), itemKey), 0, false), key_(itemKey) {}
55 
56  // General purpose "constructor" from a Ref.
57  // Use the conversion function template:
58  // ptr = refToPtr(ref)
59  // defined in DataFormats/Common/interface/RefToPtr.h
60  // to construct a Ptr<T> from a Ref<C>, where T is C::value_type.
61 
62  // Constructor for ref to object that is not in an event.
63  // An exception will be thrown if an attempt is made to persistify
64  // any object containing this Ptr. Also, in the future work will
65  // be done to throw an exception if an attempt is made to put any object
66  // containing this Ptr into an event(or run or lumi).
67  template<typename C>
68  Ptr(C const* iProduct, key_type iItemKey, bool /*setNow*/ = true):
69  core_(ProductID(), iProduct != 0 ? getItem_(iProduct,iItemKey) : 0, 0, true),
70  key_(iProduct != 0 ? iItemKey : key_traits<key_type>::value) {}
71 
72  // Constructor from test handle.
73  // An exception will be thrown if an attempt is made to persistify
74  // any object containing this Ptr.
75  template<typename C>
76  Ptr(TestHandle<C> const& handle, key_type itemKey, bool /*setNow*/ = true):
77  core_(handle.id(), getItem_(handle.product(), itemKey), 0, true), key_(itemKey) {}
78 
82  Ptr(ProductID const& productID, key_type itemKey, EDProductGetter const* prodGetter) :
83  core_(productID, 0, mustBeNonZero(prodGetter, "Ptr", productID), false), key_(itemKey) {
84  }
85 
93  Ptr(ProductID const& productID, T const* item, key_type item_key) :
94  core_(productID, item, 0, false),
95  key_(item_key) {
96  }
97 
102  explicit Ptr(ProductID const& iId) :
103  core_(iId, 0, 0, false),
105  { }
106 
107  Ptr():
108  core_(),
110  {}
111 
112  template<typename U>
113  Ptr(Ptr<U> const& iOther, typename boost::enable_if_c<boost::is_base_of<T, U>::value>::type * = 0):
114  core_(iOther.id(),
115  (iOther.hasProductCache() ? static_cast<T const*>(iOther.get()): static_cast<T const*>(0)),
116  iOther.productGetter(),
117  iOther.isTransient()),
118  key_(iOther.key()) {
119  }
120 
121  template<typename U>
122  explicit
123  Ptr(Ptr<U> const& iOther, typename boost::enable_if_c<boost::is_base_of<U, T>::value>::type * = 0):
124  core_(iOther.id(),
125  dynamic_cast<T const*>(iOther.get()),
126  0,
127  iOther.isTransient()),
128  key_(iOther.key()) {
129  }
130 
132  ~Ptr() {}
133 
135  T const&
136  operator*() const;
137 
139  T const*
140  operator->() const;
141 
143  T const* get() const {
144  return isNull() ? 0 : this->operator->();
145  }
146 
148  bool isNull() const {return !isNonnull(); }
149 
151  //bool isNonnull() const {return id().isValid(); }
152  bool isNonnull() const {return key_traits<key_type>::value != key_;}
154  bool operator!() const {return isNull();}
155 
158  bool isAvailable() const {return core_.isAvailable();}
159 
161  bool isTransient() const {return core_.isTransient();}
162 
164  ProductID id() const {return core_.id();}
165 
168 
169  key_type key() const {return key_;}
170 
171  bool hasProductCache() const { return 0 != core_.productPtr(); }
172 
173  RefCore const& refCore() const {return core_;}
174  // ---------- member functions ---------------------------
175 
176  void const* product() const {return 0;}
177 
178  //Used by ROOT storage
180 
181  private:
182  //Ptr(Ptr const&); // stop default
183 
185  Ptr(T const* item, key_type item_key) :
186  core_(ProductID(), item, 0, true),
187  key_(item_key) {
188  }
189 
190  //Ptr const& operator=(Ptr const&); // stop default
191  template<typename C>
192  T const* getItem_(C const* product, key_type iKey);
193 
194  void getData_() const {
195  if(!hasProductCache() && 0 != productGetter()) {
196  void const* ad = 0;
198  if(!prod.isValid()) {
200  }
201  prod.setPtr(typeid(T), key_, ad);
202  core_.setProductPtr(ad);
203  }
204  }
205  // ---------- member data --------------------------------
208  };
209 
210  template<typename T>
211  template<typename C>
212  T const* Ptr<T>::getItem_(C const* iProduct, key_type iKey) {
213  assert (iProduct != 0);
214  typename C::const_iterator it = iProduct->begin();
215  std::advance(it,iKey);
217  return address;
218  }
219 
221  template<typename T>
222  inline
223  T const&
225  getData_();
226  return *reinterpret_cast<T const*>(core_.productPtr());
227  }
228 
230  template<typename T>
231  inline
232  T const*
234  getData_();
235  return reinterpret_cast<T const*>(core_.productPtr());
236  }
237 
238  template<typename T>
239  inline
240  bool
241  operator==(Ptr<T> const& lhs, Ptr<T> const& rhs) {
242  return lhs.refCore() == rhs.refCore() && lhs.key() == rhs.key();
243  }
244 
245  template<typename T>
246  inline
247  bool
248  operator!=(Ptr<T> const& lhs, Ptr<T> const& rhs) {
249  return !(lhs == rhs);
250  }
251 
252  template<typename T>
253  inline
254  bool
255  operator<(Ptr<T> const& lhs, Ptr<T> const& rhs) {
258  return (lhs.refCore() == rhs.refCore() ? lhs.key() < rhs.key() : lhs.refCore() < rhs.refCore());
259  }
260 
261 }
262 
263 #endif
key_type key_
Definition: Ptr.h:207
Ptr()
Definition: Ptr.h:107
type
Definition: HCALResponse.h:22
unsigned long key_type
Definition: Ptr.h:43
T const * operator->() const
Member dereference operator.
Definition: Ptr.h:233
~Ptr()
Destructor.
Definition: Ptr.h:132
EDProductGetter const * mustBeNonZero(EDProductGetter const *prodGetter, std::string refType, ProductID const &productID)
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:34
char * address
Definition: mlp_lapack.h:14
static const element_type * address(const iter &i)
Definition: GetProduct.h:34
Ptr(Ptr< U > const &iOther, typename boost::enable_if_c< boost::is_base_of< U, T >::value >::type *=0)
Definition: Ptr.h:123
bool operator!() const
Checks for null.
Definition: Ptr.h:154
Ptr(TestHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:76
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:32
T const & operator*() const
Dereference operator.
Definition: Ptr.h:224
Ptr(ProductID const &iId)
Definition: Ptr.h:102
Ptr(Handle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:48
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:152
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:143
virtual WrapperHolder getIt(ProductID const &) const =0
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:164
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
void const * productPtr() const
Definition: RefCore.h:32
bool isAvailable() const
Definition: RefCore.cc:112
tuple handle
Definition: patZpeak.py:22
RefCore const & refCore() const
Definition: Ptr.h:173
T value_type
Definition: Ptr.h:44
Ptr(Ptr< U > const &iOther, typename boost::enable_if_c< boost::is_base_of< T, U >::value >::type *=0)
Definition: Ptr.h:113
void productNotFoundException(std::type_info const &type) const
Definition: RefCore.cc:84
Ptr(ProductID const &productID, T const *item, key_type item_key)
Definition: Ptr.h:93
void const * product() const
Definition: Ptr.h:176
void setPtr(std::type_info const &iToType, unsigned long iIndex, void const *&oPtr) const
Definition: WrapperHolder.h:37
key_type key() const
Definition: Ptr.h:169
RefCore core_
Definition: Ptr.h:206
string const
Definition: compareJSON.py:14
EDProductGetter const * productGetter() const
Definition: RefCore.h:53
void getData_() const
Definition: Ptr.h:194
ProductID id() const
Definition: RefCore.h:29
Ptr(C const *iProduct, key_type iItemKey, bool=true)
Definition: Ptr.h:68
#define private
Definition: FWFileEntry.h:18
bool isTransient() const
Checks if this Ptr is transient (i.e. not persistable).
Definition: Ptr.h:161
Ptr(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ptr.h:82
bool isTransient() const
Definition: RefCore.h:69
bool isNull() const
Checks for null.
Definition: Ptr.h:148
bool hasProductCache() const
Definition: Ptr.h:171
bool isValid() const
Definition: WrapperHolder.h:27
bool isAvailable() const
Definition: Ptr.h:158
Ptr(OrphanHandle< C > const &handle, key_type itemKey, bool=true)
Definition: Ptr.h:53
T const * getItem_(C const *product, key_type iKey)
Definition: Ptr.h:212
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ptr.h:167
long double T