Program Listing for File resourceManager.h

Return to documentation for file (PrismEngine/src/resourceManager.h)

#pragma once
#include "resource.h"
#include <queue>
#include <memory>
#include <unordered_map>
#include <typeindex>
#include <functional>
#include <any>

namespace prism {
    namespace scene {
        class ResourceManager
        {
        public:
            ResourceManager() = default;

            template<typename T>
            void set(T resource) {
                auto typeIndex = std::type_index(typeid(T));

                resources[typeIndex] = std::move(resource);
            }

            template<typename T>
            T* get() {
                auto typeIndex = std::type_index(typeid(T));
                auto it = resources.find(typeIndex);
                return it != resources.end() ? std::any_cast<T>(&it->second) : nullptr;
            }

            template<typename T>
            const T* get() const {
                auto typeIndex = std::type_index(typeid(T));
                auto it = resources.find(typeIndex);
                return it != resources.end() ? std::any_cast<T>(&it->second) : nullptr;
            }

            template<typename T>
            bool has() const {
                auto typeIndex = std::type_index(typeid(T));
                return resources.find(typeIndex) != resources.end();
            }

            template<typename T>
            bool remove() {
                auto typeIndex = std::type_index(typeid(T));
                return resources.erase(typeIndex) > 0;
            }

            void clear() {
                resources.clear();
            }

            ~ResourceManager() {
                clear();
            }

        private:

            std::unordered_map<std::type_index, std::any> resources;
        };
    }
}