#pragma once #include #include /** * Some objects should only exist once but be available to multiple objects (e.g. multiple * models sharing the same VAO and shaders. Any object can register objects here and other * objects can then retrieve them if they have the ID. * * Objects are stored and returned as shared_ptr's, so even if a part of the program deletes * an object from the manager, any part of the program that has the objects stored in them will * not break and still work. But realistically this shouldn't happen in the first place. * As a consequence, even if no objects are using an object stored in here it will continue to * exist. */ template class ObjectManager { public: static ObjectManager& GetInstance() { static ObjectManager instance; return instance; } public: ObjectManager(const ObjectManager&) = delete; void operator=(const ObjectManager&) = delete; /** * Add new (existing) object to manager */ inline void Register(unsigned int id, std::shared_ptr obj) { objects.insert(std::make_pair(id, obj)); } /** * Remove object from manager */ inline void Delete(unsigned int id) { objects.erase(id); } /** * Retrieve object from manager */ inline std::shared_ptr Get(unsigned int id) { auto it = objects.find(id); if (it == objects.end()) return nullptr; return it->second; } private: ObjectManager() {} private: std::map> objects; }; class AbstractShader; class AbstractVertexArrayObject; typedef ObjectManager VAOManager; typedef ObjectManager ShaderManager;