服务定位器实施

时间:2018-05-16 22:46:42

标签: c++ templates design-patterns casting service-locator

我试图在我的游戏引擎中实现服务定位器设计模式。问题是我不能让它发挥作用。我仍然无法理解演员表是如何运作的。

ServiceLocator.h

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();
}

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

EngineManager.cpp

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

编译器(VS2017)这样说:

2>------ Build started: Project: ElkGameEngine, Configuration: Debug x64 ------
2>EngineManager.cpp
2>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\utility(292): error C2259: 'ElkAPI::IManager': cannot instantiate abstract class
2>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\utility(292): note: due to following members:
2>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.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 studio\2017\community\vc\tools\msvc\14.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 studio\2017\community\vc\tools\msvc\14.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 studio\2017\community\vc\tools\msvc\14.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 ==========

编译器输出为:

- 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)

我无法弄清楚为什么它说IManager是一个抽象类并且得到实例化(在错误列表中说)。我没有尝试实例化任何IManager,我只是实例化我的经理(实现IManager)。我的经理很好,在尝试实现这种服务定位器设计模式之前,我正在使用它们。

orderLayupDateSet.getLayupDate()

注意:我试图注释掉EngineManager.cpp中的提供行,但我仍然遇到此错误,所以我可能会说问题来自我的ServiceLocator的Get方法。

1 个答案:

答案 0 :(得分:2)

我认为ElkAPI :: IManager实际上是一个抽象类,I ...代表“接口”,这些通常是抽象的。

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

只有使用引用映射或指向基类型的指针以及生命周期管理指向对象的其他方法时,才能使用多态对象映射。

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

指定