From 773e7855b0dc05e834a5b54140362500ebd42ed2 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 13 May 2025 10:14:41 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20cpp=20b?= =?UTF-8?q?e=20like?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/AResource.hpp | 3 ++- includes/server/ResourceManager.hpp | 24 +++++++++++++++++++----- src/main.cpp | 3 ++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/includes/server/AResource.hpp b/includes/server/AResource.hpp index a8150ed..e3112f9 100644 --- a/includes/server/AResource.hpp +++ b/includes/server/AResource.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */ -/* Updated: 2025/05/13 09:55:45 by adjoly ### ########.fr */ +/* Updated: 2025/05/13 10:14:27 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,18 @@ namespace webserv { namespace server { +class CompareId { + public: + CompareId(int id) : _id(id) {} + + bool operator()(const webserv::server::AClientResource *resource) const { + return resource->getId() == _id; + } + + private: + int _id; +}; + class ResourceManager { public: /** @@ -37,7 +49,7 @@ class ResourceManager { * @throw std::out_of_range If the resource does not exist */ static AClientResource *get(int id) { - auto it = std::find(_res.begin(), _res.end(), id); + auto it = std::find_if(_res.begin(), _res.end(), CompareId(id)); if (it != _res.end()) return (*it); @@ -59,10 +71,12 @@ class ResourceManager { * @param The id of the resource to remove */ static void remove(int id) { - auto it = std::find(_res.begin(), _res.end(), id); - - if (it != _res.end()) + auto it = std::find_if(_res.begin(), _res.end(), CompareId(id)); + + if (it != _res.end()) { delete (*it); + _res.erase(it); + } // TODO throw or not - to see } diff --git a/src/main.cpp b/src/main.cpp index b042dd2..9204f96 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou #include #include +#include namespace webserv { Logger *_log = not_nullptr;