Skip to content

Commit

Permalink
Use std::optional for the reader and writer instead of std::unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Oct 1, 2024
1 parent 386197a commit 0ae6af4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
7 changes: 1 addition & 6 deletions k4FWCore/components/IIOSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
* The interface implemented by any class making IO with functional algorithms
*/
class IIOSvc : virtual public IInterface {
public:
struct EndOfInput : std::logic_error {
EndOfInput() : logic_error("Reached end of input while more data were expected"){};
};

public:
/// InterfaceID
DeclareInterfaceID(IIOSvc, 1, 0);
Expand All @@ -48,7 +43,7 @@ class IIOSvc : virtual public IInterface {
next() = 0;
virtual std::shared_ptr<std::vector<std::string>> getCollectionNames() const = 0;

virtual std::shared_ptr<podio::Writer> getWriter() = 0;
virtual podio::Writer& getWriter() = 0;
virtual void deleteWriter() = 0;
virtual void deleteReader() = 0;
virtual bool checkIfWriteCollection(const std::string& collName) = 0;
Expand Down
2 changes: 1 addition & 1 deletion k4FWCore/components/IOSvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ StatusCode IOSvc::initialize() {
}
if (!m_readingFileNames.empty()) {
try {
m_reader = std::make_unique<podio::Reader>(podio::makeReader(m_readingFileNames.value()));
m_reader = podio::makeReader(m_readingFileNames.value());
} catch (std::runtime_error& e) {
error() << "Error when opening files: " << e.what() << endmsg;
return StatusCode::FAILURE;
Expand Down
10 changes: 5 additions & 5 deletions k4FWCore/components/IOSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ class IOSvc : public extends<Service, IIOSvc, IIncidentListener> {

KeepDropSwitch m_switch;

std::unique_ptr<podio::Reader> m_reader{nullptr};
std::shared_ptr<podio::Writer> m_writer{nullptr};
std::optional<podio::Reader> m_reader;
std::optional<podio::Writer> m_writer;

std::shared_ptr<podio::Writer> getWriter() override {
podio::Writer& getWriter() override {
if (!m_writer) {
m_writer = std::make_shared<podio::Writer>(podio::makeWriter(m_writingFileName.value(), m_outputType));
m_writer = podio::makeWriter(m_writingFileName.value(), m_outputType);
}
return m_writer;
return *m_writer;
}

// Gaudi doesn't always run the destructor of the Services so we have to
Expand Down
6 changes: 3 additions & 3 deletions k4FWCore/components/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ class Writer final : public Gaudi::Functional::Consumer<void(const EventContext&
if (const char* env_key4hep_stack = std::getenv("KEY4HEP_STACK")) {
config_metadata_frame.putParameter("key4hepstack", env_key4hep_stack);
}
iosvc->getWriter()->writeFrame(config_metadata_frame, "configuration_metadata");
iosvc->getWriter().writeFrame(config_metadata_frame, "configuration_metadata");

if (const auto* metadata_frame = m_metadataSvc->getFrame(); metadata_frame) {
iosvc->getWriter()->writeFrame(*metadata_frame, podio::Category::Metadata);
iosvc->getWriter().writeFrame(*metadata_frame, podio::Category::Metadata);
}

iosvc->deleteWriter();
Expand Down Expand Up @@ -262,7 +262,7 @@ class Writer final : public Gaudi::Functional::Consumer<void(const EventContext&
}

debug() << "Writing frame" << endmsg;
iosvc->getWriter()->writeFrame(ptr->getData(), podio::Category::Event, m_collectionsToSave);
iosvc->getWriter().writeFrame(ptr->getData(), podio::Category::Event, m_collectionsToSave);
}
};

Expand Down

0 comments on commit 0ae6af4

Please sign in to comment.