CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RefToBase.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefToBase_h
2 #define DataFormats_Common_RefToBase_h
3 // -*- C++ -*-
4 //
5 // Package: Common
6 // Class : RefToBase
7 //
28 //
29 // Original Author: Chris Jones
30 // Created: Mon Apr 3 16:37:59 EDT 2006
31 //
32 
33 // system include files
34 
35 // user include files
36 
41 
45 
46 #include <memory>
47 #ifndef __GCCXML__
48 #include <type_traits>
49 #endif
50 
51 namespace edm {
52  //--------------------------------------------------------------------
53  // Class template RefToBase<T>
54  //--------------------------------------------------------------------
55 
59 
60  template<typename T> class RefToBaseVector;
61  template<typename C, typename T, typename F> class Ref;
62  template<typename C> class RefProd;
63  template<typename T> class RefToBaseProd;
64  template<typename T> class View;
65 
66  template <class T>
67  class RefToBase
68  {
69  public:
70  typedef T value_type;
71 
72  RefToBase();
73  RefToBase(RefToBase const& other);
74  template <typename C1, typename T1, typename F1>
75  explicit RefToBase(Ref<C1, T1, F1> const& r);
76  template <typename C>
77  explicit RefToBase(RefProd<C> const& r);
78  RefToBase(RefToBaseProd<T> const& r, size_t i);
79  RefToBase(Handle<View<T> > const& handle, size_t i);
80 #ifndef __GCCXML__
81  template <typename T1>
82  explicit RefToBase(RefToBase<T1> const & r );
84 #endif
85  RefToBase(std::shared_ptr<reftobase::RefHolderBase> p);
86 
87  ~RefToBase();
88 
89  RefToBase& operator= (RefToBase const& rhs);
90 
91  value_type const& operator*() const;
92  value_type const* operator->() const;
93  value_type const* get() const;
94 
95  ProductID id() const;
96  size_t key() const;
97 
98  template <class REF> REF castTo() const;
99 
100  bool isNull() const;
101  bool isNonnull() const;
102  bool operator!() const;
103 
104  bool operator==(RefToBase const& rhs) const;
105  bool operator!=(RefToBase const& rhs) const;
106 
107  void swap(RefToBase& other);
108 
109  std::auto_ptr<reftobase::RefHolderBase> holder() const;
110 
111  EDProductGetter const* productGetter() const;
112 
115  bool isAvailable() const { return holder_? holder_->isAvailable(): false; }
116 
117  bool isTransient() const { return holder_ ? holder_->isTransient() : false; }
118 
119  //Needed for ROOT storage
121  private:
123  reftobase::BaseHolder<value_type>* holder_;
124  friend class RefToBaseVector<T>;
125  friend class RefToBaseProd<T>;
126  template<typename B> friend class RefToBase;
127  };
128 
129  //--------------------------------------------------------------------
130  // Implementation of RefToBase<T>
131  //--------------------------------------------------------------------
132 
133  template <class T>
134  inline
135  RefToBase<T>::RefToBase() :
136  holder_(0)
137  { }
138 
139  template <class T>
140  inline
142  holder_(other.holder_ ? other.holder_->clone() : 0)
143  { }
144 
145  template <class T>
146  template <typename C1, typename T1, typename F1>
147  inline
149  holder_(new reftobase::Holder<T,Ref<C1, T1, F1> >(iRef))
150  { }
151 
152  template <class T>
153  template <typename C>
154  inline
156  holder_(new reftobase::Holder<T,RefProd<C> >(iRef))
157  { }
158 
159 #ifndef __GCCXML__
160  template <class T>
161  template <typename T1>
162  inline
164  holder_(new reftobase::IndirectHolder<T> (
165  std::shared_ptr< edm::reftobase::RefHolderBase>(iRef.holder().release())
166  ) )
167  {
168  // OUT: holder_( new reftobase::Holder<T,RefToBase<T1> >(iRef ) ) {
169  // Forcing the conversion through IndirectHolder,
170  // as Holder<T,RefToBase<T1>> would need dictionaries we will never have.
171  // In this way we only need the IndirectHolder<T> and the RefHolder of the real type of the item
172  // This might cause a small performance penalty.
173  static_assert( std::is_base_of<T, T1>::value, "RefToBase::RefToBase T not base of T1" );
174  }
175 
176  template <class T>
177  inline
179  holder_(p.release())
180  {}
181 #endif
182 
183  template <class T>
184  inline
185  RefToBase<T>::RefToBase(std::shared_ptr<reftobase::RefHolderBase> p) :
186  holder_(new reftobase::IndirectHolder<T>(p))
187  { }
188 
189  template <class T>
190  inline
192  {
193  delete holder_;
194  }
195 
196  template <class T>
197  inline
198  RefToBase<T>&
200  {
201  RefToBase<T> temp( iRHS);
202  temp.swap(*this);
203  return *this;
204  }
205 
206  template <class T>
207  inline
208  T const&
210  {
211  return *getPtrImpl();
212  }
213 
214  template <class T>
215  inline
216  T const*
218  {
219  return getPtrImpl();
220  }
221 
222  template <class T>
223  inline
224  T const*
226  {
227  return getPtrImpl();
228  }
229 
230  template <class T>
231  inline
232  ProductID
234  {
235  return holder_ ? holder_->id() : ProductID();
236  }
237 
238  template <class T>
239  inline
240  size_t
242  {
243  if ( holder_ == 0 )
245  "attempting get key from null RefToBase;\n"
246  "You should check for nullity before calling key().");
247  return holder_->key();
248  }
249 
250 #ifndef __GCCXML__
251  namespace {
252  // If the template parameters are classes or structs they should be
253  // related by inheritance, otherwise they should be the same type.
254  template<typename T, typename U>
256  checkTypeCompatibility() { static_assert(std::is_base_of<T, U>::value ||
258  "RefToBase::castTo error element types are not related by inheritance"); }
259 
260  template<typename T, typename U>
262  checkTypeCompatibility() { static_assert(std::is_same<T, U>::value,
263  "RefToBase::castTo error non-class element types are not the same"); }
264 
265  // Convert the pointer types, use dynamic_cast if they are classes
266  template<typename T, typename OUT>
268  convertTo(T const* t) { return dynamic_cast<OUT const*>(t); }
269 
270  template<typename T, typename OUT>
272  convertTo(T const* t) { return t;}
273  }
274 
275  template <class T>
276  template <class REF>
277  REF
279 
280  if (!holder_) {
282  "attempting to cast a null RefToBase;\n"
283  "You should check for nullity before casting.");
284  }
285 
286  checkTypeCompatibility<T, typename REF::value_type>();
287 
288  // If REF is type edm::Ref<C,T,F>, then it is impossible to
289  // check the container type C here. We just have to assume
290  // that the caller provided the correct type.
291 
292  EDProductGetter const* getter = productGetter();
293  if(getter) {
294  return REF(id(), key(), getter);
295  }
296 
297  T const* value = get();
298  if(value == nullptr) {
299  return REF(id());
300  }
301  typename REF::value_type const* newValue = convertTo<T, typename REF::value_type>(value);
302  if(newValue) {
303  return REF(id(), newValue, key(), isTransient());
304  }
305 
307  "RefToBase<T>::castTo Error attempting to cast mismatched types\n"
308  "casting from RefToBase with T: ",
309  typeid(T).name(),
310  "\ncasting to: ",
311  typeid(REF).name()
312  );
313  return REF();
314  }
315 #endif
316 
318  template <class T>
319  inline
320  bool
322  {
323  return !id().isValid();
324  }
325 
327  template <class T>
328  inline
329  bool
331  {
332  return !isNull();
333  }
334 
336  template <class T>
337  inline
338  bool
340  {
341  return isNull();
342  }
343 
344  template <class T>
345  inline
346  bool
348  {
349  return holder_
350  ? holder_->isEqualTo(*rhs.holder_)
351  : holder_ == rhs.holder_;
352  }
353 
354  template <class T>
355  inline
356  bool
358  {
359  return !(*this == rhs);
360  }
361 
362  template <class T>
363  inline
364  void
366  {
367  std::swap(holder_, other.holder_);
368  }
369 
370  template <class T>
371  inline
373  return holder_? holder_->productGetter():nullptr;
374  }
375 
376  template <class T>
377  inline
378  T const*
380  {
381  return holder_ ? holder_->getPtr() : 0;
382  }
383 
384  template <class T>
385  std::auto_ptr<reftobase::RefHolderBase> RefToBase<T>::holder() const {
386  return holder_? holder_->holder() : std::auto_ptr<reftobase::RefHolderBase>();
387  }
388 
389  // Free swap function
390  template <class T>
391  inline
392  void
394  {
395  a.swap(b);
396  }
397 }
398 
402 
403 namespace edm {
404  template <class T>
405  inline
407  holder_( r.operator->()->refAt( i ).holder_->clone() ) {
408  }
409 
410  template<typename T>
411  inline
413  holder_( handle.operator->()->refAt( i ).holder_->clone() ) {
414  }
415 
416 }
417 
418 #endif
type
Definition: HCALResponse.h:21
value_type const * get() const
Definition: RefToBase.h:225
tuple t
Definition: tree.py:139
int i
Definition: DBlmapReader.cc:9
virtual bool isTransient() const =0
bool isAvailable() const
Definition: RefToBase.h:115
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:330
virtual bool isAvailable() const =0
std::auto_ptr< reftobase::RefHolderBase > holder() const
Definition: RefToBase.h:385
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
EDProductGetter const * productGetter(std::atomic< void const * > const &iCache)
ProductID id() const
Definition: RefToBase.h:233
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
bool operator==(RefToBase const &rhs) const
Definition: RefToBase.h:347
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
void swap(RefToBase &other)
Definition: RefToBase.h:365
reftobase::BaseHolder< value_type > * holder_
Definition: RefToBase.h:123
size_t key() const
Definition: RefToBase.h:241
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
value_type const * operator->() const
Definition: RefToBase.h:217
tuple handle
Definition: patZpeak.py:22
#define private
Definition: FWEveView.cc:22
bool operator!=(RefToBase const &rhs) const
Definition: RefToBase.h:357
Container::value_type value_type
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
edm::RefProd< Container > RefProd
REF castTo() const
Definition: RefToBase.h:278
bool isNull() const
Checks for null.
Definition: RefToBase.h:321
value_type const * getPtrImpl() const
Definition: RefToBase.h:379
double b
Definition: hdecay.h:120
string const
Definition: compareJSON.py:14
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
EDProductGetter const * productGetter() const
Definition: RefToBase.h:372
bool operator!() const
Checks for null.
Definition: RefToBase.h:339
double a
Definition: hdecay.h:121
value_type const & operator*() const
Definition: RefToBase.h:209
bool isTransient() const
Definition: RefToBase.h:117
ProductIndex id() const
Definition: ProductID.h:38
long double T
RefToBase & operator=(RefToBase const &rhs)
Definition: RefToBase.h:199
def template
Definition: svgfig.py:520