From 1e9cfe84e004db881aa9346790f2133bcd14cd93 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Fri, 16 Aug 2024 09:24:50 +0200 Subject: [PATCH] make methods const when appropriate --- k4FWCore/components/MetadataSvc.cpp | 3 ++- k4FWCore/components/MetadataSvc.h | 5 +++-- k4FWCore/components/Writer.cpp | 3 ++- k4FWCore/include/k4FWCore/IMetadataSvc.h | 11 +++++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/k4FWCore/components/MetadataSvc.cpp b/k4FWCore/components/MetadataSvc.cpp index 25adeaf8..d87f0845 100644 --- a/k4FWCore/components/MetadataSvc.cpp +++ b/k4FWCore/components/MetadataSvc.cpp @@ -43,7 +43,8 @@ StatusCode MetadataSvc::initialize() { StatusCode MetadataSvc::finalize() { return Service::finalize(); } -podio::Frame* MetadataSvc::getFrame() { return m_frame.get(); } +const podio::Frame* MetadataSvc::getFrame() const { return m_frame.get(); } +podio::Frame* MetadataSvc::getFrame() { return m_frame.get(); } void MetadataSvc::setFrame(podio::Frame&& frame) { m_frame = std::make_unique(std::move(frame)); } DECLARE_COMPONENT(MetadataSvc) diff --git a/k4FWCore/components/MetadataSvc.h b/k4FWCore/components/MetadataSvc.h index 4023124a..b7273ebd 100644 --- a/k4FWCore/components/MetadataSvc.h +++ b/k4FWCore/components/MetadataSvc.h @@ -42,8 +42,9 @@ class MetadataSvc : public extends { std::unique_ptr m_frame; - podio::Frame* getFrame() override; - void setFrame(podio::Frame&& frame) override; + const podio::Frame* getFrame() const override; + podio::Frame* getFrame() override; + void setFrame(podio::Frame&& frame) override; }; #endif diff --git a/k4FWCore/components/Writer.cpp b/k4FWCore/components/Writer.cpp index 94952bcf..f44dce11 100644 --- a/k4FWCore/components/Writer.cpp +++ b/k4FWCore/components/Writer.cpp @@ -34,6 +34,7 @@ #include #include +#include class Writer final : public Gaudi::Functional::Consumer { public: @@ -121,7 +122,7 @@ class Writer final : public Gaudi::Functional::ConsumergetWriter()->writeFrame(config_metadata_frame, "configuration_metadata"); - if (auto* metadata_frame = m_metadataSvc->getFrame(); metadata_frame) { + if (const auto* metadata_frame = std::as_const(*m_metadataSvc).getFrame(); metadata_frame) { iosvc->getWriter()->writeFrame(*metadata_frame, podio::Category::Metadata); } diff --git a/k4FWCore/include/k4FWCore/IMetadataSvc.h b/k4FWCore/include/k4FWCore/IMetadataSvc.h index 8ad64727..c88df593 100644 --- a/k4FWCore/include/k4FWCore/IMetadataSvc.h +++ b/k4FWCore/include/k4FWCore/IMetadataSvc.h @@ -27,8 +27,8 @@ class IMetadataSvc : virtual public IInterface { public: DeclareInterfaceID(IMetadataSvc, 1, 0); - virtual podio::Frame* getFrame() = 0; - virtual void setFrame(podio::Frame&& frame) = 0; + virtual const podio::Frame* getFrame() const = 0; + virtual void setFrame(podio::Frame&& frame) = 0; template void put(const std::string& name, const T& obj) { if (!getFrame()) { @@ -37,13 +37,16 @@ class IMetadataSvc : virtual public IInterface { getFrame()->putParameter(name, obj); } - template std::optional get(const std::string& name) { - auto* frame = getFrame(); + template std::optional get(const std::string& name) const { + const auto* frame = getFrame(); if (!frame) { return std::nullopt; } return frame->getParameter(name); } + +protected: + virtual podio::Frame* getFrame() = 0; }; #endif