From f46c0c2c4c6120a844db7335a00a751ec0685ba3 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Sat, 15 Nov 2025 22:00:31 -0600 Subject: [PATCH] feat(list): implement copy constructor --- storm/list/TSList.hpp | 22 ++++++++++++++++++++-- test/List.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/storm/list/TSList.hpp b/storm/list/TSList.hpp index 440027b..c4dd4ca 100644 --- a/storm/list/TSList.hpp +++ b/storm/list/TSList.hpp @@ -19,14 +19,17 @@ template class TSList { public: // Member variables - ptrdiff_t m_linkoffset = 0; + ptrdiff_t m_linkoffset; TSLink m_terminator; // Member functions TSList(); + TSList(const TSList& source); ~TSList(); void ChangeLinkOffset(ptrdiff_t linkoffset); void Clear(); + void Constructor(); + void CopyConstructor(const TSList& source); T* DeleteNode(T* ptr); T* Head(); void InitializeTerminator(); @@ -49,7 +52,12 @@ class TSList { template TSList::TSList() { - this->InitializeTerminator(); + this->Constructor(); +} + +template +TSList::TSList(const TSList& source) { + this->CopyConstructor(source); } template @@ -74,6 +82,16 @@ void TSList::Clear() { } } +template +void TSList::Constructor() { + this->SetLinkOffset(0); +} + +template +void TSList::CopyConstructor(const TSList& source) { + this->SetLinkOffset(source.m_linkoffset); +} + template T* TSList::DeleteNode(T* ptr) { T* next = this->Next(ptr); diff --git a/test/List.cpp b/test/List.cpp index 972bc5c..1d1a793 100644 --- a/test/List.cpp +++ b/test/List.cpp @@ -5,11 +5,29 @@ struct TestListNode : TSLinkedNode { uint32_t index = 0; }; +struct TestExplicitListNode { + uint32_t index = 0; + TSLink m_explicitLink; +}; + TEST_CASE("TSList", "[list]") { SECTION("constructs correctly") { STORM_LIST(TestListNode) list; REQUIRE(list.Head() == nullptr); } + + SECTION("constructs copy correctly") { + STORM_LIST(TestListNode) list; + auto node = new TestListNode(); + list.LinkToHead(node); + + STORM_LIST(TestListNode) listCopy(list); + + // List copy constructor does not transfer ownership of nodes. It merely copies the link + // offset from the source list and initializes the list terminator. + REQUIRE(list.Head() == node); + REQUIRE(listCopy.Head() == nullptr); + } } TEST_CASE("TSList::Head", "[list]") { @@ -135,3 +153,24 @@ TEST_CASE("TSList::Tail", "[list]") { REQUIRE(list.Tail() == nullptr); } } + +TEST_CASE("TSExplicitList", "[list]") { + SECTION("constructs correctly") { + STORM_EXPLICIT_LIST(TestExplicitListNode, m_explicitLink) list; + REQUIRE(list.Head() == nullptr); + } + + SECTION("constructs copy correctly") { + STORM_EXPLICIT_LIST(TestExplicitListNode, m_explicitLink) list; + auto node = new TestExplicitListNode(); + list.LinkToHead(node); + + STORM_EXPLICIT_LIST(TestExplicitListNode, m_explicitLink) listCopy(list); + + // Explicit list copy constructor does not transfer ownership of nodes. It merely calls + // the copy constructor on the list base class, which only copies the link offset from + // the source list and initializes the list terminator. + REQUIRE(list.Head() == node); + REQUIRE(listCopy.Head() == nullptr); + } +}