Program Listing for File CodeRegistry.hpp

Return to documentation for file (engine/include/Cacao/CodeRegistry.hpp)

#pragma once

#include "DllHelper.hpp"
#include "Component.hpp"
#include "Script.hpp"

#include <functional>
#include <memory>
#include <type_traits>
#include <typeindex>

namespace Cacao {
    class CACAO_API CodeRegistry {
      public:
        static CodeRegistry& Get();

        CodeRegistry(const CodeRegistry&) = delete;
        CodeRegistry(CodeRegistry&&) = delete;
        CodeRegistry& operator=(const CodeRegistry&) = delete;
        CodeRegistry& operator=(CodeRegistry&&) = delete;

        struct Impl;

        template<typename T>
            requires std::is_same_v<Component, T> || std::is_same_v<Script, T>
        void RegisterFactory(const std::string& id, std::function<T*()> factory, std::type_index type) = delete;

        template<typename T>
            requires std::is_same_v<Component, T> || std::is_same_v<Script, T>
        std::pair<std::shared_ptr<T>, std::type_index> Instantiate(const std::string& id) = delete;

      private:
        std::unique_ptr<Impl> impl;
        friend class ImplAccessor;

        CodeRegistry();
        ~CodeRegistry();
    };

    //Declare explicitly allowed specializations
    template<>
    void CodeRegistry::RegisterFactory<Component>(const std::string& id, std::function<Component*()> factory, std::type_index type);

    template<>
    void CodeRegistry::RegisterFactory<Script>(const std::string& id, std::function<Script*()> factory, std::type_index type);

    template<>
    std::pair<std::shared_ptr<Component>, std::type_index> CodeRegistry::Instantiate<Component>(const std::string& id);

    template<>
    std::pair<std::shared_ptr<Script>, std::type_index> CodeRegistry::Instantiate<Script>(const std::string& id);
}