Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:51

0001 #ifndef DataFormats_Common_HandleBase_h
0002 #define DataFormats_Common_HandleBase_h
0003 
0004 /*----------------------------------------------------------------------
0005   
0006 Handle: Non-owning "smart pointer" for reference to products and
0007 their provenances.
0008 
0009 This is a very preliminary version, and lacks safety features and
0010 elegance.
0011 
0012 If the pointed-to product or provenance is destroyed, use of the
0013 Handle becomes undefined. There is no way to query the Handle to
0014 discover if this has happened.
0015 
0016 Handles can have:
0017   -- Product and Provenance pointers both null;
0018   -- Both pointers valid
0019 
0020 To check validity, one can use the isValid() function.
0021 
0022 If failedToGet() returns true then the requested data is not available
0023 If failedToGet() returns false but isValid() is also false then no attempt 
0024   to get data has occurred
0025 
0026 ----------------------------------------------------------------------*/
0027 
0028 #include <cassert>
0029 #include "DataFormats/Provenance/interface/ProductID.h"
0030 #include "DataFormats/Provenance/interface/ProvenanceFwd.h"
0031 #include "DataFormats/Common/interface/HandleExceptionFactory.h"
0032 #include <algorithm>
0033 
0034 #include <memory>
0035 
0036 namespace cms {
0037   class Exception;
0038 }
0039 namespace edm {
0040   class HandleBase {
0041   public:
0042     HandleBase() : product_(nullptr), prov_(nullptr) {}
0043 
0044     HandleBase(void const* prod, Provenance const* prov) : product_(prod), prov_(prov) {
0045       assert(prod);
0046       assert(prov);
0047     }
0048 
0049     ~HandleBase() {}
0050 
0051     void clear() {
0052       product_ = nullptr;
0053       prov_ = nullptr;
0054       whyFailedFactory_.reset();
0055     }
0056 
0057     void swap(HandleBase& other) {
0058       using std::swap;
0059       swap(product_, other.product_);
0060       std::swap(prov_, other.prov_);
0061       swap(whyFailedFactory_, other.whyFailedFactory_);
0062     }
0063 
0064     HandleBase& operator=(HandleBase const& rhs) {
0065       HandleBase temp(rhs);
0066       this->swap(temp);
0067       return *this;
0068     }
0069 
0070     bool isValid() const { return product_ && prov_; }
0071 
0072     bool failedToGet() const { return bool(whyFailedFactory_); }
0073 
0074     Provenance const* provenance() const { return prov_; }
0075 
0076     ProductID id() const;
0077 
0078     HandleBase(HandleBase const&) = default;
0079 
0080     ///Used when the attempt to get the data failed
0081     HandleBase(std::shared_ptr<HandleExceptionFactory const>&& iWhyFailed)
0082         : product_(), prov_(nullptr), whyFailedFactory_(iWhyFailed) {}
0083 
0084     HandleBase& operator=(HandleBase&& rhs) {
0085       product_ = rhs.product_;
0086       prov_ = rhs.prov_;
0087       whyFailedFactory_ = std::move(rhs.whyFailedFactory_);
0088       return *this;
0089     }
0090 
0091     std::shared_ptr<cms::Exception> whyFailed() const {
0092       if (whyFailedFactory_.get()) {
0093         return whyFailedFactory_->make();
0094       }
0095       return std::shared_ptr<cms::Exception>();
0096     }
0097 
0098     std::shared_ptr<HandleExceptionFactory const> const& whyFailedFactory() const { return whyFailedFactory_; }
0099 
0100     explicit operator bool() const { return isValid(); }
0101 
0102     bool operator!() const { return not isValid(); }
0103 
0104   protected:
0105     void const* productStorage() const;
0106 
0107   private:
0108     void const* product_;
0109     Provenance const* prov_;
0110     std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_;
0111   };
0112 
0113   // Free swap function
0114   inline void swap(HandleBase& a, HandleBase& b) { a.swap(b); }
0115 }  // namespace edm
0116 
0117 #endif