Unified some previously duplicated code

This commit is contained in:
2016-01-07 14:06:14 +01:00
parent 03456ad731
commit ee83394678
3 changed files with 107 additions and 129 deletions
+42 -45
View File
@@ -2,6 +2,7 @@
#define EntityFile_h__ #define EntityFile_h__
#include <stack> #include <stack>
#include <boost/lexical_cast.hpp>
#include <xercesc/util/XercesDefs.hpp> #include <xercesc/util/XercesDefs.hpp>
#include <xercesc/util/PlatformUtils.hpp> #include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax2/SAX2XMLReader.hpp> #include <xercesc/sax2/SAX2XMLReader.hpp>
@@ -245,57 +246,53 @@ private:
public: public:
static std::size_t GetTypeStride(std::string typeName); static std::size_t GetTypeStride(std::string typeName);
static void WriteElementData(const xercesc::DOMElement* element, std::string typeName, char* outData)
{
using namespace xercesc;
if (typeName == "Vector") { static void WriteAttributeData(char* outData, const std::string fieldType, const std::map<std::string, std::string>& attributes)
{
if (fieldType == "Vector") {
glm::vec3 vec; glm::vec3 vec;
vec.x = getFloatAttribute(element, "X"); vec.x = boost::lexical_cast<float>(attributes.at("X"));
vec.y = getFloatAttribute(element, "Y"); vec.y = boost::lexical_cast<float>(attributes.at("Y"));
vec.z = getFloatAttribute(element, "Z"); vec.z = boost::lexical_cast<float>(attributes.at("Z"));
memcpy(outData, reinterpret_cast<char*>(&vec), GetTypeStride(typeName)); memcpy(outData, reinterpret_cast<char*>(&vec), EntityFile::GetTypeStride(fieldType));
} else if (typeName == "Color") { } else if (fieldType == "Color") {
glm::vec4 vec; glm::vec4 vec;
vec.r = getFloatAttribute(element, "R"); vec.r = boost::lexical_cast<float>(attributes.at("R"));
vec.g = getFloatAttribute(element, "G"); vec.g = boost::lexical_cast<float>(attributes.at("G"));
vec.b = getFloatAttribute(element, "B"); vec.b = boost::lexical_cast<float>(attributes.at("B"));
vec.a = getFloatAttribute(element, "A"); vec.a = boost::lexical_cast<float>(attributes.at("A"));
memcpy(outData, reinterpret_cast<char*>(&vec), GetTypeStride(typeName)); memcpy(outData, reinterpret_cast<char*>(&vec), EntityFile::GetTypeStride(fieldType));
} else if (typeName == "Quaternion") { } else if (fieldType == "Quaternion") {
glm::quat q; glm::quat q;
q.x = getFloatAttribute(element, "X"); q.x = boost::lexical_cast<float>(attributes.at("X"));
q.y = getFloatAttribute(element, "Y"); q.y = boost::lexical_cast<float>(attributes.at("Y"));
q.z = getFloatAttribute(element, "Z"); q.z = boost::lexical_cast<float>(attributes.at("Z"));
q.w = getFloatAttribute(element, "W"); q.w = boost::lexical_cast<float>(attributes.at("W"));
memcpy(outData, reinterpret_cast<char*>(&q), GetTypeStride(typeName)); memcpy(outData, reinterpret_cast<char*>(&q), EntityFile::GetTypeStride(fieldType));
} else if (typeName == "float") { } else if (!attributes.empty()) {
XSValue::Status status; LOG_WARNING("%i attributes not handled by any type conversion!", attributes.size());
XSValue* val = XSValue::getActualValue(element->getTextContent(), xercesc::XSValue::DataType::dt_float, status);
memcpy(outData, reinterpret_cast<char*>(&val->fData.fValue.f_float), GetTypeStride(typeName));
} else if (typeName == "double") {
XSValue::Status status;
XSValue* val = XSValue::getActualValue(element->getTextContent(), xercesc::XSValue::DataType::dt_double, status);
memcpy(outData, reinterpret_cast<char*>(&val->fData.fValue.f_double), GetTypeStride(typeName));
} else if (typeName == "bool") {
XSValue::Status status;
XSValue* val = XSValue::getActualValue(element->getTextContent(), xercesc::XSValue::DataType::dt_boolean, status);
memcpy(outData, reinterpret_cast<char*>(&val->fData.fValue.f_bool), GetTypeStride(typeName));
} else {
XSValue::DataType dataType = XSValue::getDataType(XS::ToXMLCh(typeName));
if (dataType == XSValue::DataType::dt_string) {
char* str = XMLString::transcode(element->getTextContent());
std::string standardString(str);
new (outData) std::string(str);
XMLString::release(&str);
//memcpy(outData, reinterpret_cast<char*>(&standardString), getTypeStride(typeName));
} else {
//XSValue::Status status;
//XSValue* val = XSValue::getActualValue(element->getTextContent(), dataType, status);
//memcpy(outData, reinterpret_cast<char*>(&val->fData.fValue), getTypeStride(typeName));
LOG_WARNING("Unknown native data type: %s", typeName.c_str());
} }
} }
static void WriteValueData(char* outData, const std::string fieldType, const char* valueData)
{
if (fieldType == "int") {
int value = boost::lexical_cast<int>(valueData);
memcpy(outData, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "float") {
float value = boost::lexical_cast<float>(valueData);
memcpy(outData, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "double") {
double value = boost::lexical_cast<double>(valueData);
memcpy(outData, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "bool") {
bool value = (valueData[0] == 't'); // Lazy bool evaluation
memcpy(outData, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "string") {
new (outData) std::string(valueData);
} else {
LOG_WARNING("Unknown value data type: %s", fieldType.c_str());
}
} }
void Parse(const EntityFileHandler* handler); void Parse(const EntityFileHandler* handler);
+2 -40
View File
@@ -55,29 +55,7 @@ private:
} }
char* data = component.Data + component.Info.FieldOffsets.at(fieldName); char* data = component.Data + component.Info.FieldOffsets.at(fieldName);
if (fieldType == "Vector") { EntityFile::WriteAttributeData(data, fieldType, attributes);
glm::vec3 vec;
vec.x = boost::lexical_cast<float>(attributes.at("X"));
vec.y = boost::lexical_cast<float>(attributes.at("Y"));
vec.z = boost::lexical_cast<float>(attributes.at("Z"));
memcpy(data, reinterpret_cast<char*>(&vec), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "Color") {
glm::vec4 vec;
vec.r = boost::lexical_cast<float>(attributes.at("R"));
vec.g = boost::lexical_cast<float>(attributes.at("G"));
vec.b = boost::lexical_cast<float>(attributes.at("B"));
vec.a = boost::lexical_cast<float>(attributes.at("A"));
memcpy(data, reinterpret_cast<char*>(&vec), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "Quaternion") {
glm::quat q;
q.x = boost::lexical_cast<float>(attributes.at("X"));
q.y = boost::lexical_cast<float>(attributes.at("Y"));
q.z = boost::lexical_cast<float>(attributes.at("Z"));
q.w = boost::lexical_cast<float>(attributes.at("W"));
memcpy(data, reinterpret_cast<char*>(&q), EntityFile::GetTypeStride(fieldType));
} else if (!attributes.empty()) {
LOG_WARNING("%i attributes not handled by any type conversion!", attributes.size());
}
} }
void onFieldData(EntityID entity, std::string componentType, std::string fieldName, const char* fieldData) void onFieldData(EntityID entity, std::string componentType, std::string fieldName, const char* fieldData)
@@ -87,22 +65,6 @@ private:
std::string fieldType = component.Info.FieldTypes.at(fieldName); std::string fieldType = component.Info.FieldTypes.at(fieldName);
char* data = component.Data + component.Info.FieldOffsets.at(fieldName); char* data = component.Data + component.Info.FieldOffsets.at(fieldName);
if (fieldType == "int") { EntityFile::WriteValueData(data, fieldType, fieldData);
int value = boost::lexical_cast<int>(fieldData);
memcpy(data, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "float") {
float value = boost::lexical_cast<float>(fieldData);
memcpy(data, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "double") {
double value = boost::lexical_cast<double>(fieldData);
memcpy(data, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "bool") {
bool value = (fieldData[0] == 't'); // Lazy bool evaluation
memcpy(data, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
} else if (fieldType == "string") {
new (data) std::string(fieldData);
} else {
LOG_WARNING("Unknown data type: %s", fieldType.c_str());
}
} }
}; };
+20 -1
View File
@@ -219,7 +219,26 @@ void parseDefaults()
std::string fieldType = ci.second.FieldTypes.at(fieldName); std::string fieldType = ci.second.FieldTypes.at(fieldName);
unsigned int fieldOffset = ci.second.FieldOffsets.at(fieldName); unsigned int fieldOffset = ci.second.FieldOffsets.at(fieldName);
EntityFile::WriteElementData(fieldElement, fieldType, ci.second.Defaults.get() + fieldOffset); char* data = ci.second.Defaults.get() + fieldOffset;
// Handle potential field attributes
if (fieldElement->hasAttributes()) {
std::map<std::string, std::string> attributes;
auto attributeMap = fieldElement->getAttributes();
for (int i = 0; i < attributeMap->getLength(); ++i) {
auto attribItem = attributeMap->item(i);
attributes[XS::ToString(attribItem->getNodeName())] = XS::ToString(attribItem->getNodeValue());
}
EntityFile::WriteAttributeData(data, fieldType, attributes);
}
// Handle potential field values
auto childNode = fieldElement->getFirstChild();
if (childNode != nullptr && childNode->getNodeType() == DOMNode::TEXT_NODE) {
char* cstrValue = XMLString::transcode(childNode->getNodeValue());
EntityFile::WriteValueData(data, fieldType, cstrValue);
XMLString::release(&cstrValue);
}
} }
} }
} }