Я пытаюсь внедрить шаблон дизайна сервисного локатора в мой игровой движок.Проблема в том, что я не могу заставить его работать.У меня все еще есть проблемы с пониманием того, как на самом деле работает приведение.
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 studio\2017\community\vc\tools\msvc\14.12.25827\include\utility 292
И вывод компилятора:
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 ==========
Я не могу понять, почему он говорит, что IManager является абстрактным классом и его экземпляр создается (указано в списке ошибок),Я не пытаюсь создать экземпляр любого IManager, я только создаю экземпляры своих менеджеров (которые реализуют IManager).Мои менеджеры хороши, я использовал их, прежде чем пытаться реализовать этот шаблон проектирования Service Locator.
- 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.