Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a createBuffers macro for the jinja template #480

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 1 addition & 46 deletions python/templates/Collection.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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<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) {
// 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) }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{{ macros.createBuffers(class, package_name, collection_type, OneToManyRelations, OneToOneRelations, VectorMembers, 1) }}
{{ macros.create_buffers(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
53 changes: 52 additions & 1 deletion python/templates/macros/collections.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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) %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

consistency with other macros (and the release notes).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, schemaVersion is currently unused (it will get it's usage in #472 though).


// 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) {
// 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 %}