CMS 3D CMS Logo

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: Non-owning "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 $Id: BasicHandle.h,v 1.7 2007/10/05 21:55:05 chrjones Exp $
00027 
00028 ----------------------------------------------------------------------*/
00029 
00030 #include "DataFormats/Provenance/interface/Provenance.h"
00031 #include "DataFormats/Provenance/interface/ProductID.h"
00032 #include "FWCore/Utilities/interface/Exception.h"
00033 #include <boost/shared_ptr.hpp>
00034 
00035 namespace edm {
00036   class EDProduct;
00037   class BasicHandle {
00038   public:
00039     BasicHandle() :
00040       wrap_(0),
00041       prov_(0) {}
00042 
00043     BasicHandle(BasicHandle const& h) :
00044       wrap_(h.wrap_),
00045       prov_(h.prov_),
00046       whyFailed_(h.whyFailed_){}
00047 
00048     BasicHandle(EDProduct const* prod, Provenance const* prov) :
00049       wrap_(prod), prov_(prov) {
00050     }
00051 
00053     BasicHandle(const boost::shared_ptr<cms::Exception>& iWhyFailed):
00054     wrap_(0),
00055     prov_(0),
00056     whyFailed_(iWhyFailed) {}
00057     
00058     ~BasicHandle() {}
00059 
00060     void swap(BasicHandle& other) {
00061       using std::swap;
00062       std::swap(wrap_, other.wrap_);
00063       std::swap(prov_, other.prov_);
00064       swap(whyFailed_,other.whyFailed_);
00065     }
00066 
00067     
00068     BasicHandle& operator=(BasicHandle const& rhs) {
00069       BasicHandle temp(rhs);
00070       this->swap(temp);
00071       return *this;
00072     }
00073 
00074     bool isValid() const {
00075       return wrap_ && prov_;
00076     }
00077 
00078     bool failedToGet() const {
00079       return 0 != whyFailed_.get();
00080     }
00081     
00082     EDProduct const* wrapper() const {
00083       return wrap_;
00084     }
00085 
00086     Provenance const* provenance() const {
00087       return prov_;
00088     }
00089 
00090     ProductID id() const {
00091       if (!prov_) {
00092         return ProductID();
00093       }
00094       return prov_->productID();
00095     }
00096 
00097     boost::shared_ptr<cms::Exception> whyFailed() const {
00098       return whyFailed_;
00099     }
00100   private:
00101     EDProduct const* wrap_;
00102     Provenance const* prov_;
00103     boost::shared_ptr<cms::Exception> whyFailed_;
00104   };
00105 
00106   // Free swap function
00107   inline
00108   void
00109   swap(BasicHandle& a, BasicHandle& b) {
00110     a.swap(b);
00111   }
00112 }
00113 
00114 #endif

Generated on Tue Jun 9 17:28:35 2009 for CMSSW by  doxygen 1.5.4