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 
70  bool operator!() const { return isNull(); }
71 
73  ProductID id() const { return product_.id(); }
74 
76  EDProductGetter const* productGetter() const { return product_.productGetter(); }
77 
79  bool hasCache() const { return product_.productPtr() != nullptr; }
80 
82 
83  void swap(RefToBaseProd<T>&);
84 
85  //Needed for ROOT storage
87  private:
88  //NOTE: Access to RefCore should be private since we modify the use of productPtr
89  RefCore const& refCore() const { return product_; }
90 
91  View<T> const* viewPtr() const { return reinterpret_cast<const View<T>*>(product_.productPtr()); }
92 
94  };
95 
96  template <typename T>
98  : product_(handle.id(), nullptr, nullptr, false) {
100  }
101 
102  template <typename T>
103  inline RefToBaseProd<T>::RefToBaseProd(const RefToBaseProd<T>& ref) : product_(ref.product_) {
104  if (product_.productPtr()) {
105  product_.setProductPtr(ref.viewPtr() ? (new View<T>(*ref)) : nullptr);
106  }
107  }
108 
109  template <typename T>
112  this->swap(temp);
113  return *this;
114  }
115 
117  template <typename T>
118  inline View<T> const& RefToBaseProd<T>::operator*() const {
119  return *operator->();
120  }
121 
123  template <typename T>
124  inline View<T> const* RefToBaseProd<T>::operator->() const {
125  //Another thread might change the value returned so just get it once
126  auto getter = product_.productGetter();
127  if (getter != nullptr) {
128  if (product_.isNull()) {
129  Exception::throwThis(errors::InvalidReference, "attempting get view from a null RefToBaseProd.\n");
130  }
131  ProductID tId = product_.id();
132  std::vector<void const*> pointers;
134  WrapperBase const* prod = getter->getIt(tId);
135  if (prod == nullptr) {
136  Exception::throwThis(errors::InvalidReference, "attempting to get view from an unavailable RefToBaseProd.");
137  }
138  prod->fillView(tId, pointers, helpers);
139  std::unique_ptr<View<T>> tmp{new View<T>(pointers, helpers, getter)};
140  if (product_.tryToSetProductPtrForFirstTime(tmp.get())) {
141  tmp.release();
142  }
143  }
144  return viewPtr();
145  }
146 
147  template <typename T>
149  edm::swap(product_, other.product_);
150  }
151 
152  template <typename T>
153  inline bool operator==(RefToBaseProd<T> const& lhs, RefToBaseProd<T> const& rhs) {
154  return lhs.refCore() == rhs.refCore();
155  }
156 
157  template <typename T>
158  inline bool operator!=(RefToBaseProd<T> const& lhs, RefToBaseProd<T> const& rhs) {
159  return !(lhs == rhs);
160  }
161 
162  template <typename T>
163  inline bool operator<(RefToBaseProd<T> const& lhs, RefToBaseProd<T> const& rhs) {
164  return (lhs.refCore() < rhs.refCore());
165  }
166 
167  template <typename T>
168  inline void swap(edm::RefToBaseProd<T> const& lhs, edm::RefToBaseProd<T> const& rhs) {
169  lhs.swap(rhs);
170  }
171 } // namespace edm
172 
174 
175 namespace edm {
176  template <typename T>
177  template <typename C>
178  inline RefToBaseProd<T>::RefToBaseProd(const RefProd<C>& ref) : product_(ref.refCore()) {
179  std::vector<void const*> pointers;
181  fillView(*ref.product(), ref.id(), pointers, helpers);
182  product_.setProductPtr(new View<T>(pointers, helpers, ref.refCore().productGetter()));
183  }
184 
185  template <typename T>
186  template <typename C>
188  : product_(handle.id(), handle.product(), nullptr, false) {
189  std::vector<void const*> pointers;
191  fillView(*handle, handle.id(), pointers, helpers);
192  product_.setProductPtr(new View<T>(pointers, helpers, nullptr));
193  }
194 
195  template <typename T>
196  template <typename C>
198  : product_(handle.id(), handle.product(), nullptr, false) {
199  std::vector<void const*> pointers;
201  fillView(*handle, handle.id(), pointers, helpers);
202  product_.setProductPtr(new View<T>(pointers, helpers, nullptr));
203  }
204 
205  template <typename T>
207  : product_(id, nullptr, getter, false) {}
208 } // namespace edm
209 
210 #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)
~RefToBaseProd()
Destructor.
Definition: RefToBaseProd.h:47
ProductID id() const
Definition: RefCore.h:48
#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:73
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefToBaseProd.h:76
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:89
bool hasCache() const
Checks if product is in memory.
Definition: RefToBaseProd.h:79
HLT enums.
View< T > const * viewPtr() const
Definition: RefToBaseProd.h:91
View< T > product_type
Definition: RefToBaseProd.h:31
bool operator!() const
Checks for null.
Definition: RefToBaseProd.h:70
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