Update otml and casts

Improve casts performance
OTML can now understand value escaped sequences
Use long instead of int for lua integer
This commit is contained in:
Eduardo Bart 2012-07-23 01:33:08 -03:00
parent 253a22db3d
commit c9dce51458
6 changed files with 103 additions and 28 deletions

View File

@ -962,10 +962,10 @@ void LuaInterface::pop(int n)
} }
} }
int LuaInterface::popInteger() long LuaInterface::popInteger()
{ {
assert(hasIndex(-1)); assert(hasIndex(-1));
int v = toInteger(-1); long v = toInteger(-1);
pop(); pop();
return v; return v;
} }
@ -1021,7 +1021,7 @@ void LuaInterface::pushNil()
checkStack(); checkStack();
} }
void LuaInterface::pushInteger(int v) void LuaInterface::pushInteger(long v)
{ {
lua_pushinteger(L, v); lua_pushinteger(L, v);
checkStack(); checkStack();

View File

@ -270,7 +270,7 @@ public:
void* newUserdata(int size); void* newUserdata(int size);
void pop(int n = 1); void pop(int n = 1);
int popInteger(); long popInteger();
double popNumber(); double popNumber();
bool popBoolean(); bool popBoolean();
std::string popString(); std::string popString();
@ -279,7 +279,7 @@ public:
LuaObjectPtr popObject(); LuaObjectPtr popObject();
void pushNil(); void pushNil();
void pushInteger(int v); void pushInteger(long v);
void pushNumber(double v); void pushNumber(double v);
void pushBoolean(bool v); void pushBoolean(bool v);
void pushCString(const char* v); void pushCString(const char* v);

View File

@ -112,13 +112,13 @@ bool luavalue_cast(int index, Color& color)
{ {
if(g_lua.isTable(index)) { if(g_lua.isTable(index)) {
g_lua.getField("r", index); g_lua.getField("r", index);
color.setRed(g_lua.popInteger()); color.setRed((int)g_lua.popInteger());
g_lua.getField("g", index); g_lua.getField("g", index);
color.setGreen(g_lua.popInteger()); color.setGreen((int)g_lua.popInteger());
g_lua.getField("b", index); g_lua.getField("b", index);
color.setBlue(g_lua.popInteger()); color.setBlue((int)g_lua.popInteger());
g_lua.getField("a", index); g_lua.getField("a", index);
color.setAlpha(g_lua.popInteger()); color.setAlpha((int)g_lua.popInteger());
return true; return true;
} else if(g_lua.isString()) { } else if(g_lua.isString()) {
return stdext::cast(g_lua.toString(index), color); return stdext::cast(g_lua.toString(index), color);
@ -225,11 +225,20 @@ bool luavalue_cast(int index, Size& size)
void push_otml_subnode_luavalue(const OTMLNodePtr& node) void push_otml_subnode_luavalue(const OTMLNodePtr& node)
{ {
if(node->hasValue()) { if(node->hasValue()) {
// convert boolean types union {
if(node->value() == "true" || node->value() == "false") bool b;
g_lua.pushBoolean(node->value<bool>()); double d;
long l;
};
std::string value = node->rawValue();
if(stdext::cast(value, b))
g_lua.pushBoolean(b);
else if(stdext::cast(value, l))
g_lua.pushInteger(l);
else if(stdext::cast(value, d))
g_lua.pushNumber(d);
else else
g_lua.pushString(node->value()); g_lua.pushString(value);
} else if(node->hasChildren()) { } else if(node->hasChildren()) {
g_lua.newTable(); g_lua.newTable();
bool pushedChild = false; bool pushedChild = false;

View File

@ -150,7 +150,7 @@ bool OTMLNode::replaceChild(const OTMLNodePtr& oldChild, const OTMLNodePtr& newC
void OTMLNode::copy(const OTMLNodePtr& node) void OTMLNode::copy(const OTMLNodePtr& node)
{ {
setTag(node->tag()); setTag(node->tag());
setValue(node->value()); setValue(node->rawValue());
setUnique(node->isUnique()); setUnique(node->isUnique());
setNull(node->isNull()); setNull(node->isNull());
setSource(node->source()); setSource(node->source());

View File

@ -37,6 +37,7 @@ public:
int size() { return m_children.size(); } int size() { return m_children.size(); }
OTMLNodePtr parent() { return m_parent.lock(); } OTMLNodePtr parent() { return m_parent.lock(); }
std::string source() { return m_source; } std::string source() { return m_source; }
std::string rawValue() { return m_value; }
bool isUnique() { return m_unique; } bool isUnique() { return m_unique; }
bool isNull() { return m_null; } bool isNull() { return m_null; }
@ -104,11 +105,25 @@ protected:
#include "otmlexception.h" #include "otmlexception.h"
template<>
inline std::string OTMLNode::value<std::string>() {
std::string value = m_value;
if(boost::starts_with(value, "\"") && boost::ends_with(value, "\"")) {
value = value.substr(1, value.length()-2);
boost::replace_all(value, "\\\\", "\\");
boost::replace_all(value, "\\\"", "\"");
boost::replace_all(value, "\\t", "\t");
boost::replace_all(value, "\\n", "\n");
boost::replace_all(value, "\\'", "\'");
}
return value;
}
template<typename T> template<typename T>
T OTMLNode::value() { T OTMLNode::value() {
T ret; T ret;
if(!stdext::cast(m_value, ret)) if(!stdext::cast(m_value, ret))
throw OTMLException(shared_from_this(), stdext::mkstr("failed to cast node value to type '%s'", stdext::demangle_type<T>())); throw OTMLException(shared_from_this(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type<T>()));
return ret; return ret;
} }

View File

@ -62,20 +62,71 @@ inline bool cast(const std::string& in, std::string& out) {
// special cast from string to boolean // special cast from string to boolean
template<> template<>
inline bool cast(const std::string& in, bool& b) { inline bool cast(const std::string& in, bool& b) {
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}}; if(in == "true")
bool ret = false;
for(int i=0;i<4;++i) {
if(in == validNames[0][i]) {
b = true; b = true;
ret = true; else if(in == "false")
break;
} else if(in == validNames[1][i]) {
b = false; b = false;
ret = true; else
break; return false;
return true;
}
// special cast from string to char
template<>
inline bool cast(const std::string& in, char& c) {
if(in.length() != 1)
return false;
c = in[0];
return true;
}
// special cast from string to long
template<>
inline bool cast(const std::string& in, long& l) {
if(in.find_first_not_of("-0123456789") != std::string::npos)
return false;
std::size_t t = in.find_last_of('-');
if(t != std::string::npos && t != 0)
return false;
l = atol(in.c_str());
return true;
}
// special cast from string to int
template<>
inline bool cast(const std::string& in, int& i) {
long l;
if(cast(in, l)) {
i=l;
return true;
} }
return false;
}
// special cast from string to double
template<>
inline bool cast(const std::string& in, double& d) {
if(in.find_first_not_of("-0123456789.") != std::string::npos)
return false;
std::size_t t = in.find_last_of('-');
if(t != std::string::npos && t != 0)
return false;
t = in.find_first_of('.');
if(t != std::string::npos && (t == 0 || t == in.length()-1 || in.find_first_of('.', t+1) != std::string::npos))
return false;
d = atof(in.c_str());
return true;
}
// special cast from string to float
template<>
inline bool cast(const std::string& in, float& f) {
double d;
if(cast(in, d)) {
f=d;
return true;
} }
return ret; return false;
} }
// special cast from boolean to string // special cast from boolean to string