Skip to content

Commit

Permalink
Add get_optional overload for retrieving the optional value
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed May 31, 2024
1 parent 1334324 commit 6559e6f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
35 changes: 30 additions & 5 deletions k4FWCore/include/k4FWCore/MetaDataHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "k4FWCore/PodioDataSvc.h"

#include "podio/podioVersion.h"

template <typename T> class MetaDataHandle {
public:
MetaDataHandle();
Expand All @@ -35,6 +37,12 @@ template <typename T> class MetaDataHandle {
/// @throws GaudiException in case the value is not (yet) available
const T get() const;

/// Get the (optional) value that is stored in this MetaDataHandle
///
/// @returns An optional that contains the value if it was available from the
/// data store and is not engaged otherwise
std::optional<T> get_optional() const;

/// Get the value that is stored in the MetaDataHandle or the provided default
/// value in case that is not available
///
Expand Down Expand Up @@ -81,20 +89,37 @@ MetaDataHandle<T>::MetaDataHandle(const Gaudi::DataHandle& handle, const std::st
checkPodioDataSvc();
}

//---------------------------------------------------------------------------
template <typename T> std::optional<T> MetaDataHandle<T>::get_optional() const {
const auto& frame = m_podio_data_service->getMetaDataFrame();
#if PODIO_BUILD_VERSION > PODIO_VERSION(0, 99, 0)
return frame.getParameter<T>(fullDescriptor());
#else
// explicitly make a copy here and move it into the optional below
auto val = frame.getParameter<T>(fullDescriptor());
// This is what happens if we have a non-present key, so we can use it as a
// way to determine if the parameter was (likely) unset. For more details see
// https://github.com/AIDASoft/podio/issues/576
if (val == T{}) {
return std::nullopt;
}
return std::optional<T>(std::move(val));
#endif
}

//---------------------------------------------------------------------------
template <typename T> const T MetaDataHandle<T>::get() const {
const auto& frame = m_podio_data_service->getMetaDataFrame();
const auto maybeVal = frame.getParameter<T>(fullDescriptor());
const auto maybeVal = get_optional();
if (!maybeVal.has_value()) {
throw GaudiException("MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE);
throw GaudiException("MetaDataHandle empty handle access",
"MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE);
}
return maybeVal.value();
}

//---------------------------------------------------------------------------
template <typename T> const T MetaDataHandle<T>::get(const T& defaultValue) const {
const auto& frame = m_podio_data_service->getMetaDataFrame();
return frame.getParameter<T>(fullDescriptor()).value_or(defaultValue);
return get_optional().value_or(defaultValue);
}

//---------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@ StatusCode k4FWCoreTest_cellID_reader::initialize() {
StatusCode k4FWCoreTest_cellID_reader::execute(const EventContext&) const {
const auto simtrackerhits_coll = m_simTrackerHitReaderHandle.get();

#if PODIO_BUILD_VERSION > PODIO_VERSION(0, 99, 0)
const auto cellIDstr = m_cellIDHandle.get().value_or("");
#else
const auto cellIDstr = m_cellIDHandle.get();
#endif
const auto cellIDstr = m_cellIDHandle.get("");

if (cellIDstr != cellIDtest) {
error() << "ERROR cellID is: " << cellIDstr << endmsg;
error() << "ERROR cellID is: " << cellIDstr << "expected (" << cellIDtest << ")" << endmsg;
return StatusCode::FAILURE;
}

Expand Down

0 comments on commit 6559e6f

Please sign in to comment.