Skip to content

Commit

Permalink
Avoid hardcoded magic UserData schema versions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed May 3, 2023
1 parent f6898c1 commit f0f7d4e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
14 changes: 10 additions & 4 deletions include/podio/SIOBlockUserData.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef PODIO_SIOBLOCKUSERDATA_H
#define PODIO_SIOBLOCKUSERDATA_H

#include "podio/CollectionBufferFactory.h"
#include "podio/CollectionBuffers.h"
#include "podio/SIOBlock.h"
#include "podio/UserDataCollection.h"
Expand Down Expand Up @@ -29,17 +30,22 @@ namespace podio {
template <typename BasicType, typename = EnableIfSupportedUserType<BasicType>>
class SIOBlockUserData : public podio::SIOBlock {
public:
SIOBlockUserData() : SIOBlock(::sio_name<BasicType>(), sio::version::encode_version(0, 1)) {
SIOBlockUserData() :
SIOBlock(::sio_name<BasicType>(), sio::version::encode_version(UserDataCollection<BasicType>::schemaVersion, 0)) {

podio::SIOBlockFactory::instance().registerBlockForCollection(podio::userDataTypeName<BasicType>(), this);
}

SIOBlockUserData(const std::string& name) : SIOBlock(name, sio::version::encode_version(0, 1)) {
SIOBlockUserData(const std::string& name) :
SIOBlock(name, sio::version::encode_version(UserDataCollection<BasicType>::schemaVersion, 0)) {
}

void read(sio::read_device& device, sio::version_type /*version*/) override {
void read(sio::read_device& device, sio::version_type version) override {
const auto& bufferFactory = podio::CollectionBufferFactory::instance();
m_buffers = bufferFactory.createBuffers(podio::userDataCollTypeName<BasicType>(), 1, false).value();
m_buffers =
bufferFactory
.createBuffers(podio::userDataCollTypeName<BasicType>(), sio::version::major_version(version), false)
.value();

auto* dataVec = new std::vector<BasicType>();
unsigned size(0);
Expand Down
7 changes: 5 additions & 2 deletions include/podio/UserDataCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define PODIO_USERDATACOLLECTION_H

#include "podio/CollectionBase.h"
#include "podio/CollectionBufferFactory.h"
#include "podio/CollectionBuffers.h"
#include "podio/DatamodelRegistry.h"
#include "podio/SchemaEvolution.h"
#include "podio/utilities/TypeHelpers.h"

#include <map>
Expand Down Expand Up @@ -90,6 +90,9 @@ class UserDataCollection : public CollectionBase {
UserDataCollection& operator=(UserDataCollection&&) = default;
~UserDataCollection() = default;

/// The schema version of UserDataCollections
static constexpr SchemaVersionT schemaVersion = 1;

/// prepare buffers for serialization
void prepareForWrite() const override {
}
Expand Down Expand Up @@ -160,7 +163,7 @@ class UserDataCollection : public CollectionBase {

/// The schema version is fixed manually
SchemaVersionT getSchemaVersion() const final {
return 1;
return schemaVersion;
}

/// Print this collection to the passed stream
Expand Down
21 changes: 11 additions & 10 deletions src/UserDataCollection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ namespace {
template <typename T>
int registerUserDataCollection(T) {
// Register with schema version 1 to allow for potential changes
CollectionBufferFactory::mutInstance().registerCreationFunc(userDataCollTypeName<T>(), 1, [](bool) {
return podio::CollectionReadBuffers{new std::vector<T>(), nullptr, nullptr,
[](podio::CollectionReadBuffers buffers, bool) {
return std::make_unique<UserDataCollection<T>>(
std::move(*buffers.dataAsVector<T>()));
},
[](podio::CollectionReadBuffers& buffers) {
buffers.data = podio::CollectionWriteBuffers::asVector<T>(buffers.data);
}};
});
CollectionBufferFactory::mutInstance().registerCreationFunc(
userDataCollTypeName<T>(), UserDataCollection<T>::schemaVersion, [](bool) {
return podio::CollectionReadBuffers{new std::vector<T>(), nullptr, nullptr,
[](podio::CollectionReadBuffers buffers, bool) {
return std::make_unique<UserDataCollection<T>>(
std::move(*buffers.dataAsVector<T>()));
},
[](podio::CollectionReadBuffers& buffers) {
buffers.data = podio::CollectionWriteBuffers::asVector<T>(buffers.data);
}};
});

return 1;
}
Expand Down

0 comments on commit f0f7d4e

Please sign in to comment.