diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-29 23:30:57 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-29 23:30:57 +0100 |
commit | 5c34847218e8d754447f5cf71ed595bbb412eee7 (patch) | |
tree | 2872948e6d2f566dd66cb1a9b2a5d9900db4c44f /backend | |
parent | a04cb74fee079e3ee43ae5fae32fc2674409822c (diff) |
more WIP
Diffstat (limited to 'backend')
-rw-r--r-- | backend/Dungeon.h | 2 | ||||
-rw-r--r-- | backend/List.h | 42 | ||||
-rw-r--r-- | backend/List.hpp | 110 | ||||
-rw-r--r-- | backend/ListIterator.h | 38 | ||||
-rw-r--r-- | backend/ListIterator.hpp | 41 | ||||
-rw-r--r-- | backend/Location.cpp | 5 | ||||
-rw-r--r-- | backend/Location.h | 4 | ||||
-rw-r--r-- | backend/Object.cpp | 3 | ||||
-rw-r--r-- | backend/Object.h | 1 |
9 files changed, 192 insertions, 54 deletions
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 <stdlib.h> + +template <typename T> +class ListRange; + +template <typename T> +class ListIterator; + +template <typename T> +struct ListLink { + ListLink<T> * prev; + ListLink<T> * next; + T value; +}; + +template<typename T> +class List { +public: + size_t size(); + + void push_back(T el); + + void pop_back(); + + void clear(); + + T & operator [] (size_t index); + + ListIterator<T> begin(); + ListIterator<T> end(); + ListRange<T> range(); + +private: + ListLink<T> * head = nullptr; + ListLink<T> * 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 <stdlib.h> +#include "List.h" template <typename T> -struct ListLink { - ListLink<T> * prev; - ListLink<T> * next; - T value; -}; +size_t List<T>::size() { + return this->length; +} -template<typename T> -class List { -public: - size_t size() { return this->length; } +template <typename T> +void List<T>::push_back(T el) { + ListLink<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>))); + 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<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>))); - 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 <typename T> +void List<T>::pop_back() { + if (this->head == nullptr) return; // empty list - void pop_back() { - if (this->head == nullptr) return; // empty list + ListLink<T> * 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<T> * 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 <typename T> +void List<T>::clear() { + ListLink<T> * link = this->tail; + while (link != nullptr) { + ListLink<T> * next = link->next; + free(link); + link = next; } + this->head = nullptr; + this->tail = nullptr; + this->length = 0; +} - void clear() { - ListLink<T> * link = this->tail; - while (link != nullptr) { - ListLink<T> * next = link->next; - free(link); - link = next; - } - this->head = nullptr; - this->tail = nullptr; - this->length = 0; - } +template <typename T> +T & List<T>::operator [] (size_t index) { + ListLink<T> * link = this->tail; + for (size_t i = 0; i < index; i++) + link = link->next; + return link->value; +} - T & operator [] (size_t index) { - ListLink<T> * link = this->tail; - for (size_t i = 0; i < index; i++) - link = link->next; - return link->value; - } +template <typename T> +ListRange<T> List<T>::range() { + return { *this }; +} -private: - ListLink<T> * head = nullptr; - ListLink<T> * tail = nullptr; - size_t length = 0; -}; +template <typename T> +ListIterator<T> List<T>::begin() { + return this->range().begin(); +} + +template <typename T> +ListIterator<T> List<T>::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 <cstddef> + +template <typename T> +class List; + +template <typename T> +class ListIterator { +public: + ListIterator(List<T> & list, size_t index); + +public: + T operator * () const; + ListIterator<T> & operator ++ (); + bool operator != (const ListIterator<T> &) const; + +private: + List<T> & list; + size_t index; +}; + +template <typename T> +class ListRange { +public: + ListRange(List<T> & list); + +public: + ListIterator<T> begin() const; + ListIterator<T> end() const; + size_t size() const; + +private: + List<T> & 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 <typename T> +ListRange<T>::ListRange(List<T> & list) : list(list) { } + +template <typename T> +ListIterator<T> ListRange<T>::begin() const { + return { this->list, 0 }; +} + +template <typename T> +ListIterator<T> ListRange<T>::end() const { + return { this->list, this->list.size() }; +} + +template <typename T> +size_t ListRange<T>::size() const { + return this->list.size(); +} + +template <typename T> +ListIterator<T>::ListIterator(List<T> & list, size_t index) : list(list), index(index) { } + +template <typename T> +T ListIterator<T>::operator * () const { + return this->list[this->index]; +} + +template <typename T> +ListIterator<T> & ListIterator<T>::operator ++ () { + this->index++; + return *this; +} + +template <typename T> +bool ListIterator<T>::operator != (const ListIterator<T> & 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 <string.h> #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<Object *> 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<Object *> 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); |