CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Ref.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_Ref_h
2 #define DataFormats_Common_Ref_h
3 
4 /*----------------------------------------------------------------------
5 
6 Ref: A template for a interproduct reference to a member of a product_.
7 
8 ----------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------
75 // This defines the public interface to the class Ref<C, T, F>.
76 // C is the collection type.
77 // T (default C::value_type) is the type of an element in the collection.
78 //
79 // ProductID productID is the product ID of the collection.
80 // key_type itemKey is the key of the element in the collection.
81 // C::value_type *itemPtr is a C++ pointer to the element
82 // Ref<C, T, F> const& ref is another Ref<C, T, F>
83 
84 // Constructors
85  Ref(); // Default constructor
86  Ref(Ref<C, T> const& ref); // Copy constructor (default, not explicitly specified)
87 
88  Ref(Handle<C> const& handle, key_type itemKey);
89  Ref(ProductID pid, key_type itemKey, EDProductGetter const* prodGetter);
90 
91 // Destructor
92  virtual ~Ref() {}
93 
94  // Operators and methods
95  Ref<C, T>& operator=(Ref<C, T> const&); // assignment (default, not explicitly specified)
96  T const& operator*() const; // dereference
97  T const* const operator->() const; // member dereference
98  bool operator==(Ref<C, T> const& ref) const; // equality
99  bool operator!=(Ref<C, T> const& ref) const; // inequality
100  bool operator<(Ref<C, T> const& ref) const; // ordering
101  bool isNonnull() const; // true if an object is referenced
102  bool isNull() const; // equivalent to !isNonnull()
103  bool operator!() const; // equivalent to !isNonnull()
104  ----------------------------------------------------------------------*/
105 
116 
117 #include "boost/functional.hpp"
118 #include "boost/call_traits.hpp"
119 #include "boost/type_traits.hpp"
120 #include "boost/mpl/has_xxx.hpp"
121 #include "boost/utility/enable_if.hpp"
122 
123 BOOST_MPL_HAS_XXX_TRAIT_DEF(key_compare)
124 
125  template <typename C, typename K>
126  typename boost::enable_if<has_key_compare<C>, bool>::type
127  compare_key(K const& lhs, K const& rhs) {
128  typedef typename C::key_compare comparison_functor;
129  return comparison_functor()(lhs, rhs);
130  }
131 
132  template <typename C, typename K>
133  typename boost::disable_if<has_key_compare<C>, bool>::type
134  compare_key(K const& lhs, K const& rhs) {
135  return lhs < rhs;
136  }
137 
139 
140 namespace edm {
141  template<typename C, typename T, typename F>
142  class RefVector;
143 
144  template<typename T>
145  class RefToBaseVector;
146 
147  template <typename C,
148  typename T = typename refhelper::ValueTrait<C>::value,
149  typename F = typename refhelper::FindTrait<C, T>::value>
150  class Ref {
151  private:
154  friend class RefVectorIterator<C, T, F>;
155  friend class RefVector<C, T, F>;
156  friend class RefVector<RefVector<C, T, F>, T, VF>;
157  friend class RefVector<RefVector<RefVector<C, T, F>, T, VF>, T, VF>;
158  friend class RefVector<RefVector<C, T, F>, T, VBF>;
159  friend class RefVector<RefVector<RefVector<C, T, F>, T, VBF>, T, VBF>;
161 
162  public:
164  typedef C product_type;
165  typedef T value_type;
166  typedef T const element_type; //used for generic programming
167  typedef F finder_type;
168  typedef typename boost::binary_traits<F>::second_argument_type argument_type;
172 
175 
177  Ref(Handle<C> const& handle, key_type itemKey, bool setNow=true);
178 
180  Ref(OrphanHandle<C> const& handle, key_type itemKey, bool setNow=true);
181 
183  // Note. refvector[index] returns a Ref where index is the index into
184  // the RefVector. This index argument is the index into the COLLECTION,
185  // not the index into the RefVector.
186  Ref(RefVector<C, T, F> const& refvector, key_type itemKey, bool setNow=true);
187 
189  // An exception will be thrown if an attempt is made to persistify
190  // any object containing this Ref. Also, in the future work will
191  // be done to throw an exception if an attempt is made to put any object
192  // containing this Ref into an event(or run or lumi).
193  Ref(C const* product, key_type itemKey, bool setNow=true);
194 
196  // An exception will be thrown if an attempt is made to persistify
197  // any object containing this Ref. Also, in the future work will
198  Ref(TestHandle<C> const& handle, key_type itemKey, bool setNow=true);
199 
203  Ref(ProductID const& productID, key_type itemKey, EDProductGetter const* prodGetter) :
204  product_(productID, 0, mustBeNonZero(prodGetter, "Ref", productID), false), index_(itemKey) {
205  }
206 
208  // It is an error (not diagnosable at compile- or run-time) to call
209  // this constructor with a pointer to a T unless the pointed-to T
210  // object is already in a collection of type C stored in the
211  // Event. The given ProductID must be the id of the collection in
212  // the Event.
213 
214  Ref(ProductID const& iProductID, T const* /*item*/, key_type iItemKey, C const* iProduct) :
215  product_(iProductID, iProduct, 0, false), index_(iItemKey)
216  { }
217 
221 
222  explicit Ref(ProductID const& iId) :
223  product_(iId, 0, 0, false), index_(key_traits<key_type>::value)
224  { }
225 
227  Ref(RefProd<C> const& refProd, key_type itemKey);
228 
230  ~Ref() {}
231 
233  T const&
234  operator*() const;
235 
237  T const*
238  operator->() const;
239 
241  T const* get() const {
242  return isNull() ? 0 : this->operator->();
243  }
244 
246  bool isNull() const {return !isNonnull(); }
247 
250 
252  bool operator!() const {return isNull();}
253 
255  ProductID id() const {return product_.id();}
256 
259 
261  // Accessor must get the product if necessary
262  C const* product() const;
263 
265  key_type key() const {return index_;}
266 
267  // This one just for backward compatibility. Will be removed soon.
268  key_type index() const {return index_;}
269 
271  bool hasProductCache() const {return product_.productPtr() != 0;}
272 
275  bool isAvailable() const {return product_.isAvailable();}
276 
278  bool isTransient() const {return product_.isTransient();}
279 
280  RefCore const& refCore() const {return product_;}
281 
282  //Used by ROOT storage
284  // private:
285  // Constructor from member of RefVector
286  Ref(RefCore const& iRefCore, key_type const& iKey) :
287  product_(iRefCore), index_(iKey) {
288  }
289 
290  private:
291  // Compile time check that the argument is a C* or C const*
292  // or derived from it.
293  void checkTypeAtCompileTime(C const*) {}
294 
295  mutable RefCore product_;
297 
298  };
299 }
300 
304 
305 namespace edm {
307  template <typename C, typename T, typename F>
308  inline
309  Ref<C, T, F>::Ref(Handle<C> const& handle, key_type itemKey, bool setNow) :
310  product_(handle.id(), handle.product(), 0, false), index_(itemKey){
312  assert(key() == itemKey);
313 
314  if (setNow) {getPtr_<C, T, F>(product_, index_);}
315  }
316 
318  template <typename C, typename T, typename F>
319  inline
320  Ref<C, T, F>::Ref(OrphanHandle<C> const& handle, key_type itemKey, bool setNow) :
321  product_(handle.id(), handle.product(), 0, false), index_(itemKey) {
323  assert(key() == itemKey);
324 
325  if (setNow) {getPtr_<C, T, F>(product_, index_);}
326  }
327 
329  template <typename C, typename T, typename F>
330  inline
331  Ref<C, T, F>::Ref(RefVector<C, T, F> const& refvector, key_type itemKey, bool setNow) :
332  product_(refvector.id(), refvector.product(), 0, refvector.isTransient()), index_(itemKey) {
333  checkTypeAtCompileTime(refvector.product());
334  assert(key() == itemKey);
335 
336  if (setNow) {getPtr_<C, T, F>(product_, index_);}
337  }
338 
340  // An exception will be thrown if an attempt is made to persistify
341  // any object containing this Ref. Also, in the future work will
342  // be done to throw an exception if an attempt is made to put any object
343  // containing this Ref into an event(or run or lumi).
344  // Note: It is legal for the referenced object to be put into the event
345  // and persistified. It is this Ref itself that cannot be persistified.
346  template <typename C, typename T, typename F>
347  inline
348  Ref<C, T, F>::Ref(C const* iProduct, key_type iItemKey, bool iSetNow) :
349  product_(ProductID(), iProduct, 0, true), index_(iProduct != 0 ? iItemKey : key_traits<key_type>::value) {
350  checkTypeAtCompileTime(iProduct);
351  assert(key() == (iProduct != 0 ? iItemKey : key_traits<key_type>::value));
352 
353  if (iSetNow && iProduct != 0) {getPtr_<C, T, F>(product_, index_);}
354  }
355 
357  // An exception will be thrown if an attempt is made to persistify any object containing this Ref.
358  template <typename C, typename T, typename F>
359  inline
360  Ref<C, T, F>::Ref(TestHandle<C> const& handle, key_type itemKey, bool setNow) :
361  product_(handle.id(), handle.product(), 0, true), index_(itemKey) {
363  assert(key() == itemKey);
364 
365  if (setNow) {getPtr_<C, T, F>(product_, index_);}
366  }
367 
369  template <typename C, typename T, typename F>
370  inline
371  Ref<C, T, F>::Ref(RefProd<C> const& refProd, key_type itemKey) :
372  product_(refProd.id(), refProd.refCore().productPtr(), refProd.refCore().productGetter(), refProd.refCore().isTransient()), index_(itemKey) {
373  assert(index() == itemKey);
374  }
375 
377  // Accessor must get the product if necessary
378  template <typename C, typename T, typename F>
379  inline
380  C const*
382  return isNull() ? 0 : edm::template getProduct<C>(product_);
383  }
384 
386  template <typename C, typename T, typename F>
387  inline
388  T const&
390  return *getPtr<C, T, F>(product_, index_);
391  }
392 
394  template <typename C, typename T, typename F>
395  inline
396  T const*
398  return getPtr<C, T, F>(product_, index_);
399  }
400 
401  template <typename C, typename T, typename F>
402  inline
403  bool
404  operator==(Ref<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
405  return lhs.key() == rhs.key() && lhs.refCore() == rhs.refCore() ;
406  }
407 
408  template <typename C, typename T, typename F>
409  inline
410  bool
411  operator!=(Ref<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
412  return !(lhs == rhs);
413  }
414 
415  template <typename C, typename T, typename F>
416  inline
417  bool
418  operator<(Ref<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
421  return (lhs.refCore() == rhs.refCore() ? compare_key<C>(lhs.key(), rhs.key()) : lhs.refCore() < rhs.refCore());
422  }
423 
424 }
425 
430 
431 namespace edm {
432  namespace reftobase {
433 
434  template <typename T, typename REF>
436  static std::auto_ptr<BaseVectorHolder<T> > makeVectorHolder() {
437  typedef RefVector<typename REF::product_type,
438  typename REF::value_type,
439  typename REF::finder_type> REFV;
440  return std::auto_ptr<BaseVectorHolder<T> >(new VectorHolder<T, REFV>);
441  }
442  static std::auto_ptr<RefVectorHolderBase> makeVectorBaseHolder() {
443  typedef RefVector<typename REF::product_type,
444  typename REF::value_type,
445  typename REF::finder_type> REFV;
446  return std::auto_ptr<RefVectorHolderBase>(new RefVectorHolder<REFV>);
447  }
448  };
449 
450  template<typename T1, typename C, typename T, typename F>
451  struct HolderToVectorTrait<T1, Ref<C, T, F> > {
453  };
454 
455  template <typename REF>
457  static std::auto_ptr<RefVectorHolderBase> makeVectorHolder() {
458  typedef RefVector<typename REF::product_type,
459  typename REF::value_type,
460  typename REF::finder_type> REFV;
461  return std::auto_ptr<RefVectorHolderBase>(new RefVectorHolder<REFV>);
462  }
463  };
464 
465  template<typename C, typename T, typename F>
468  };
469 
470  }
471 }
472 
473 #endif
type
Definition: HCALResponse.h:22
EDProductGetter const * mustBeNonZero(EDProductGetter const *prodGetter, std::string refType, ProductID const &productID)
RefHolderToVector< T1, Ref< C, T, F > > type
Definition: Ref.h:452
T const * operator->() const
Member dereference operator.
Definition: Ref.h:397
static std::auto_ptr< BaseVectorHolder< T > > makeVectorHolder()
Definition: Ref.h:436
C const * product() const
Accessor for product collection.
Definition: RefVector.h:269
T const * product() const
Definition: TestHandle.h:61
boost::enable_if< has_key_compare< C >, bool >::type compare_key(K const &lhs, K const &rhs)
Definition: Ref.h:127
void checkTypeAtCompileTime(C const *)
Definition: Ref.h:293
bool operator!() const
Checks for null.
Definition: Ref.h:252
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
T const & operator*() const
Dereference operator.
Definition: Ref.h:389
key_type index_
Definition: Ref.h:296
bool isAvailable() const
Definition: Ref.h:275
#define CMS_CLASS_VERSION(_version_)
T const element_type
Definition: Ref.h:166
static std::auto_ptr< RefVectorHolderBase > makeVectorHolder()
Definition: Ref.h:457
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:249
bool isNull() const
Checks for null.
Definition: Ref.h:246
RefRefHolderToRefVector< Ref< C, T, F > > type
Definition: Ref.h:467
boost::binary_traits< F >::second_argument_type argument_type
Definition: Ref.h:168
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
void const * productPtr() const
Definition: RefCore.h:26
bool isAvailable() const
Definition: RefCore.cc:103
tuple handle
Definition: patZpeak.py:22
Ref(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ref.h:203
RefCore const & refCore() const
Definition: Ref.h:280
C product_type
etc. etc.: more nesting levels could be supported ...
Definition: Ref.h:164
Container::value_type value_type
Ref(ProductID const &iId)
Definition: Ref.h:222
RefCore product_
Definition: Ref.h:295
refhelper::FindRefVectorUsingAdvance< RefVector< C, T, F > > VF
Definition: Ref.h:152
Ref()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: Ref.h:174
refhelper::FindRefVectorUsingAdvance< RefToBaseVector< T > > VBF
Definition: Ref.h:153
F finder_type
Definition: Ref.h:167
Ref(ProductID const &iProductID, T const *, key_type iItemKey, C const *iProduct)
Constructor for use in the various X::fillView(...) functions.
Definition: Ref.h:214
~Ref()
Destructor.
Definition: Ref.h:230
T value_type
Definition: Ref.h:165
T const * product() const
Definition: OrphanHandle.h:61
key_type key() const
Accessor for product key.
Definition: Ref.h:265
string const
Definition: compareJSON.py:14
T const * product() const
Definition: Handle.h:74
EDProductGetter const * productGetter() const
Definition: RefCore.h:47
ProductID id() const
Definition: RefCore.h:23
bool isTransient() const
Definition: RefCore.h:64
bool hasProductCache() const
Returns true if container referenced by the Ref has been cached.
Definition: Ref.h:271
key_type index() const
Definition: Ref.h:268
ProductID id() const
Accessor for product ID.
Definition: Ref.h:255
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ref.h:258
static std::auto_ptr< RefVectorHolderBase > makeVectorBaseHolder()
Definition: Ref.h:442
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:169
long double T
C const * product() const
Accessor for product collection.
Definition: Ref.h:381
edm::RefVector< Container > RefVector
bool isTransient() const
Checks if this ref is transient (i.e. not persistable).
Definition: Ref.h:278
def template
Definition: svgfig.py:520