Handle.h
Go to the documentation of this file.
1 #ifndef gallery_Handle_h
2 #define gallery_Handle_h
3 
4 // ==================================================================
5 // Similar to art::Handle, constructors are different and unneeded
6 // things have been removed but otherwise the interface is identical.
7 // ==================================================================
8 
11 
12 #include <memory>
13 
14 namespace gallery {
15 
16  template <typename T>
17  class Handle {
18  public:
19  using element_type = T;
20  class HandleTag {};
21 
22  Handle() = default;
23  explicit Handle(T const*, art::ProductID);
24  explicit Handle(std::shared_ptr<art::Exception const>);
25  Handle(Handle const&) = default;
26  Handle& operator=(Handle const&) = default;
27 
28  // pointer behaviors
29  T const& operator*() const noexcept(false);
30  T const* operator->() const noexcept(false); // alias for product()
31  T const* product() const noexcept(false);
32 
33  // inspectors
34  bool isValid() const noexcept;
35  art::ProductID id() const noexcept;
36  std::shared_ptr<art::Exception const> whyFailed() const noexcept;
37 
38  private:
39  [[noreturn]] void throwHandleWhyFailed_() const noexcept(false);
40 
41  T const* prod_{nullptr};
43  std::shared_ptr<art::Exception const> whyFailed_;
44  };
45 
46  template <class T>
47  Handle<T>::Handle(T const* prod, art::ProductID const productID)
48  : prod_{prod}, productID_{productID}
49  {}
50 
51  template <class T>
52  Handle<T>::Handle(std::shared_ptr<art::Exception const> iWhyFailed)
53  : whyFailed_{iWhyFailed}
54  {}
55 
56  template <class T>
57  inline T const& Handle<T>::operator*() const noexcept(false)
58  {
59  return *product();
60  }
61 
62  template <class T>
63  inline T const* Handle<T>::operator->() const noexcept(false)
64  {
65  return product();
66  }
67 
68  template <class T>
69  inline T const*
70  Handle<T>::product() const noexcept(false)
71  {
72  if (!prod_)
74  return prod_;
75  }
76 
77  template <class T>
78  bool
80  {
81  return prod_ != nullptr;
82  }
83 
84  template <class T>
86  Handle<T>::id() const noexcept
87  {
88  return productID_;
89  }
90 
91  template <class T>
92  inline std::shared_ptr<art::Exception const>
94  {
95  return whyFailed_;
96  }
97 
98  template <class T>
99  void
101  {
102  if (whyFailed_) {
103  throw *whyFailed_;
104  }
106  << "Attempt to dereference invalid Handle with no stored exception\n"
107  << "Maybe you forgot to call getByLabel before dereferencing\n";
108  }
109 } // namespace gallery
110 #endif /* gallery_Handle_h */
111 
112 // Local Variables:
113 // mode: c++
114 // End:
std::shared_ptr< art::Exception const > whyFailed_
Definition: Handle.h:43
T const * product() const noexcept(false)
Definition: Handle.h:70
art::ProductID id() const noexcept
Definition: Handle.h:86
std::shared_ptr< art::Exception const > whyFailed() const noexcept
Definition: Handle.h:93
bool isValid() const noexcept
Definition: Handle.h:79
T const * prod_
Definition: Handle.h:41
art::ProductID productID_
Definition: Handle.h:42
void throwHandleWhyFailed_() const noexcept(false)
Definition: Handle.h:100
T const & operator*() const noexcept(false)
Definition: Handle.h:57
T const * operator->() const noexcept(false)
Definition: Handle.h:63
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
static constexpr ProductID invalid() noexcept
Definition: ProductID.h:26
Handle()=default
Handle & operator=(Handle const &)=default