CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/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/Common/interface/ProductData.h"
00029 #include "DataFormats/Common/interface/WrapperHolder.h"
00030 #include "DataFormats/Provenance/interface/ProductID.h"
00031 #include "DataFormats/Provenance/interface/Provenance.h"
00032 #include "DataFormats/Provenance/interface/WrapperInterfaceBase.h"
00033 
00034 #include "boost/shared_ptr.hpp"
00035 
00036 namespace cms {
00037   class Exception;
00038 }
00039 
00040 namespace edm {
00041   template <typename T> class Wrapper;
00042 
00043   class BasicHandle {
00044   public:
00045     BasicHandle() :
00046       product_(),
00047       prov_(0) {}
00048 
00049     BasicHandle(BasicHandle const& h) :
00050       product_(h.product_),
00051       prov_(h.prov_),
00052       whyFailed_(h.whyFailed_){}
00053 
00054     BasicHandle(void const* iProd, WrapperInterfaceBase const* iInterface, Provenance const* iProv) :
00055       product_(WrapperHolder(iProd, iInterface)),
00056       prov_(iProv) {
00057     }
00058 
00059     BasicHandle(WrapperHolder const& iWrapperHolder, Provenance const* iProv) :
00060       product_(iWrapperHolder),
00061       prov_(iProv) {
00062     }
00063 
00064     BasicHandle(ProductData const& productData) :
00065       product_(WrapperHolder(productData.wrapper_.get(), productData.getInterface())),
00066       prov_(&productData.prov_) {
00067     }
00068 
00070     BasicHandle(boost::shared_ptr<cms::Exception> const& iWhyFailed):
00071     product_(),
00072     prov_(0),
00073     whyFailed_(iWhyFailed) {}
00074 
00075     ~BasicHandle() {}
00076 
00077     void swap(BasicHandle& other) {
00078       using std::swap;
00079       swap(product_, other.product_);
00080       std::swap(prov_, other.prov_);
00081       swap(whyFailed_,other.whyFailed_);
00082     }
00083 
00084     BasicHandle& operator=(BasicHandle const& rhs) {
00085       BasicHandle temp(rhs);
00086       this->swap(temp);
00087       return *this;
00088     }
00089 
00090     bool isValid() const {
00091       return product_.wrapper() != 0 && prov_ != 0;
00092     }
00093 
00094     bool failedToGet() const {
00095       return 0 != whyFailed_.get();
00096     }
00097 
00098     WrapperInterfaceBase const* interface() const {
00099       return product_.interface();
00100     }
00101 
00102     void const* wrapper() const {
00103       return product_.wrapper();
00104     }
00105 
00106     WrapperHolder wrapperHolder() const {
00107       return product_;
00108     }
00109 
00110     Provenance const* provenance() const {
00111       return prov_;
00112     }
00113 
00114     ProductID id() const {
00115       return prov_->productID();
00116     }
00117 
00118     boost::shared_ptr<cms::Exception> whyFailed() const {
00119       return whyFailed_;
00120     }
00121   private:
00122     WrapperHolder product_;
00123     Provenance const* prov_;
00124     boost::shared_ptr<cms::Exception> whyFailed_;
00125   };
00126 
00127   // Free swap function
00128   inline
00129   void
00130   swap(BasicHandle& a, BasicHandle& b) {
00131     a.swap(b);
00132   }
00133 }
00134 
00135 #endif