服务定位器实施

Service Locator implementation

我正在尝试在我的游戏引擎中实施服务定位器设计模式。问题是我无法让它工作。我仍然无法理解转换的真正工作原理。

ServiceLocator.h

class ELK_TOOLS_EXPORT ServiceLocator
{
public:
    template<typename ServiceType>
    static void Provide(ServiceType&& p_service)
    {
        m_services[typeid(ServiceType).hash_code()] = p_service;
    }

    template<typename ServiceType>
    static ServiceType& Get()
    {
        return static_cast<ServiceType&>(m_services[typeid(ServiceType).hash_code()]);
    }

private:
    static std::unordered_map<size_t, ElkAPI::IManager> m_services;
};

我的想法是将服务存储到堆栈中,所以我的 unordered_map 中没有指针或类似的东西。问题是,当我尝试以这种方式将我的服务提供商用于我的引擎时:

EngineManager.cpp

void ElkGameEngine::Managers::EngineManager::Setup()
{
    m_quit = false;

    /* Here I provide to the service locator a new Service (That inherits from IManager) */
    ServiceLocator::Provide<WindowManager>(WindowManager());
    ServiceLocator::Provide<SceneManager>(SceneManager());
    ServiceLocator::Provide<InputManager>(InputManager());
    ServiceLocator::Provide<RenderingManager>(RenderingManager(ServiceLocator::Get<WindowManager>().GetWidth(), ServiceLocator::Get<WindowManager>().GetHeight()));
    ServiceLocator::Provide<PhysicsManager>(PhysicsManager());

    ElkTools::Debug::Log::Process("Engine setup completed", ElkTools::Debug::Log::LogLevel::LOG_INFO);
}

// [...]

void ElkGameEngine::Managers::EngineManager::UpdatePhysics()
{
    PROFILER_SPY("EngineManager::UpdatePhysics");

    /* Here I try to get the service */
    ServiceLocator::Get<PhysicsManager>().ApplyPhysics();
    ServiceLocator::Get<PhysicsManager>().ClearPhysicsEntities();
}

编译器 (VS2017) 告诉我:

Severity    Code    Description Project File    Line    Suppression State
Error   C2259   'ElkAPI::IManager': cannot instantiate abstract class   ElkGameEngine   c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.12.25827\include\utility 292 

编译器输出为:

2>------ Build started: Project: ElkGameEngine, Configuration: Debug x64 ------
2>EngineManager.cpp
2>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.12.25827\include\utility(292): error C2259: 'ElkAPI::IManager': cannot instantiate abstract class
2>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.12.25827\include\utility(292): note: due to following members:
2>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.12.25827\include\utility(292): note: 'void ElkAPI::IManager::Setup(void)': is abstract
2>c:\users\adrie\desktop\group_2\pfa\elkengine\elkapi\include\elkapi\imanager.h(15): note: see declaration of 'ElkAPI::IManager::Setup'
2>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.12.25827\include\utility(292): note: 'void ElkAPI::IManager::Close(void)': is abstract
2>c:\users\adrie\desktop\group_2\pfa\elkengine\elkapi\include\elkapi\imanager.h(16): note: see declaration of 'ElkAPI::IManager::Close'
2>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.12.25827\include\unordered_map(284): note: see reference to class template instantiation 'std::pair<const _Kty,_Ty>' being compiled
2>        with
2>        [
2>            _Kty=::size_t,
2>            _Ty=ElkAPI::IManager
2>        ]
2>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.12.25827\include\unordered_map(283): note: while compiling class template member function 'ElkAPI::IManager &std::unordered_map<::size_t,ElkAPI::IManager,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator [](unsigned __int64 &&)'
2>        with
2>        [
2>            _Kty=::size_t,
2>            _Ty=ElkAPI::IManager
2>        ]
2>c:\users\adrie\desktop\group_2\pfa\elkengine\build\elkrendering\include\elktools\utils\servicelocator.h(28): note: see reference to function template instantiation 'ElkAPI::IManager &std::unordered_map<::size_t,ElkAPI::IManager,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator [](unsigned __int64 &&)' being compiled
2>        with
2>        [
2>            _Kty=::size_t,
2>            _Ty=ElkAPI::IManager
2>        ]
2>c:\users\adrie\desktop\group_2\pfa\elkengine\build\elkrendering\include\elktools\utils\servicelocator.h(28): note: see reference to class template instantiation 'std::unordered_map<::size_t,ElkAPI::IManager,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled
2>        with
2>        [
2>            _Kty=::size_t,
2>            _Ty=ElkAPI::IManager
2>        ]
2>c:\users\adrie\desktop\group_2\pfa\elkengine\elkgameengine\src\managers\enginemanager.cpp(22): note: see reference to function template instantiation 'ServiceType &ElkTools::Utils::ServiceLocator::Get<ElkGameEngine::Managers::WindowManager>(void)' being compiled
2>        with
2>        [
2>            ServiceType=ElkGameEngine::Managers::WindowManager
2>        ]
2>Done building project "ElkGameEngine.vcxproj" -- FAILED.
========== Build: 1 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

我不明白为什么它说 IManager 是一个抽象 class 并被实例化(在错误列表中说)。我没有尝试实例化任何 IManager,我只实例化我的管理器(实现 IManager)。我的经理很好,在尝试实现此服务定位器设计模式之前我一直在使用它们。

- Do you have any idea why I get this error ?
- Is it due to my cast (I'm trying to cast something onto the stack)

注意: 我试图注释掉 EngineManager.cpp 中的提供行,但我仍然得到这个错误,所以我可以说问题出在 Get我的 ServiceLocator 的方法。

我假设 ElkAPI::IManager 实际上是抽象的 class,I... 代表 "Interface",这些通常是抽象的。

您正在尝试维护这些的 C++ 容器 class(映射)。 C++ 容器 class 默认存储实例。因此,如果您向地图添加一些内容,它会尝试构建一个实例并将源的内容复制或移动到其中。这是不可能的,因为地图元素类型是抽象的 - 因此错误!

仅当您使用指向基类型的引用或指针的映射并通过其他方式对指向的对象进行生命周期管理时,才有可能拥有多态对象的映射。

你必须认识到 c++ 与大多数其他语言的根本区别,即 classes 是类似于结构的值类型,因此 class 类型的变量或成员是 copied/moved 分配时。如果只想使用引用,则必须使用指针或引用,并用 * 或 &

指定