我正在考虑移植Java库Artemis。一个实体系统框架。我还没开始呢。相反,我一直在分析如何将java代码放在一起的内部工作。我知道有一半工作了CPP port。移植Java:C++ - >类的类型
我查看了两个代码,并注意到java代码使某些事情更优雅。 而且主要有以下几种:
package com.artemis;
import java.util.HashMap;
public class ComponentTypeManager {
private static HashMap<Class<? extends Component>, ComponentType> componentTypes = new HashMap<Class<? extends Component>, ComponentType>();
public static final ComponentType getTypeFor(Class<? extends Component> c){
ComponentType type = componentTypes.get(c);
if(type == null){
type = new ComponentType();
componentTypes.put(c, type);
}
return type;
}
public static long getBit(Class<? extends Component> c){
return getTypeFor(c).getBit();
}
public static int getId(Class<? extends Component> c){
return getTypeFor(c).getId();
}
}
而组件类型“对象”
package com.artemis;
public class ComponentType {
private static long nextBit = 1;
private static int nextId = 0;
private long bit;
private int id;
public ComponentType() {
init();
}
private void init() {
bit = nextBit;
nextBit = nextBit << 1;
id = nextId++;
}
public long getBit() {
return bit;
}
public int getId() {
return id;
}
}
基本上什么componentTypeManager确实是一个组件类型映射到一个类类型。 这使得它在添加新组件时具有动态性。
的C++端口解决方案如下:
#ifndef __COMPONENT_TYPE_H__
#define __COMPONENT_TYPE_H__
namespace SGF
{
enum ComponentType
{
CT_TRANSFORM = 0,
CT_HEALTH,
CT_RENDERABLE,
CT_RIGID_BODY,
CT_JOINT,
CT_LAST
};
// Component type bits. Used by the entity systems to determine if an entity is compatible.
const unsigned int CT_TRANSFORM_BIT = 1 << CT_TRANSFORM;
const unsigned int CT_HEALTH_BIT = 1 << CT_HEALTH;
const unsigned int CT_RENDERABLE_BIT = 1 << CT_RENDERABLE;
const unsigned int CT_RIGID_BODY_BIT = 1 << CT_RIGID_BODY;
const unsigned int CT_JOINT_BIT = 1 << CT_JOINT;
};
#endif
这里的ComponentManager被完全排除在外。而是使用枚举。 我的问题是,你必须添加新的组件类型到枚举器和常量作为“类型”标识符。 java框架允许你传递一个组件类类型来识别它的id。
我的问题是,我将如何获得类似的效果,将类型映射为像Java代码那样的id,而不用为每个新组件编写枚举类型的硬编码? 我知道C++不支持类类型作为参数。所以这对我来说很让人难以置信。
可能'std :: type_info'用作_class类型arguments_的替代品吗? –
@ K-ballo不代表我必须首先创建我的对象才能读取数据吗? – Sidar