From f8d8d7499ba4433678db2a68fb1cae74448ca31e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 31 Oct 2024 14:14:09 +0100 Subject: make ListIterator continue working on a changing list --- backend/List.h | 10 ++++++---- backend/List.hpp | 8 ++++---- backend/ListIterator.h | 16 ++++++++-------- backend/ListIterator.hpp | 26 ++++++++++++++++---------- backend/Location.cpp | 6 +++--- backend/Location.h | 6 +++--- backend/PtrList.hpp | 2 +- frontend/cmd/get.cpp | 4 ++-- frontend/cmd/search.cpp | 5 +---- 9 files changed, 44 insertions(+), 39 deletions(-) diff --git a/backend/List.h b/backend/List.h index 1af0b35..3bdbb23 100644 --- a/backend/List.h +++ b/backend/List.h @@ -10,6 +10,7 @@ class ListIterator; template struct ListLink { + friend class ListIterator; ListLink * prev; ListLink * next; T value; @@ -24,7 +25,7 @@ public: public: size_t size() const; - void push_back(T & el); + void push_back(const T & el); void remove(const T & val); void pop_back(); @@ -33,11 +34,12 @@ public: T & operator [] (size_t index) const; - ListIterator begin() const; - ListIterator end() const; - ListRange range() const; + ListIterator begin(); + ListIterator end(); + ListRange range(); private: + friend class ListRange; ListLink * head = nullptr; ListLink * tail = nullptr; size_t length = 0; diff --git a/backend/List.hpp b/backend/List.hpp index 25e7a56..634f14b 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -8,7 +8,7 @@ size_t List::size() const { } template -void List::push_back(T & el) { +void List::push_back(const T & el) { ListLink * link = new ListLink { .prev = this->head, .next = nullptr, @@ -78,17 +78,17 @@ T & List::operator [] (size_t index) const { } template -ListRange List::range() const { +ListRange List::range() { return { *this }; } template -ListIterator List::begin() const { +ListIterator List::begin() { return this->range().begin(); } template -ListIterator List::end() const { +ListIterator List::end() { return this->range().end(); } diff --git a/backend/ListIterator.h b/backend/ListIterator.h index c7fbf4a..3d11806 100644 --- a/backend/ListIterator.h +++ b/backend/ListIterator.h @@ -10,30 +10,30 @@ class List; template class ListIterator { public: - ListIterator(const List & list, size_t index); + ListIterator(ListLink * & here); public: - T operator * () const; + T & operator * () const; ListIterator & operator ++ (); bool operator != (const ListIterator &) const; private: - const List & list; - size_t index; + ListLink * here = nullptr; + ListLink * next = nullptr; }; template class ListRange { public: - ListRange(const List & list); + ListRange(List & list); public: - ListIterator begin() const; - ListIterator end() const; + ListIterator begin(); + ListIterator end(); size_t size() const; private: - const List & list; + List & list; }; #include "ListIterator.hpp" diff --git a/backend/ListIterator.hpp b/backend/ListIterator.hpp index b6b7a36..30a556f 100644 --- a/backend/ListIterator.hpp +++ b/backend/ListIterator.hpp @@ -3,16 +3,16 @@ #include "ListIterator.h" template -ListRange::ListRange(const List & list) : list(list) { } +ListRange::ListRange(List & list) : list(list) { } template -ListIterator ListRange::begin() const { - return { this->list, 0 }; +ListIterator ListRange::begin() { + return { this->list.tail }; } template -ListIterator ListRange::end() const { - return { this->list, this->list.size() }; +ListIterator ListRange::end() { + return { this->list.head }; } template @@ -21,21 +21,27 @@ size_t ListRange::size() const { } template -ListIterator::ListIterator(const List & list, size_t index) : list(list), index(index) { } +ListIterator::ListIterator(ListLink * & here) { + this->here = here; + if (this->here != nullptr) + this->next = this->here->next; +} template -T ListIterator::operator * () const { - return this->list[this->index]; +T & ListIterator::operator * () const { + return this->here->value; } template ListIterator & ListIterator::operator ++ () { - this->index++; + this->here = this->next; + if (this->here != nullptr) + this->next = this->here->next; return *this; } template bool ListIterator::operator != (const ListIterator & rhs) const { - return this->index < rhs.index; + return this->here != nullptr; } diff --git a/backend/Location.cpp b/backend/Location.cpp index bfd2107..c036c7b 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -39,7 +39,7 @@ void Location::add_visible_object(Object * object) { void Location::remove_visible_object(Object * object) { this->visible_objects.remove(object); } -ListRange Location::get_visible_objects() const { +ListRange Location::get_visible_objects() { return this->visible_objects.range(); } @@ -49,7 +49,7 @@ void Location::add_hidden_object(Object * object) { void Location::remove_hidden_object(Object * object) { this->hidden_objects.remove(object); } -ListRange Location::get_hidden_objects() const { +ListRange Location::get_hidden_objects() { return this->hidden_objects.range(); } @@ -68,7 +68,7 @@ void Location::add_enemy(Enemy * enemy) { void Location::remove_enemy(Enemy * enemy) { this->enemies.remove(enemy); } -ListRange Location::get_enemies() const { +ListRange Location::get_enemies() { return this->enemies.range(); } diff --git a/backend/Location.h b/backend/Location.h index 3a1f5ac..d29c117 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -32,18 +32,18 @@ public: void add_visible_object(Object *); void remove_visible_object(Object *); - ListRange get_visible_objects() const; + ListRange get_visible_objects(); void add_hidden_object(Object *); void remove_hidden_object(Object *); - ListRange get_hidden_objects() const; + ListRange get_hidden_objects(); void hide_object(Object *); void unhide_object(Object *); void add_enemy(Enemy *); void remove_enemy(Enemy *); - ListRange get_enemies() const; + ListRange get_enemies(); private: friend class LocationFactory; diff --git a/backend/PtrList.hpp b/backend/PtrList.hpp index 96f2643..cbfd6e0 100644 --- a/backend/PtrList.hpp +++ b/backend/PtrList.hpp @@ -4,7 +4,7 @@ template PtrList::~PtrList() { - for (T * obj : *this) + for (T * & obj : *this) delete obj; } diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp index 0d6d12c..359a5c5 100644 --- a/frontend/cmd/get.cpp +++ b/frontend/cmd/get.cpp @@ -12,13 +12,13 @@ FollowupAction Player::cmd_get(string & target_name) { for (Object * object : this->location.get_visible_objects()) { if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue; target = unique_ptr(object); + this->location.remove_visible_object(object); break; } if (target == nullptr) { lprtf("Object \"%s\" niet gevonden.\n", target_name.c_str()); return FollowupAction::NONE; } - this->location.remove_visible_object(target.get()); // gold objects are collected and (implicitly) destroyed GoldObject * gold = dynamic_cast(target.get()); @@ -30,7 +30,7 @@ FollowupAction Player::cmd_get(string & target_name) { } // other objects go in the inventory - lprtf("Je voegt %s toe aan je bezit.\n", target->get_displayname().c_str()); + lprtf("Je voegt %s toe aan je bezit.\n", target->get_name().c_str()); this->inventory.push_back(std::move(target)); return FollowupAction::NONE; } diff --git a/frontend/cmd/search.cpp b/frontend/cmd/search.cpp index 16e4592..f10709a 100644 --- a/frontend/cmd/search.cpp +++ b/frontend/cmd/search.cpp @@ -7,17 +7,14 @@ using namespace std; FollowupAction Player::cmd_search(string &) { bool found = false; - List to_unhide; for (Object * object : this->location.get_hidden_objects()) { if (!found) lprtf("Je vindt:\n"); lprtf("- %s\n", object->get_displayname().c_str()); - to_unhide.push_back(object); + this->location.unhide_object(object); found = true; } if (!found) lprtf("Je hebt niks gevonden.\n"); - for (Object * object : to_unhide) - this->location.unhide_object(object); return FollowupAction::UPDATE; } -- cgit v1.2.3