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 
139  bool isAvailable() const { return product_.isAvailable(); }
140 
142  bool isTransient() const { return product_.isTransient(); }
143 
144  void swap(RefProd<C>&);
145 
146  //Needed for ROOT storage
148 
149  private:
150  // Compile time check that the argument is a C* or C const*
151  // or derived from it.
152  void checkTypeAtCompileTime(C const* /*ptr*/) {}
153 
155  };
156 } // namespace edm
157 
159 
160 namespace edm {
161 
163  template <typename C>
164  inline C const& RefProd<C>::operator*() const {
165  return *(edm::template getProduct<C>(product_));
166  }
167 
169  template <typename C>
170  inline C const* RefProd<C>::operator->() const {
171  return edm::template getProduct<C>(product_);
172  }
173 
174  template <typename C>
176  edm::swap(product_, other.product_);
177  }
178 
179  template <typename C>
180  inline bool operator==(RefProd<C> const& lhs, RefProd<C> const& rhs) {
181  return lhs.refCore() == rhs.refCore();
182  }
183 
184  template <typename C>
185  inline bool operator!=(RefProd<C> const& lhs, RefProd<C> const& rhs) {
186  return !(lhs == rhs);
187  }
188 
189  template <typename C>
190  inline bool operator<(RefProd<C> const& lhs, RefProd<C> const& rhs) {
191  return (lhs.refCore() < rhs.refCore());
192  }
193 
194  template <typename C>
195  inline void swap(RefProd<C> const& lhs, RefProd<C> const& rhs) {
196  lhs.swap(rhs);
197  }
198 } // namespace edm
199 
200 //Handle specialization here
202 
203 #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:139
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:164
bool isTransient() const
Checks if this RefProd is transient (i.e. not persistable).
Definition: RefProd.h:142
bool operator!() const
Checks for null.
Definition: RefProd.h:121
void checkTypeAtCompileTime(C const *)
Definition: RefProd.h:152
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:175
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:170
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:154
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