Skip to content

Commit

Permalink
use enable_if to allow using containers of interfaces with object wra…
Browse files Browse the repository at this point in the history
…ppers (#683)

* check container of interfaces move constructor and direct initialization

* use enable_if in interface constructor

* test elements of moved container of interfaces

* bring back allowed types comment
  • Loading branch information
m-fila authored Sep 30, 2024
1 parent 8651fdd commit 2064274
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/templates/Interface.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ private:
std::unique_ptr<Concept> m_self{nullptr};

public:
template<typename ValueT>
// {{ class.bare_type }} can only be initialized with one of the following types (and their Mutable counter parts): {{ Types | join(", ") }}
template<typename ValueT, typename = std::enable_if_t<isInitializableFrom<ValueT>>>
{{ class.bare_type }}(ValueT value) :
m_self(std::make_unique<Model<podio::detail::GetDefaultHandleType<ValueT>>>(value)) {
static_assert(isInitializableFrom<ValueT>, "{{ class.bare_type }} can only be initialized with one of the following types (and their Mutable counter parts): {{ Types | join(", ") }}");
}

{{ class.bare_type }}(const {{ class.bare_type }}& other) :
Expand Down
14 changes: 14 additions & 0 deletions tests/unittests/interface_types.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#include "catch2/catch_test_macros.hpp"

#include "datamodel/ExampleHit.h"
#include "datamodel/ExampleHitCollection.h"
#include "datamodel/MutableExampleCluster.h"
#include "datamodel/MutableExampleMC.h"
#include "datamodel/TypeWithEnergy.h"

#include "podio/ObjectID.h"
#include "podio/utilities/TypeHelpers.h"

#include <map>
#include <stdexcept>
#include <utility>
#include <vector>

TEST_CASE("InterfaceTypes basic functionality", "[interface-types][basics]") {
using WrapperT = TypeWithEnergy;
Expand Down Expand Up @@ -67,6 +71,16 @@ TEST_CASE("InterfaceTypes STL usage", "[interface-types][basics]") {
REQUIRE(counterMap[empty] == 1);
REQUIRE(counterMap[hit] == 2);
REQUIRE(counterMap[wrapper] == 2);

auto mc = MutableExampleMC{};
mc.energy() = 3.14f;

// check container of interfaces move constructor and direct initialization
auto interfaces = std::vector<TypeWithEnergy>{empty, hit, mc};
auto interfaces2 = std::vector<TypeWithEnergy>{std::move(interfaces)};
REQUIRE_FALSE(interfaces2.at(0).isAvailable());
REQUIRE(interfaces2.at(1).isA<ExampleHit>());
REQUIRE(interfaces2.at(2).energy() == 3.14f);
}

TEST_CASE("InterfaceType from immutable", "[interface-types][basics]") {
Expand Down

0 comments on commit 2064274

Please sign in to comment.