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 
37 #include <memory>
38 #include "boost/static_assert.hpp"
39 #include "boost/type_traits/is_base_of.hpp"
40 
45 
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  template <typename T1>
81  explicit RefToBase(RefToBase<T1> const & r );
82  RefToBase(std::shared_ptr<reftobase::RefHolderBase> p);
83  ~RefToBase();
84 
85  RefToBase& operator= (RefToBase const& rhs);
86 
87  value_type const& operator*() const;
88  value_type const* operator->() const;
89  value_type const* get() const;
90 
91  ProductID id() const;
92  size_t key() const;
93 
94  template <class REF> REF castTo() const;
95 
96  bool isNull() const;
97  bool isNonnull() const;
98  bool operator!() const;
99 
100  bool operator==(RefToBase const& rhs) const;
101  bool operator!=(RefToBase const& rhs) const;
102 
103  void swap(RefToBase& other);
104 
105  std::auto_ptr<reftobase::RefHolderBase> holder() const;
106 
107  EDProductGetter const* productGetter() const;
108  bool hasProductCache() const;
109  void const * product() const;
110 
113  bool isAvailable() const { return holder_? holder_->isAvailable(): false; }
114 
115  //Needed for ROOT storage
117  private:
119  reftobase::BaseHolder<value_type>* holder_;
120  friend class RefToBaseVector<T>;
121  friend class RefToBaseProd<T>;
122  template<typename B> friend class RefToBase;
123  };
124 
125  //--------------------------------------------------------------------
126  // Implementation of RefToBase<T>
127  //--------------------------------------------------------------------
128 
129  template <class T>
130  inline
131  RefToBase<T>::RefToBase() :
132  holder_(0)
133  { }
134 
135  template <class T>
136  inline
138  holder_(other.holder_ ? other.holder_->clone() : 0)
139  { }
140 
141  template <class T>
142  template <typename C1, typename T1, typename F1>
143  inline
145  holder_(new reftobase::Holder<T,Ref<C1, T1, F1> >(iRef))
146  { }
147 
148  template <class T>
149  template <typename C>
150  inline
152  holder_(new reftobase::Holder<T,RefProd<C> >(iRef))
153  { }
154 
155  template <class T>
156  template <typename T1>
157  inline
159  holder_(new reftobase::IndirectHolder<T> (
160  std::shared_ptr< edm::reftobase::RefHolderBase>(iRef.holder().release())
161  ) )
162  {
163  // OUT: holder_( new reftobase::Holder<T,RefToBase<T1> >(iRef ) ) {
164  // Forcing the conversion through IndirectHolder,
165  // as Holder<T,RefToBase<T1>> would need dictionaries we will never have.
166  // In this way we only need the IndirectHolder<T> and the RefHolder of the real type of the item
167  // This might cause a small performance penalty.
168  BOOST_STATIC_ASSERT( ( boost::is_base_of<T, T1>::value ) );
169  }
170 
171  template <class T>
172  inline
173  RefToBase<T>::RefToBase(std::shared_ptr<reftobase::RefHolderBase> p) :
174  holder_(new reftobase::IndirectHolder<T>(p))
175  { }
176 
177  template <class T>
178  inline
180  {
181  delete holder_;
182  }
183 
184  template <class T>
185  inline
186  RefToBase<T>&
188  {
189  RefToBase<T> temp( iRHS);
190  temp.swap(*this);
191  return *this;
192  }
193 
194  template <class T>
195  inline
196  T const&
198  {
199  return *getPtrImpl();
200  }
201 
202  template <class T>
203  inline
204  T const*
206  {
207  return getPtrImpl();
208  }
209 
210  template <class T>
211  inline
212  T const*
214  {
215  return getPtrImpl();
216  }
217 
218  template <class T>
219  inline
220  ProductID
222  {
223  return holder_ ? holder_->id() : ProductID();
224  }
225 
226  template <class T>
227  inline
228  size_t
230  {
231  if ( holder_ == 0 )
233  "attempting get key from null RefToBase;\n"
234  "You should check for nullity before calling key().");
235  return holder_->key();
236  }
237 
239  template <class T>
240  template <class REF>
241  REF
243  {
244  if (!holder_)
245  {
247  "attempting to cast a null RefToBase;\n"
248  "You should check for nullity before casting.");
249  }
250 
251  reftobase::RefHolder<REF> concrete_holder;
252  std::string hidden_ref_type;
253  if (!holder_->fillRefIfMyTypeMatches(concrete_holder,
254  hidden_ref_type))
255  {
257  "cast to type: ",
258  typeid(REF).name(),
259  "\nfrom type: ",
260  hidden_ref_type.c_str(),
261  " failed. Catch this exception in case you need to check"
262  " the concrete reference type.");
263  }
264  return concrete_holder.getRef();
265  }
266 
268  template <class T>
269  inline
270  bool
272  {
273  return !id().isValid();
274  }
275 
277  template <class T>
278  inline
279  bool
281  {
282  return !isNull();
283  }
284 
286  template <class T>
287  inline
288  bool
290  {
291  return isNull();
292  }
293 
294  template <class T>
295  inline
296  bool
298  {
299  return holder_
300  ? holder_->isEqualTo(*rhs.holder_)
301  : holder_ == rhs.holder_;
302  }
303 
304  template <class T>
305  inline
306  bool
308  {
309  return !(*this == rhs);
310  }
311 
312  template <class T>
313  inline
314  void
316  {
317  std::swap(holder_, other.holder_);
318  }
319 
320  template <class T>
321  inline
323  return holder_? holder_->productGetter():nullptr;
324  }
325 
326  template <class T>
327  inline
329  return holder_?holder_->hasProductCache():false;
330  }
331 
332  template <class T>
333  inline
334  void const * RefToBase<T>::product() const {
335  return holder_?holder_->product():nullptr;
336  }
337 
338  template <class T>
339  inline
340  T const*
342  {
343  return holder_ ? holder_->getPtr() : 0;
344  }
345 
346  template <class T>
347  std::auto_ptr<reftobase::RefHolderBase> RefToBase<T>::holder() const {
348  return holder_? holder_->holder() : std::auto_ptr<reftobase::RefHolderBase>();
349  }
350 
351  // Free swap function
352  template <class T>
353  inline
354  void
356  {
357  a.swap(b);
358  }
359 }
360 
364 
365 namespace edm {
366  template <class T>
367  inline
369  holder_( r.operator->()->refAt( i ).holder_->clone() ) {
370  }
371 
372  template<typename T>
373  inline
375  holder_( handle.operator->()->refAt( i ).holder_->clone() ) {
376  }
377 
378 }
379 
380 #endif
value_type const * get() const
Definition: RefToBase.h:213
int i
Definition: DBlmapReader.cc:9
bool isAvailable() const
Definition: RefToBase.h:113
REF const & getRef() const
Definition: RefHolder_.h:107
bool hasProductCache() const
Definition: RefToBase.h:328
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:280
virtual bool isAvailable() const =0
std::auto_ptr< reftobase::RefHolderBase > holder() const
Definition: RefToBase.h:347
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
ProductID id() const
Definition: RefToBase.h:221
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
bool operator==(RefToBase const &rhs) const
Definition: RefToBase.h:297
void const * product() const
Definition: RefToBase.h:334
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:315
reftobase::BaseHolder< value_type > * holder_
Definition: RefToBase.h:119
size_t key() const
Definition: RefToBase.h:229
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
value_type const * operator->() const
Definition: RefToBase.h:205
tuple handle
Definition: patZpeak.py:22
bool operator!=(RefToBase const &rhs) const
Definition: RefToBase.h:307
edm::RefProd< Container > RefProd
REF castTo() const
cast to a concrete type
Definition: RefToBase.h:242
bool isNull() const
Checks for null.
Definition: RefToBase.h:271
value_type const * getPtrImpl() const
Definition: RefToBase.h:341
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:322
bool operator!() const
Checks for null.
Definition: RefToBase.h:289
#define private
Definition: FWFileEntry.h:17
double a
Definition: hdecay.h:121
value_type const & operator*() const
Definition: RefToBase.h:197
ProductIndex id() const
Definition: ProductID.h:38
long double T
RefToBase & operator=(RefToBase const &rhs)
Definition: RefToBase.h:187
def template
Definition: svgfig.py:520