CMS 3D CMS Logo

RefProd.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefProd_h
2 #define DataFormats_Common_RefProd_h
3 
4 /*----------------------------------------------------------------------
5 
6 Ref: A template for an interproduct reference to a product.
7 
8 ----------------------------------------------------------------------*/
9 
10 /*----------------------------------------------------------------------
11 // This defines the public interface to the class RefProd<T>.
12 //
13 // ProductID productID is the product ID of the collection. (0 is invalid)
14 // RefProd<T> const& ref is another RefProd<T>
15 
16 // Constructors
17  RefProd(); // Default constructor
18  RefProd(RefProd<T> const& ref); // Copy constructor (default, not explicitly specified)
19 
20  RefProd(Handle<T> const& handle);
21  RefProd(ProductID pid, EDProductGetter const* prodGetter);
22 
23 // Destructor
24  virtual ~RefProd() {}
25 
26 // Operators and methods
27  RefProd<T>& operator=(RefProd<T> const&); // assignment (default, not explicitly specified)
28  T const& operator*() const; // dereference
29  T const* operator->() const; // member dereference
30  bool operator==(RefProd<T> const& ref) const; // equality
31  bool operator!=(RefProd<T> const& ref) const; // inequality
32  bool operator<(RefProd<T> const& ref) const; // ordering
33  bool isNonnull() const; // true if an object is referenced
34  bool isNull() const; // equivalent to !isNonnull()
35  bool operator!() const; // equivalent to !isNonnull()
36 ----------------------------------------------------------------------*/
37 
44 
45 // Not really needed but things depend on it being here ...
47 
48 #include <utility>
49 
50 namespace edm {
51 
52  class EDProductGetter;
53 
54  template<typename C>
55  class RefProd {
56  public:
57  typedef C product_type;
58  typedef C value_type;
59 
61  RefProd() : product_() {}
62 
64  explicit RefProd(Handle<C> const& handle) :
65  product_(handle.id(), handle.product(), nullptr, false) {
67  }
68 
70  explicit RefProd(OrphanHandle<C> const& handle) :
71  product_(handle.id(), handle.product(), nullptr, false) {
73  }
74 
76  // An exception will be thrown if an attempt is made to persistify
77  // any object containing this RefProd. Also, in the future work will
78  // be done to throw an exception if an attempt is made to put any object
79  // containing this RefProd into an event(or run or lumi).
80  RefProd(C const* iProduct) :
81  product_(ProductID(), iProduct, 0, true) {
82  checkTypeAtCompileTime(iProduct);
83  }
84 
86  // An exception will be thrown if an attempt is made to persistify
87  // any object containing this RefProd. Also, in the future work will
88  // be done to throw an exception if an attempt is made to put any object
89  // containing this RefProd into an event(or run or lumi).
90  explicit RefProd(TestHandle<C> const& handle) :
91  product_(handle.id(), handle.product(), 0, true) {
93  }
94 
95  // Constructor for those users who do not have a product handle,
96  // but have a pointer to a product getter (such as the EventPrincipal).
97  // prodGetter will ususally be a pointer to the event principal.
98  RefProd(ProductID const& productID, EDProductGetter const* prodGetter) :
99  product_(productID, nullptr, mustBeNonZero(prodGetter, "RefProd", productID), false) {
100  }
101 
103  ~RefProd() {}
104 
106  product_type const& operator*() const;
107 
109  product_type const* operator->() const;
110 
113  product_type const* get() const {
114  return isNull() ? nullptr : this->operator->();
115  }
116 
119  product_type const* product() const {
120  return isNull() ? 0 : this->operator->();
121  }
122 
123  RefCore const& refCore() const {
124  return product_;
125  }
126 
128  bool isNull() const {return !isNonnull();}
129 
131  bool isNonnull() const {return product_.isNonnull();}
132 
134  bool operator!() const {return isNull();}
135 
137  ProductID id() const {return product_.id();}
138 
141 
143  bool hasCache() const {return product_.productPtr() != 0;}
144 
146  bool hasProductCache() const {return hasCache();}
147 
150  bool isAvailable() const {return product_.isAvailable();}
151 
153  bool isTransient() const {return product_.isTransient();}
154 
155  void swap(RefProd<C> &);
156 
157  //Needed for ROOT storage
159 
160  private:
161  // Compile time check that the argument is a C* or C const*
162  // or derived from it.
163  void checkTypeAtCompileTime(C const* /*ptr*/) {}
164 
166  };
167 }
168 
170 
171 namespace edm {
172 
174  template<typename C>
175  inline
176  C const& RefProd<C>::operator*() const {
177  return *(edm::template getProduct<C>(product_));
178  }
179 
181  template<typename C>
182  inline
183  C const* RefProd<C>::operator->() const {
184  return edm::template getProduct<C>(product_);
185  }
186 
187 
188  template<typename C>
189  inline
191  edm::swap(product_, other.product_);
192  }
193 
194  template<typename C>
195  inline
196  bool
197  operator==(RefProd<C> const& lhs, RefProd<C> const& rhs) {
198  return lhs.refCore() == rhs.refCore();
199  }
200 
201  template<typename C>
202  inline
203  bool
204  operator!=(RefProd<C> const& lhs, RefProd<C> const& rhs) {
205  return !(lhs == rhs);
206  }
207 
208  template<typename C>
209  inline
210  bool
211  operator<(RefProd<C> const& lhs, RefProd<C> const& rhs) {
212  return(lhs.refCore() < rhs.refCore());
213  }
214 
215  template<typename C>
216  inline
217  void swap(RefProd<C> const& lhs, RefProd<C> const& rhs) {
218  lhs.swap(rhs);
219  }
220 }
221 
222 //Handle specialization here
224 
225 #endif
bool hasCache() const
Checks if product is in memory.
Definition: RefProd.h:143
EDProductGetter const * mustBeNonZero(EDProductGetter const *prodGetter, std::string refType, ProductID const &productID)
RefProd()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: RefProd.h:61
bool operator!() const
Checks for null.
Definition: RefProd.h:134
T const * product() const
Definition: TestHandle.h:60
C value_type
Definition: RefProd.h:58
#define nullptr
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
~RefProd()
Destructor.
Definition: RefProd.h:103
bool isNonnull() const
Definition: RefCore.h:74
product_type const * operator->() const
Member dereference operator.
Definition: RefProd.h:183
RefProd(Handle< C > const &handle)
General purpose constructor from handle.
Definition: RefProd.h:64
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefProd.h:140
RefProd(OrphanHandle< C > const &handle)
General purpose constructor from orphan handle.
Definition: RefProd.h:70
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
void checkTypeAtCompileTime(C const *)
Definition: RefProd.h:163
product_type const * product() const
Definition: RefProd.h:119
void const * productPtr() const
Definition: RefCore.h:52
bool isAvailable() const
Definition: RefCore.cc:169
RefProd(TestHandle< C > const &handle)
General purpose constructor from test handle.
Definition: RefProd.h:90
void swap(RefProd< C > &)
Definition: RefProd.h:190
RefCore const & refCore() const
Definition: RefProd.h:123
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:520
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
ProductID id() const
Accessor for product ID.
Definition: RefProd.h:137
bool isTransient() const
Checks if this RefProd is transient (i.e. not persistable).
Definition: RefProd.h:153
bool isNull() const
Checks for null.
Definition: RefProd.h:128
T const * product() const
Definition: Handle.h:81
edm::RefProd< Container > RefProd
product_type const & operator*() const
Dereference operator.
Definition: RefProd.h:176
T const * product() const
Definition: OrphanHandle.h:57
bool isAvailable() const
Definition: RefProd.h:150
EDProductGetter const * productGetter() const
Definition: RefCore.h:84
RefProd(ProductID const &productID, EDProductGetter const *prodGetter)
Definition: RefProd.h:98
ProductID id() const
Definition: RefCore.h:49
bool isTransient() const
Definition: RefCore.h:106
HLT enums.
RefCore product_
Definition: RefProd.h:165
RefProd(C const *iProduct)
Constructor for ref to object that is not in an event.
Definition: RefProd.h:80
bool hasProductCache() const
Checks if product is in memory.
Definition: RefProd.h:146
bool isNonnull() const
Checks for non-null.
Definition: RefProd.h:131
C product_type
Definition: RefProd.h:57