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 
30 
31 #include "boost/shared_ptr.hpp"
32 
33 namespace cms {
34  class Exception;
35 }
36 namespace edm {
37  class HandleBase {
38  public:
40  product_(0),
41  prov_(0) {}
42 
43  HandleBase(void const* prod, Provenance const* prov) :
44  product_(prod), prov_(prov) {
45  assert(prod);
46  assert(prov);
47  }
48 
50  HandleBase(boost::shared_ptr<cms::Exception> const& iWhyFailed) :
51  product_(),
52  prov_(0),
53  whyFailed_(iWhyFailed) {}
54 
56 
57  void clear() {
58  product_ = 0;
59  prov_ = 0;
60  whyFailed_.reset();
61  }
62 
63  void swap(HandleBase& other) {
64  using std::swap;
65  swap(product_, other.product_);
66  std::swap(prov_, other.prov_);
67  swap(whyFailed_, other.whyFailed_);
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 0 != whyFailed_.get();
82  }
83 
84  Provenance const* provenance() const {
85  return prov_;
86  }
87 
88  ProductID id() const;
89 
90  boost::shared_ptr<cms::Exception> whyFailed() const {
91  return whyFailed_;
92  }
93 
94  protected:
95 
96  void const* productStorage() const;
97 
98  private:
99  void const* product_;
101  boost::shared_ptr<cms::Exception> whyFailed_;
102  };
103 
104  // Free swap function
105  inline
106  void
108  a.swap(b);
109  }
110 }
111 
112 #endif
ProductID id() const
Definition: HandleBase.cc:15
HandleBase(boost::shared_ptr< cms::Exception > const &iWhyFailed)
Used when the attempt to get the data failed.
Definition: HandleBase.h:50
void const * productStorage() const
Definition: HandleBase.cc:7
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
boost::shared_ptr< cms::Exception > whyFailed_
Definition: HandleBase.h:101
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
bool isValid() const
Definition: HandleBase.h:76
boost::shared_ptr< cms::Exception > whyFailed() const
Definition: HandleBase.h:90
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:100
void const * product_
Definition: HandleBase.h:99
HandleBase(void const *prod, Provenance const *prov)
Definition: HandleBase.h:43
Provenance const * provenance() const
Definition: HandleBase.h:84
HandleBase & operator=(HandleBase const &rhs)
Definition: HandleBase.h:70