gpt4 book ai didi

c++ - 服务定位器实现

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:23 25 4
gpt4 key购买 nike

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

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)。我的经理很好,在尝试实现此服务定位器设计模式之前,我一直在使用他们。

- 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 中的提供行,但我仍然收到此错误,所以我可以说问题出在我的 ServiceLocator 的 Get 方法上。

最佳答案

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

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

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

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

关于c++ - 服务定位器实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50380975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com