CMS 3D CMS Logo

HandleBase.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_HandleBase_h
2 #define DataFormats_Common_HandleBase_h
3 
4 /*----------------------------------------------------------------------
5 
6 Handle: Non-owning "smart pointer" for reference to products and
7 their provenances.
8 
9 This is a very preliminary version, and lacks safety features and
10 elegance.
11 
12 If the pointed-to product or provenance is destroyed, use of the
13 Handle becomes undefined. There is no way to query the Handle to
14 discover if this has happened.
15 
16 Handles can have:
17  -- Product and Provenance pointers both null;
18  -- Both pointers valid
19 
20 To check validity, one can use the isValid() function.
21 
22 If failedToGet() returns true then the requested data is not available
23 If failedToGet() returns false but isValid() is also false then no attempt
24  to get data has occurred
25 
26 ----------------------------------------------------------------------*/
27 
28 #include <cassert>
32 #include <algorithm>
33 
34 #include <memory>
35 
36 namespace cms {
37  class Exception;
38 }
39 namespace edm {
40  class HandleBase {
41  public:
42  HandleBase() : product_(nullptr), prov_(nullptr) {}
43 
44  HandleBase(void const* prod, Provenance const* prov) : product_(prod), prov_(prov) {
45  assert(prod);
46  assert(prov);
47  }
48 
50 
51  void clear() {
52  product_ = nullptr;
53  prov_ = nullptr;
54  whyFailedFactory_.reset();
55  }
56 
58  using std::swap;
59  swap(product_, other.product_);
60  std::swap(prov_, other.prov_);
61  swap(whyFailedFactory_, other.whyFailedFactory_);
62  }
63 
65  HandleBase temp(rhs);
66  this->swap(temp);
67  return *this;
68  }
69 
70  bool isValid() const { return product_ && prov_; }
71 
72  bool failedToGet() const { return bool(whyFailedFactory_); }
73 
74  Provenance const* provenance() const { return prov_; }
75 
76  ProductID id() const;
77 
78  HandleBase(HandleBase const&) = default;
79 
81  HandleBase(std::shared_ptr<HandleExceptionFactory const>&& iWhyFailed)
82  : product_(), prov_(nullptr), whyFailedFactory_(iWhyFailed) {}
83 
85  product_ = rhs.product_;
86  prov_ = rhs.prov_;
87  whyFailedFactory_ = std::move(rhs.whyFailedFactory_);
88  return *this;
89  }
90 
91  std::shared_ptr<cms::Exception> whyFailed() const {
92  if (whyFailedFactory_.get()) {
93  return whyFailedFactory_->make();
94  }
95  return std::shared_ptr<cms::Exception>();
96  }
97 
98  std::shared_ptr<HandleExceptionFactory const> const& whyFailedFactory() const { return whyFailedFactory_; }
99 
100  explicit operator bool() const { return isValid(); }
101 
102  bool operator!() const { return not isValid(); }
103 
104  protected:
105  void const* productStorage() const;
106 
107  private:
108  void const* product_;
110  std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_;
111  };
112 
113  // Free swap function
114  inline void swap(HandleBase& a, HandleBase& b) { a.swap(b); }
115 } // namespace edm
116 
117 #endif
ProductID id() const
Definition: HandleBase.cc:29
assert(be >=bs)
Provenance const * provenance() const
Definition: HandleBase.h:74
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
std::shared_ptr< HandleExceptionFactory const > const & whyFailedFactory() const
Definition: HandleBase.h:98
bool failedToGet() const
Definition: HandleBase.h:72
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::shared_ptr< cms::Exception > whyFailed() const
Definition: HandleBase.h:91
bool operator!() const
Definition: HandleBase.h:102
void swap(HandleBase &other)
Definition: HandleBase.h:57
Namespace of DDCMS conversion namespace.
std::shared_ptr< HandleExceptionFactory const > whyFailedFactory_
Definition: HandleBase.h:110
double b
Definition: hdecay.h:120
HandleBase & operator=(HandleBase &&rhs)
Definition: HandleBase.h:84
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
double a
Definition: hdecay.h:121
Provenance const * prov_
Definition: HandleBase.h:109
void const * product_
Definition: HandleBase.h:108
HandleBase(std::shared_ptr< HandleExceptionFactory const > &&iWhyFailed)
Used when the attempt to get the data failed.
Definition: HandleBase.h:81
HandleBase(void const *prod, Provenance const *prov)
Definition: HandleBase.h:44
def move(src, dest)
Definition: eostools.py:511
void const * productStorage() const
Definition: HandleBase.cc:18
HandleBase & operator=(HandleBase const &rhs)
Definition: HandleBase.h:64