CMS 3D CMS Logo

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_(std::move(id)) {
27  }
28 
29  //NOTE: C++ disallows references to null
30  ValidHandle(T const& prod, ProductID id) noexcept(true) : product_(&prod), id_(std::move(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 id_
Definition: ValidHandle.h:44
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
T const & operator*() const noexcept(true)
Definition: ValidHandle.h:40
T const * operator->() const noexcept(true)
Definition: ValidHandle.h:39
void throwIfNotValid(const void *) noexcept(false)
Definition: ValidHandle.cc:5
#define noexcept
ProductID const & id() const noexcept(true)
Definition: ValidHandle.h:35
HLT enums.
auto makeValid(const U &iOtherHandleType) noexcept(false)
Definition: ValidHandle.h:52
long double T
def move(src, dest)
Definition: eostools.py:511