diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-01 23:26:26 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-01 23:26:26 +0100 |
commit | f4d71aa6e241ae59ed7d305e8aadddf9d90b2b46 (patch) | |
tree | 06ac7c54936c3ccc3b086428630bc6d1a3399cab | |
parent | 07796bea15a2d5f43766f062379b63fc9e9e1b5d (diff) |
make ListIterator compatible with STL algorithm
-rw-r--r-- | backend/ListIterator.h | 13 | ||||
-rw-r--r-- | backend/ListIterator.hpp | 5 | ||||
-rw-r--r-- | frontend/cmd/use.cpp | 27 |
3 files changed, 33 insertions, 12 deletions
diff --git a/backend/ListIterator.h b/backend/ListIterator.h index 3d11806..cf14724 100644 --- a/backend/ListIterator.h +++ b/backend/ListIterator.h @@ -2,6 +2,8 @@ #include "backend/List.h" +#include <iterator> + #include <stddef.h> template <typename T> @@ -10,12 +12,21 @@ class List; template <typename T> class ListIterator { public: + // required for <algorithm> compatibility + using iterator_category = std::forward_iterator_tag; + using value_type = T; + using difference_type = std::ptrdiff_t; + using pointer = T*; + using reference = T&; + +public: ListIterator(ListLink<T> * & here); public: T & operator * () const; - ListIterator<T> & operator ++ (); bool operator != (const ListIterator<T> &) const; + ListIterator<T> & operator ++ (); + bool operator ! () const; private: ListLink<T> * here = nullptr; diff --git a/backend/ListIterator.hpp b/backend/ListIterator.hpp index 30a556f..45f9acc 100644 --- a/backend/ListIterator.hpp +++ b/backend/ListIterator.hpp @@ -45,3 +45,8 @@ bool ListIterator<T>::operator != (const ListIterator<T> & rhs) const { return this->here != nullptr; } +template <typename T> +bool ListIterator<T>::operator ! () const { + return this->here == nullptr; +} + diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp index 36824b5..c636077 100644 --- a/frontend/cmd/use.cpp +++ b/frontend/cmd/use.cpp @@ -1,3 +1,5 @@ +#include <algorithm> + #include "backend/ConsumableObject.h" #include "backend/Exception.h" #include "backend/print.h" @@ -10,19 +12,22 @@ using namespace std; void GameController::cmd_use(string & target_name) { Player & player = this->dungeon->get_player(); Location & location = player.get_location(); - for (Object * object : player.inventory) { - if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue; + + auto it = find_if(player.inventory.begin(), player.inventory.end(), + [target_name] (Object * object) { + return str_lower(object->get_name().c_str()) == str_lower(target_name); + } + ); - auto consumable = unique_ptr<ConsumableObject>(dynamic_cast<ConsumableObject *>(object)); - if (consumable == nullptr) - throw Exception("%s is niet consumeerbaar", object->get_name().c_str()); + if (!it) + throw Exception("object \"%s\" niet gevonden", target_name.c_str()); - lprtf("Je drinkt %s.\n", object->get_displayname().c_str()); - player.inventory.remove(object); - consumable->consume(player); - return; - } + auto consumable = unique_ptr<ConsumableObject>(dynamic_cast<ConsumableObject *>(*it)); + if (consumable == nullptr) + throw Exception("%s is niet consumeerbaar", (*it)->get_name().c_str()); - throw Exception("object \"%s\" niet gevonden", target_name.c_str()); + lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str()); + player.inventory.remove(consumable.get()); + consumable->consume(player); } |