Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-05 03:13:49

0001 #ifndef DataFormats_Common_interface_HandleBase_h
0002 #define DataFormats_Common_interface_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 <algorithm>
0029 #include <cassert>
0030 #include <memory>
0031 
0032 #include "DataFormats/Provenance/interface/ProductID.h"
0033 #include "DataFormats/Provenance/interface/ProvenanceFwd.h"
0034 #include "DataFormats/Common/interface/HandleExceptionFactory.h"
0035 
0036 namespace cms {
0037   class Exception;
0038 }
0039 
0040 namespace edm {
0041   class HandleBase {
0042   public:
0043     HandleBase() : product_(nullptr), prov_(nullptr) {}
0044 
0045     HandleBase(void const* prod, Provenance const* prov) : product_(prod), prov_(prov) {
0046       assert(prod);
0047       assert(prov);
0048     }
0049 
0050     ///Used when the attempt to get the data failed
0051     HandleBase(std::shared_ptr<HandleExceptionFactory const>&& iWhyFailed)
0052         : product_(nullptr), prov_(nullptr), whyFailedFactory_(iWhyFailed) {}
0053 
0054     void clear() {
0055       product_ = nullptr;
0056       prov_ = nullptr;
0057       whyFailedFactory_.reset();
0058     }
0059 
0060     void swap(HandleBase& other) {
0061       std::swap(product_, other.product_);
0062       std::swap(prov_, other.prov_);
0063       std::swap(whyFailedFactory_, other.whyFailedFactory_);
0064     }
0065 
0066     bool isValid() const { return product_ && prov_; }
0067 
0068     bool failedToGet() const { return bool(whyFailedFactory_); }
0069 
0070     Provenance const* provenance() const { return prov_; }
0071 
0072     ProductID id() const;
0073 
0074     std::shared_ptr<cms::Exception> whyFailed() const {
0075       if (whyFailedFactory_.get()) {
0076         return whyFailedFactory_->make();
0077       }
0078       return std::shared_ptr<cms::Exception>();
0079     }
0080 
0081     std::shared_ptr<HandleExceptionFactory const> const& whyFailedFactory() const { return whyFailedFactory_; }
0082 
0083     explicit operator bool() const { return isValid(); }
0084 
0085     bool operator!() const { return not isValid(); }
0086 
0087   protected:
0088     void const* productStorage() const;
0089 
0090   private:
0091     void const* product_;
0092     Provenance const* prov_;
0093     std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_;
0094   };
0095 
0096   // Free swap function
0097   inline void swap(HandleBase& a, HandleBase& b) { a.swap(b); }
0098 
0099 }  // namespace edm
0100 
0101 #endif  // DataFormats_Common_interface_HandleBase_h