CMS 3D CMS Logo

RefToBaseProd.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefToBaseProd_h
2 #define DataFormats_Common_RefToBaseProd_h
3 
4 /* \class edm::RefToBaseProd<T>
5  *
6  * \author Luca Lista, INFN
7  *
8  */
10 
22 
23 #include <utility>
24 #include <vector>
25 
26 namespace edm {
27 
28  template <typename T>
29  class RefToBaseProd {
30  public:
32 
35 
36  template <typename C>
37  explicit RefToBaseProd(Handle<C> const& handle);
38  explicit RefToBaseProd(Handle<View<T>> const& handle);
39  template <typename C>
40  explicit RefToBaseProd(OrphanHandle<C> const& handle);
42  template <typename C>
43  explicit RefToBaseProd(const RefProd<C>&);
44  RefToBaseProd(ProductID const&, EDProductGetter const*);
45 
47  ~RefToBaseProd() { delete viewPtr(); }
48 
50  product_type const& operator*() const;
51 
53  product_type const* operator->() const;
54 
57  product_type const* get() const { return isNull() ? nullptr : this->operator->(); }
58 
61  product_type const* product() const { return isNull() ? 0 : this->operator->(); }
62 
64  bool isNull() const { return !isNonnull(); }
65 
67  bool isNonnull() const { return product_.isNonnull(); }
68 
73  bool isAvailable() const { return product_.isAvailable(); }
74 
76  bool operator!() const { return isNull(); }
77 
79  ProductID id() const { return product_.id(); }
80 
82  EDProductGetter const* productGetter() const { return product_.productGetter(); }
83 
85  bool hasCache() const { return product_.productPtr() != nullptr; }
86 
88 
89  void swap(RefToBaseProd<T>&);
90 
91  //Needed for ROOT storage
93  private:
94  //NOTE: Access to RefCore should be private since we modify the use of productPtr
95  RefCore const& refCore() const { return product_; }
96 
97  View<T> const* viewPtr() const { return reinterpret_cast<const View<T>*>(product_.productPtr()); }
98 
100  };
101 
102  template <typename T>
104  : product_(handle.id(), nullptr, nullptr, false) {
106  }
107 
108  template <typename T>
109  inline RefToBaseProd<T>::RefToBaseProd(const RefToBaseProd<T>& ref) : product_(ref.product_) {
110  if (product_.productPtr()) {
111  product_.setProductPtr(ref.viewPtr() ? (new View<T>(*ref)) : nullptr);
112  }
113  }
114 
115  template <typename T>
118  this->swap(temp);
119  return *this;
120  }
121 
123  template <typename T>
124  inline View<T> const& RefToBaseProd<T>::operator*() const {
125  return *operator->();
126  }
127 
129  template <typename T>
130  inline View<T> const* RefToBaseProd<T>::operator->() const {
131  //Another thread might change the value returned so just get it once
132  auto getter = product_.productGetter();
133  if (getter != nullptr) {
134  if (product_.isNull()) {
135  Exception::throwThis(errors::InvalidReference, "attempting get view from a null RefToBaseProd.\n");
136  }
137  ProductID tId = product_.id();
138  std::vector<void const*> pointers;
140  WrapperBase const* prod = getter->getIt(tId);
141  if (prod == nullptr) {
142  Exception::throwThis(errors::InvalidReference, "attempting to get view from an unavailable RefToBaseProd.");
143  }
144  prod->fillView(tId, pointers, helpers);
145  std::unique_ptr<View<T>> tmp{new View<T>(pointers, helpers, getter)};
146  if (product_.tryToSetProductPtrForFirstTime(tmp.get())) {
147  tmp.release();
148  }
149  }
150  return viewPtr();
151  }
152 
153  template <typename T>
155  edm::swap(product_, other.product_);
156  }
157 
158  template <typename T>
159  inline bool operator==(RefToBaseProd<T> const& lhs, RefToBaseProd<T> const& rhs) {
160  return lhs.refCore() == rhs.refCore();
161  }
162 
163  template <typename T>
164  inline bool operator!=(RefToBaseProd<T> const& lhs, RefToBaseProd<T> const& rhs) {
165  return !(lhs == rhs);
166  }
167 
168  template <typename T>
169  inline bool operator<(RefToBaseProd<T> const& lhs, RefToBaseProd<T> const& rhs) {
170  return (lhs.refCore() < rhs.refCore());
171  }
172 
173  template <typename T>
174  inline void swap(edm::RefToBaseProd<T> const& lhs, edm::RefToBaseProd<T> const& rhs) {
175  lhs.swap(rhs);
176  }
177 } // namespace edm
178 
180 
181 namespace edm {
182  template <typename T>
183  template <typename C>
184  inline RefToBaseProd<T>::RefToBaseProd(const RefProd<C>& ref) : product_(ref.refCore()) {
185  std::vector<void const*> pointers;
187  fillView(*ref.product(), ref.id(), pointers, helpers);
188  product_.setProductPtr(new View<T>(pointers, helpers, ref.refCore().productGetter()));
189  }
190 
191  template <typename T>
192  template <typename C>
194  : product_(handle.id(), handle.product(), nullptr, false) {
195  std::vector<void const*> pointers;
197  fillView(*handle, handle.id(), pointers, helpers);
198  product_.setProductPtr(new View<T>(pointers, helpers, nullptr));
199  }
200 
201  template <typename T>
202  template <typename C>
204  : product_(handle.id(), handle.product(), nullptr, false) {
205  std::vector<void const*> pointers;
207  fillView(*handle, handle.id(), pointers, helpers);
208  product_.setProductPtr(new View<T>(pointers, helpers, nullptr));
209  }
210 
211  template <typename T>
213  : product_(id, nullptr, getter, false) {}
214 } // namespace edm
215 
216 #endif
constexpr bool operator==(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const *> &pointers, FillViewHelperVector &helpers)
bool isAvailable() const
Definition: RefToBaseProd.h:73
~RefToBaseProd()
Destructor.
Definition: RefToBaseProd.h:47
ProductID id() const
Definition: RefCore.h:48
bool isAvailable() const
Definition: RefCore.cc:146
#define CMS_CLASS_VERSION(_version_)
void const * productPtr() const
Definition: RefCore.h:51
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
RefCore const & refCore() const
Definition: RefProd.h:112
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:85
ProductID id() const
Accessor for product ID.
Definition: RefToBaseProd.h:79
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefToBaseProd.h:82
bool isNonnull() const
Checks for non-null.
Definition: RefToBaseProd.h:67
product_type const * operator->() const
Member dereference operator.
RefToBaseProd()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: RefToBaseProd.h:34
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:57
RefToBaseProd< T > & operator=(const RefToBaseProd< T > &other)
product_type const * product() const
Definition: RefToBaseProd.h:61
bool isNonnull() const
Definition: RefCore.h:71
constexpr bool operator!=(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
ProductID id() const
Accessor for product ID.
Definition: RefProd.h:124
ProductIndex id() const
Definition: ProductID.h:35
RefCore const & refCore() const
Definition: RefToBaseProd.h:95
bool hasCache() const
Checks if product is in memory.
Definition: RefToBaseProd.h:85
HLT enums.
View< T > const * viewPtr() const
Definition: RefToBaseProd.h:97
View< T > product_type
Definition: RefToBaseProd.h:31
bool operator!() const
Checks for null.
Definition: RefToBaseProd.h:76
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
tmp
align.sh
Definition: createJobs.py:716
void swap(RefToBaseProd< T > &)
product_type const & operator*() const
Dereference operator.
product_type const * product() const
Definition: RefProd.h:110
bool isNull() const
Checks for null.
Definition: RefToBaseProd.h:64
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector