diff --git a/k4FWCore/include/k4FWCore/DataHandle.h b/k4FWCore/include/k4FWCore/DataHandle.h index 92b48c4f..8dfd31af 100644 --- a/k4FWCore/include/k4FWCore/DataHandle.h +++ b/k4FWCore/include/k4FWCore/DataHandle.h @@ -22,12 +22,10 @@ #include "k4FWCore/DataWrapper.h" #include "k4FWCore/PodioDataSvc.h" -#include "Gaudi/Algorithm.h" #include "GaudiKernel/DataObjectHandle.h" -#include "edm4hep/Constants.h" - #include +#include #include /** @@ -68,8 +66,6 @@ template class DataHandle : public DataObjectHandle> private: ServiceHandle m_eds; - bool m_isGoodType{false}; - bool m_isCollection{false}; T* m_dataPtr; }; @@ -108,40 +104,28 @@ DataHandle::DataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode * static cast: we do not need the checks of the dynamic cast for every access! */ template const T* DataHandle::get() { - DataObject* dataObjectp = nullptr; - auto sc = m_eds->retrieveObject(DataObjectHandle>::fullKey().key(), dataObjectp); - - if (sc.isSuccess()) { - if (!m_isGoodType && !m_isCollection) { - // only do this once (if both are false after this, we throw exception) - m_isGoodType = nullptr != dynamic_cast*>(dataObjectp); - if (!m_isGoodType) { - auto* tmp = dynamic_cast*>(dataObjectp); - if (tmp != nullptr) { - m_isCollection = nullptr != dynamic_cast(tmp->collectionBase()); - } - } - } - if (m_isGoodType) { - return static_cast*>(dataObjectp)->getData(); - } else if (m_isCollection) { - // The reader does not know the specific type of the collection. So we need a reinterpret_cast if the handle was - // created by the reader. - auto* tmp = static_cast*>(dataObjectp); - return reinterpret_cast(tmp->collectionBase()); - } else { - // When a functional has pushed a std::shared_ptr into the store - auto ptr = static_cast>*>(dataObjectp); - if (ptr) { - return static_cast(ptr->getData().get()); - } - std::string errorMsg("The type provided for " + DataObjectHandle>::pythonRepr() + - " is different from the one of the object in the store."); - throw GaudiException(errorMsg, "wrong product type", StatusCode::FAILURE); - } + DataObject* dataObjectp; + auto sc = m_eds->retrieveObject(DataObjectHandle>::fullKey().key(), dataObjectp); + + if (sc.isFailure()) { + std::string msg("Could not retrieve product " + DataObjectHandle>::pythonRepr()); + throw GaudiException(msg, "wrong product name", StatusCode::FAILURE); + } + bool isGoodType = nullptr != dynamic_cast*>(dataObjectp); + if (isGoodType) { + return static_cast*>(dataObjectp)->getData(); + } + + // When a functional has pushed a std::unique_ptr into the store + // We wrap it inside constexpr because if the handle has a type that is not a collection + // then the static_cast will fail at compile time + if constexpr (std::is_base_of_v) { + auto ptr = static_cast>*>(dataObjectp); + return static_cast(ptr->getData().get()); } - std::string msg("Could not retrieve product " + DataObjectHandle>::pythonRepr()); - throw GaudiException(msg, "wrong product name", StatusCode::FAILURE); + std::string errorMsg("The type provided for " + DataObjectHandle>::pythonRepr() + + " is different from the one of the object in the store " + typeid(*dataObjectp).name()); + throw std::runtime_error(errorMsg); } //---------------------------------------------------------------------------