From b234a231018cb0013f6745a470ef9b5681747ec1 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 13 May 2025 10:07:35 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20redone=20correctly=20ResourceManager=20and=20commentary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/AResource.hpp | 15 ++++++--- includes/server/ResourceManager.hpp | 49 ++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/includes/server/AResource.hpp b/includes/server/AResource.hpp index 43f757e..a8150ed 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/12 19:06:28 by adjoly ### ########.fr */ +/* Updated: 2025/05/13 09:55:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once +#include #include +#include #include #include @@ -25,22 +27,45 @@ namespace server { class ResourceManager { public: - static AClientResource *get(int i) { - for (auto it = range(_res)) { - if ((**it) == i) { - return *it; - } - } - std::stringstream str; - str << "resource not found for client"; - str << i; - _log->debug(str.str()); - return not_nullptr; + /** + * @brief Can be used to get a resource correcponding to the id of the + * resource + * + * @param The id of the resource + * + * @return The resource + * @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); + + if (it != _res.end()) + return (*it); + throw std::out_of_range("resource does not exist for client D:"); } + + /** + * @brief Can be used to append a resource to the ResourceManager + * + * @param The resource to add + */ static void append(AClientResource *new_client) { _res.push_back(new_client); } + /** + * @brief Can be used to remove a resource by giving it it's id + * + * @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()) + delete (*it); + // TODO throw or not - to see + } + protected: private: static std::vector _res;