mirror of
https://github.com/edubart/otclient.git
synced 2025-10-17 04:53:27 +02:00
Rework stdext classes
Implement new classes: * stdext::any => ligher replacement for boost::any * stdext::packed_any => like any but optimized to use less memory * stdext::shared_object => ligher replacement for std::shared_ptr * stdext::shared_object_ptr => replacement for boost::intrusive_ptr * stdext::fast_storage => for storing dynamic data * stdext::packed_storage => same but with less memory * stdext::packed_vector => std::vector with less memory Compiling should be a little faster now because global boost including is not needed anymore
This commit is contained in:
@@ -25,11 +25,13 @@
|
||||
|
||||
#include <framework/global.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class LuaInterface;
|
||||
class LuaObject;
|
||||
|
||||
typedef std::function<int(LuaInterface*)> LuaCppFunction;
|
||||
typedef std::unique_ptr<LuaCppFunction> LuaCppFunctionPtr;
|
||||
typedef boost::intrusive_ptr<LuaObject> LuaObjectPtr;
|
||||
typedef stdext::shared_object_ptr<LuaObject> LuaObjectPtr;
|
||||
|
||||
#endif
|
||||
|
@@ -27,6 +27,9 @@
|
||||
#include "luainterface.h"
|
||||
#include "luaexception.h"
|
||||
|
||||
#include <framework/stdext/traits.h>
|
||||
#include <tuple>
|
||||
|
||||
/// This namespace contains some dirty metaprogamming that uses a lot of C++0x features
|
||||
/// The purpose here is to create templates that can bind any function from C++
|
||||
/// and expose in lua environment. This is done combining variadic templates,
|
||||
@@ -36,12 +39,6 @@
|
||||
/// pushes the result to lua.
|
||||
namespace luabinder
|
||||
{
|
||||
/// Removes const references, transforming 'const T&' into 'T'
|
||||
template<typename T>
|
||||
struct remove_const_ref {
|
||||
typedef typename std::remove_const<typename std::remove_reference<T>::type>::type type;
|
||||
};
|
||||
|
||||
/// Pack arguments from lua stack into a tuple recursively
|
||||
template<int N>
|
||||
struct pack_values_into_tuple {
|
||||
@@ -117,8 +114,8 @@ namespace luabinder
|
||||
/// Bind a std::function
|
||||
template<typename Ret, typename... Args>
|
||||
LuaCppFunction bind_fun(const std::function<Ret(Args...)>& f) {
|
||||
typedef typename std::tuple<typename remove_const_ref<Args>::type...> Tuple;
|
||||
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
||||
typedef typename std::tuple<typename stdext::remove_const_ref<Args>::type...> Tuple;
|
||||
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
|
||||
decltype(f),
|
||||
Tuple>(f);
|
||||
}
|
||||
@@ -130,8 +127,8 @@ namespace luabinder
|
||||
template<typename Lambda, typename Ret, typename... Args>
|
||||
struct bind_lambda_fun<Ret(Lambda::*)(Args...) const> {
|
||||
static LuaCppFunction call(const Lambda& f) {
|
||||
typedef typename std::tuple<typename remove_const_ref<Args>::type...> Tuple;
|
||||
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
||||
typedef typename std::tuple<typename stdext::remove_const_ref<Args>::type...> Tuple;
|
||||
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
|
||||
decltype(f),
|
||||
Tuple>(f);
|
||||
|
||||
@@ -152,18 +149,18 @@ namespace luabinder
|
||||
|
||||
/// Create member function lambdas
|
||||
template<typename Ret, typename C, typename... Args>
|
||||
std::function<Ret(const boost::intrusive_ptr<C>&, const Args&...)> make_mem_func(Ret (C::* f)(Args...)) {
|
||||
std::function<Ret(const stdext::shared_object_ptr<C>&, const Args&...)> make_mem_func(Ret (C::* f)(Args...)) {
|
||||
auto mf = std::mem_fn(f);
|
||||
return [=](const boost::intrusive_ptr<C>& obj, const Args&... args) mutable -> Ret {
|
||||
return [=](const stdext::shared_object_ptr<C>& obj, const Args&... args) mutable -> Ret {
|
||||
if(!obj)
|
||||
throw LuaException("failed to call a member function because the passed object is nil");
|
||||
return mf(obj.get(), args...);
|
||||
};
|
||||
}
|
||||
template<typename C, typename... Args>
|
||||
std::function<void(const boost::intrusive_ptr<C>&, const Args&...)> make_mem_func(void (C::* f)(Args...)) {
|
||||
std::function<void(const stdext::shared_object_ptr<C>&, const Args&...)> make_mem_func(void (C::* f)(Args...)) {
|
||||
auto mf = std::mem_fn(f);
|
||||
return [=](const boost::intrusive_ptr<C>& obj, const Args&... args) mutable -> void {
|
||||
return [=](const stdext::shared_object_ptr<C>& obj, const Args&... args) mutable -> void {
|
||||
if(!obj)
|
||||
throw LuaException("failed to call a member function because the passed object is nil");
|
||||
mf(obj.get(), args...);
|
||||
@@ -186,9 +183,9 @@ namespace luabinder
|
||||
/// Bind member functions
|
||||
template<typename C, typename Ret, class FC, typename... Args>
|
||||
LuaCppFunction bind_mem_fun(Ret (FC::* f)(Args...)) {
|
||||
typedef typename std::tuple<boost::intrusive_ptr<FC>, typename remove_const_ref<Args>::type...> Tuple;
|
||||
typedef typename std::tuple<stdext::shared_object_ptr<FC>, typename stdext::remove_const_ref<Args>::type...> Tuple;
|
||||
auto lambda = make_mem_func<Ret,FC>(f);
|
||||
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
||||
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
|
||||
decltype(lambda),
|
||||
Tuple>(lambda);
|
||||
}
|
||||
@@ -196,10 +193,10 @@ namespace luabinder
|
||||
/// Bind singleton member functions
|
||||
template<typename C, typename Ret, class FC, typename... Args>
|
||||
LuaCppFunction bind_singleton_mem_fun(Ret (FC::*f)(Args...), C *instance) {
|
||||
typedef typename std::tuple<typename remove_const_ref<Args>::type...> Tuple;
|
||||
typedef typename std::tuple<typename stdext::remove_const_ref<Args>::type...> Tuple;
|
||||
assert(instance);
|
||||
auto lambda = make_mem_func_singleton<Ret,FC>(f, static_cast<FC*>(instance));
|
||||
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
||||
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
|
||||
decltype(lambda),
|
||||
Tuple>(lambda);
|
||||
}
|
||||
@@ -209,7 +206,7 @@ namespace luabinder
|
||||
LuaCppFunction bind_mem_fun(int (C::*f)(LuaInterface*)) {
|
||||
auto mf = std::mem_fn(f);
|
||||
return [=](LuaInterface* lua) -> int {
|
||||
auto obj = lua->castValue<boost::intrusive_ptr<C>>(1);
|
||||
auto obj = lua->castValue<stdext::shared_object_ptr<C>>(1);
|
||||
lua->remove(1);
|
||||
return mf(obj, lua);
|
||||
};
|
||||
|
@@ -325,7 +325,7 @@ void LuaInterface::loadScript(const std::string& fileName)
|
||||
{
|
||||
// resolve file full path
|
||||
std::string filePath = fileName;
|
||||
if(!boost::starts_with(fileName, "/"))
|
||||
if(!stdext::starts_with(fileName, "/"))
|
||||
filePath = getCurrentSourcePath() + "/" + filePath;
|
||||
|
||||
std::string buffer = g_resources.loadFile(fileName);
|
||||
@@ -341,7 +341,7 @@ void LuaInterface::loadFunction(const std::string& buffer, const std::string& so
|
||||
}
|
||||
|
||||
std::string buf;
|
||||
if(boost::starts_with(buffer, "function"))
|
||||
if(stdext::starts_with(buffer, "function"))
|
||||
buf = stdext::format("__func = %s", buffer);
|
||||
else
|
||||
buf = stdext::format("__func = function(self)\n%s\nend", buffer);
|
||||
@@ -553,7 +553,7 @@ int LuaInterface::luaScriptLoader(lua_State* L)
|
||||
g_lua.loadScript(fileName);
|
||||
return 1;
|
||||
} catch(stdext::exception& e) {
|
||||
g_lua.pushString(stdext::mkstr("\n\t", e.what()));
|
||||
g_lua.pushString(std::string("\n\t") + e.what());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -561,7 +561,7 @@ int LuaInterface::luaScriptLoader(lua_State* L)
|
||||
int LuaInterface::lua_dofile(lua_State* L)
|
||||
{
|
||||
std::string fileName = g_lua.popString();
|
||||
if(!boost::ends_with(fileName, ".lua"))
|
||||
if(!stdext::ends_with(fileName, ".lua"))
|
||||
fileName += ".lua";
|
||||
|
||||
try {
|
||||
@@ -580,7 +580,7 @@ int LuaInterface::lua_dofiles(lua_State* L)
|
||||
std::string directory = g_lua.popString();
|
||||
|
||||
for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) {
|
||||
if(!boost::ends_with(fileName, ".lua"))
|
||||
if(!stdext::ends_with(fileName, ".lua"))
|
||||
continue;
|
||||
|
||||
try {
|
||||
@@ -597,7 +597,7 @@ int LuaInterface::lua_dofiles(lua_State* L)
|
||||
int LuaInterface::lua_loadfile(lua_State* L)
|
||||
{
|
||||
std::string fileName = g_lua.popString();
|
||||
if(!boost::ends_with(fileName, ".lua"))
|
||||
if(!stdext::ends_with(fileName, ".lua"))
|
||||
fileName += ".lua";
|
||||
|
||||
try {
|
||||
|
@@ -320,7 +320,7 @@ public:
|
||||
|
||||
/// Pushes any type onto the stack
|
||||
template<typename T, typename... Args>
|
||||
int polymorphicPush(T v, Args... args);
|
||||
int polymorphicPush(const T& v, const Args&... args);
|
||||
int polymorphicPush() { return 0; }
|
||||
|
||||
/// Casts a value from stack to any type
|
||||
@@ -349,7 +349,7 @@ extern LuaInterface g_lua;
|
||||
#include "luavaluecasts.h"
|
||||
|
||||
template<typename T, typename... Args>
|
||||
int LuaInterface::polymorphicPush(T v, Args... args) {
|
||||
int LuaInterface::polymorphicPush(const T& v, const Args&... args) {
|
||||
int r = push_luavalue(v);
|
||||
return r + polymorphicPush(args...);
|
||||
}
|
||||
|
@@ -24,6 +24,7 @@
|
||||
#define LUAOBJECT_H
|
||||
|
||||
#include "declarations.h"
|
||||
#include <typeinfo>
|
||||
|
||||
/// LuaObject, all script-able classes have it as base
|
||||
// @bindclass
|
||||
@@ -80,7 +81,7 @@ public:
|
||||
return stdext::demangle_name(typeid(*this).name());
|
||||
}
|
||||
|
||||
LuaObjectPtr asLuaObject() { return self_cast<LuaObject>(); }
|
||||
LuaObjectPtr asLuaObject() { return static_self_cast<LuaObject>(); }
|
||||
|
||||
void operator=(const LuaObject& other) { }
|
||||
|
||||
|
@@ -111,7 +111,7 @@ bool luavalue_cast(int index, LuaObjectPtr& obj);
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<std::is_base_of<LuaObject, T>::value, bool>::type
|
||||
luavalue_cast(int index, boost::intrusive_ptr<T>& ptr);
|
||||
luavalue_cast(int index, stdext::shared_object_ptr<T>& ptr);
|
||||
|
||||
// std::function
|
||||
template<typename Ret, typename... Args>
|
||||
@@ -186,7 +186,7 @@ push_luavalue(const T& obj) {
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<std::is_base_of<LuaObject, T>::value, bool>::type
|
||||
luavalue_cast(int index, boost::intrusive_ptr<T>& ptr) {
|
||||
luavalue_cast(int index, stdext::shared_object_ptr<T>& ptr) {
|
||||
LuaObjectPtr obj;
|
||||
if(!luavalue_cast(index, obj))
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user