Created ComponentInfo::EnumType which defines which native type to use for enums.

This commit is contained in:
2016-01-19 12:33:53 +01:00
parent a6445c65aa
commit cfe023cc1d
5 changed files with 12 additions and 7 deletions
+3 -1
View File
@@ -5,12 +5,14 @@
struct ComponentInfo
{
typedef int EnumType;
struct Meta_t
{
std::string Annotation;
unsigned int Allocation = 0;
std::map<std::string, std::string> FieldAnnotations;
std::map<std::string, std::map<std::string, int>> FieldEnumDefinitions;
std::map<std::string, std::map<std::string, EnumType>> FieldEnumDefinitions;
};
struct Field_t
+2 -2
View File
@@ -18,7 +18,7 @@ struct ComponentWrapper
const ::EntityID EntityID;
char* Data;
int Enum(const char* fieldName, const char* enumKey)
ComponentInfo::EnumType Enum(const char* fieldName, const char* enumKey)
{
return Info.Meta->FieldEnumDefinitions.at(fieldName).at(enumKey);
}
@@ -58,7 +58,7 @@ struct ComponentWrapper
public:
// Return the integer value of an enum type key for this field
int Enum(const char* enumKey) { return m_Component->Enum(m_PropertyName.c_str(), enumKey); }
ComponentInfo::EnumType Enum(const char* enumKey) { return m_Component->Enum(m_PropertyName.c_str(), enumKey); }
template <typename T>
operator T&() { return m_Component->Field<T>(m_PropertyName); }
+5 -2
View File
@@ -47,7 +47,7 @@ std::size_t EntityFile::GetTypeStride(std::string typeName)
{ "float", sizeof(float) },
{ "double", sizeof(double) },
{ "string", sizeof(std::string) },
{ "enum", sizeof(int) },
{ "enum", sizeof(ComponentInfo::EnumType) },
{ "Vector", sizeof(glm::vec3) },
{ "Quaternion", sizeof(glm::quat) },
{ "Color", sizeof(glm::vec4) }
@@ -88,9 +88,12 @@ void EntityFile::WriteValueData(char* outData, const ComponentInfo::Field_t& fie
{
// Catch and ignore casting errors so whitespace around string enums won't mess anything up
try {
if (field.Type == "int" || field.Type == "enum") {
if (field.Type == "int") {
int value = boost::lexical_cast<int>(valueData);
memcpy(outData, reinterpret_cast<char*>(&value), field.Stride);
} else if (field.Type == "enum") {
ComponentInfo::EnumType value = boost::lexical_cast<ComponentInfo::EnumType>(valueData);
memcpy(outData, reinterpret_cast<char*>(&value), field.Stride);
} else if (field.Type == "float") {
float value = boost::lexical_cast<float>(valueData);
memcpy(outData, reinterpret_cast<char*>(&value), field.Stride);
+1 -1
View File
@@ -146,7 +146,7 @@ void EntityFilePreprocessor::parseComponentInfo()
auto enumElement = xsChoiceParticles->elementAt(i)->getElementTerm();
std::string enumName = XS::ToString(enumElement->getName());
std::string enumValue = XS::ToString(enumElement->getConstraintValue());
compInfo.Meta->FieldEnumDefinitions[name][enumName] = boost::lexical_cast<int>(enumValue);
compInfo.Meta->FieldEnumDefinitions[name][enumName] = boost::lexical_cast<ComponentInfo::EnumType>(enumValue);
LOG_DEBUG("ENUM %s = %s", enumName.c_str(), enumValue.c_str());
}
}
+1 -1
View File
@@ -118,7 +118,7 @@ void EntityFileWriter::appentEntityComponents(xercesc::DOMElement* parentElement
const int& value = c[fieldName];
fieldElement->appendChild(doc->createTextNode(X(boost::lexical_cast<std::string>(value))));
} else if (field.Type == "enum") {
const int& value = c[fieldName];
const ComponentInfo::EnumType& value = c[fieldName];
auto& enumDef = c.Info.Meta->FieldEnumDefinitions.at(fieldName);
for (auto& kv : enumDef) {
if (kv.second == value) {