Skip to content

Commit 60e9ff9

Browse files
ImmanuelHaffneralir3zakh
authored andcommitted
[Util] Add Pooled::assert_not_none().
Converts an optional to a non-optional `Pooled`. In a debug build, asserts that a value is indeed present.
1 parent 1fff390 commit 60e9ff9

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

include/mutable/util/Pool.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ struct Pooled
232232

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

235+
Pooled<T, Pool, false> assert_not_none() const requires can_be_none {
236+
M_insist(has_value());
237+
return { pool_, ref_ };
238+
}
239+
235240
/**
236241
* Returns the number of references to the pooled object or 0 if
237242
* this `Pooled` CanBeNone and does *not* hold a reference to an object.

unittest/util/PoolTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,15 @@ TEST_CASE("PooledOptionalString Utilization", "[core][util][pool]")
151151
PooledOptionalString ps0{ pool("ps0") };
152152
REQUIRE(pool.size() == 1);
153153
REQUIRE(ps0.has_value());
154+
CHECK(*ps0.assert_not_none() == "ps0"sv);
154155
REQUIRE(ps0.count() == 1);
155156

156157
// copy constructing a non-empty PooledOptionalString should not affect the pool
157158
PooledOptionalString ps1{ ps0 };
158159
REQUIRE(pool.size() == 1);
159160
REQUIRE(ps0.has_value());
160161
REQUIRE(ps1.has_value());
162+
CHECK(*ps1.assert_not_none() == "ps0"sv);
161163
REQUIRE(ps0 == ps1);
162164
REQUIRE(ps0.count() == 2);
163165
REQUIRE(ps1.count() == 2);

0 commit comments

Comments
 (0)