From 5c34847218e8d754447f5cf71ed595bbb412eee7 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 29 Oct 2024 23:30:57 +0100 Subject: more WIP --- backend/Dungeon.h | 2 +- backend/List.h | 42 ++++++++++++++++++ backend/List.hpp | 110 +++++++++++++++++++++++++---------------------- backend/ListIterator.h | 38 ++++++++++++++++ backend/ListIterator.hpp | 41 ++++++++++++++++++ backend/Location.cpp | 5 +++ backend/Location.h | 4 +- backend/Object.cpp | 3 ++ backend/Object.h | 1 + 9 files changed, 192 insertions(+), 54 deletions(-) create mode 100644 backend/List.h create mode 100644 backend/ListIterator.h create mode 100644 backend/ListIterator.hpp (limited to 'backend') diff --git a/backend/Dungeon.h b/backend/Dungeon.h index 2d5fa61..5740eb6 100644 --- a/backend/Dungeon.h +++ b/backend/Dungeon.h @@ -1,6 +1,6 @@ #pragma once -#include "List.hpp" +#include "List.h" class Location; diff --git a/backend/List.h b/backend/List.h new file mode 100644 index 0000000..2d3d6b1 --- /dev/null +++ b/backend/List.h @@ -0,0 +1,42 @@ +#pragma once + +#include + +template +class ListRange; + +template +class ListIterator; + +template +struct ListLink { + ListLink * prev; + ListLink * next; + T value; +}; + +template +class List { +public: + size_t size(); + + void push_back(T el); + + void pop_back(); + + void clear(); + + T & operator [] (size_t index); + + ListIterator begin(); + ListIterator end(); + ListRange range(); + +private: + ListLink * head = nullptr; + ListLink * tail = nullptr; + size_t length = 0; +}; + +#include "List.hpp" + diff --git a/backend/List.hpp b/backend/List.hpp index d6c7738..df075f9 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -1,66 +1,72 @@ #pragma once -#include +#include "List.h" template -struct ListLink { - ListLink * prev; - ListLink * next; - T value; -}; +size_t List::size() { + return this->length; +} -template -class List { -public: - size_t size() { return this->length; } +template +void List::push_back(T el) { + ListLink * link = static_cast *>(malloc(sizeof(ListLink))); + link->prev = this->head; + link->next = nullptr; + link->value = el; + if (this->head != nullptr) this->head->next = link; + if (this->head == nullptr) this->tail = link; + this->head = link; + this->length++; +} - void push_back(T el) { - ListLink * link = static_cast *>(malloc(sizeof(ListLink))); - link->prev = this->head; - link->next = nullptr; - link->value = el; - if (this->head != nullptr) this->head->next = link; - if (this->head == nullptr) this->tail = link; - this->head = link; - this->length++; - } +template +void List::pop_back() { + if (this->head == nullptr) return; // empty list - void pop_back() { - if (this->head == nullptr) return; // empty list + ListLink * secondlast = this->head->prev; + if (secondlast != nullptr) + secondlast->next = nullptr; + free(this->head); + if (this->tail == this->head) + this->tail = nullptr; + this->head = secondlast; - ListLink * secondlast = this->head->prev; - if (secondlast != nullptr) - secondlast->next = nullptr; - free(this->head); - if (this->tail == this->head) - this->tail = nullptr; - this->head = secondlast; + this->length--; +} - this->length--; +template +void List::clear() { + ListLink * link = this->tail; + while (link != nullptr) { + ListLink * next = link->next; + free(link); + link = next; } + this->head = nullptr; + this->tail = nullptr; + this->length = 0; +} - void clear() { - ListLink * link = this->tail; - while (link != nullptr) { - ListLink * next = link->next; - free(link); - link = next; - } - this->head = nullptr; - this->tail = nullptr; - this->length = 0; - } +template +T & List::operator [] (size_t index) { + ListLink * link = this->tail; + for (size_t i = 0; i < index; i++) + link = link->next; + return link->value; +} - T & operator [] (size_t index) { - ListLink * link = this->tail; - for (size_t i = 0; i < index; i++) - link = link->next; - return link->value; - } +template +ListRange List::range() { + return { *this }; +} -private: - ListLink * head = nullptr; - ListLink * tail = nullptr; - size_t length = 0; -}; +template +ListIterator List::begin() { + return this->range().begin(); +} + +template +ListIterator List::end() { + return this->range().end(); +} diff --git a/backend/ListIterator.h b/backend/ListIterator.h new file mode 100644 index 0000000..aa78931 --- /dev/null +++ b/backend/ListIterator.h @@ -0,0 +1,38 @@ +#pragma once + +#include "backend/List.h" +#include + +template +class List; + +template +class ListIterator { +public: + ListIterator(List & list, size_t index); + +public: + T operator * () const; + ListIterator & operator ++ (); + bool operator != (const ListIterator &) const; + +private: + List & list; + size_t index; +}; + +template +class ListRange { +public: + ListRange(List & list); + +public: + ListIterator begin() const; + ListIterator end() const; + size_t size() const; + +private: + List & list; +}; + +#include "ListIterator.hpp" diff --git a/backend/ListIterator.hpp b/backend/ListIterator.hpp new file mode 100644 index 0000000..5e4ea91 --- /dev/null +++ b/backend/ListIterator.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "ListIterator.h" + +template +ListRange::ListRange(List & list) : list(list) { } + +template +ListIterator ListRange::begin() const { + return { this->list, 0 }; +} + +template +ListIterator ListRange::end() const { + return { this->list, this->list.size() }; +} + +template +size_t ListRange::size() const { + return this->list.size(); +} + +template +ListIterator::ListIterator(List & list, size_t index) : list(list), index(index) { } + +template +T ListIterator::operator * () const { + return this->list[this->index]; +} + +template +ListIterator & ListIterator::operator ++ () { + this->index++; + return *this; +} + +template +bool ListIterator::operator != (const ListIterator & rhs) const { + return this->index < rhs.index; +} + diff --git a/backend/Location.cpp b/backend/Location.cpp index 8a79af5..96d06ca 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -1,6 +1,7 @@ #include #include "Location.h" +#include "ListIterator.h" #include "util.h" Location::Location(const char * name, const char * description) { @@ -36,3 +37,7 @@ Location * Location::get_exit(Direction dir) { return this->edges[dir]; } +ListRange Location::get_objects() { + return this->objects.range(); +} + diff --git a/backend/Location.h b/backend/Location.h index f102728..8b600bb 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -1,6 +1,7 @@ #pragma once -#include "List.hpp" +#include "List.h" +#include "ListIterator.h" class Enemy; class Object; @@ -21,6 +22,7 @@ public: const char * get_description(); void set_exit(Direction dir, Location * location = nullptr); Location * get_exit(Direction dir); + ListRange get_objects(); protected: Location(const char * name = "", const char * description = ""); diff --git a/backend/Object.cpp b/backend/Object.cpp index e14c780..ed8bc46 100644 --- a/backend/Object.cpp +++ b/backend/Object.cpp @@ -22,6 +22,9 @@ void Object::set_name(const char * name) { const char * Object::get_name() { return this->name; } +const char * Object::get_displayname() { + return this->get_name(); +} void Object::set_description(const char * description) { safe_free(this->description); diff --git a/backend/Object.h b/backend/Object.h index 92652c4..cb2c209 100644 --- a/backend/Object.h +++ b/backend/Object.h @@ -8,6 +8,7 @@ private: public: void set_name(const char * name); const char * get_name(); + virtual const char * get_displayname(); void set_description(const char * description); const char * get_description(); void set_hidden(bool hidden); -- cgit v1.2.3