From 967bf6b161b7082e4e68f4a79330039cfc9c5569 Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Sun, 26 Dec 2021 19:38:22 +0100 Subject: [PATCH] reworked objectmanager --- CMakeLists.txt | 3 +- include/lol/Shader.hpp | 2 - include/lol/Texture.hpp | 2 - include/lol/VertexArrayObject.hpp | 3 -- include/lol/util/Exceptions.hpp | 16 +++++++ include/lol/util/ObjectManager.hpp | 69 +++++++++--------------------- src/ObjectManager.cpp | 33 ++++++++++++++ 7 files changed, 71 insertions(+), 57 deletions(-) create mode 100644 include/lol/util/Exceptions.hpp create mode 100644 src/ObjectManager.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b80c26f..6bf3c1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,8 @@ add_library(lol STATIC "src/Buffer.cpp" "src/buffers/VertexBuffer.cpp" "src/buffers/ElementBuffer.cpp" - "src/Image.cpp") + "src/Image.cpp" + "src/ObjectManager.cpp") target_include_directories(lol PUBLIC ${GLM_INCLUDE_DIRS} diff --git a/include/lol/Shader.hpp b/include/lol/Shader.hpp index 31ea750..8c28e96 100644 --- a/include/lol/Shader.hpp +++ b/include/lol/Shader.hpp @@ -82,6 +82,4 @@ namespace lol private: unsigned int id; }; - - typedef ObjectManager ShaderManager; } \ No newline at end of file diff --git a/include/lol/Texture.hpp b/include/lol/Texture.hpp index 5806ece..7e627f1 100644 --- a/include/lol/Texture.hpp +++ b/include/lol/Texture.hpp @@ -28,8 +28,6 @@ namespace lol unsigned int id; }; - typedef ObjectManager TextureManager; - class Texture2D : public Texture { diff --git a/include/lol/VertexArrayObject.hpp b/include/lol/VertexArrayObject.hpp index 1a99230..2c5cd9f 100644 --- a/include/lol/VertexArrayObject.hpp +++ b/include/lol/VertexArrayObject.hpp @@ -36,7 +36,4 @@ namespace lol std::shared_ptr elementBuffer; }; - // Object manager for managing said shared pointers - typedef ObjectManager VAOManager; - } \ No newline at end of file diff --git a/include/lol/util/Exceptions.hpp b/include/lol/util/Exceptions.hpp new file mode 100644 index 0000000..4fd9ad1 --- /dev/null +++ b/include/lol/util/Exceptions.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +#define TYPENAME(x) #x + +namespace lol +{ + class ObjectNotFoundException : public std::runtime_error + { + public: + ObjectNotFoundException(unsigned int id) : + std::runtime_error("Failed to Get() object with ID " + std::to_string(id) + " from ObjectManager. It does not exist.") + { } + }; +} \ No newline at end of file diff --git a/include/lol/util/ObjectManager.hpp b/include/lol/util/ObjectManager.hpp index 442418f..fed00b2 100644 --- a/include/lol/util/ObjectManager.hpp +++ b/include/lol/util/ObjectManager.hpp @@ -3,6 +3,9 @@ #include #include +#include +#include + namespace lol { @@ -17,75 +20,43 @@ namespace lol * As a consequence, even if no objects are using an object stored in here it will continue to * exist. */ - template class ObjectManager : public NonCopyable { public: - static ObjectManager& GetInstance() - { - static ObjectManager instance; - return instance; - } + ObjectManager() {} - /** - * Add new (existing) object to manager - */ - inline void Register(unsigned int id, const std::shared_ptr& obj) + template + std::shared_ptr Create(unsigned int id, Args... args) { - objects.insert(std::make_pair(id, obj)); + std::shared_ptr object = std::make_shared(args...); + objects.insert({id, object}); + + return object; } /** * Remove object from manager */ - inline void Delete(unsigned int id) - { - objects.erase(id); - } + void Delete(unsigned int id); /** * Retrieve object from manager */ - inline std::shared_ptr Get(unsigned int id) + std::shared_ptr Get(unsigned int id); + + template + std::shared_ptr Get(unsigned int id) { - auto it = objects.find(id); - if (it == objects.end()) - return nullptr; - - return it->second; + std::shared_ptr object = Get(id); + return std::static_pointer_cast(object); } - inline void CleanupUnused() - { - for(auto& [key, val] : objects) - { - if (val.use_count() < 3) - objects.erase(key); - } - } - - inline void Cleanup() - { - objects.clear(); - } - - inline void Return(unsigned int id) - { - auto it = objects.find(id); - - if (it == objects.end()) - return; - - if (it->second.use_count() < 3) // I hope I don't forget about this if I ever go multithreaded - objects.erase(it); - } + void ClearUnused(); + void Clear(); private: - ObjectManager() {} - - private: - std::map> objects; + std::map> objects; }; } \ No newline at end of file diff --git a/src/ObjectManager.cpp b/src/ObjectManager.cpp new file mode 100644 index 0000000..b05da61 --- /dev/null +++ b/src/ObjectManager.cpp @@ -0,0 +1,33 @@ +#include + +namespace lol +{ + std::shared_ptr ObjectManager::Get(unsigned int id) + { + auto it = objects.find(id); + + if(it == objects.end()) + throw ObjectNotFoundException(id); + + return it->second; + } + + void ObjectManager::Delete(unsigned int id) + { + objects.erase(id); + } + + void ObjectManager::ClearUnused() + { + for(const auto& [key, value] : objects) + { + if(value.unique()) + objects.erase(key); + } + } + + void ObjectManager::Clear() + { + objects.clear(); + } +} \ No newline at end of file