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:
43  product_(0),
44  prov_(0) {}
45 
46  HandleBase(void const* prod, Provenance const* prov) :
47  product_(prod), prov_(prov) {
48  assert(prod);
49  assert(prov);
50  }
51 
53 
54  void clear() {
55  product_ = 0;
56  prov_ = 0;
57  whyFailedFactory_.reset();
58  }
59 
60 
62  using std::swap;
63  swap(product_, other.product_);
64  std::swap(prov_, other.prov_);
65  swap(whyFailedFactory_, other.whyFailedFactory_);
66  }
67 
69  HandleBase temp(rhs);
70  this->swap(temp);
71  return *this;
72  }
73 
74  bool isValid() const {
75  return product_ && prov_;
76  }
77 
78  bool failedToGet() const {
79  return bool(whyFailedFactory_);
80  }
81 
82 
83  Provenance const* provenance() const {
84  return prov_;
85  }
86 
87  ProductID id() const;
88 
89  HandleBase(HandleBase const&) = default;
90 
91 
93  HandleBase(std::shared_ptr<HandleExceptionFactory>&& iWhyFailed) :
94  product_(),
95  prov_(0),
96  whyFailedFactory_(iWhyFailed) {}
97 
98 
100  product_ = rhs.product_;
101  prov_ = rhs.prov_;
102  whyFailedFactory_ = std::move(rhs.whyFailedFactory_);
103  return *this;
104  }
105 
106  std::shared_ptr<cms::Exception> whyFailed() const {
107  if(whyFailedFactory_.get()) {
108  return whyFailedFactory_->make();
109  }
110  return std::shared_ptr<cms::Exception>();
111  }
112 
113  std::shared_ptr<HandleExceptionFactory> const&
114  whyFailedFactory() const { return whyFailedFactory_;}
115 
116  protected:
117 
118  void const* productStorage() const;
119 
120  private:
121  void const* product_;
123  std::shared_ptr<HandleExceptionFactory> whyFailedFactory_;
124  };
125 
126  // Free swap function
127  inline
128  void
130  a.swap(b);
131  }
132 }
133 
134 #endif
std::shared_ptr< HandleExceptionFactory > const & whyFailedFactory() const
Definition: HandleBase.h:114
std::shared_ptr< HandleExceptionFactory > whyFailedFactory_
Definition: HandleBase.h:123
void swap(HandleBase &a, HandleBase &b)
Definition: HandleBase.h:129
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
bool isValid() const
Definition: HandleBase.h:74
HandleBase(std::shared_ptr< HandleExceptionFactory > &&iWhyFailed)
Used when the attempt to get the data failed.
Definition: HandleBase.h:93
void swap(HandleBase &other)
Definition: HandleBase.h:61
bool failedToGet() const
Definition: HandleBase.h:78
double b
Definition: hdecay.h:120
HandleBase & operator=(HandleBase &&rhs)
Definition: HandleBase.h:99
HLT enums.
double a
Definition: hdecay.h:121
Provenance const * prov_
Definition: HandleBase.h:122
void const * product_
Definition: HandleBase.h:121
std::shared_ptr< cms::Exception > whyFailed() const
Definition: HandleBase.h:106
HandleBase(void const *prod, Provenance const *prov)
Definition: HandleBase.h:46
def move(src, dest)
Definition: eostools.py:510
Provenance const * provenance() const
Definition: HandleBase.h:83
HandleBase & operator=(HandleBase const &rhs)
Definition: HandleBase.h:68