CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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>
33 
34 
35 #include <memory>
36 
37 namespace cms {
38  class Exception;
39 }
40 namespace edm {
41  class HandleBase {
42  public:
44  product_(0),
45  prov_(0) {}
46 
47  HandleBase(void const* prod, Provenance const* prov) :
48  product_(prod), prov_(prov) {
49  assert(prod);
50  assert(prov);
51  }
52 
54 
55  void clear() {
56  product_ = 0;
57  prov_ = 0;
58  whyFailedFactory_.reset();
59  }
60 
61 
62  void swap(HandleBase& other) {
63  using std::swap;
64  swap(product_, other.product_);
65  std::swap(prov_, other.prov_);
67  }
68 
70  HandleBase temp(rhs);
71  this->swap(temp);
72  return *this;
73  }
74 
75  bool isValid() const {
76  return product_ && prov_;
77  }
78 
79  bool failedToGet() const {
80  return bool(whyFailedFactory_);
81  }
82 
83 
84  Provenance const* provenance() const {
85  return prov_;
86  }
87 
88  ProductID id() const;
89 
90 
91 #if defined( __GXX_EXPERIMENTAL_CXX0X__)
92  HandleBase(HandleBase const&) = default;
93 
94 
96  HandleBase(std::shared_ptr<HandleExceptionFactory>&& iWhyFailed) :
97  product_(),
98  prov_(0),
99  whyFailedFactory_(iWhyFailed) {}
100 
101 
103  product_ = rhs.product_;
104  prov_ = rhs.prov_;
105  whyFailedFactory_ = std::move(rhs.whyFailedFactory_);
106  return *this;
107  }
108 #endif
109 
110  std::shared_ptr<cms::Exception> whyFailed() const {
111  if(whyFailedFactory_.get()) {
112  return whyFailedFactory_->make();
113  }
114  return std::shared_ptr<cms::Exception>();
115  }
116 
117  std::shared_ptr<HandleExceptionFactory> const&
119 
120  protected:
121 
122  void const* productStorage() const;
123 
124  private:
125  void const* product_;
127  std::shared_ptr<HandleExceptionFactory> whyFailedFactory_;
128  };
129 
130  // Free swap function
131  inline
132  void
134  a.swap(b);
135  }
136 }
137 
138 #endif
std::shared_ptr< HandleExceptionFactory > const & whyFailedFactory() const
Definition: HandleBase.h:118
ProductID id() const
Definition: HandleBase.cc:15
assert(m_qm.get())
std::shared_ptr< HandleExceptionFactory > whyFailedFactory_
Definition: HandleBase.h:127
void const * productStorage() const
Definition: HandleBase.cc:7
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
def move
Definition: eostools.py:510
bool isValid() const
Definition: HandleBase.h:75
void swap(HandleBase &other)
Definition: HandleBase.h:62
bool failedToGet() const
Definition: HandleBase.h:79
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
Provenance const * prov_
Definition: HandleBase.h:126
void const * product_
Definition: HandleBase.h:125
std::shared_ptr< cms::Exception > whyFailed() const
Definition: HandleBase.h:110
HandleBase(void const *prod, Provenance const *prov)
Definition: HandleBase.h:47
Provenance const * provenance() const
Definition: HandleBase.h:84
HandleBase & operator=(HandleBase const &rhs)
Definition: HandleBase.h:69