CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/DataFormats/Common/interface/OutputHandle.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_OutputHandle_h
00002 #define DataFormats_Common_OutputHandle_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 object or provenance 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/ProductProvenance.h"
00029 
00030 #include "boost/shared_ptr.hpp"
00031 
00032 namespace cms {
00033   class Exception;
00034 }
00035 
00036 namespace edm {
00037   class ConstBranchDescription;
00038   class EDProduct;
00039   class OutputHandle {
00040   public:
00041     OutputHandle() :
00042       wrap_(0),
00043       desc_(0),
00044       productProvenance_() {}
00045 
00046     OutputHandle(OutputHandle const& h) :
00047       wrap_(h.wrap_),
00048       desc_(h.desc_),
00049       productProvenance_(h.productProvenance_),
00050       whyFailed_(h.whyFailed_){}
00051 
00052     OutputHandle(EDProduct const* prod, ConstBranchDescription const* desc, boost::shared_ptr<ProductProvenance> productProvenance) :
00053       wrap_(prod),
00054       desc_(desc),
00055       productProvenance_(productProvenance) {}
00056 
00058     OutputHandle(boost::shared_ptr<cms::Exception> const& iWhyFailed):
00059       wrap_(0),
00060       desc_(0),
00061       productProvenance_(),
00062       whyFailed_(iWhyFailed) {}
00063     
00064     ~OutputHandle() {}
00065 
00066     void swap(OutputHandle& other) {
00067       using std::swap;
00068       std::swap(wrap_, other.wrap_);
00069       std::swap(desc_, other.desc_);
00070       std::swap(productProvenance_, other.productProvenance_);
00071       swap(whyFailed_,other.whyFailed_);
00072     }
00073 
00074     
00075     OutputHandle& operator=(OutputHandle const& rhs) {
00076       OutputHandle temp(rhs);
00077       this->swap(temp);
00078       return *this;
00079     }
00080 
00081     bool isValid() const {
00082       return wrap_ && desc_ &&productProvenance_;
00083     }
00084 
00085     bool failedToGet() const {
00086       return 0 != whyFailed_.get();
00087     }
00088     
00089     EDProduct const* wrapper() const {
00090       return wrap_;
00091     }
00092 
00093     boost::shared_ptr<cms::Exception> whyFailed() const {
00094       return whyFailed_;
00095     }
00096 
00097     ProductProvenance const* productProvenance() const {
00098       return productProvenance_.get();
00099     }
00100 
00101     boost::shared_ptr<ProductProvenance> productProvenanceSharedPtr() const {
00102       return productProvenance_;
00103     }
00104 
00105     ConstBranchDescription const* desc() const {
00106       return desc_;
00107     }
00108 
00109   private:
00110     EDProduct const* wrap_;
00111     ConstBranchDescription const* desc_;
00112     boost::shared_ptr<ProductProvenance> productProvenance_;
00113     boost::shared_ptr<cms::Exception> whyFailed_;
00114   };
00115 
00116   // Free swap function
00117   inline
00118   void
00119   swap(OutputHandle& a, OutputHandle& b) {
00120     a.swap(b);
00121   }
00122 }
00123 
00124 #endif