Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:44

0001 #ifndef FWCore_Framework_ESHandle_h
0002 #define FWCore_Framework_ESHandle_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Framework
0006 // Class  :     ESHandle
0007 //
0008 /**\class ESHandle ESHandle.h FWCore/Framework/interface/ESHandle.h
0009 
0010  Description: <one line class summary>
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Author:      Chris Jones
0018 // Created:     Fri Apr  1 14:47:35 EST 2005
0019 //
0020 
0021 // system include files
0022 
0023 // user include files
0024 #include "FWCore/Framework/interface/ComponentDescription.h"
0025 #include "FWCore/Framework/interface/ESHandleExceptionFactory.h"
0026 
0027 #include <exception>
0028 #include <memory>
0029 #include <utility>
0030 
0031 namespace edm {
0032 
0033   class ESHandleBase {
0034   public:
0035     ESHandleBase() = default;
0036     ESHandleBase(void const* iData, edm::eventsetup::ComponentDescription const* desc)
0037         : data_(iData), description_(desc) {}
0038 
0039     ///Used when the attempt to get the data failed
0040     ESHandleBase(std::shared_ptr<ESHandleExceptionFactory>&& iWhyFailed) : whyFailedFactory_(std::move(iWhyFailed)) {}
0041 
0042     edm::eventsetup::ComponentDescription const* description() const;
0043 
0044     bool isValid() const { return nullptr != data_ && nullptr != description_; }
0045 
0046     bool failedToGet() const { return bool(whyFailedFactory_); }
0047 
0048     explicit operator bool() const { return isValid(); }
0049 
0050     bool operator!() const { return not isValid(); }
0051 
0052     void swap(ESHandleBase& iOther) {
0053       std::swap(data_, iOther.data_);
0054       std::swap(description_, iOther.description_);
0055       std::swap(whyFailedFactory_, iOther.whyFailedFactory_);
0056     }
0057 
0058     std::shared_ptr<ESHandleExceptionFactory> const& whyFailedFactory() const { return whyFailedFactory_; }
0059 
0060   protected:
0061     void const* productStorage() const {
0062       if (whyFailedFactory_) {
0063         std::rethrow_exception(whyFailedFactory_->make());
0064       }
0065       return data_;
0066     }
0067 
0068   private:
0069     // ---------- member data --------------------------------
0070     void const* data_{nullptr};
0071     edm::eventsetup::ComponentDescription const* description_{nullptr};
0072     std::shared_ptr<ESHandleExceptionFactory> whyFailedFactory_{nullptr};
0073   };
0074 
0075   template <typename T>
0076   class ESHandle : public ESHandleBase {
0077   public:
0078     typedef T value_type;
0079 
0080     ESHandle() = default;
0081     ESHandle(T const* iData) : ESHandleBase(iData, nullptr) {}
0082     ESHandle(T const* iData, edm::eventsetup::ComponentDescription const* desc) : ESHandleBase(iData, desc) {}
0083     ESHandle(std::shared_ptr<ESHandleExceptionFactory>&&);
0084 
0085     // ---------- const member functions ---------------------
0086     T const* product() const { return static_cast<T const*>(productStorage()); }
0087     T const* operator->() const { return product(); }
0088     T const& operator*() const { return *product(); }
0089     // ---------- static member functions --------------------
0090     static constexpr bool transientAccessOnly = false;
0091 
0092     // ---------- member functions ---------------------------
0093   };
0094 
0095   template <class T>
0096   ESHandle<T>::ESHandle(std::shared_ptr<edm::ESHandleExceptionFactory>&& iWhyFailed)
0097       : ESHandleBase(std::move(iWhyFailed)) {}
0098 
0099   // Free swap function
0100   inline void swap(ESHandleBase& a, ESHandleBase& b) { a.swap(b); }
0101 }  // namespace edm
0102 #endif