00001 #ifndef DataFormats_Common_HandleBase_h 00002 #define DataFormats_Common_HandleBase_h 00003 00004 /*---------------------------------------------------------------------- 00005 00006 Handle: Non-owning "smart pointer" for reference to products and 00007 their provenances. 00008 00009 This is a very preliminary version, and lacks safety features and 00010 elegance. 00011 00012 If the pointed-to product or provenance is destroyed, use of the 00013 Handle becomes undefined. There is no way to query the Handle to 00014 discover if this has happened. 00015 00016 Handles can have: 00017 -- Product and Provenance pointers both null; 00018 -- Both pointers valid 00019 00020 To check validity, one can use the isValid() function. 00021 00022 If failedToGet() returns true then the requested data is not available 00023 If failedToGet() returns false but isValid() is also false then no attempt 00024 to get data has occurred 00025 00026 ----------------------------------------------------------------------*/ 00027 00028 #include "DataFormats/Provenance/interface/ProductID.h" 00029 #include "DataFormats/Provenance/interface/ProvenanceFwd.h" 00030 00031 #include "boost/shared_ptr.hpp" 00032 00033 namespace cms { 00034 class Exception; 00035 } 00036 namespace edm { 00037 class HandleBase { 00038 public: 00039 HandleBase() : 00040 product_(0), 00041 prov_(0) {} 00042 00043 HandleBase(void const* prod, Provenance const* prov) : 00044 product_(prod), prov_(prov) { 00045 assert(prod); 00046 assert(prov); 00047 } 00048 00050 HandleBase(boost::shared_ptr<cms::Exception> const& iWhyFailed) : 00051 product_(), 00052 prov_(0), 00053 whyFailed_(iWhyFailed) {} 00054 00055 ~HandleBase() {} 00056 00057 void clear() { 00058 product_ = 0; 00059 prov_ = 0; 00060 whyFailed_.reset(); 00061 } 00062 00063 void swap(HandleBase& other) { 00064 using std::swap; 00065 swap(product_, other.product_); 00066 std::swap(prov_, other.prov_); 00067 swap(whyFailed_, other.whyFailed_); 00068 } 00069 00070 HandleBase& operator=(HandleBase const& rhs) { 00071 HandleBase temp(rhs); 00072 this->swap(temp); 00073 return *this; 00074 } 00075 00076 bool isValid() const { 00077 return product_ && prov_; 00078 } 00079 00080 bool failedToGet() const { 00081 return 0 != whyFailed_.get(); 00082 } 00083 00084 Provenance const* provenance() const { 00085 return prov_; 00086 } 00087 00088 ProductID id() const; 00089 00090 boost::shared_ptr<cms::Exception> whyFailed() const { 00091 return whyFailed_; 00092 } 00093 00094 protected: 00095 00096 void const* productStorage() const; 00097 00098 private: 00099 void const* product_; 00100 Provenance const* prov_; 00101 boost::shared_ptr<cms::Exception> whyFailed_; 00102 }; 00103 00104 // Free swap function 00105 inline 00106 void 00107 swap(HandleBase& a, HandleBase& b) { 00108 a.swap(b); 00109 } 00110 } 00111 00112 #endif