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 {
58  return isNull() ? 0 : this->operator->();
59  }
60 
63  product_type const* product() const {
64  return isNull() ? 0 : this->operator->();
65  }
66 
68  bool isNull() const {return !isNonnull(); }
69 
71  bool isNonnull() const { return product_.isNonnull(); }
72 
74  bool operator!() const {return isNull(); }
75 
77  ProductID id() const {return product_.id();}
78 
81 
83  bool hasCache() const {return product_.productPtr() != 0;}
84 
86 
87  void swap(RefToBaseProd<T>&);
88 
89  //Needed for ROOT storage
91  private:
92  //NOTE: Access to RefCore should be private since we modify the use of productPtr
93  RefCore const& refCore() const {
94  return product_;
95  }
96 
97  View<T> const* viewPtr() const {
98  return reinterpret_cast<const View<T>*>(product_.productPtr());
99  }
100 
102  };
103 
104  template<typename T>
105  inline
107  product_(handle.id(), 0, 0, false){
109  }
110 
111  template<typename T>
112  inline
114  product_(ref.product_) {
115  if(product_.productPtr()) {
116  product_.setProductPtr(ref.viewPtr() ? (new View<T>(* ref)) : 0);
117  }
118  }
119 
120  template<typename T>
121  inline
123  RefToBaseProd<T> temp(other);
124  this->swap(temp);
125  return *this;
126  }
127 
129  template<typename T>
130  inline
132  return * operator->();
133  }
134 
136  template<typename T>
137  inline
139  //Another thread might change the value returned so just get it once
140  auto getter = product_.productGetter();
141  if(getter != nullptr) {
142  if(product_.isNull()) {
144  "attempting get view from a null RefToBaseProd.\n");
145  }
146  ProductID tId = product_.id();
147  std::vector<void const*> pointers;
149  WrapperBase const* prod = getter->getIt(tId);
150  if(prod == nullptr) {
152  "attempting to get view from an unavailable RefToBaseProd.");
153  }
154  prod->fillView(tId, pointers, helpers);
155  std::unique_ptr<View<T>> tmp{ new View<T>(pointers, helpers, getter) };
157  tmp.release();
158  }
159  }
160  return viewPtr();
161  }
162 
163  template<typename T>
164  inline
166  edm::swap(product_, other.product_);
167  }
168 
169  template<typename T>
170  inline
171  bool
173  return lhs.refCore() == rhs.refCore();
174  }
175 
176  template<typename T>
177  inline
178  bool
180  return !(lhs == rhs);
181  }
182 
183  template<typename T>
184  inline
185  bool
186  operator< (RefToBaseProd<T> const& lhs, RefToBaseProd<T> const& rhs) {
187  return (lhs.refCore() < rhs.refCore());
188  }
189 
190  template<typename T>
191  inline void swap(edm::RefToBaseProd<T> const& lhs, edm::RefToBaseProd<T> const& rhs) {
192  lhs.swap(rhs);
193  }
194 }
195 
197 
198 namespace edm {
199  template<typename T>
200  template<typename C>
201  inline
203  product_(ref.refCore()) {
204  std::vector<void const*> pointers;
206  fillView(* ref.product(), ref.id(), pointers, helpers);
207  product_.setProductPtr(new View<T>(pointers, helpers, ref.refCore().productGetter()));
208  }
209 
210  template<typename T>
211  template<typename C>
212  inline
214  product_(handle.id(), handle.product(), 0, false) {
215  std::vector<void const*> pointers;
217  fillView(* handle, handle.id(), pointers, helpers);
218  product_.setProductPtr(new View<T>(pointers, helpers,0));
219  }
220 
221  template<typename T>
222  template<typename C>
223  inline
225  product_(handle.id(), handle.product(), 0, false) {
226  std::vector<void const*> pointers;
228  fillView(* handle, handle.id(), pointers, helpers);
229  product_.setProductPtr(new View<T>(pointers, helpers,0));
230  }
231 
232  template<typename T>
233  inline
235  product_(id, nullptr, getter, false) { }
236 }
237 
238 #endif
View< T > const * viewPtr() const
Definition: RefToBaseProd.h:97
~RefToBaseProd()
Destructor.
Definition: RefToBaseProd.h:47
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:58
product_type const * operator->() const
Member dereference operator.
ProductID id() const
Definition: HandleBase.cc:15
bool tryToSetProductPtrForFirstTime(void const *prodPtr) const
Definition: RefCore.h:66
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
#define nullptr
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
bool isNonnull() const
Definition: RefCore.h:74
void fillView(ProductID const &id, std::vector< void const * > &view, FillViewHelperVector &helpers) const
Definition: WrapperBase.cc:14
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
ProductID id() const
RefCore const & refCore() const
Definition: RefToBaseProd.h:93
RefToBaseProd()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: RefToBaseProd.h:34
bool hasCache() const
Checks if product is in memory.
Definition: RefToBaseProd.h:83
product_type const * product() const
Definition: RefProd.h:119
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
void const * productPtr() const
Definition: RefCore.h:52
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers)
RefCore const & refCore() const
Definition: RefProd.h:123
RefToBaseProd< T > & operator=(const RefToBaseProd< T > &other)
ProductID id() const
Accessor for product ID.
Definition: RefToBaseProd.h:77
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefToBaseProd.h:80
bool isNull() const
Definition: RefCore.h:71
ProductID id() const
Accessor for product ID.
Definition: RefProd.h:137
EDProductGetter const * productGetter() const
Definition: RefCore.h:84
product_type const * product() const
Definition: RefToBaseProd.h:63
ProductID id() const
Definition: RefCore.h:49
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
HLT enums.
bool operator!() const
Checks for null.
Definition: RefToBaseProd.h:74
product_type const & operator*() const
Dereference operator.
View< T > product_type
Definition: RefToBaseProd.h:31
bool isNull() const
Checks for null.
Definition: RefToBaseProd.h:68
void swap(RefToBaseProd< T > &)
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
bool isNonnull() const
Checks for non-null.
Definition: RefToBaseProd.h:71