From 60e9ff9cbcacf5faa2ab8ae560e9486cf5772dc7 Mon Sep 17 00:00:00 2001 From: Immanuel Haffner Date: Sun, 25 Feb 2024 12:20:39 +0100 Subject: [PATCH] [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. --- include/mutable/util/Pool.hpp | 5 +++++ unittest/util/PoolTest.cpp | 2 ++ 2 files changed, 7 insertions(+) diff --git a/include/mutable/util/Pool.hpp b/include/mutable/util/Pool.hpp index 38a244e4..16cd0a04 100644 --- a/include/mutable/util/Pool.hpp +++ b/include/mutable/util/Pool.hpp @@ -232,6 +232,11 @@ struct Pooled bool has_value() const requires can_be_none { return ref_ != nullptr; } + Pooled 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. diff --git a/unittest/util/PoolTest.cpp b/unittest/util/PoolTest.cpp index 7144d693..867b7c4b 100644 --- a/unittest/util/PoolTest.cpp +++ b/unittest/util/PoolTest.cpp @@ -151,6 +151,7 @@ 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 @@ -158,6 +159,7 @@ TEST_CASE("PooledOptionalString Utilization", "[core][util][pool]") 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);