Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:49:20

0001 #ifndef DataFormats_Common_ConvertHandle_h
0002 #define DataFormats_Common_ConvertHandle_h
0003 
0004 #include "DataFormats/Common/interface/BasicHandle.h"
0005 #include "DataFormats/Common/interface/Handle.h"
0006 #include "DataFormats/Common/interface/Wrapper.h"
0007 #include "FWCore/Utilities/interface/Likely.h"
0008 
0009 #include <typeinfo>
0010 #include <algorithm>
0011 #include <memory>
0012 
0013 namespace edm {
0014 
0015   namespace handleimpl {
0016     void throwConvertTypeError(std::type_info const& expected, std::type_info const& actual);
0017     std::shared_ptr<edm::HandleExceptionFactory const> makeInvalidReferenceException();
0018   }  // namespace handleimpl
0019 
0020   // Convert from handle-to-void to handle-to-T
0021   template <typename T>
0022   Handle<T> convert_handle(BasicHandle&& bh) noexcept(true) {
0023     if UNLIKELY (bh.failedToGet()) {
0024       return Handle<T>(std::move(bh.whyFailedFactory()));
0025     }
0026     void const* basicWrapper = bh.wrapper();
0027     if UNLIKELY (nullptr == basicWrapper) {
0028       return Handle<T>{handleimpl::makeInvalidReferenceException()};
0029     }
0030     auto wrapper = static_cast<Wrapper<T> const*>(basicWrapper);
0031 
0032     return Handle<T>(wrapper->product(), bh.provenance());
0033   }
0034 
0035   template <typename T>
0036   Handle<T> convert_handle_check_type(BasicHandle&& bh) {
0037     if UNLIKELY (bh.failedToGet()) {
0038       return Handle<T>(std::move(bh.whyFailedFactory()));
0039     }
0040     void const* basicWrapper = bh.wrapper();
0041     if UNLIKELY (basicWrapper == nullptr) {
0042       return Handle<T>{handleimpl::makeInvalidReferenceException()};
0043     }
0044     if UNLIKELY (!(bh.wrapper()->dynamicTypeInfo() == typeid(T))) {
0045       handleimpl::throwConvertTypeError(typeid(T), bh.wrapper()->dynamicTypeInfo());
0046     }
0047     Wrapper<T> const* wrapper = static_cast<Wrapper<T> const*>(basicWrapper);
0048 
0049     return Handle<T>(wrapper->product(), bh.provenance());
0050   }
0051 
0052 }  // namespace edm
0053 
0054 #endif