mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-22 18:15:31 +01:00
Core/Utils: unique_trackable_ptr improvements
* Added comparison operators * Added type casting helper functions
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user