mirror of
https://github.com/edubart/otclient.git
synced 2026-01-17 19:06:22 +01:00
rewrite and reoganize tools functions
* create stdext namespace which contains additional C++ algorithms * organize stdext in string, math, cast and exception utilities
This commit is contained in:
@@ -33,21 +33,21 @@ void LuaException::generateLuaErrorMessage(const std::string& error, int traceLe
|
||||
{
|
||||
// append trace level to error message
|
||||
if(traceLevel >= 0)
|
||||
m_what = Fw::mkstr("LUA ERROR: ", g_lua.traceback(error, traceLevel));
|
||||
m_what = stdext::mkstr("LUA ERROR: ", g_lua.traceback(error, traceLevel));
|
||||
else
|
||||
m_what = Fw::mkstr("LUA ERROR: ", error);
|
||||
m_what = stdext::mkstr("LUA ERROR: ", error);
|
||||
}
|
||||
|
||||
LuaBadNumberOfArgumentsException::LuaBadNumberOfArgumentsException(int expected, int got)
|
||||
{
|
||||
std::string error = "attempt to call a function with wrong number of arguments";
|
||||
if(expected >= 0 && got >= 0)
|
||||
error = Fw::mkstr(error, " (expected ", expected, ", but got ", got, ")");
|
||||
error = stdext::mkstr(error, " (expected ", expected, ", but got ", got, ")");
|
||||
generateLuaErrorMessage(error, 1);
|
||||
}
|
||||
|
||||
LuaBadValueCastException::LuaBadValueCastException(const std::string& luaTypeName, const std::string& cppTypeName)
|
||||
{
|
||||
std::string error = Fw::mkstr("attempt to cast a '", luaTypeName, "' lua value to '", cppTypeName, "'");
|
||||
std::string error = stdext::mkstr("attempt to cast a '", luaTypeName, "' lua value to '", cppTypeName, "'");
|
||||
generateLuaErrorMessage(error, 0);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class LuaException : public Exception
|
||||
class LuaException : public stdext::exception
|
||||
{
|
||||
public:
|
||||
LuaException(const std::string& error, int traceLevel = -1);
|
||||
|
||||
@@ -46,7 +46,7 @@ void LuaInterface::init()
|
||||
createLuaState();
|
||||
|
||||
// check if demangle_type is working as expected
|
||||
assert(Fw::demangleType<LuaObject>() == "LuaObject");
|
||||
assert(stdext::demangle_type<LuaObject>() == "LuaObject");
|
||||
|
||||
// register LuaObject, the base of all other objects
|
||||
registerClass<LuaObject>();
|
||||
@@ -163,12 +163,12 @@ void LuaInterface::registerClassMemberField(const std::string& className,
|
||||
|
||||
if(getFunction) {
|
||||
pushCppFunction(getFunction);
|
||||
setField(Fw::mkstr("get_", field));
|
||||
setField(stdext::mkstr("get_", field));
|
||||
}
|
||||
|
||||
if(setFunction) {
|
||||
pushCppFunction(setFunction);
|
||||
setField(Fw::mkstr("set_", field));
|
||||
setField(stdext::mkstr("set_", field));
|
||||
}
|
||||
|
||||
pop();
|
||||
@@ -310,7 +310,7 @@ void LuaInterface::loadScript(const std::string& fileName)
|
||||
std::string buffer = g_resources.loadFile(filePath);
|
||||
std::string source = "@" + filePath;
|
||||
loadBuffer(buffer, source);
|
||||
} catch(Exception& e) {
|
||||
} catch(stdext::exception& e) {
|
||||
throw LuaException(e.what());
|
||||
}
|
||||
}
|
||||
@@ -324,9 +324,9 @@ void LuaInterface::loadFunction(const std::string& buffer, const std::string& so
|
||||
|
||||
std::string buf;
|
||||
if(boost::starts_with(buffer, "function"))
|
||||
buf = Fw::mkstr("__func = ", buffer);
|
||||
buf = stdext::mkstr("__func = ", buffer);
|
||||
else
|
||||
buf = Fw::mkstr("__func = function(self)\n", buffer,"\nend");
|
||||
buf = stdext::mkstr("__func = function(self)\n", buffer,"\nend");
|
||||
|
||||
loadBuffer(buf, source);
|
||||
safeCall();
|
||||
@@ -343,7 +343,7 @@ void LuaInterface::evaluateExpression(const std::string& expression, const std::
|
||||
{
|
||||
// evaluates the expression
|
||||
if(!expression.empty()) {
|
||||
std::string buffer = Fw::mkstr("__exp = (", expression, ")");
|
||||
std::string buffer = stdext::mkstr("__exp = (", expression, ")");
|
||||
loadBuffer(buffer, source);
|
||||
safeCall();
|
||||
|
||||
@@ -1019,7 +1019,7 @@ void LuaInterface::pushObject(const LuaObjectPtr& obj)
|
||||
new(newUserdata(sizeof(LuaObjectPtr))) LuaObjectPtr(obj);
|
||||
|
||||
// set the userdata metatable
|
||||
getGlobal(Fw::mkstr(obj->getClassName(), "_mt"));
|
||||
getGlobal(stdext::mkstr(obj->getClassName(), "_mt"));
|
||||
assert(!isNil());
|
||||
setMetatable();
|
||||
}
|
||||
|
||||
@@ -64,24 +64,24 @@ public:
|
||||
// register shortcuts using templates
|
||||
template<class C, class B = LuaObject>
|
||||
void registerClass() {
|
||||
registerClass(Fw::demangleType<C>(), Fw::demangleType<B>());
|
||||
registerClass(stdext::demangle_type<C>(), stdext::demangle_type<B>());
|
||||
}
|
||||
|
||||
template<class C>
|
||||
void registerClassStaticFunction(const std::string& functionName, const LuaCppFunction& function) {
|
||||
registerClassStaticFunction(Fw::demangleType<C>(), functionName, function);
|
||||
registerClassStaticFunction(stdext::demangle_type<C>(), functionName, function);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
void registerClassMemberFunction(const std::string& functionName, const LuaCppFunction& function) {
|
||||
registerClassMemberFunction(Fw::demangleType<C>(), functionName, function);
|
||||
registerClassMemberFunction(stdext::demangle_type<C>(), functionName, function);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
void registerClassMemberField(const std::string& field,
|
||||
const LuaCppFunction& getFunction,
|
||||
const LuaCppFunction& setFunction) {
|
||||
registerClassMemberField(Fw::demangleType<C>(), field, getFunction, setFunction);
|
||||
registerClassMemberField(stdext::demangle_type<C>(), field, getFunction, setFunction);
|
||||
}
|
||||
|
||||
// methods for binding functions
|
||||
@@ -388,7 +388,7 @@ template<class T>
|
||||
T LuaInterface::castValue(int index) {
|
||||
T o;
|
||||
if(!luavalue_cast(index, o))
|
||||
throw LuaBadValueCastException(typeName(index), Fw::demangleType<T>());
|
||||
throw LuaBadValueCastException(typeName(index), stdext::demangle_type<T>());
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ public:
|
||||
/// Returns the derived class name, its the same name used in Lua
|
||||
virtual std::string getClassName() const {
|
||||
// TODO: this could be cached for more performance
|
||||
return Fw::demangleName(typeid(*this).name());
|
||||
return stdext::demangle_name(typeid(*this).name());
|
||||
}
|
||||
|
||||
LuaObjectPtr asLuaObject() { return shared_from_this(); }
|
||||
|
||||
@@ -114,7 +114,7 @@ bool luavalue_cast(int index, Color& color)
|
||||
color.setAlpha(g_lua.popInteger());
|
||||
return true;
|
||||
} else if(g_lua.isString()) {
|
||||
return Fw::cast(g_lua.toString(index), color);
|
||||
return stdext::cast(g_lua.toString(index), color);
|
||||
} else if(g_lua.isNil()) {
|
||||
color = Color::white;
|
||||
return true;
|
||||
@@ -149,7 +149,7 @@ bool luavalue_cast(int index, Rect& rect)
|
||||
rect.setHeight(g_lua.popInteger());
|
||||
return true;
|
||||
} else if(g_lua.isString()) {
|
||||
return Fw::cast(g_lua.toString(index), rect);
|
||||
return stdext::cast(g_lua.toString(index), rect);
|
||||
} else if(g_lua.isNil()) {
|
||||
rect = Rect();
|
||||
return true;
|
||||
@@ -176,7 +176,7 @@ bool luavalue_cast(int index, Point& point)
|
||||
point.y = g_lua.popInteger();
|
||||
return true;
|
||||
} else if(g_lua.isString()) {
|
||||
return Fw::cast(g_lua.toString(index), point);
|
||||
return stdext::cast(g_lua.toString(index), point);
|
||||
} else if(g_lua.isNil()) {
|
||||
point = Point();
|
||||
return true;
|
||||
@@ -203,7 +203,7 @@ bool luavalue_cast(int index, Size& size)
|
||||
size.setHeight(g_lua.popInteger());
|
||||
return true;
|
||||
} else if(g_lua.isString()) {
|
||||
return Fw::cast(g_lua.toString(index), size);
|
||||
return stdext::cast(g_lua.toString(index), size);
|
||||
} else if(g_lua.isNil()) {
|
||||
size = Size();
|
||||
return true;
|
||||
@@ -283,7 +283,7 @@ bool luavalue_cast(int index, OTMLNodePtr& node)
|
||||
} else {
|
||||
std::string value;
|
||||
if(g_lua.isBoolean())
|
||||
value = Fw::unsafeCast<std::string>(g_lua.toBoolean());
|
||||
value = stdext::unsafe_cast<std::string>(g_lua.toBoolean());
|
||||
else
|
||||
value = g_lua.toString();
|
||||
if(cnodeName.empty())
|
||||
|
||||
Reference in New Issue
Block a user