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