Skip to content

Commit

Permalink
move Collection::createBuffers template into macro
Browse files Browse the repository at this point in the history
  • Loading branch information
hegner committed Jul 6, 2023
1 parent 77fec67 commit b65c6ee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 46 deletions.
46 changes: 1 addition & 45 deletions python/templates/Collection.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -165,51 +165,7 @@ podio::SchemaVersionT {{ collection_type }}::getSchemaVersion() const {
return {{ package_name }}::meta::schemaVersion;
}

// anonymous namespace for registration with the CollectionBufferFactory. This
// ensures that we don't have to make up arbitrary namespace names here, since
// none of this is publicly visible
namespace {
podio::CollectionReadBuffers createBuffers(bool isSubset) {
auto readBuffers = podio::CollectionReadBuffers{};
readBuffers.type = "{{ class.full_type }}Collection";
readBuffers.schemaVersion = {{ package_name }}::meta::schemaVersion;
readBuffers.data = isSubset ? nullptr : new {{ class.bare_type }}DataContainer;

// The number of ObjectID vectors is either 1 or the sum of OneToMany and
// OneToOne relations
const auto nRefs = isSubset ? 1 : {{ OneToManyRelations | length }} + {{ OneToOneRelations | length }};
readBuffers.references = new podio::CollRefCollection(nRefs);
for (auto& ref : *readBuffers.references) {
// Make sure to place usable buffer pointers here
ref = std::make_unique<std::vector<podio::ObjectID>>();
}

readBuffers.vectorMembers = new podio::VectorMembersInfo();
if (!isSubset) {
readBuffers.vectorMembers->reserve({{ VectorMembers | length }});
{% for member in VectorMembers %}
readBuffers.vectorMembers->emplace_back("{{ member.full_type }}", new std::vector<{{ member.full_type }}>);
{% endfor %}
}

readBuffers.createCollection = [](podio::CollectionReadBuffers buffers, bool isSubsetColl) {
{{ collection_type }}Data data(buffers, isSubsetColl);
return std::make_unique<{{ collection_type }}>(std::move(data), isSubsetColl);
};

readBuffers.recast = [](podio::CollectionReadBuffers& buffers) {
if (buffers.data) {
buffers.data = podio::CollectionWriteBuffers::asVector<{{ class.full_type }}Data>(buffers.data);
}
{% if VectorMembers %}
{% for member in VectorMembers %}
(*buffers.vectorMembers)[{{ loop.index0 }}].second = podio::CollectionWriteBuffers::asVector<{{ member.full_type }}>((*buffers.vectorMembers)[{{ loop.index0 }}].second);
{% endfor %}
{% endif %}
};

return readBuffers;
}
{{ macros.createBuffers(class, package_name, collection_type, OneToManyRelations, OneToOneRelations, VectorMembers, 1) }}

// The usual trick with an IIFE and a static variable inside a funtion and then
// making sure to call that function during shared library loading
Expand Down
52 changes: 51 additions & 1 deletion python/templates/macros/collections.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,54 @@ void {{ class.bare_type }}Collection::print(std::ostream& os, bool flush) const
os.flush();
}
}
{%- endmacro %}
{% endmacro %}

{% macro createBuffers(class, package_name, collection_type, OneToManyRelations, OneToOneRelations, VectorMembers, schemaVersion) %}

// anonymous namespace for registration with the CollectionBufferFactory. This
// ensures that we don't have to make up arbitrary namespace names here, since
// none of this is publicly visible
namespace {
podio::CollectionReadBuffers createBuffers(bool isSubset) {
auto readBuffers = podio::CollectionReadBuffers{};
readBuffers.type = "{{ class.full_type }}Collection";
readBuffers.schemaVersion = {{ package_name }}::meta::schemaVersion;
readBuffers.data = isSubset ? nullptr : new {{ class.bare_type }}DataContainer;

// The number of ObjectID vectors is either 1 or the sum of OneToMany and
// OneToOne relations
const auto nRefs = isSubset ? 1 : {{ OneToManyRelations | length }} + {{ OneToOneRelations | length }};
readBuffers.references = new podio::CollRefCollection(nRefs);
for (auto& ref : *readBuffers.references) {
// Make sure to place usable buffer pointers here
ref = std::make_unique<std::vector<podio::ObjectID>>();
}

readBuffers.vectorMembers = new podio::VectorMembersInfo();
if (!isSubset) {
readBuffers.vectorMembers->reserve({{ VectorMembers | length }});
{% for member in VectorMembers %}
readBuffers.vectorMembers->emplace_back("{{ member.full_type }}", new std::vector<{{ member.full_type }}>);
{% endfor %}
}

readBuffers.createCollection = [](podio::CollectionReadBuffers buffers, bool isSubsetColl) {
{{ collection_type }}Data data(buffers, isSubsetColl);
return std::make_unique<{{ collection_type }}>(std::move(data), isSubsetColl);
};

readBuffers.recast = [](podio::CollectionReadBuffers& buffers) {
if (buffers.data) {
buffers.data = podio::CollectionWriteBuffers::asVector<{{ class.full_type }}Data>(buffers.data);
}
{% if VectorMembers %}
{% for member in VectorMembers %}
(*buffers.vectorMembers)[{{ loop.index0 }}].second = podio::CollectionWriteBuffers::asVector<{{ member.full_type }}>((*buffers.vectorMembers)[{{ loop.index0 }}].second);
{% endfor %}
{% endif %}
};

return readBuffers;
}

{% endmacro %}
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ SET(podio_PYTHON_DIR ${CMAKE_SOURCE_DIR}/python)

PODIO_GENERATE_DATAMODEL(datamodel datalayout.yaml headers sources
IO_BACKEND_HANDLERS ${PODIO_IO_HANDLERS}
OLD_DESCRIPTION schema_evolution/datalayout_old.yaml
SCHEMA_EVOLUTION schema_evolution/schema_evolution.yaml
)

# Use the cmake building blocks to add the different parts (conditionally)
Expand Down

0 comments on commit b65c6ee

Please sign in to comment.