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 #include <vector>
124 
125 BOOST_MPL_HAS_XXX_TRAIT_DEF(key_compare)
126 
127  template <typename C, typename K>
128  typename boost::enable_if<has_key_compare<C>, bool>::type
129  compare_key(K const& lhs, K const& rhs) {
130  typedef typename C::key_compare comparison_functor;
131  return comparison_functor()(lhs, rhs);
132  }
133 
134  template <typename C, typename K>
135  typename boost::disable_if<has_key_compare<C>, bool>::type
136  compare_key(K const& lhs, K const& rhs) {
137  return lhs < rhs;
138  }
139 
141 
142 namespace edm {
143  template<typename C, typename T, typename F>
144  class RefVector;
145 
146  template<typename T>
147  class RefToBaseVector;
148 
149  template <typename C,
150  typename T = typename refhelper::ValueTrait<C>::value,
151  typename F = typename refhelper::FindTrait<C, T>::value>
152  class Ref {
153  private:
156  friend class RefVectorIterator<C, T, F>;
157  friend class RefVector<C, T, F>;
158  friend class RefVector<RefVector<C, T, F>, T, VF>;
159  friend class RefVector<RefVector<C, T, F>, T, VBF>;
160 
161  public:
163  typedef C product_type;
164  typedef T value_type;
165  typedef T const element_type; //used for generic programming
166  typedef F finder_type;
167  typedef typename boost::binary_traits<F>::second_argument_type argument_type;
171 
173 
176 
178  Ref(Handle<C> const& handle, key_type itemKey, bool setNow=true);
179 
181  Ref(OrphanHandle<C> const& handle, key_type itemKey, bool setNow=true);
182 
184  // An exception will be thrown if an attempt is made to persistify
185  // any object containing this Ref. Also, in the future work will
186  // be done to throw an exception if an attempt is made to put any object
187  // containing this Ref into an event(or run or lumi).
188  Ref(C const* product, key_type itemKey, bool setNow=true);
189 
191  // An exception will be thrown if an attempt is made to persistify
192  // any object containing this Ref. Also, in the future work will
193  Ref(TestHandle<C> const& handle, key_type itemKey, bool setNow=true);
194 
198  Ref(ProductID const& productID, key_type itemKey, EDProductGetter const* prodGetter) :
199  product_(productID, 0, mustBeNonZero(prodGetter, "Ref", productID), false), index_(itemKey) {
200  }
201 
203  // It is an error (not diagnosable at compile- or run-time) to call
204  // this constructor with a pointer to a T unless the pointed-to T
205  // object is already in a collection of type C stored in the
206  // Event. The given ProductID must be the id of the collection in
207  // the Event.
208 
209  Ref(ProductID const& iProductID, T const* item, key_type itemKey, C const* /* iProduct */) :
210  product_(iProductID, item, 0, false), index_(itemKey)
211  { }
212 
213  Ref(ProductID const& iProductID, T const* item, key_type itemKey) :
214  product_(iProductID, item, 0, false), index_(itemKey)
215  { }
216 
217  Ref(ProductID const& iProductID, T const* item, key_type itemKey, bool transient) :
218  product_(iProductID, item, 0, transient), index_(itemKey)
219  { }
220 
224 
225  explicit Ref(ProductID const& iId) :
226  product_(iId, 0, 0, false), index_(key_traits<key_type>::value)
227  { }
228 
230  Ref(RefProd<C> const& refProd, key_type itemKey);
231 
233  ~Ref() {}
234 
236  T const&
237  operator*() const;
238 
240  T const*
241  operator->() const;
242 
244  T const* get() const {
245  return isNull() ? 0 : this->operator->();
246  }
247 
249  bool isNull() const {return !isNonnull(); }
250 
253 
255  bool operator!() const {return isNull();}
256 
258  ProductID id() const {return product_.id();}
259 
262 
264  key_type key() const {return index_;}
265 
266  // This one just for backward compatibility. Will be removed soon.
267  key_type index() const {return index_;}
268 
270  bool hasProductCache() const {return product_.productPtr() != 0;}
271 
274  bool isAvailable() const;
275 
277  bool isTransient() const {return product_.isTransient();}
278 
279  RefCore const& refCore() const {return product_;}
280 
281  //Used by ROOT storage
283  // private:
284  // Constructor from member of RefVector
285  Ref(RefCore const& iRefCore, key_type const& iKey) :
286  product_(iRefCore), index_(iKey) {
287  }
288 
289  private:
290 
291  // Compile time check that the argument is a C* or C const*
292  // or derived from it.
293  void checkTypeAtCompileTime(C const*) {}
294 
297  };
298 
299  //***************************
300  //Specialization for a vector
301  //***************************
302 #define REF_FOR_VECTOR_ARGS std::vector<E>,typename refhelper::ValueTrait<std::vector<E> >::value,typename refhelper::FindTrait<std::vector<E>, typename refhelper::ValueTrait<std::vector<E> >::value>::value
303 
304  template <typename E>
306  private:
309 
312  friend class RefVectorIterator<std::vector<E>, T, F>;
313  friend class RefVector<std::vector<E>, T, F>;
314  friend class RefVector<RefVector<std::vector<E>, T, F>, T, VF>;
315  friend class RefVector<RefVector<std::vector<E>, T, F>, T, VBF>;
316 
317  public:
319  typedef std::vector<E> product_type;
320  typedef typename refhelper::ValueTrait<std::vector<E> >::value value_type;
321  typedef value_type const element_type; //used for generic programming
322  typedef typename refhelper::FindTrait<std::vector<E>,
323  typename refhelper::ValueTrait<std::vector<E> >::value>::value finder_type;
324  typedef typename boost::binary_traits<F>::second_argument_type argument_type;
325  typedef unsigned int key_type;
328 
330 
332  Ref() : product_() {}
333 
335  Ref(Handle<product_type> const& handle, key_type itemKey, bool setNow=true);
336 
338  Ref(OrphanHandle<product_type> const& handle, key_type itemKey, bool setNow=true);
339 
341  // An exception will be thrown if an attempt is made to persistify
342  // any object containing this Ref. Also, in the future work will
343  // be done to throw an exception if an attempt is made to put any object
344  // containing this Ref into an event(or run or lumi).
345  Ref(product_type const* product, key_type itemKey, bool setNow=true);
346 
348  // An exception will be thrown if an attempt is made to persistify
349  // any object containing this Ref. Also, in the future work will
350  Ref(TestHandle<product_type> const& handle, key_type itemKey, bool setNow=true);
351 
355  Ref(ProductID const& productID, key_type itemKey, EDProductGetter const* prodGetter) :
356  product_(productID, 0, mustBeNonZero(prodGetter, "Ref", productID), false,itemKey) {
357  }
358 
360  // It is an error (not diagnosable at compile- or run-time) to call
361  // this constructor with a pointer to a T unless the pointed-to T
362  // object is already in a collection of type C stored in the
363  // Event. The given ProductID must be the id of the collection in
364  // the Event.
365 
366  Ref(ProductID const& iProductID, T const* item, key_type itemKey, product_type const* /* iProduct */) :
367  product_(iProductID, item, 0, false, itemKey)
368  { }
369 
370  Ref(ProductID const& iProductID, T const* item, key_type itemKey) :
371  product_(iProductID, item, 0, false, itemKey)
372  { }
373 
374  Ref(ProductID const& iProductID, T const* item, key_type itemKey, bool transient) :
375  product_(iProductID, item, 0, transient, itemKey)
376  { }
377 
381 
382  explicit Ref(ProductID const& iId) :
383  product_(iId, 0, 0, false,key_traits<key_type>::value)
384  { }
385 
387  Ref(RefProd<product_type> const& refProd, key_type itemKey);
388 
390  ~Ref() {}
391 
393  T const&
394  operator*() const;
395 
397  T const*
398  operator->() const;
399 
401  T const* get() const {
402  return isNull() ? 0 : this->operator->();
403  }
404 
406  bool isNull() const {return !isNonnull(); }
407 
409  bool isNonnull() const { return key()!=edm::key_traits<key_type>::value; }
410 
412  bool operator!() const {return isNull();}
413 
415  ProductID id() const {return product_.id();}
416 
419 
421  key_type key() const {return product_.index();}
422 
423  // This one just for backward compatibility. Will be removed soon.
424  key_type index() const {return product_.index();}
425 
427  bool hasProductCache() const {return product_.productPtr() != 0;}
428 
431  bool isAvailable() const;
432 
434  bool isTransient() const {return product_.isTransient();}
435 
436  RefCore const& refCore() const {return product_.toRefCore();}
437 
438  //Used by ROOT storage
440  // private:
441  // Constructor from member of RefVector
442  Ref(RefCore const& iRefCore, key_type const& iKey) :
443  product_(iRefCore,iKey) {
444  }
445 
446  private:
447  // Compile time check that the argument is a C* or C const*
448  // or derived from it.
450 
452  };
453 }
454 
458 
459 namespace edm {
460 
462  template <typename C, typename T, typename F>
463  inline
464  Ref<C, T, F>::Ref(Handle<C> const& handle, key_type itemKey, bool) :
465  product_(handle.id(), nullptr, nullptr, false), index_(itemKey) {
466  if(itemKey == key_traits<key_type>::value) return;
467  refitem::findRefItem<C, T, F, key_type>(product_, handle.product(), itemKey);
468  }
469 
471  template <typename E>
472  inline
473  Ref<REF_FOR_VECTOR_ARGS>::Ref(Handle<std::vector<E> > const& handle, key_type itemKey, bool) :
474  product_(handle.id(), nullptr, nullptr, false, itemKey){
475  if(itemKey == key_traits<key_type>::value) return;
476  refitem::findRefItem<product_type, value_type, finder_type, key_type>(product_.toRefCore(),
477  handle.product(),
478  itemKey);
479  }
480 
482  template <typename C, typename T, typename F>
483  inline
485  product_(handle.id(), nullptr, nullptr, false), index_(itemKey) {
486  if(itemKey == key_traits<key_type>::value) return;
487  refitem::findRefItem<C, T, F, key_type>(product_, handle.product(), itemKey);
488  }
489 
491  template <typename E>
492  inline
493  Ref<REF_FOR_VECTOR_ARGS>::Ref(OrphanHandle<std::vector<E> > const& handle, key_type itemKey, bool) :
494  product_(handle.id(), nullptr, nullptr, false, itemKey){
495  if(itemKey == key_traits<key_type>::value) return;
496  refitem::findRefItem<product_type, value_type, finder_type, key_type>(product_.toRefCore(),
497  handle.product(),
498  itemKey);
499  }
500 
502  // An exception will be thrown if an attempt is made to persistify
503  // any object containing this Ref. Also, in the future work will
504  // be done to throw an exception if an attempt is made to put any object
505  // containing this Ref into an event(or run or lumi).
506  // Note: It is legal for the referenced object to be put into the event
507  // and persistified. It is this Ref itself that cannot be persistified.
508  template <typename C, typename T, typename F>
509  inline
510  Ref<C, T, F>::Ref(C const* iProduct, key_type itemKey, bool) :
511  product_(ProductID(), nullptr, nullptr, true), index_(iProduct != nullptr ? itemKey : key_traits<key_type>::value) {
512  if(iProduct != nullptr) {
513  refitem::findRefItem<C, T, F, key_type>(product_, iProduct, itemKey);
514  }
515  }
516 
517  template <typename E>
518  inline
519  Ref<REF_FOR_VECTOR_ARGS>::Ref(std::vector<E> const* iProduct, key_type itemKey, bool) :
520  product_(ProductID(), nullptr, nullptr, true, iProduct != 0 ? itemKey : key_traits<key_type>::value) {
521  if(iProduct != nullptr) {
522  refitem::findRefItem<product_type, value_type, finder_type, key_type>(product_.toRefCore(), iProduct, itemKey);
523  }
524  }
525 
527  // An exception will be thrown if an attempt is made to persistify any object containing this Ref.
528  template <typename C, typename T, typename F>
529  inline
531  product_(handle.id(), nullptr, nullptr, true), index_(itemKey) {
532  if(itemKey == key_traits<key_type>::value) return;
533  refitem::findRefItem<C, T, F, key_type>(product_, handle.product(), itemKey);
534  }
535 
536  template <typename E>
537  inline
538  Ref<REF_FOR_VECTOR_ARGS>::Ref(TestHandle<std::vector<E> > const& handle, key_type itemKey, bool) :
539  product_(handle.id(), nullptr, nullptr, true, itemKey){
540  if(itemKey == key_traits<key_type>::value) return;
541  refitem::findRefItem<product_type, value_type, finder_type, key_type>(product_.toRefCore(),
542  handle.product(),
543  itemKey);
544  }
545 
547  template <typename C, typename T, typename F>
548  inline
549  Ref<C, T, F>::Ref(RefProd<C> const& refProd, key_type itemKey) :
550  product_(refProd.id(), nullptr, refProd.refCore().productGetter(), refProd.refCore().isTransient()),
551  index_(itemKey) {
552 
553  if(refProd.refCore().productPtr() != nullptr && itemKey != key_traits<key_type>::value) {
554  refitem::findRefItem<C, T, F, key_type>(product_,
555  static_cast<product_type const*>(refProd.refCore().productPtr()),
556  itemKey);
557  }
558  }
559 
560  template <typename E>
561  inline
562  Ref<REF_FOR_VECTOR_ARGS>::Ref(RefProd<std::vector<E> > const& refProd, key_type itemKey) :
563  product_(refProd.id(), nullptr, refProd.refCore().productGetter(), refProd.refCore().isTransient(), itemKey) {
564 
565  if(refProd.refCore().productPtr() != nullptr && itemKey != key_traits<key_type>::value) {
566  refitem::findRefItem<product_type, value_type, finder_type, key_type>(
567  product_.toRefCore(),
568  static_cast<product_type const*>(refProd.refCore().productPtr()),
569  itemKey);
570  }
571  }
572 
573  template <typename C, typename T, typename F>
574  inline
575  bool
577  if(product_.isAvailable()) {
578  return true;
579  }
580  return isThinnedAvailable<C>(product_, index_);
581  }
582 
583  template <typename E>
584  inline
585  bool
587  if(product_.isAvailable()) {
588  return true;
589  }
590  return isThinnedAvailable<std::vector<E> >(product_.toRefCore(), key());
591  }
592 
594  template <typename C, typename T, typename F>
595  inline
596  T const&
598  return *getRefPtr<C, T, F>(product_, index_);
599  }
600  template <typename E>
601  inline
604  return *getRefPtr<REF_FOR_VECTOR_ARGS>(product_.toRefCore(), key());
605  }
606 
608  template <typename C, typename T, typename F>
609  inline
610  T const*
612  return getRefPtr<C, T, F>(product_, index_);
613  }
614  template <typename E>
615  inline
618  return getRefPtr<REF_FOR_VECTOR_ARGS>(product_.toRefCore(), key());
619  }
620 
621  template <typename C, typename T, typename F>
622  inline
623  bool
624  operator==(Ref<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
625  return lhs.key() == rhs.key() && lhs.refCore() == rhs.refCore() ;
626  }
627 
628  template <typename C, typename T, typename F>
629  inline
630  bool
631  operator!=(Ref<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
632  return !(lhs == rhs);
633  }
634 
635  template <typename C, typename T, typename F>
636  inline
637  bool
638  operator<(Ref<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
641  return (lhs.refCore() == rhs.refCore() ? compare_key<C>(lhs.key(), rhs.key()) : lhs.refCore() < rhs.refCore());
642  }
643 
644 }
645 
646 //Handle specialization here
648 #endif
bool isAvailable() const
Definition: Ref.h:576
type
Definition: HCALResponse.h:21
EDProductGetter const * mustBeNonZero(EDProductGetter const *prodGetter, std::string refType, ProductID const &productID)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
void checkTypeAtCompileTime(C const *)
Definition: Ref.h:293
T const * product() const
Definition: TestHandle.h:60
boost::enable_if< has_key_compare< C >, bool >::type compare_key(K const &lhs, K const &rhs)
Definition: Ref.h:129
Ref(ProductID const &iProductID, T const *item, key_type itemKey, product_type const *)
Constructor for use in the various X::fillView(...) functions.
Definition: Ref.h:366
boost::binary_traits< F >::second_argument_type argument_type
Definition: Ref.h:167
~Ref()
Destructor.
Definition: Ref.h:233
bool isTransient() const
Checks if this ref is transient (i.e. not persistable).
Definition: Ref.h:277
key_type index_
Definition: Ref.h:296
bool hasProductCache() const
Returns true if container referenced by the Ref has been cached.
Definition: Ref.h:270
RefCore const & refCore() const
Definition: Ref.h:279
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
key_type index() const
Definition: Ref.h:267
key_type key() const
Accessor for product key.
Definition: Ref.h:264
RefCore product_
Definition: Ref.h:295
#define REF_FOR_VECTOR_ARGS
Definition: Ref.h:302
#define nullptr
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
EDProductGetter const * productGetter(std::atomic< void const * > const &iCache)
ProductID id() const
Accessor for product ID.
Definition: Ref.h:258
Ref(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ref.h:198
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
void const * productPtr() const
Definition: RefCore.h:45
static key_type invalidKey()
Definition: Ref.h:172
tuple handle
Definition: patZpeak.py:22
Ref(ProductID const &iProductID, T const *item, key_type itemKey)
Definition: Ref.h:213
T const element_type
Definition: Ref.h:165
refhelper::FindRefVectorUsingAdvance< RefToBaseVector< T > > VBF
Definition: Ref.h:155
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
bool isNull() const
Checks for null.
Definition: Ref.h:249
RefCore const & refCore() const
Definition: RefProd.h:123
refhelper::FindRefVectorUsingAdvance< RefVector< C, T, F > > VF
Definition: Ref.h:154
T const * product() const
Definition: Handle.h:81
T value_type
Definition: Ref.h:164
T const * product() const
Definition: OrphanHandle.h:57
F finder_type
Definition: Ref.h:166
Ref(ProductID const &iProductID, T const *item, key_type itemKey, bool transient)
Definition: Ref.h:217
string const
Definition: compareJSON.py:14
C product_type
for export
Definition: Ref.h:163
Ref(ProductID const &iId)
Definition: Ref.h:225
EDProductGetter const * productGetter() const
Definition: RefCore.h:77
bool operator!() const
Checks for null.
Definition: Ref.h:255
ProductID id() const
Definition: RefCore.h:42
bool isTransient() const
Definition: RefCore.h:99
Ref()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: Ref.h:175
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ref.h:261
T const * operator->() const
Member dereference operator.
Definition: Ref.h:611
volatile std::atomic< bool > shutdown_flag false
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
Ref(ProductID const &iProductID, T const *item, key_type itemKey, C const *)
Constructor for use in the various X::fillView(...) functions.
Definition: Ref.h:209
long double T
edm::RefVector< Container > RefVector
def template
Definition: svgfig.py:520
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:168
T const & operator*() const
Dereference operator.
Definition: Ref.h:597