CMS 3D CMS Logo

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 #include <type_traits>
48 
49 namespace edm {
50  //--------------------------------------------------------------------
51  // Class template RefToBase<T>
52  //--------------------------------------------------------------------
53 
57 
58  template <typename T>
59  class RefToBaseVector;
60  template <typename C, typename T, typename F>
61  class Ref;
62  template <typename C>
63  class RefProd;
64  template <typename T>
66  template <typename T>
67  class View;
68 
69  template <class T>
70  class RefToBase {
71  public:
72  typedef T value_type;
73 
74  RefToBase();
75  RefToBase(RefToBase const& other);
76  RefToBase(RefToBase&& other) noexcept;
77  RefToBase& operator=(RefToBase&& other) noexcept;
78 
79  template <typename C1, typename T1, typename F1>
80  explicit RefToBase(Ref<C1, T1, F1> const& r);
81  template <typename C>
82  explicit RefToBase(RefProd<C> const& r);
83  RefToBase(RefToBaseProd<T> const& r, size_t i);
84  RefToBase(Handle<View<T>> const& handle, size_t i);
85  template <typename T1>
86  explicit RefToBase(RefToBase<T1> const& r);
88  RefToBase(std::shared_ptr<reftobase::RefHolderBase> p);
89 
90  ~RefToBase() noexcept;
91 
93 
97 
98  ProductID id() const;
99  size_t key() const;
100 
101  template <class REF>
103  typename REF::value_type;
104  requires std::is_same_v<T, typename REF::value_type> or std::is_base_of_v<T, typename REF::value_type> or
105  std::is_base_of_v<typename REF::value_type, T>;
106  }
107  REF castTo() const;
108 
109  bool isNull() const;
110  bool isNonnull() const;
111  bool operator!() const;
112 
113  bool operator==(RefToBase const& rhs) const;
114  bool operator!=(RefToBase const& rhs) const;
115 
116  void swap(RefToBase& other);
117 
118  std::unique_ptr<reftobase::RefHolderBase> holder() const;
119 
120  EDProductGetter const* productGetter() const;
121 
126  bool isAvailable() const { return holder_ ? holder_->isAvailable() : false; }
127 
128  bool isTransient() const { return holder_ ? holder_->isTransient() : false; }
129 
130  //Needed for ROOT storage
132  private:
134  reftobase::BaseHolder<value_type>* holder_;
137  template <typename B>
139  };
140 
141  //--------------------------------------------------------------------
142  // Implementation of RefToBase<T>
143  //--------------------------------------------------------------------
144 
145  template <class T>
146  inline RefToBase<T>::RefToBase() : holder_(nullptr) {}
147 
148  template <class T>
150 
151  template <class T>
152  inline RefToBase<T>::RefToBase(RefToBase&& other) noexcept : holder_(other.holder_) {
153  other.holder_ = nullptr;
154  }
155 
156  template <class T>
158  delete holder_;
159  holder_ = other.holder_;
160  other.holder_ = nullptr;
161  return *this;
162  }
163 
164  template <class T>
165  template <typename C1, typename T1, typename F1>
167  : holder_(new reftobase::Holder<T, Ref<C1, T1, F1>>(iRef)) {}
168 
169  template <class T>
170  template <typename C>
171  inline RefToBase<T>::RefToBase(RefProd<C> const& iRef) : holder_(new reftobase::Holder<T, RefProd<C>>(iRef)) {}
172 
173  template <class T>
174  template <typename T1>
176  : holder_(
177  new reftobase::IndirectHolder<T>(std::shared_ptr<edm::reftobase::RefHolderBase>(iRef.holder().release()))) {
178  // OUT: holder_( new reftobase::Holder<T,RefToBase<T1> >(iRef ) ) {
179  // Forcing the conversion through IndirectHolder,
180  // as Holder<T,RefToBase<T1>> would need dictionaries we will never have.
181  // In this way we only need the IndirectHolder<T> and the RefHolder of the real type of the item
182  // This might cause a small performance penalty.
183  static_assert(std::is_base_of<T, T1>::value, "RefToBase::RefToBase T not base of T1");
184  }
185 
186  template <class T>
188 
189  template <class T>
190  inline RefToBase<T>::RefToBase(std::shared_ptr<reftobase::RefHolderBase> p)
191  : holder_(new reftobase::IndirectHolder<T>(p)) {}
192 
193  template <class T>
194  inline RefToBase<T>::~RefToBase() noexcept {
195  delete holder_;
196  }
197 
198  template <class T>
199  inline RefToBase<T>& RefToBase<T>::operator=(RefToBase<T> const& iRHS) {
200  RefToBase<T> temp(iRHS);
201  temp.swap(*this);
202  return *this;
203  }
204 
205  template <class T>
206  inline T const& RefToBase<T>::operator*() const {
207  return *getPtrImpl();
208  }
209 
210  template <class T>
211  inline T const* RefToBase<T>::operator->() const {
212  return getPtrImpl();
213  }
214 
215  template <class T>
216  inline T const* RefToBase<T>::get() const {
217  return getPtrImpl();
218  }
219 
220  template <class T>
221  inline ProductID RefToBase<T>::id() const {
222  return holder_ ? holder_->id() : ProductID();
223  }
224 
225  template <class T>
226  inline size_t RefToBase<T>::key() const {
227  if (holder_ == nullptr) {
228  Exception::throwThis(errors::InvalidReference,
229  "attempting get key from null RefToBase;\n"
230  "You should check for nullity before calling key().");
231  return 0;
232  }
233  return holder_->key();
234  }
235 
236  template <class T>
237  template <class REF>
239  typename REF::value_type;
240  requires std::is_same_v<T, typename REF::value_type> or std::is_base_of_v<T, typename REF::value_type> or
241  std::is_base_of_v<typename REF::value_type, T>;
242  }
243  REF RefToBase<T>::castTo() const {
244  if (!holder_) {
245  Exception::throwThis(errors::InvalidReference,
246  "attempting to cast a null RefToBase;\n"
247  "You should check for nullity before casting.");
248  }
249 
250  // If REF is type edm::Ref<C,T,F>, then it is impossible to
251  // check the container type C here. We just have to assume
252  // that the caller provided the correct type.
253 
254  EDProductGetter const* getter = productGetter();
255  if (getter) {
256  return REF(id(), key(), getter);
257  }
258 
259  T const* value = get();
260  if (value == nullptr) {
261  return REF(id());
262  }
263  typename REF::value_type const* newValue;
264  if constexpr (std::is_same_v<T, typename REF::value_type> or std::is_base_of_v<typename REF::value_type, T>) {
265  newValue = value;
266  } else {
267  newValue = dynamic_cast<typename REF::value_type const*>(value);
268  }
269  if (newValue) {
270  return REF(id(), newValue, key(), isTransient());
271  }
272 
273  Exception::throwThis(errors::InvalidReference,
274  "RefToBase<T>::castTo Error attempting to cast mismatched types\n"
275  "casting from RefToBase with T: ",
276  typeid(T).name(),
277  "\ncasting to: ",
278  typeid(REF).name());
279  return REF();
280  }
281 
283  template <class T>
284  inline bool RefToBase<T>::isNull() const {
285  return !id().isValid();
286  }
287 
289  template <class T>
290  inline bool RefToBase<T>::isNonnull() const {
291  return !isNull();
292  }
293 
295  template <class T>
296  inline bool RefToBase<T>::operator!() const {
297  return isNull();
298  }
299 
300  template <class T>
301  inline bool RefToBase<T>::operator==(RefToBase<T> const& rhs) const {
302  return holder_ ? holder_->isEqualTo(*rhs.holder_) : holder_ == rhs.holder_;
303  }
304 
305  template <class T>
306  inline bool RefToBase<T>::operator!=(RefToBase<T> const& rhs) const {
307  return !(*this == rhs);
308  }
309 
310  template <class T>
312  std::swap(holder_, other.holder_);
313  }
314 
315  template <class T>
317  return holder_ ? holder_->productGetter() : nullptr;
318  }
319 
320  template <class T>
321  inline T const* RefToBase<T>::getPtrImpl() const {
322  return holder_ ? holder_->getPtr() : nullptr;
323  }
324 
325  template <class T>
326  std::unique_ptr<reftobase::RefHolderBase> RefToBase<T>::holder() const {
327  return holder_ ? holder_->holder() : std::unique_ptr<reftobase::RefHolderBase>();
328  }
329 
330  // Free swap function
331  template <class T>
332  inline void swap(RefToBase<T>& a, RefToBase<T>& b) {
333  a.swap(b);
334  }
335 } // namespace edm
336 
340 
341 namespace edm {
342  template <class T>
343  inline RefToBase<T>::RefToBase(RefToBaseProd<T> const& r, size_t i)
344  : holder_(r.operator->()->refAt(i).holder_->clone()) {}
345 
346  template <typename T>
348  : holder_(handle.operator->()->refAt(i).holder_->clone()) {}
349 
350 } // namespace edm
351 
352 #endif
RefToBase & operator=(RefToBase &&other) noexcept
Definition: RefToBase.h:157
bool isAvailable() const
Definition: RefToBase.h:126
Basic3DVector & operator=(const Basic3DVector &)=default
Assignment operator.
std::unique_ptr< reftobase::RefHolderBase > holder() const
Definition: RefToBase.h:326
Definition: APVGainStruct.h:7
requires requires
Definition: RefToBase.h:238
void swap(RefToBase< T > &a, RefToBase< T > &b)
Definition: RefToBase.h:332
virtual bool isTransient() const =0
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:290
REF castTo() const
Definition: RefToBase.h:243
bool isTransient() const
Definition: RefToBase.h:128
virtual bool isAvailable() const =0
#define CMS_CLASS_VERSION(_version_)
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
void swap(RefToBase &other)
Definition: RefToBase.h:311
bool operator!=(RefToBase const &rhs) const
Definition: RefToBase.h:306
reftobase::BaseHolder< value_type > * holder_
Definition: RefToBase.h:134
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
EDProductGetter const * productGetter() const
Definition: RefToBase.h:316
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
value_type const * getPtrImpl() const
Definition: RefToBase.h:321
key
prepare the HTCondor submission files and eventually submit them
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
Definition: value.py:1
bool isNull() const
Checks for null.
Definition: RefToBase.h:284
requires requires
Definition: RefToBase.h:102
bool operator!=(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:81
size_t key() const
Definition: RefToBase.h:226
bool operator==(RefToBase const &rhs) const
Definition: RefToBase.h:301
edm::RefProd< Container > RefProd
T castTo(const char *arg)
Definition: Parameter.h:159
double b
Definition: hdecay.h:120
~RefToBase() noexcept
Definition: RefToBase.h:194
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
HLT enums.
double a
Definition: hdecay.h:121
bool operator!() const
Checks for null.
Definition: RefToBase.h:296
EDProductGetter const * productGetter(std::atomic< void const *> const &iCache)
MatrixMeschach operator*(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
#define get
long double T
value_type const * get() const
Definition: RefToBase.h:216