Core/Utils: unique_trackable_ptr improvements

* Added comparison operators
* Added type casting helper functions
This commit is contained in:
Shauren
2024-03-13 17:04:26 +01:00
parent 6b255efb2d
commit f690b69338
2 changed files with 301 additions and 17 deletions

View File

@@ -23,7 +23,7 @@ struct TestObj
{
TestObj(bool* deleted = nullptr) : Deleted(deleted) { }
~TestObj()
virtual ~TestObj()
{
if (Deleted)
*Deleted = true;
@@ -32,6 +32,21 @@ struct TestObj
bool* Deleted = nullptr;
};
struct TestObj2
{
virtual ~TestObj2() = default;
int a = 5;
};
struct TestObj3 : public TestObj2, public TestObj
{
};
struct TestObj4 : public TestObj
{
};
TEST_CASE("Trinity::unique_trackable_ptr frees memory", "[UniqueTrackablePtr]")
{
bool deleted = false;
@@ -88,3 +103,52 @@ TEST_CASE("Trinity::unique_weak_ptr", "[UniqueTrackablePtr]")
REQUIRE(!!weakRef.lock());
}
}
TEST_CASE("Trinity::unique_strong_ref_ptr type casts", "[UniqueTrackablePtr]")
{
Trinity::unique_trackable_ptr<TestObj> ptr = Trinity::make_unique_trackable<TestObj3>();
Trinity::unique_weak_ptr<TestObj> weak = ptr;
Trinity::unique_strong_ref_ptr<TestObj> temp = weak.lock();
REQUIRE(temp != nullptr);
SECTION("static_pointer_cast")
{
Trinity::unique_strong_ref_ptr<TestObj3> testObj2 = Trinity::static_pointer_cast<TestObj3>(temp);
REQUIRE(testObj2.get() == static_cast<TestObj3*>(ptr.get()));
// sanity check that we didn't accidentally setup inheritance of TestObjs incorrectly
REQUIRE(testObj2.get() != reinterpret_cast<TestObj3*>(ptr.get()));
REQUIRE(testObj2 == Trinity::static_pointer_cast<TestObj3>(weak).lock());
}
SECTION("reinterpret_pointer_cast")
{
Trinity::unique_strong_ref_ptr<TestObj3> testObj2 = Trinity::reinterpret_pointer_cast<TestObj3>(temp);
REQUIRE(testObj2.get() == reinterpret_cast<TestObj3*>(ptr.get()));
REQUIRE(testObj2 == Trinity::reinterpret_pointer_cast<TestObj3>(weak).lock());
}
SECTION("succeeding dynamic_pointer_cast")
{
Trinity::unique_strong_ref_ptr<TestObj3> testObj2 = Trinity::dynamic_pointer_cast<TestObj3>(temp);
REQUIRE(testObj2.get() == dynamic_cast<TestObj3*>(ptr.get()));
REQUIRE(testObj2 == Trinity::dynamic_pointer_cast<TestObj3>(weak).lock());
}
SECTION("failing dynamic_pointer_cast")
{
Trinity::unique_strong_ref_ptr<TestObj4> testObj2 = Trinity::dynamic_pointer_cast<TestObj4>(temp);
REQUIRE(testObj2 == nullptr);
REQUIRE(testObj2 == Trinity::dynamic_pointer_cast<TestObj4>(weak).lock());
}
}