我正在考慮移植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