CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DataFormats/Common/interface/BasicHandle.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_BasicHandle_h
00002 #define DataFormats_Common_BasicHandle_h
00003 
00004 /*----------------------------------------------------------------------
00005   
00006 Handle: Shared "smart pointer" for reference to EDProducts 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 EDProduct 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/Provenance.h"
00029 #include "DataFormats/Provenance/interface/ProductID.h"
00030 
00031 #include "boost/shared_ptr.hpp"
00032 
00033 namespace cms {
00034   class Exception;
00035 }
00036 
00037 namespace edm {
00038   class EDProduct;
00039   template <typename T> class Wrapper;
00040 
00041   class BasicHandle {
00042   public:
00043     BasicHandle() :
00044       product_(),
00045       prov_(0) {}
00046 
00047     BasicHandle(BasicHandle const& h) :
00048       product_(h.product_),
00049       prov_(h.prov_),
00050       whyFailed_(h.whyFailed_){}
00051 
00052     BasicHandle(boost::shared_ptr<EDProduct const> prod, Provenance const* prov) :
00053       product_(prod), prov_(prov) {
00054 
00055     }
00056 
00058     BasicHandle(boost::shared_ptr<cms::Exception> const& iWhyFailed):
00059     product_(),
00060     prov_(0),
00061     whyFailed_(iWhyFailed) {}
00062     
00063     ~BasicHandle() {}
00064 
00065     void swap(BasicHandle& other) {
00066       using std::swap;
00067       swap(product_, other.product_);
00068       std::swap(prov_, other.prov_);
00069       swap(whyFailed_,other.whyFailed_);
00070     }
00071 
00072     
00073     BasicHandle& operator=(BasicHandle const& rhs) {
00074       BasicHandle temp(rhs);
00075       this->swap(temp);
00076       return *this;
00077     }
00078 
00079     bool isValid() const {
00080       return product_ && prov_;
00081     }
00082 
00083     bool failedToGet() const {
00084       return 0 != whyFailed_.get();
00085     }
00086     
00087     EDProduct const* wrapper() const {
00088       return product_.get();
00089     }
00090 
00091     boost::shared_ptr<EDProduct const> product() const {
00092       return product_;
00093     }
00094 
00095     Provenance const* provenance() const {
00096       return prov_;
00097     }
00098 
00099     ProductID id() const {
00100       return prov_->productID();
00101     }
00102 
00103     boost::shared_ptr<cms::Exception> whyFailed() const {
00104       return whyFailed_;
00105     }
00106   private:
00107     boost::shared_ptr<EDProduct const> product_;
00108     Provenance const* prov_;
00109     boost::shared_ptr<cms::Exception> whyFailed_;
00110   };
00111 
00112   // Free swap function
00113   inline
00114   void
00115   swap(BasicHandle& a, BasicHandle& b) {
00116     a.swap(b);
00117   }
00118 }
00119 
00120 #endif