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