diff --git a/python/templates/macros/implementations.jinja2 b/python/templates/macros/implementations.jinja2 index 9a6b7659e..af847145d 100644 --- a/python/templates/macros/implementations.jinja2 +++ b/python/templates/macros/implementations.jinja2 @@ -147,6 +147,9 @@ bool {{ full_type }}::operator==(const {{ inverse_type }}& other) const { {% macro ostream_operator(type, members, single_relations, multi_relations, get_syntax) %} std::ostream& operator<<(std::ostream& o, const {{ type }}& value) { + if (!value.isAvailable()) { + return o << "[not available]"; + } o << " id: " << value.id() << '\n'; {% for member in members %} {% if member.is_array %} diff --git a/tests/unittests/unittest.cpp b/tests/unittests/unittest.cpp index f29771d95..215e84149 100644 --- a/tests/unittests/unittest.cpp +++ b/tests/unittests/unittest.cpp @@ -88,6 +88,15 @@ TEST_CASE("Assignment-operator ref count", "[basics][memory-management]") { } } +TEST_CASE("ostream-operator", "[basics]") { + // Make sure that trying to print an object that is not available does not crash + auto hit = ExampleHit::makeEmpty(); + REQUIRE_FALSE(hit.isAvailable()); + std::stringstream sstr; + sstr << hit; + REQUIRE(sstr.str() == "[not available]"); +} + TEST_CASE("Clearing", "[UBSAN-FAIL][ASAN-FAIL][THREAD-FAIL][basics][memory-management]") { auto store = podio::EventStore(); auto& hits = store.create("hits");