Skip to content

Commit

Permalink
[Util] Add Pooled::assert_not_none().
Browse files Browse the repository at this point in the history
Converts an optional to a non-optional `Pooled`.  In a debug build,
asserts that a value is indeed present.
  • Loading branch information
ImmanuelHaffner authored and alir3zakh committed Feb 28, 2024
1 parent 1fff390 commit 60e9ff9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/mutable/util/Pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ struct Pooled

bool has_value() const requires can_be_none { return ref_ != nullptr; }

Pooled<T, Pool, false> assert_not_none() const requires can_be_none {
M_insist(has_value());
return { pool_, ref_ };
}

/**
* Returns the number of references to the pooled object or 0 if
* this `Pooled` CanBeNone and does *not* hold a reference to an object.
Expand Down
2 changes: 2 additions & 0 deletions unittest/util/PoolTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ TEST_CASE("PooledOptionalString Utilization", "[core][util][pool]")
PooledOptionalString ps0{ pool("ps0") };
REQUIRE(pool.size() == 1);
REQUIRE(ps0.has_value());
CHECK(*ps0.assert_not_none() == "ps0"sv);
REQUIRE(ps0.count() == 1);

// copy constructing a non-empty PooledOptionalString should not affect the pool
PooledOptionalString ps1{ ps0 };
REQUIRE(pool.size() == 1);
REQUIRE(ps0.has_value());
REQUIRE(ps1.has_value());
CHECK(*ps1.assert_not_none() == "ps0"sv);
REQUIRE(ps0 == ps1);
REQUIRE(ps0.count() == 2);
REQUIRE(ps1.count() == 2);
Expand Down

0 comments on commit 60e9ff9

Please sign in to comment.