CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ValidHandle.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_ValidHandle_h
2 #define DataFormats_Common_ValidHandle_h
3 
4 /*----------------------------------------------------------------------
5 
6 Handle: Non-owning "smart pointer" for reference to products and
7 their provenances.
8 
9 The data product is always guaranteed to be valid for this handle type.
10 ----------------------------------------------------------------------*/
11 
12 #include <utility>
14 
15 namespace edm {
16  namespace vhhelper {
17  void throwIfNotValid(const void*) noexcept(false);
18  }
19  template <typename T>
20  class ValidHandle {
21  public:
22  using element_type = T;
23 
24  ValidHandle() = delete;
25  ValidHandle(T const* prod, ProductID id) noexcept(false) : product_(prod), id_(id) {
27  }
28 
29  //NOTE: C++ disallows references to null
30  ValidHandle(T const& prod, ProductID id) noexcept(true) : product_(&prod), id_(id) {}
31  ValidHandle(const ValidHandle<T>&) = default;
32  ValidHandle<T>& operator=(ValidHandle<T> const& rhs) = default;
33  ~ValidHandle() = default;
34 
35  ProductID const& id() const noexcept(true) { return id_; }
36 
37  T const* product() const noexcept(true) { return product_; }
38 
39  T const* operator->() const noexcept(true) { return product(); }
40  T const& operator*() const noexcept(true) { return *product(); }
41 
42  private:
43  T const* product_;
45  };
46 
51  template <typename U>
52  auto makeValid(const U& iOtherHandleType) noexcept(false) {
53  vhhelper::throwIfNotValid(iOtherHandleType.product());
54  //because of the check, we know this is valid and do not have to check again
55  return ValidHandle<typename U::element_type>(*iOtherHandleType.product(), iOtherHandleType.id());
56  }
57 } // namespace edm
58 
59 #endif
ValidHandle(T const &prod, ProductID id) noexcept(true)
Definition: ValidHandle.h:30
ProductID const & id() const noexcept(true)
Definition: ValidHandle.h:35
ProductID id_
Definition: ValidHandle.h:44
~ValidHandle()=default
ValidHandle(T const *prod, ProductID id) noexcept(false)
Definition: ValidHandle.h:25
T const * product_
Definition: ValidHandle.h:43
T const * product() const noexcept(true)
Definition: ValidHandle.h:37
ValidHandle()=delete
T const & operator*() const noexcept(true)
Definition: ValidHandle.h:40
void throwIfNotValid(const void *) noexcept(false)
Definition: ValidHandle.cc:5
T const * operator->() const noexcept(true)
Definition: ValidHandle.h:39
auto makeValid(const U &iOtherHandleType) noexcept(false)
Definition: ValidHandle.h:52
long double T
ValidHandle< T > & operator=(ValidHandle< T > const &rhs)=default