From 06eae8eab4424d5ccb35dcb71cafb4fc99c5a131 Mon Sep 17 00:00:00 2001 From: hegner Date: Wed, 13 Sep 2023 15:12:15 +0200 Subject: [PATCH] Introduce a createBuffers macro for the jinja template (#480) * move Collection::createBuffers template into macro --------- Co-authored-by: tmadlener --- python/templates/Collection.cc.jinja2 | 47 +------------------ python/templates/macros/collections.jinja2 | 53 +++++++++++++++++++++- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/python/templates/Collection.cc.jinja2 b/python/templates/Collection.cc.jinja2 index 4828750a9..665e2188d 100644 --- a/python/templates/Collection.cc.jinja2 +++ b/python/templates/Collection.cc.jinja2 @@ -173,52 +173,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>(); - } - - 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) { - // We only have any of these buffers if this is not a subset collection - 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 diff --git a/python/templates/macros/collections.jinja2 b/python/templates/macros/collections.jinja2 index d07abad19..3224b6fc0 100644 --- a/python/templates/macros/collections.jinja2 +++ b/python/templates/macros/collections.jinja2 @@ -151,4 +151,55 @@ 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>(); + } + + 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) { + // We only have any of these buffers if this is not a subset collection + 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 %}