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) : product_(handle.id(), handle.product(), nullptr, false) {
65  checkTypeAtCompileTime(handle.product());
66  }
67 
69  explicit RefProd(OrphanHandle<C> const& handle) : product_(handle.id(), handle.product(), nullptr, false) {
70  checkTypeAtCompileTime(handle.product());
71  }
72 
74  // An exception will be thrown if an attempt is made to persistify
75  // any object containing this RefProd. Also, in the future work will
76  // be done to throw an exception if an attempt is made to put any object
77  // containing this RefProd into an event(or run or lumi).
78  RefProd(C const* iProduct) : product_(ProductID(), iProduct, nullptr, true) { checkTypeAtCompileTime(iProduct); }
79 
81  // An exception will be thrown if an attempt is made to persistify
82  // any object containing this RefProd. Also, in the future work will
83  // be done to throw an exception if an attempt is made to put any object
84  // containing this RefProd into an event(or run or lumi).
85  explicit RefProd(TestHandle<C> const& handle) : product_(handle.id(), handle.product(), nullptr, true) {
86  checkTypeAtCompileTime(handle.product());
87  }
88 
89  // Constructor for those users who do not have a product handle,
90  // but have a pointer to a product getter (such as the EventPrincipal).
91  // prodGetter will ususally be a pointer to the event principal.
92  RefProd(ProductID const& productID, EDProductGetter const* prodGetter)
93  : product_(productID, nullptr, mustBeNonZero(prodGetter, "RefProd", productID), false) {}
94 
96  ~RefProd() {}
97 
99  product_type const& operator*() const;
100 
102  product_type const* operator->() const;
103 
106  product_type const* get() const { return isNull() ? nullptr : this->operator->(); }
107 
110  product_type const* product() const { return isNull() ? nullptr : this->operator->(); }
111 
112  RefCore const& refCore() const { return product_; }
113 
115  bool isNull() const { return !isNonnull(); }
116 
118  bool isNonnull() const { return product_.isNonnull(); }
119 
121  bool operator!() const { return isNull(); }
122 
124  ProductID id() const { return product_.id(); }
125 
128 
130  bool hasCache() const { return product_.productPtr() != nullptr; }
131 
133  bool hasProductCache() const { return hasCache(); }
134 
137  bool isAvailable() const { return product_.isAvailable(); }
138 
140  bool isTransient() const { return product_.isTransient(); }
141 
142  void swap(RefProd<C>&);
143 
144  //Needed for ROOT storage
146 
147  private:
148  // Compile time check that the argument is a C* or C const*
149  // or derived from it.
150  void checkTypeAtCompileTime(C const* /*ptr*/) {}
151 
153  };
154 } // namespace edm
155 
157 
158 namespace edm {
159 
161  template <typename C>
162  inline C const& RefProd<C>::operator*() const {
163  return *(edm::template getProduct<C>(product_));
164  }
165 
167  template <typename C>
168  inline C const* RefProd<C>::operator->() const {
169  return edm::template getProduct<C>(product_);
170  }
171 
172  template <typename C>
174  edm::swap(product_, other.product_);
175  }
176 
177  template <typename C>
178  inline bool operator==(RefProd<C> const& lhs, RefProd<C> const& rhs) {
179  return lhs.refCore() == rhs.refCore();
180  }
181 
182  template <typename C>
183  inline bool operator!=(RefProd<C> const& lhs, RefProd<C> const& rhs) {
184  return !(lhs == rhs);
185  }
186 
187  template <typename C>
188  inline bool operator<(RefProd<C> const& lhs, RefProd<C> const& rhs) {
189  return (lhs.refCore() < rhs.refCore());
190  }
191 
192  template <typename C>
193  inline void swap(RefProd<C> const& lhs, RefProd<C> const& rhs) {
194  lhs.swap(rhs);
195  }
196 } // namespace edm
197 
198 //Handle specialization here
200 
201 #endif
constexpr bool operator==(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
RefProd()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: RefProd.h:61
ProductID id() const
Definition: RefCore.h:48
C value_type
Definition: RefProd.h:58
bool isAvailable() const
Definition: RefCore.cc:146
bool hasCache() const
Checks if product is in memory.
Definition: RefProd.h:130
EDProductGetter const * mustBeNonZero(EDProductGetter const *prodGetter, std::string refType, ProductID const &productID)
#define CMS_CLASS_VERSION(_version_)
~RefProd()
Destructor.
Definition: RefProd.h:96
void const * productPtr() const
Definition: RefCore.h:51
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:117
bool isAvailable() const
Definition: RefProd.h:137
RefCore const & refCore() const
Definition: RefProd.h:112
RefProd(OrphanHandle< C > const &handle)
General purpose constructor from orphan handle.
Definition: RefProd.h:69
product_type const & operator*() const
Dereference operator.
Definition: RefProd.h:162
bool isTransient() const
Checks if this RefProd is transient (i.e. not persistable).
Definition: RefProd.h:140
bool operator!() const
Checks for null.
Definition: RefProd.h:121
void checkTypeAtCompileTime(C const *)
Definition: RefProd.h:150
bool isNonnull() const
Checks for non-null.
Definition: RefProd.h:118
RefProd(TestHandle< C > const &handle)
General purpose constructor from test handle.
Definition: RefProd.h:85
bool isTransient() const
Definition: RefCore.h:105
void swap(RefProd< C > &)
Definition: RefProd.h:173
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
bool isNonnull() const
Definition: RefCore.h:71
bool hasProductCache() const
Checks if product is in memory.
Definition: RefProd.h:133
constexpr bool operator!=(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
ProductID id() const
Accessor for product ID.
Definition: RefProd.h:124
product_type const * operator->() const
Member dereference operator.
Definition: RefProd.h:168
edm::RefProd< Container > RefProd
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefProd.h:127
RefProd(ProductID const &productID, EDProductGetter const *prodGetter)
Definition: RefProd.h:92
HLT enums.
RefCore product_
Definition: RefProd.h:152
bool isNull() const
Checks for null.
Definition: RefProd.h:115
RefProd(C const *iProduct)
Constructor for ref to object that is not in an event.
Definition: RefProd.h:78
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
product_type const * product() const
Definition: RefProd.h:110
C product_type
Definition: RefProd.h:57