mirror of
https://github.com/thunderbrewhq/squall.git
synced 2026-02-03 16:39:08 +00:00
feat(list): implement copy constructor
This commit is contained in:
parent
48a2315122
commit
f46c0c2c4c
2 changed files with 59 additions and 2 deletions
|
|
@ -19,14 +19,17 @@ template <class T, class TGetLink>
|
|||
class TSList {
|
||||
public:
|
||||
// Member variables
|
||||
ptrdiff_t m_linkoffset = 0;
|
||||
ptrdiff_t m_linkoffset;
|
||||
TSLink<T> 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 <class T, class TGetLink>
|
||||
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>
|
||||
|
|
@ -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>
|
||||
T* TSList<T, TGetLink>::DeleteNode(T* ptr) {
|
||||
T* next = this->Next(ptr);
|
||||
|
|
|
|||
|
|
@ -5,11 +5,29 @@ struct TestListNode : TSLinkedNode<TestListNode> {
|
|||
uint32_t index = 0;
|
||||
};
|
||||
|
||||
struct TestExplicitListNode {
|
||||
uint32_t index = 0;
|
||||
TSLink<TestExplicitListNode> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue