Skip to content

Commit

Permalink
Fix: mutable to immutable object conversion with python bindings (AID…
Browse files Browse the repository at this point in the history
…ASoft#564)

* added python bindings test for mutable object conversion

* replaced object conversion function with converting constructor

* ignore emplace_back clang-tidy suggestion in test
  • Loading branch information
m-fila authored Feb 24, 2024
1 parent 4851698 commit d12cf45
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
33 changes: 33 additions & 0 deletions python/podio/test_CodeGen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""cppyy python binding tests"""

import unittest
import ROOT
from ROOT import ExampleMCCollection, MutableExampleMC


class ObjectConversionsTest(unittest.TestCase):
"""Object conversion binding tests"""

def test_conversion_mutable_to_immutable(self):
ROOT.gInterpreter.Declare(
"""
void test_accepts_immutable(ExampleMC) {}
"""
)
accepts_immutable = ROOT.test_accepts_immutable
mutable_obj = MutableExampleMC()
accepts_immutable(mutable_obj)


class OneToManyRelationsTest(unittest.TestCase):
"""OneToManyRelations binding tests"""

def test_add(self):
particles = ExampleMCCollection()
parent_particle = particles.create()
daughter_particle = particles.create()

self.assertEqual(len(daughter_particle.parents()), 0)
daughter_particle.addparents(parent_particle)
self.assertEqual(len(daughter_particle.parents()), 1)
1 change: 0 additions & 1 deletion python/templates/MutableObject.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
{{ utils.namespace_open(class.namespace) }}

{{ macros.constructors_destructors(class.bare_type, Members, prefix='Mutable') }}
Mutable{{ class.bare_type }}::operator {{ class.bare_type }}() const { return {{ class.bare_type }}(m_obj); }

{{ macros.member_getters(class, Members, use_get_syntax, prefix='Mutable') }}
{{ macros.single_relation_getters(class, OneToOneRelations, use_get_syntax, prefix='Mutable') }}
Expand Down
3 changes: 0 additions & 3 deletions python/templates/MutableObject.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public:

{{ macros.constructors_destructors(class.bare_type, Members, prefix='Mutable') }}

/// conversion to const object
operator {{ class.bare_type }}() const;

public:

{{ macros.member_getters(Members, use_get_syntax) }}
Expand Down
2 changes: 2 additions & 0 deletions python/templates/Object.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

{{ macros.constructors_destructors(class.bare_type, Members) }}

{{ class.bare_type }}::{{ class.bare_type }}(const Mutable{{ class.bare_type }}& other): {{ class.bare_type }}(other.m_obj) {}

{{ class.bare_type }}::{{ class.bare_type }}({{ class.bare_type }}Obj* obj) : m_obj(podio::utils::MaybeSharedPtr<{{ class.bare_type }}Obj>(obj)) {}

{{ class.bare_type }} {{ class.bare_type }}::makeEmpty() {
Expand Down
2 changes: 2 additions & 0 deletions python/templates/Object.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public:
using collection_type = {{ class.bare_type }}Collection;

{{ macros.constructors_destructors(class.bare_type, Members) }}
/// converting constructor from mutable object
{{ class.bare_type }}(const Mutable{{ class.bare_type }}& other);

static {{ class.bare_type }} makeEmpty();

Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ TEST_CASE("Container lifetime", "[basics][memory-management]") {
{
MutableExampleHit hit;
hit.energy(3.14f);
hits.push_back(hit);
hits.push_back(hit); // NOLINT(modernize-use-emplace)
}
auto hit = hits[0];
REQUIRE(hit.energy() == 3.14f);
Expand Down

0 comments on commit d12cf45

Please sign in to comment.