Skip to content

Commit

Permalink
Use unique_ptr to manage one to one relations
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Sep 25, 2024
1 parent adf225f commit 2d4f5b4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
13 changes: 8 additions & 5 deletions include/podio/detail/RelationIOHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "podio/utilities/TypeHelpers.h"
#include <podio/CollectionBase.h>

#include <memory>
#include <tuple>
#include <vector>

Expand Down Expand Up @@ -109,9 +110,10 @@ void addMultiRelation(std::vector<RelType>& relElements, const podio::Collection
/// @param coll The collection that holds the actual element
/// @param id The ObjectID of the element that we are currently looking for
template <typename T, typename InterfaceType>
void tryAssignTo(T, InterfaceType*& relation, const podio::CollectionBase* coll, const podio::ObjectID id) {
void tryAssignTo(T, std::unique_ptr<InterfaceType>& relation, const podio::CollectionBase* coll,
const podio::ObjectID id) {
if (const auto* typeColl = dynamic_cast<const typename T::collection_type*>(coll)) {
relation = new InterfaceType((*typeColl)[id.index]);
relation = std::make_unique<InterfaceType>((*typeColl)[id.index]);
}
}

Expand All @@ -129,7 +131,7 @@ void tryAssignTo(T, InterfaceType*& relation, const podio::CollectionBase* coll,
/// @param coll The collection that holds the actual element
/// @param id The ObjectID of the element that we are currently looking for
template <typename InterfaceType>
void addInterfaceToSingleRelation(InterfaceType*& relation, const podio::CollectionBase* coll,
void addInterfaceToSingleRelation(std::unique_ptr<InterfaceType>& relation, const podio::CollectionBase* coll,
const podio::ObjectID id) {
std::apply([&](auto... t) { (tryAssignTo(t, relation, coll, id), ...); }, typename InterfaceType::interfaced_types{});
}
Expand Down Expand Up @@ -162,12 +164,13 @@ void addInterfaceToSingleRelation(InterfaceType*& relation, const podio::Collect
/// necessary type casting
/// @param id The ObjectID of the object that should be retrieved and added.
template <typename RelType>
void addSingleRelation(RelType*& relation, const podio::CollectionBase* coll, const podio::ObjectID id) {
void addSingleRelation(std::unique_ptr<RelType>& relation, const podio::CollectionBase* coll,
const podio::ObjectID id) {
if constexpr (podio::detail::isInterfaceType<RelType>) {
addInterfaceToSingleRelation(relation, coll, id);
} else {
const auto* typeColl = static_cast<const typename RelType::collection_type*>(coll);
relation = new RelType((*typeColl)[id.index]);
relation = std::make_unique<RelType>((*typeColl)[id.index]);
}
}

Expand Down
6 changes: 1 addition & 5 deletions python/templates/Obj.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
{% for relation in OneToOneRelations %}
if (other.m_{{ relation.name }}) {
m_{{ relation.name }} = new {{ relation.full_type }}(*(other.m_{{ relation.name }}));
m_{{ relation.name }} = std::make_unique<{{ relation.full_type }}>(*(other.m_{{ relation.name }}));
}
{% endfor %}
}
Expand All @@ -55,10 +55,6 @@
}
{% endif %}
{%- endwith %}

{% for relation in OneToOneRelations %}
delete m_{{ relation.name }};
{% endfor %}
}
{%- endif %}

Expand Down
5 changes: 4 additions & 1 deletion python/templates/Obj.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include "podio/ObjectID.h"
{% if OneToManyRelations or VectorMembers %}
#include <vector>
{% endif %}
{% if OneToOneRelations %}
#include <memory>
{%- endif %}

{{ utils.forward_decls(forward_declarations_obj) }}
Expand Down Expand Up @@ -42,7 +45,7 @@ public:
podio::ObjectID id;
{{ class.bare_type }}Data data;
{% for relation in OneToOneRelations %}
{{ relation.full_type }}* m_{{ relation.name }}{nullptr};
std::unique_ptr<{{ relation.full_type }}> m_{{ relation.name }}{nullptr};
{% endfor %}
{% for relation in OneToManyRelations + VectorMembers %}
std::vector<{{ relation.full_type }}>* m_{{ relation.name }}{nullptr};
Expand Down
5 changes: 2 additions & 3 deletions python/templates/macros/implementations.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Mutable{{ type }} {{ full_type }}::clone(bool cloneRelations) const {
if (cloneRelations) {
{% for relation in one_to_one_relations %}
if (m_obj->m_{{ relation.name }}) {
tmp->m_{{ relation.name }} = new {{ relation.full_type }}(*m_obj->m_{{ relation.name }});
tmp->m_{{ relation.name }} = std::make_unique<{{ relation.full_type }}>((*m_obj->m_{{ relation.name }}));
}
{% endfor %}
{% for relation in multi_relations %}
Expand Down Expand Up @@ -124,8 +124,7 @@ const {{ relation.full_type }} {{ class_type }}::{{ relation.getter_name(get_syn
{% set class_type = prefix + class.bare_type %}
{% for relation in relations %}
void {{ class_type }}::{{ relation.setter_name(get_syntax) }}(const {{ relation.full_type }}& value) {
delete m_obj->m_{{ relation.name }};
m_obj->m_{{ relation.name }} = new {{ relation.full_type }}(value);
m_obj->m_{{ relation.name }} = std::make_unique<{{ relation.full_type }}>(value);
}

{% endfor %}
Expand Down

0 comments on commit 2d4f5b4

Please sign in to comment.