CMS 3D CMS Logo

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/BranchDescription.h"
00029 #include "DataFormats/Provenance/interface/ProductID.h"
00030 #include "FWCore/Utilities/interface/Exception.h"
00031 #include <boost/shared_ptr.hpp>
00032 
00033 namespace edm {
00034   class EDProduct;
00035   template <typename T>
00036   class OutputHandle {
00037   public:
00038     OutputHandle() :
00039       wrap_(0),
00040       desc_(0),
00041       entryInfo_() {}
00042 
00043     OutputHandle(OutputHandle const& h) :
00044       wrap_(h.wrap_),
00045       desc_(h.desc_),
00046       entryInfo_(h.entryInfo_),
00047       whyFailed_(h.whyFailed_){}
00048 
00049     OutputHandle(EDProduct const* prod, ConstBranchDescription const* desc, boost::shared_ptr<EventEntryInfo> entryInfo) :
00050       wrap_(prod),
00051       desc_(desc),
00052       entryInfo_(boost::shared_ptr<T>(new T(*entryInfo))) {}
00053 
00055     OutputHandle(const boost::shared_ptr<cms::Exception>& iWhyFailed):
00056       wrap_(0),
00057       desc_(0),
00058       entryInfo_(),
00059       whyFailed_(iWhyFailed) {}
00060     
00061     ~OutputHandle() {}
00062 
00063     void swap(OutputHandle& other) {
00064       using std::swap;
00065       std::swap(wrap_, other.wrap_);
00066       std::swap(desc_, other.desc_);
00067       std::swap(entryInfo_, other.entryInfo_);
00068       swap(whyFailed_,other.whyFailed_);
00069     }
00070 
00071     
00072     OutputHandle& operator=(OutputHandle const& rhs) {
00073       OutputHandle temp(rhs);
00074       this->swap(temp);
00075       return *this;
00076     }
00077 
00078     bool isValid() const {
00079       return wrap_ && desc_ &&entryInfo_;
00080     }
00081 
00082     bool failedToGet() const {
00083       return 0 != whyFailed_.get();
00084     }
00085     
00086     EDProduct const* wrapper() const {
00087       return wrap_;
00088     }
00089 
00090     ProductID id() const {
00091       if (!entryInfo_) {
00092         return ProductID();
00093       }
00094       return entryInfo_->productID();
00095     }
00096 
00097     boost::shared_ptr<cms::Exception> whyFailed() const {
00098       return whyFailed_;
00099     }
00100 
00101     T const* entryInfo() const {
00102       return entryInfo_.get();
00103     }
00104 
00105     boost::shared_ptr<T> entryInfoSharedPtr() const {
00106       return entryInfo_;
00107     }
00108 
00109     ConstBranchDescription const* desc() const {
00110       return desc_;
00111     }
00112 
00113   private:
00114     EDProduct const* wrap_;
00115     ConstBranchDescription const* desc_;
00116     boost::shared_ptr<T> entryInfo_;
00117     boost::shared_ptr<cms::Exception> whyFailed_;
00118   };
00119 
00120   // Free swap function
00121   template <typename T>
00122   inline
00123   void
00124   swap(OutputHandle<T>& a, OutputHandle<T>& b) {
00125     a.swap(b);
00126   }
00127 }
00128 
00129 #endif

Generated on Tue Jun 9 17:29:30 2009 for CMSSW by  doxygen 1.5.4