C++ 创建具有未知模板类型列表的库
不久前,我开始创作游戏,我遇到了一种我非常喜欢的风格。它被称为基于组件的体系结构。本教程是针对Objto-C,而不是C++,但我也包括了链接: 我注意到我从一个项目到另一个项目复制和粘贴了很多代码,所以我决定只为通用框架创建一个库。通常,组件包含在实体对象中,如下所示:C++ 创建具有未知模板类型列表的库,c++,templates,wrapper,component-based,C++,Templates,Wrapper,Component Based,不久前,我开始创作游戏,我遇到了一种我非常喜欢的风格。它被称为基于组件的体系结构。本教程是针对Objto-C,而不是C++,但我也包括了链接: 我注意到我从一个项目到另一个项目复制和粘贴了很多代码,所以我决定只为通用框架创建一个库。通常,组件包含在实体对象中,如下所示: #ifndef ENTITY_H #define ENTITY_H #include "HealthComponent.h" #include "MoveComponent.h" #include "PlayerCompon
#ifndef ENTITY_H
#define ENTITY_H
#include "HealthComponent.h"
#include "MoveComponent.h"
#include "PlayerComponent.h"
#include "RenderComponent.h"
class Entity
{
public:
Entity();
HealthComponent* getHealth();
//Returns a reference to the object's health component
//If there is none, returns a null pointer
MoveComponent* getMove();
//Returns a reference to the object's movement component
//If there is none, returns a null pointer
PlayerComponent* getPlayer();
//Returns a reference to the object's player component
//If there is none, returns a null pointer
RenderComponent* getRender();
//Returns a reference to the object's render component
//If there is none, returns a null pointer
~Entity();
private:
HealthComponent* health;
MoveComponent* move;
PlayerComponent* player;
RenderComponent* render;
};
#endif //ENTITY_H
由于我的库将包含通用框架,而不是单个组件,因此此方法不再有效。相反,我创建了一个ComponentManager
类来管理我的组件。当前,实体如下所示:
#ifndef ENTITY_H
#define ENTITY_H
#include "Component.h"
#include "ComponentManager.h"
#include <list>
class Entity
{
public:
Entity();
Entity(ComponentManager const & cmanager);
template <T>
T* getComponent();
//Returns the first component of the specified type
//If there is none, returns NULL
template <T>
T* getComponent(int i);
//Returns the ith component of the specified type
//If the component does not exist, returns NULL
template<T>
int getComponentCount();
//Returns the number of components of the specific type that the
//entity contains
template <T>
int addComponent(T &component);
//Adds a component of the specified type to the class. If a component of the
//class already exists, the component is assigned a number, and the number is returned.
//If no components of the class exist, 0 is returned
~Entity();
private:
int eid;
std::list<ComponentReference> components;
ComponentManager * manager;
};
#endif //ENTITY
\ifndef实体
#定义实体
#包括“组件h”
#包括“ComponentManager.h”
#包括
类实体
{
公众:
实体();
实体(组件管理器const和cmanager);
模板
T*getComponent();
//返回指定类型的第一个组件
//如果没有,则返回NULL
模板
T*getComponent(inti);
//返回指定类型的第i个组件
//如果组件不存在,则返回NULL
模板
int getComponentCount();
//返回指定的特定类型的组件数
//实体包含
模板
内部添加组件(T&component);
//将指定类型的组件添加到类中。如果
//类已存在,组件被分配了一个编号,并返回该编号。
//如果不存在该类的任何组件,则返回0
~Entity();
私人:
int eid;
std::列出组件;
组件管理器*管理器;
};
#endif//实体
我还没有定义这些函数,所以我没有包括实现文件
ComponentReference
struct只是用来定位特定组件的int和字符串的集合。实体将组件引用
传递给管理器
中的模板化函数,该函数将在其内部列表中搜索特定对象。还有那堵砖墙。我不知道如何创建一个包含未知数量的不同未知类型对象的列表。它们派生自父组件
类,但它们都包含不同的函数和成员变量。我试图使用指针数组和类型转换,但三个小时后,当我读到过度依赖类型转换是代码编写糟糕的一个指标时,我放弃了
我试着使用STL容器,但它们只需要一种类型,所以显然它们不起作用
()
我看到了一些关于包装器的东西,但从我所读到的,它们似乎只与函数一起工作:()如果它们可以用来解决我的问题,请解释它们是如何工作的(我今天以前从未听说过它们)
因为这是我的第一个问题,请随时留下反馈。我做了研究,但如果这是重复的,我很抱歉。我不是C++新手,但我也不是专家,而且是一门大语言,所以我可以要求澄清。提前谢谢 看起来像是在
std::type_index
上键入的映射与类型为boost::any
的映射值的组合可能会起作用。。。