mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 14:03:26 +02:00
Huge engine change, replace all std::shared_ptrs
Create a new shared pointer type stdext::shared_object_ptr and stdext::shared_obj using boost::intrusive_ptr Advantages: * half memory usage * faster and lightweight Disadvantages: * using weak_ptr is not supported anymore * compiling seems slower
This commit is contained in:
@@ -30,6 +30,6 @@ class LuaObject;
|
||||
|
||||
typedef std::function<int(LuaInterface*)> LuaCppFunction;
|
||||
typedef std::unique_ptr<LuaCppFunction> LuaCppFunctionPtr;
|
||||
typedef std::shared_ptr<LuaObject> LuaObjectPtr;
|
||||
typedef stdext::shared_object_ptr<LuaObject> LuaObjectPtr;
|
||||
|
||||
#endif
|
||||
|
@@ -152,18 +152,18 @@ namespace luabinder
|
||||
|
||||
/// Create member function lambdas
|
||||
template<typename Ret, typename C, typename... Args>
|
||||
std::function<Ret(const std::shared_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 std::shared_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 std::shared_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 std::shared_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,7 +186,7 @@ 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<std::shared_ptr<FC>, typename remove_const_ref<Args>::type...> Tuple;
|
||||
typedef typename std::tuple<stdext::shared_object_ptr<FC>, typename remove_const_ref<Args>::type...> Tuple;
|
||||
auto lambda = make_mem_func<Ret,FC>(f);
|
||||
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
||||
decltype(lambda),
|
||||
@@ -209,7 +209,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<std::shared_ptr<C>>(1);
|
||||
auto obj = lua->castValue<stdext::shared_object_ptr<C>>(1);
|
||||
lua->remove(1);
|
||||
return mf(obj, lua);
|
||||
};
|
||||
|
@@ -108,5 +108,5 @@ void LuaObject::luaGetFieldsTable()
|
||||
|
||||
int LuaObject::getUseCount()
|
||||
{
|
||||
return shared_from_this().use_count() - 1;
|
||||
return ref_count();
|
||||
}
|
||||
|
@@ -27,8 +27,7 @@
|
||||
|
||||
/// LuaObject, all script-able classes have it as base
|
||||
// @bindclass
|
||||
#pragma pack(push,1) // disable memory alignment
|
||||
class LuaObject : public std::enable_shared_from_this<LuaObject>
|
||||
class LuaObject : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
LuaObject();
|
||||
@@ -81,7 +80,7 @@ public:
|
||||
return stdext::demangle_name(typeid(*this).name());
|
||||
}
|
||||
|
||||
LuaObjectPtr asLuaObject() { return shared_from_this(); }
|
||||
LuaObjectPtr asLuaObject() { return self_cast<LuaObject>(); }
|
||||
|
||||
void operator=(const LuaObject& other) { }
|
||||
|
||||
@@ -89,7 +88,6 @@ private:
|
||||
int m_fieldsTableRef;
|
||||
int m_metatableRef;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
#include "luainterface.h"
|
||||
|
||||
|
@@ -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, std::shared_ptr<T>& ptr);
|
||||
luavalue_cast(int index, stdext::shared_object_ptr<T>& ptr);
|
||||
|
||||
// std::function
|
||||
template<typename Ret, typename... Args>
|
||||
@@ -156,7 +156,7 @@ int push_internal_luavalue(const std::tuple<Args...>& tuple);
|
||||
|
||||
#include "luaexception.h"
|
||||
#include "luainterface.h"
|
||||
|
||||
#include "luaobject.h"
|
||||
|
||||
template<typename T>
|
||||
int push_internal_luavalue(T v) {
|
||||
@@ -181,11 +181,14 @@ 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, std::shared_ptr<T>& ptr) {
|
||||
luavalue_cast(int index, stdext::shared_object_ptr<T>& ptr) {
|
||||
LuaObjectPtr obj;
|
||||
if(!luavalue_cast(index, obj))
|
||||
return false;
|
||||
ptr = std::dynamic_pointer_cast<T>(obj);
|
||||
if(obj)
|
||||
ptr = obj->dynamic_self_cast<T>();
|
||||
else
|
||||
ptr = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user