feat(list): implement copy constructor

This commit is contained in:
fallenoak 2025-11-15 22:00:31 -06:00
parent 48a2315122
commit f46c0c2c4c
2 changed files with 59 additions and 2 deletions

View file

@ -19,14 +19,17 @@ template <class T, class TGetLink>
class TSList { class TSList {
public: public:
// Member variables // Member variables
ptrdiff_t m_linkoffset = 0; ptrdiff_t m_linkoffset;
TSLink<T> m_terminator; TSLink<T> m_terminator;
// Member functions // Member functions
TSList(); TSList();
TSList(const TSList& source);
~TSList(); ~TSList();
void ChangeLinkOffset(ptrdiff_t linkoffset); void ChangeLinkOffset(ptrdiff_t linkoffset);
void Clear(); void Clear();
void Constructor();
void CopyConstructor(const TSList& source);
T* DeleteNode(T* ptr); T* DeleteNode(T* ptr);
T* Head(); T* Head();
void InitializeTerminator(); void InitializeTerminator();
@ -49,7 +52,12 @@ class TSList {
template <class T, class TGetLink> template <class T, class TGetLink>
TSList<T, TGetLink>::TSList() { TSList<T, TGetLink>::TSList() {
this->InitializeTerminator(); this->Constructor();
}
template <class T, class TGetLink>
TSList<T, TGetLink>::TSList(const TSList& source) {
this->CopyConstructor(source);
} }
template <class T, class TGetLink> template <class T, class TGetLink>
@ -74,6 +82,16 @@ void TSList<T, TGetLink>::Clear() {
} }
} }
template <class T, class TGetLink>
void TSList<T, TGetLink>::Constructor() {
this->SetLinkOffset(0);
}
template <class T, class TGetLink>
void TSList<T, TGetLink>::CopyConstructor(const TSList& source) {
this->SetLinkOffset(source.m_linkoffset);
}
template <class T, class TGetLink> template <class T, class TGetLink>
T* TSList<T, TGetLink>::DeleteNode(T* ptr) { T* TSList<T, TGetLink>::DeleteNode(T* ptr) {
T* next = this->Next(ptr); T* next = this->Next(ptr);

View file

@ -5,11 +5,29 @@ struct TestListNode : TSLinkedNode<TestListNode> {
uint32_t index = 0; uint32_t index = 0;
}; };
struct TestExplicitListNode {
uint32_t index = 0;
TSLink<TestExplicitListNode> m_explicitLink;
};
TEST_CASE("TSList", "[list]") { TEST_CASE("TSList", "[list]") {
SECTION("constructs correctly") { SECTION("constructs correctly") {
STORM_LIST(TestListNode) list; STORM_LIST(TestListNode) list;
REQUIRE(list.Head() == nullptr); 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]") { TEST_CASE("TSList::Head", "[list]") {
@ -135,3 +153,24 @@ TEST_CASE("TSList::Tail", "[list]") {
REQUIRE(list.Tail() == nullptr); 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);
}
}