From 2d4f5b43b3adb7f2e00f449cb5f2ea0fa8386723 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 25 Sep 2024 16:35:20 +0200 Subject: [PATCH] Use unique_ptr to manage one to one relations --- include/podio/detail/RelationIOHelpers.h | 13 ++++++++----- python/templates/Obj.cc.jinja2 | 6 +----- python/templates/Obj.h.jinja2 | 5 ++++- python/templates/macros/implementations.jinja2 | 5 ++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/podio/detail/RelationIOHelpers.h b/include/podio/detail/RelationIOHelpers.h index 1ef2ee0ca..36dd4de34 100644 --- a/include/podio/detail/RelationIOHelpers.h +++ b/include/podio/detail/RelationIOHelpers.h @@ -4,6 +4,7 @@ #include "podio/utilities/TypeHelpers.h" #include +#include #include #include @@ -109,9 +110,10 @@ void addMultiRelation(std::vector& 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 -void tryAssignTo(T, InterfaceType*& relation, const podio::CollectionBase* coll, const podio::ObjectID id) { +void tryAssignTo(T, std::unique_ptr& relation, const podio::CollectionBase* coll, + const podio::ObjectID id) { if (const auto* typeColl = dynamic_cast(coll)) { - relation = new InterfaceType((*typeColl)[id.index]); + relation = std::make_unique((*typeColl)[id.index]); } } @@ -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 -void addInterfaceToSingleRelation(InterfaceType*& relation, const podio::CollectionBase* coll, +void addInterfaceToSingleRelation(std::unique_ptr& relation, const podio::CollectionBase* coll, const podio::ObjectID id) { std::apply([&](auto... t) { (tryAssignTo(t, relation, coll, id), ...); }, typename InterfaceType::interfaced_types{}); } @@ -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 -void addSingleRelation(RelType*& relation, const podio::CollectionBase* coll, const podio::ObjectID id) { +void addSingleRelation(std::unique_ptr& relation, const podio::CollectionBase* coll, + const podio::ObjectID id) { if constexpr (podio::detail::isInterfaceType) { addInterfaceToSingleRelation(relation, coll, id); } else { const auto* typeColl = static_cast(coll); - relation = new RelType((*typeColl)[id.index]); + relation = std::make_unique((*typeColl)[id.index]); } } diff --git a/python/templates/Obj.cc.jinja2 b/python/templates/Obj.cc.jinja2 index b127fcce7..357f3a304 100644 --- a/python/templates/Obj.cc.jinja2 +++ b/python/templates/Obj.cc.jinja2 @@ -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 %} } @@ -55,10 +55,6 @@ } {% endif %} {%- endwith %} - -{% for relation in OneToOneRelations %} - delete m_{{ relation.name }}; -{% endfor %} } {%- endif %} diff --git a/python/templates/Obj.h.jinja2 b/python/templates/Obj.h.jinja2 index f7a98ec38..90a491bb9 100644 --- a/python/templates/Obj.h.jinja2 +++ b/python/templates/Obj.h.jinja2 @@ -13,6 +13,9 @@ #include "podio/ObjectID.h" {% if OneToManyRelations or VectorMembers %} #include +{% endif %} +{% if OneToOneRelations %} +#include {%- endif %} {{ utils.forward_decls(forward_declarations_obj) }} @@ -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}; diff --git a/python/templates/macros/implementations.jinja2 b/python/templates/macros/implementations.jinja2 index 530843c48..0dd16adeb 100644 --- a/python/templates/macros/implementations.jinja2 +++ b/python/templates/macros/implementations.jinja2 @@ -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 %} @@ -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 %}