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 
106 #include "boost/functional.hpp"
107 #include "boost/call_traits.hpp"
108 #include "boost/type_traits.hpp"
109 #include "boost/mpl/has_xxx.hpp"
110 #include "boost/utility/enable_if.hpp"
111 
120 
121 BOOST_MPL_HAS_XXX_TRAIT_DEF(key_compare)
122 
123  template <typename C, typename K>
124  typename boost::enable_if<has_key_compare<C>, bool>::type
125  compare_key(K const& lhs, K const& rhs) {
126  typedef typename C::key_compare comparison_functor;
127  return comparison_functor()(lhs, rhs);
128  }
129 
130  template <typename C, typename K>
131  typename boost::disable_if<has_key_compare<C>, bool>::type
132  compare_key(K const& lhs, K const& rhs) {
133  return lhs < rhs;
134  }
135 
137 
138 namespace edm {
139  template<typename C, typename T, typename F>
140  class RefVector;
141 
142  template<typename T>
143  class RefToBaseVector;
144 
145  template <typename C,
146  typename T = typename refhelper::ValueTrait<C>::value,
147  typename F = typename refhelper::FindTrait<C, T>::value>
148  class Ref {
149  private:
152  friend class RefVectorIterator<C, T, F>;
153  friend class RefVector<C, T, F>;
154  friend class RefVector<RefVector<C, T, F>, T, VF>;
155  friend class RefVector<RefVector<RefVector<C, T, F>, T, VF>, T, VF>;
156  friend class RefVector<RefVector<C, T, F>, T, VBF>;
157  friend class RefVector<RefVector<RefVector<C, T, F>, T, VBF>, T, VBF>;
159 
160  public:
162  typedef C product_type;
163  typedef T value_type;
164  typedef T const element_type; //used for generic programming
165  typedef F finder_type;
166  typedef typename boost::binary_traits<F>::second_argument_type argument_type;
170 
172  Ref() : ref_() {}
173 
175  Ref(Handle<C> const& handle, key_type itemKey, bool setNow=true);
176 
178  Ref(OrphanHandle<C> const& handle, key_type itemKey, bool setNow=true);
179 
181  // Note. refvector[index] returns a Ref where index is the index into
182  // the RefVector. This index argument is the index into the COLLECTION,
183  // not the index into the RefVector.
184  Ref(RefVector<C, T, F> const& refvector, key_type itemKey, bool setNow=true);
185 
187  // An exception will be thrown if an attempt is made to persistify
188  // any object containing this Ref. Also, in the future work will
189  // be done to throw an exception if an attempt is made to put any object
190  // containing this Ref into an event(or run or lumi).
191  Ref(C const* product, key_type itemKey, bool setNow=true);
192 
194  // An exception will be thrown if an attempt is made to persistify
195  // any object containing this Ref. Also, in the future work will
196  Ref(TestHandle<C> const& handle, key_type itemKey, bool setNow=true);
197 
201  Ref(ProductID const& productID, key_type itemKey, EDProductGetter const* prodGetter) :
202  ref_(productID, 0, itemKey, 0, mustBeNonZero(prodGetter, "Ref", productID), false) {
203  }
204 
206  // It is an error (not diagnosable at compile- or run-time) to call
207  // this constructor with a pointer to a T unless the pointed-to T
208  // object is already in a collection of type C stored in the
209  // Event. The given ProductID must be the id of the collection in
210  // the Event.
211 
212  Ref(ProductID const& productID, T const* item, key_type item_key, C const* product ) :
213  ref_(productID, product, item_key, item, 0, false) {
214  }
215 
219 
220  explicit Ref(ProductID const& id) :
221  ref_(id, 0, key_traits<key_type>::value, 0, 0, false)
222  { }
223 
225  Ref(RefProd<C> const& refProd, key_type itemKey);
226 
228  ~Ref() {}
229 
231  T const&
232  operator*() const;
233 
235  T const*
236  operator->() const;
237 
239  T const* get() const {
240  return isNull() ? 0 : this->operator->();
241  }
242 
244  bool isNull() const {return !isNonnull(); }
245 
247  //bool isNonnull() const {return id().isValid(); }
248  bool isNonnull() const { return ref_.isNonnull(); }
249 
251  bool operator!() const {return isNull();}
252 
254  ProductID id() const {return ref_.refCore().id();}
255 
258 
260  // Accessor must get the product if necessary
261  C const* product() const;
262 
264  key_type key() const {return ref_.item().key();}
265 
266  // This one just for backward compatibility. Will be removed soon.
267  key_type index() const {return ref_.item().key();}
268 
270  RefBase<key_type> const& ref() const {return ref_;}
271 
272  bool hasProductCache() const {return ref_.refCore().productPtr() != 0;}
273 
274  bool hasCache() const {return ref_.item().ptr() != 0;}
275 
278  bool isAvailable() const {return ref_.refCore().isAvailable();}
279 
281  bool isTransient() const {return ref_.refCore().isTransient();}
282 
283  private:
284  // Constructor from member of RefVector
285  Ref(RefCore const& refCore, RefItem<key_type> const& item) :
286  ref_(refCore, item) {
287  }
288 
289  private:
290  // Compile time check that the argument is a C* or C const*
291  // or derived from it.
292  void checkTypeAtCompileTime(C const*) {}
293 
295  };
296 }
297 
301 
302 namespace edm {
304  template <typename C, typename T, typename F>
305  inline
306  Ref<C, T, F>::Ref(Handle<C> const& handle, key_type itemKey, bool setNow) :
307  ref_(handle.id(), handle.product(), itemKey, 0, 0, false) {
309  assert(ref_.item().key() == itemKey);
310 
311  if (setNow) {ref_.item().setPtr(getPtr_<C, T, F>(ref_.refCore(), ref_.item()));}
312  }
313 
315  template <typename C, typename T, typename F>
316  inline
317  Ref<C, T, F>::Ref(OrphanHandle<C> const& handle, key_type itemKey, bool setNow) :
318  ref_(handle.id(), handle.product(), itemKey, 0, 0, false) {
320  assert(ref_.item().key() == itemKey);
321 
322  if (setNow) {ref_.item().setPtr(getPtr_<C, T, F>(ref_.refCore(), ref_.item()));}
323  }
324 
326  template <typename C, typename T, typename F>
327  inline
328  Ref<C, T, F>::Ref(RefVector<C, T, F> const& refvector, key_type itemKey, bool setNow) :
329  ref_(refvector.id(), refvector.product(), itemKey, 0, 0, refvector.isTransient()) {
330  checkTypeAtCompileTime(refvector.product());
331  assert(ref_.item().key() == itemKey);
332 
333  if (setNow) {ref_.item().setPtr(getPtr_<C, T, F>(ref_.refCore(), ref_.item()));}
334  }
335 
337  // An exception will be thrown if an attempt is made to persistify
338  // any object containing this Ref. Also, in the future work will
339  // be done to throw an exception if an attempt is made to put any object
340  // containing this Ref into an event(or run or lumi).
341  // Note: It is legal for the referenced object to be put into the event
342  // and persistified. It is this Ref itself that cannot be persistified.
343  template <typename C, typename T, typename F>
344  inline
345  Ref<C, T, F>::Ref(C const* product, key_type itemKey, bool setNow) :
346  ref_(ProductID(), product, product != 0 ? itemKey : key_traits<key_type>::value, 0, 0, true) {
347  checkTypeAtCompileTime(product);
348  assert(ref_.item().key() == (product != 0 ? itemKey : key_traits<key_type>::value));
349 
350  if (setNow && product != 0) {ref_.item().setPtr(getPtr_<C, T, F>(ref_.refCore(), ref_.item()));}
351  }
352 
354  // An exception will be thrown if an attempt is made to persistify any object containing this Ref.
355  template <typename C, typename T, typename F>
356  inline
357  Ref<C, T, F>::Ref(TestHandle<C> const& handle, key_type itemKey, bool setNow) :
358  ref_(handle.id(), handle.product(), itemKey, 0, 0, true) {
360  assert(ref_.item().key() == itemKey);
361 
362  if (setNow) {ref_.item().setPtr(getPtr_<C, T, F>(ref_.refCore(), ref_.item()));}
363  }
364 
366  template <typename C, typename T, typename F>
367  inline
368  Ref<C, T, F>::Ref(RefProd<C> const& refProd, key_type itemKey) :
369  ref_(refProd.id(), refProd.refCore().productPtr(), itemKey, 0, refProd.refCore().productGetter(), refProd.refCore().isTransient()) {
370  assert(ref_.item().key() == itemKey);
371  if (0 != refProd.refCore().productPtr()) {
372  ref_.item().setPtr(getPtr_<C, T, F>(ref_.refCore(), ref_.item()));
373  }
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>(ref_.refCore());
383  }
384 
386  template <typename C, typename T, typename F>
387  inline
388  T const&
390  return *getPtr<C, T, F>(ref_.refCore(), ref_.item());
391  }
392 
394  template <typename C, typename T, typename F>
395  inline
396  T const*
398  return getPtr<C, T, F>(ref_.refCore(), ref_.item());
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.ref() == rhs.ref();
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.ref().refCore() == rhs.ref().refCore() ? compare_key<C>(lhs.key(), rhs.key()) : lhs.ref().refCore() < rhs.ref().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>
466  struct RefHolderToRefVectorTrait<Ref<C, T, 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:268
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:125
void checkTypeAtCompileTime(C const *)
Definition: Ref.h:292
Ref(RefCore const &refCore, RefItem< key_type > const &item)
Definition: Ref.h:285
RefBase< key_type > ref_
Definition: Ref.h:294
bool isNonnull() const
Definition: RefBase.h:50
bool operator!() const
Checks for null.
Definition: Ref.h:251
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
T const & operator*() const
Dereference operator.
Definition: Ref.h:389
Container::value_type value_type
bool isAvailable() const
Definition: Ref.h:278
Ref(ProductID const &id)
Definition: Ref.h:220
T const element_type
Definition: Ref.h:164
static std::auto_ptr< RefVectorHolderBase > makeVectorHolder()
Definition: Ref.h:457
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:248
RefBase< key_type > const & ref() const
Accessor for all data.
Definition: Ref.h:270
bool isNull() const
Checks for null.
Definition: Ref.h:244
RefItem< KEY > const & item() const
Accessor for index and pointer.
Definition: RefBase.h:41
RefRefHolderToRefVector< Ref< C, T, F > > type
Definition: Ref.h:467
boost::binary_traits< F >::second_argument_type argument_type
Definition: Ref.h:166
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
void const * productPtr() const
Definition: RefCore.h:23
bool isAvailable() const
Definition: RefCore.cc:86
tuple handle
Definition: patZpeak.py:22
Ref(ProductID const &productID, key_type itemKey, EDProductGetter const *prodGetter)
Definition: Ref.h:201
C product_type
etc. etc.: more nesting levels could be supported ...
Definition: Ref.h:162
refhelper::FindRefVectorUsingAdvance< RefVector< C, T, F > > VF
Definition: Ref.h:150
Ref()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: Ref.h:172
refhelper::FindRefVectorUsingAdvance< RefToBaseVector< T > > VBF
Definition: Ref.h:151
RefCore const & refCore() const
Definition: RefProd.h:127
F finder_type
Definition: Ref.h:165
~Ref()
Destructor.
Definition: Ref.h:228
void const * setPtr(void const *p) const
Definition: RefItem.h:29
T value_type
Definition: Ref.h:163
T const * product() const
Definition: OrphanHandle.h:61
key_type key() const
Accessor for product key.
Definition: Ref.h:264
RefCore const & refCore() const
Accessor for product ID and product getter.
Definition: RefBase.h:38
string const
Definition: compareJSON.py:14
T const * product() const
Definition: Handle.h:74
EDProductGetter const * productGetter() const
Definition: RefCore.h:41
ProductID id() const
Definition: RefCore.h:21
bool isTransient() const
Definition: RefCore.h:57
key_type key() const
Definition: RefItem.h:27
bool hasProductCache() const
Definition: Ref.h:272
key_type index() const
Definition: Ref.h:267
void const * ptr() const
Definition: RefItem.h:28
ProductID id() const
Accessor for product ID.
Definition: Ref.h:254
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ref.h:257
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:167
bool hasCache() const
Definition: Ref.h:274
Ref(ProductID const &productID, T const *item, key_type item_key, C const *product)
Constructor for use in the various X::fillView(...) functions.
Definition: Ref.h:212
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:281
def template
Definition: svgfig.py:520