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) :
26  product_(prod), id_(std::move(id)) {
28  }
29 
30  //NOTE: C++ disallows references to null
31  ValidHandle(T const& prod, ProductID id) noexcept(true):
32  product_(&prod), id_(std::move(id)) {
33  }
34  ValidHandle( const ValidHandle<T>& ) = default;
35  ValidHandle<T>& operator=(ValidHandle<T> const& rhs) = default;
36  ~ValidHandle() = default;
37 
38 
39  ProductID const& id() const noexcept(true) { return id_; }
40 
41  T const* product() const noexcept(true) {
42  return product_;
43  }
44 
45  T const* operator->() const noexcept(true) { return product();}
46  T const& operator*() const noexcept(true) { return *product();}
47 
48  private:
49  T const* product_;
51  };
52 
57  template<typename U>
58  auto
59  makeValid(const U& iOtherHandleType) noexcept(false) {
60  vhhelper::throwIfNotValid(iOtherHandleType.product());
61  //because of the check, we know this is valid and do not have to check again
62  return ValidHandle<typename U::element_type>(*iOtherHandleType.product(), iOtherHandleType.id());
63  }
64 }
65 
66 #endif
ValidHandle(T const &prod, ProductID id) noexcept(true)
Definition: ValidHandle.h:31
ProductID id_
Definition: ValidHandle.h:50
ValidHandle(T const *prod, ProductID id) noexcept(false)
Definition: ValidHandle.h:25
T const * product_
Definition: ValidHandle.h:49
T const * product() const noexcept(true)
Definition: ValidHandle.h:41
T const & operator*() const noexcept(true)
Definition: ValidHandle.h:46
T const * operator->() const noexcept(true)
Definition: ValidHandle.h:45
void throwIfNotValid(const void *) noexcept(false)
Definition: ValidHandle.cc:6
#define noexcept
ProductID const & id() const noexcept(true)
Definition: ValidHandle.h:39
HLT enums.
auto makeValid(const U &iOtherHandleType) noexcept(false)
Definition: ValidHandle.h:59
long double T
def move(src, dest)
Definition: eostools.py:511