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

use enable_if to allow using containers of interfaces with object wrappers #683

Merged
merged 7 commits into from
Sep 30, 2024
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
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(", ") }}");
m-fila marked this conversation as resolved.
Show resolved Hide resolved
}

{{ 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