Skip to content

Commit

Permalink
Fix some minor issues with the Reader and Writer interfaces (#618)
Browse files Browse the repository at this point in the history
* Fix const correctness and accessibility for I/O interfaces

* Remove unnecessary and unused concept API

* Remove unnecessary dictionary generation for I/O interfaces
  • Loading branch information
tmadlener authored Jun 11, 2024
1 parent af28f30 commit 335a4ae
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
26 changes: 20 additions & 6 deletions include/podio/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@
namespace podio {

class Reader {
public:
private:
struct ReaderConcept {
virtual ~ReaderConcept() = default;

virtual podio::Frame readNextFrame(const std::string& name) = 0;
virtual podio::Frame readFrame(const std::string& name, size_t index) = 0;
virtual size_t getEntries(const std::string& name) = 0;
virtual size_t getEntries(const std::string& name) const = 0;
virtual podio::version::Version currentFileVersion() const = 0;
virtual std::vector<std::string_view> getAvailableCategories() const = 0;
virtual const std::string_view getDatamodelDefinition(const std::string& name) const = 0;
virtual std::vector<std::string> getAvailableDatamodels() const = 0;
};

private:
template <typename T>
struct ReaderModel final : public ReaderConcept {
struct ReaderModel final : ReaderConcept {
ReaderModel(std::unique_ptr<T> reader) : m_reader(std::move(reader)) {
}
ReaderModel(const ReaderModel&) = delete;
ReaderModel& operator=(const ReaderModel&) = delete;
ReaderModel(ReaderModel&&) = default;
ReaderModel& operator=(ReaderModel&&) = default;

~ReaderModel() = default;

podio::Frame readNextFrame(const std::string& name) override {
auto maybeFrame = m_reader->readNextEntry(name);
Expand All @@ -43,7 +48,7 @@ class Reader {
throw std::runtime_error("Failed reading category " + name + " at frame " + std::to_string(index) +
" (reading beyond bounds?)");
}
size_t getEntries(const std::string& name) override {
size_t getEntries(const std::string& name) const override {
return m_reader->getEntries(name);
}
podio::version::Version currentFileVersion() const override {
Expand All @@ -67,9 +72,18 @@ class Reader {

std::unique_ptr<ReaderConcept> m_self{nullptr};

public:
template <typename T>
Reader(std::unique_ptr<T>);

Reader(const Reader&) = delete;
Reader& operator=(const Reader&) = delete;

Reader(Reader&&) = default;
Reader& operator=(Reader&&) = default;

~Reader() = default;

podio::Frame readNextFrame(const std::string& name) {
return m_self->readNextFrame(name);
}
Expand All @@ -82,10 +96,10 @@ class Reader {
podio::Frame readEvent(size_t index) {
return readFrame(podio::Category::Event, index);
}
size_t getEntries(const std::string& name) {
size_t getEntries(const std::string& name) const {
return m_self->getEntries(name);
}
size_t getEvents() {
size_t getEvents() const {
return getEntries(podio::Category::Event);
}
podio::version::Version currentFileVersion() const {
Expand Down
25 changes: 7 additions & 18 deletions include/podio/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@
#define PODIO_WRITER_H

#include "podio/Frame.h"
#include "podio/podioVersion.h"

namespace podio {

class Writer {
public:
private:
struct WriterConcept {
virtual ~WriterConcept() = default;

virtual void writeFrame(const podio::Frame& frame, const std::string& category) = 0;
virtual void writeFrame(const podio::Frame& frame, const std::string& category,
const std::vector<std::string>& collections) = 0;
virtual void writeEvent(const podio::Frame& frame) = 0;
virtual void writeEvent(const podio::Frame& frame, const std::vector<std::string>& collections) = 0;
virtual void finish() = 0;
};

private:
template <typename T>
struct WriterModel final : public WriterConcept {
struct WriterModel final : WriterConcept {
WriterModel(std::unique_ptr<T> writer) : m_writer(std::move(writer)) {
}
WriterModel(const WriterModel&) = delete;
Expand All @@ -30,19 +27,10 @@ class Writer {

~WriterModel() = default;

void writeFrame(const podio::Frame& frame, const std::string& category) override {
return m_writer->writeFrame(frame, category);
}
void writeFrame(const podio::Frame& frame, const std::string& category,
const std::vector<std::string>& collections) override {
return m_writer->writeFrame(frame, category, collections);
}
void writeEvent(const podio::Frame& frame) override {
return writeFrame(frame, podio::Category::Event);
}
void writeEvent(const podio::Frame& frame, const std::vector<std::string>& collections) override {
return writeFrame(frame, podio::Category::Event, collections);
}
void finish() override {
return m_writer->finish();
}
Expand All @@ -51,6 +39,7 @@ class Writer {

std::unique_ptr<WriterConcept> m_self{nullptr};

public:
template <typename T>
Writer(std::unique_ptr<T> reader) : m_self(std::make_unique<WriterModel<T>>(std::move(reader))) {
}
Expand All @@ -63,16 +52,16 @@ class Writer {
~Writer() = default;

void writeFrame(const podio::Frame& frame, const std::string& category) {
return m_self->writeFrame(frame, category);
return m_self->writeFrame(frame, category, frame.getAvailableCollections());
}
void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector<std::string>& collections) {
return m_self->writeFrame(frame, category, collections);
}
void writeEvent(const podio::Frame& frame) {
return writeFrame(frame, podio::Category::Event);
writeFrame(frame, podio::Category::Event, frame.getAvailableCollections());
}
void writeEvent(const podio::Frame& frame, const std::vector<std::string>& collections) {
return writeFrame(frame, podio::Category::Event, collections);
writeFrame(frame, podio::Category::Event, collections);
}
void finish() {
return m_self->finish();
Expand Down
8 changes: 5 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ set(io_headers
${PROJECT_SOURCE_DIR}/include/podio/Reader.h
)

PODIO_ADD_LIB_AND_DICT(podioIO "${io_headers}" "${io_sources}" io_selection.xml)
add_library(podioIO SHARED ${io_sources})
add_library(podio::podioIO ALIAS podioIO)
target_include_directories(podioIO PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(podioIO PUBLIC podio::podio podio::podioRootIO)
if(ENABLE_SIO)
target_link_libraries(podioIO PUBLIC podio::podioSioIO)
Expand All @@ -172,8 +176,6 @@ install(FILES
${CMAKE_CURRENT_BINARY_DIR}/libpodioDict_rdict.pcm
${CMAKE_CURRENT_BINARY_DIR}/podioRootIODictDict.rootmap
${CMAKE_CURRENT_BINARY_DIR}/libpodioRootIODict_rdict.pcm
${CMAKE_CURRENT_BINARY_DIR}/podioIODictDict.rootmap
${CMAKE_CURRENT_BINARY_DIR}/libpodioIODict_rdict.pcm
DESTINATION "${CMAKE_INSTALL_LIBDIR}")

if (ENABLE_SIO)
Expand Down
6 changes: 0 additions & 6 deletions src/io_selection.xml

This file was deleted.

0 comments on commit 335a4ae

Please sign in to comment.