Skip to content

Commit

Permalink
Make it safe to print empty handles
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Nov 20, 2023
1 parent 31b1314 commit 337c86c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/templates/macros/implementations.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
9 changes: 9 additions & 0 deletions tests/unittests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExampleHitCollection>("hits");
Expand Down

0 comments on commit 337c86c

Please sign in to comment.