Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef DataFormats_Common_BasicHandle_h
0002 #define DataFormats_Common_BasicHandle_h
0003 
0004 /*----------------------------------------------------------------------
0005 
0006 Handle: Shared "smart pointer" for reference to EDProducts 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 EDProduct 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 "DataFormats/Provenance/interface/ProductID.h"
0029 #include "DataFormats/Provenance/interface/Provenance.h"
0030 #include "DataFormats/Common/interface/HandleExceptionFactory.h"
0031 
0032 #include <memory>
0033 
0034 namespace cms {
0035   class Exception;
0036 }
0037 
0038 namespace edm {
0039   class WrapperBase;
0040   template <typename T>
0041   class Wrapper;
0042 
0043   class BasicHandle {
0044   public:
0045     BasicHandle() = delete;
0046 
0047     BasicHandle(BasicHandle const& h) = default;
0048 
0049     BasicHandle(BasicHandle&& h) = default;
0050 
0051     BasicHandle(WrapperBase const* iProd, Provenance const* iProv) noexcept(true) : product_(iProd), prov_(iProv) {}
0052 
0053     ///Used when the attempt to get the data failed
0054     BasicHandle(std::shared_ptr<HandleExceptionFactory const> const& iWhyFailed) noexcept(true)
0055         : product_(), prov_(nullptr), whyFailedFactory_(iWhyFailed) {}
0056 
0057     ~BasicHandle() = default;
0058 
0059     void swap(BasicHandle& other) noexcept(true) {
0060       using std::swap;
0061       swap(product_, other.product_);
0062       std::swap(prov_, other.prov_);
0063       swap(whyFailedFactory_, other.whyFailedFactory_);
0064     }
0065 
0066     BasicHandle& operator=(BasicHandle&& rhs) = default;
0067     BasicHandle& operator=(BasicHandle const& rhs) = default;
0068 
0069     bool isValid() const noexcept(true) { return product_ && prov_; }
0070 
0071     bool failedToGet() const noexcept(true) { return bool(whyFailedFactory_); }
0072 
0073     WrapperBase const* wrapper() const noexcept(true) { return product_; }
0074 
0075     Provenance const* provenance() const noexcept(true) { return prov_; }
0076 
0077     ProductID id() const noexcept(true) { return prov_->productID(); }
0078 
0079     std::shared_ptr<cms::Exception> whyFailed() const { return whyFailedFactory_->make(); }
0080 
0081     std::shared_ptr<HandleExceptionFactory const> const& whyFailedFactory() const noexcept(true) {
0082       return whyFailedFactory_;
0083     }
0084 
0085     std::shared_ptr<HandleExceptionFactory const>& whyFailedFactory() noexcept(true) { return whyFailedFactory_; }
0086 
0087     void clear() noexcept(true) {
0088       product_ = nullptr;
0089       prov_ = nullptr;
0090       whyFailedFactory_.reset();
0091     }
0092 
0093     static BasicHandle makeInvalid() { return BasicHandle(true); }
0094 
0095   private:
0096     //This is used to create a special invalid BasicHandle
0097     explicit BasicHandle(bool) : product_(nullptr) {}
0098     WrapperBase const* product_;
0099     Provenance const* prov_;
0100     std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_;
0101   };
0102 
0103   // Free swap function
0104   inline void swap(BasicHandle& a, BasicHandle& b) noexcept(true) { a.swap(b); }
0105 }  // namespace edm
0106 
0107 #endif