From 138d3f4d3d6e2aaf5ba34f89af15ef85ea074357 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 8 Nov 2021 09:23:38 +0800 Subject: *misc --- Runtime/Lua/LuaBind/LuaBind.h | 2 + Runtime/Lua/LuaBind/LuaBindClass.hpp | 2 +- Runtime/Lua/LuaBind/LuaBindInvoker.cpp | 105 +++++ Runtime/Lua/LuaBind/LuaBindInvoker.h | 77 ++++ Runtime/Lua/LuaBind/LuaBindMemberRef.h | 1 + Runtime/Lua/LuaBind/LuaBindState.cpp | 5 + Runtime/Lua/LuaBind/LuaBindState.h | 2 + Runtime/Lua/LuaBind/LuaBindTable.h | 15 + Runtime/Lua/LuaBind/LuaBindUtility.h | 2 +- Runtime/Lua/LuaBind/signal/bind.h | 510 --------------------- Runtime/Lua/LuaBind/signal/remove_from_container.h | 32 -- Runtime/Lua/LuaBind/signal/signal.h | 451 ------------------ Runtime/Lua/LuaBind/signal/slot.h | 203 -------- 13 files changed, 209 insertions(+), 1198 deletions(-) create mode 100644 Runtime/Lua/LuaBind/LuaBindInvoker.cpp create mode 100644 Runtime/Lua/LuaBind/LuaBindInvoker.h create mode 100644 Runtime/Lua/LuaBind/LuaBindTable.h delete mode 100644 Runtime/Lua/LuaBind/signal/bind.h delete mode 100644 Runtime/Lua/LuaBind/signal/remove_from_container.h delete mode 100644 Runtime/Lua/LuaBind/signal/signal.h delete mode 100644 Runtime/Lua/LuaBind/signal/slot.h (limited to 'Runtime/Lua/LuaBind') diff --git a/Runtime/Lua/LuaBind/LuaBind.h b/Runtime/Lua/LuaBind/LuaBind.h index e12c4b9..b6c5870 100644 --- a/Runtime/Lua/LuaBind/LuaBind.h +++ b/Runtime/Lua/LuaBind/LuaBind.h @@ -10,5 +10,7 @@ #include "LuaBindMemberRef.h" #include "LuaBindClass.inc" #include "LuaBindState.inc" +#include "LuaBindInvoker.h" +#include "LuaBindTable.h" #endif \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindClass.hpp b/Runtime/Lua/LuaBind/LuaBindClass.hpp index 3a407d2..a5ac978 100644 --- a/Runtime/Lua/LuaBind/LuaBindClass.hpp +++ b/Runtime/Lua/LuaBind/LuaBindClass.hpp @@ -87,7 +87,7 @@ namespace LuaBind NativeClass(LuaBind::VM* vm); virtual ~NativeClass(); - VM* GetVM() { return mOwner; } + LuaBind::VM* GetVM() { return mOwner; } // 成员引用管理,在实例的ref table里。设置、取、清除 void SetMemberRef(State& state, MemberRef& memRef, int idx); diff --git a/Runtime/Lua/LuaBind/LuaBindInvoker.cpp b/Runtime/Lua/LuaBind/LuaBindInvoker.cpp new file mode 100644 index 0000000..debebf8 --- /dev/null +++ b/Runtime/Lua/LuaBind/LuaBindInvoker.cpp @@ -0,0 +1,105 @@ +#include "LuaBindInvoker.h" + +namespace LuaBind +{ + + void GlobalInvoker::AddInt(int n) + { + state.Push(n); + ++argc; + } + + void GlobalInvoker::AddFloat(float n) + { + state.Push(n); + ++argc; + } + + void GlobalInvoker::AddNil() + { + state.PushNil(); + ++argc; + } + + void GlobalInvoker::AddBool(bool b) + { + state.Push(b); + ++argc; + } + + void GlobalInvoker::AddString(const char* str) + { + state.Push(str); + ++argc; + } + + void GlobalInvoker::AddTable(INativeTable& tb) + { + tb.CastToTable(state); + ++argc; + } + + void GlobalInvoker::Invoke(int nReturns) + { + method.PushRef(state); + state.Call(argc, nReturns, onErrorOccured); + } + + + void MemberInvoker::AddInt(int n) + { + state.Push(n); + ++argc; + } + + void MemberInvoker::AddFloat(float n) + { + state.Push(n); + ++argc; + } + + void MemberInvoker::AddNil() + { + state.PushNil(); + ++argc; + } + + void MemberInvoker::AddBool(bool b) + { + state.Push(b); + ++argc; + } + + void MemberInvoker::AddString(const char* str) + { + state.Push(str); + ++argc; + } + + void MemberInvoker::AddTable(INativeTable& tb) + { + tb.CastToTable(state); + ++argc; + } + + void MemberInvoker::AddMember(MemberRef member) + { + owner->PushMemberRef(state, member); + ++argc; + } + + void MemberInvoker::Invoke(int nReturns) + { + owner->PushMemberRef(state, member); + state.GetField(-1, method); + if (!state.IsType(-1, LUA_TFUNCTION)) + { + state.Pop(2); + return; + } + lua_replace(state, -2); + lua_insert(state, -1 - argc); + state.Call(argc, nReturns, onErrorOccured); + } + +} diff --git a/Runtime/Lua/LuaBind/LuaBindInvoker.h b/Runtime/Lua/LuaBind/LuaBindInvoker.h new file mode 100644 index 0000000..8ea57a2 --- /dev/null +++ b/Runtime/Lua/LuaBind/LuaBindInvoker.h @@ -0,0 +1,77 @@ +#ifndef LUA_BIND_INVOKER_H +#define LUA_BIND_INVOKER_H + +#include "LuaBindRef.h" +#include "LuaBindTable.h" +#include "LuaBindClass.hpp" + +namespace LuaBind +{ + + // 调用全局lua方法 + struct GlobalInvoker + { + GlobalInvoker(lua_State* st) + : state(st) + { + argc = 0; + } + UniversalRef method; + + void AddInt(int n); + void AddFloat(float n); + void AddNil(); + void AddBool(bool b); + void AddString(const char* str); + void AddTable(INativeTable& tb); + template + void AddUserdata(NativeClass& udata) { + udata.PushUserdata(state); + ++argc; + } + + void Invoke(int nReturns); + + private: + State state; + int argc; + }; + + // 调用成员的方法 + struct MemberInvoker + { + MemberRef member; + const char* method; + + MemberInvoker(lua_State* st, LuaBind::Object* owner) + : state(st) + { + argc = 0; + this->owner = owner; + } + + void AddMember(MemberRef member); + void AddInt(int n); + void AddFloat(float n); + void AddNil(); + void AddBool(bool b); + void AddString(const char* str); + void AddTable(INativeTable& tb); + template + void AddUserdata(NativeClass& udata) { + udata.PushUserdata(state); + ++argc; + } + + void Invoke(int nReturns); + + private: + LuaBind::Object* owner; + State state; + int argc; + + }; + +} + +#endif \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindMemberRef.h b/Runtime/Lua/LuaBind/LuaBindMemberRef.h index ecc50f5..37fe413 100644 --- a/Runtime/Lua/LuaBind/LuaBindMemberRef.h +++ b/Runtime/Lua/LuaBind/LuaBindMemberRef.h @@ -8,6 +8,7 @@ namespace LuaBind // 实例的ref table保存的member ref。由luax class做具体的管理。实例的ref table是强引用,用来管理里面member的生命周期。 // 用来在lua和native之间进行数据沟通。 + // 不会由于循环引用而内存泄露,因为lua的GC是tracing GC class MemberRef { public: diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index 384cba2..676c19c 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -196,6 +196,11 @@ namespace LuaBind } } + void State::PushTable(INativeTable& tb) + { + tb.CastToTable(*this); + } + void State::PushNil() { lua_pushnil(mState); diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h index e639d4d..24bd092 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.h +++ b/Runtime/Lua/LuaBind/LuaBindState.h @@ -6,6 +6,7 @@ #include "LuaBindConfig.h" #include "LuaBindRefTable.h" #include "LuaBindGlobalState.h" +#include "LuaBindTable.h" namespace LuaBind { @@ -100,6 +101,7 @@ namespace LuaBind bool HasField(int idx, int name, int type); bool HasKeys(int idx); + void PushTable(INativeTable& tb); void PushNil(); void Push(bool value); void Push(cc8* value); diff --git a/Runtime/Lua/LuaBind/LuaBindTable.h b/Runtime/Lua/LuaBind/LuaBindTable.h new file mode 100644 index 0000000..3a6ec6c --- /dev/null +++ b/Runtime/Lua/LuaBind/LuaBindTable.h @@ -0,0 +1,15 @@ +#pragma once + +namespace LuaBind +{ + + class State; + + // 需要作为table传给lua的结构实现这个接口 + struct INativeTable + { + // 将结构转换为table并留在栈顶 + virtual void CastToTable(State& state) = 0; + }; + +} \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindUtility.h b/Runtime/Lua/LuaBind/LuaBindUtility.h index e88dd68..e47a326 100644 --- a/Runtime/Lua/LuaBind/LuaBindUtility.h +++ b/Runtime/Lua/LuaBind/LuaBindUtility.h @@ -34,7 +34,7 @@ }while(0) #define LUA_BIND_REGISTER_ENUM(state, name, ...) \ do{ \ - ::Enum __e[] = {__VA_ARGS__,{0, 0}}; \ + LuaBind::Enum __e[] = {__VA_ARGS__,{0, 0}}; \ state.RegisterEnum(name, __e); \ }while(0) diff --git a/Runtime/Lua/LuaBind/signal/bind.h b/Runtime/Lua/LuaBind/signal/bind.h deleted file mode 100644 index e29896b..0000000 --- a/Runtime/Lua/LuaBind/signal/bind.h +++ /dev/null @@ -1,510 +0,0 @@ -// Aseprite Base Library -// Copyright (c) 2001-2013 David Capello -// -// This file is released under the terms of the MIT license. -// Read LICENSE.txt for more information. - -#ifndef BASE_BIND_H_INCLUDED -#define BASE_BIND_H_INCLUDED -#pragma once - -// BindAdapter0_fun -template -class BindAdapter0_fun -{ - F f; -public: - BindAdapter0_fun(const F& f) : f(f) { } - - R operator()() { return f(); } - - template - R operator()(const A1& a1) { return f(); } - - template - R operator()(const A1& a1, const A2& a2) { return f(); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3) { return f(); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { return f(); } -}; - -template -class BindAdapter0_fun -{ - F f; -public: - BindAdapter0_fun(const F& f) : f(f) { } - - void operator()() { f(); } - - template - void operator()(const A1& a1) { f(); } - - template - void operator()(const A1& a1, const A2& a2) { f(); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3) { f(); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { f(); } -}; - -template -BindAdapter0_fun -Bind(const F& f) -{ - return BindAdapter0_fun(f); -} - -// BindAdapter0_mem -template -class BindAdapter0_mem -{ - R (T::*m)(); - T* t; -public: - template - BindAdapter0_mem(R (T::*m)(), T2* t) : m(m), t(t) { } - - R operator()() { return (t->*m)(); } - - template - R operator()(const A1& a1) { return (t->*m)(); } - - template - R operator()(const A1& a1, const A2& a2) { return (t->*m)(); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3) { return (t->*m)(); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { return (t->*m)(); } -}; - -template -class BindAdapter0_mem -{ - void (T::*m)(); - T* t; -public: - template - BindAdapter0_mem(void (T::*m)(), T2* t) : m(m), t(t) { } - - void operator()() { (t->*m)(); } - - template - void operator()(const A1& a1) { (t->*m)(); } - - template - void operator()(const A1& a1, const A2& a2) { (t->*m)(); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3) { (t->*m)(); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { (t->*m)(); } -}; - -template -BindAdapter0_mem -Bind(R (T::*m)(), T2* t) -{ - return BindAdapter0_mem(m, t); -} - -// BindAdapter1_fun -template -class BindAdapter1_fun -{ - F f; - X1 x1; -public: - BindAdapter1_fun(const F& f, X1 x1) : f(f), x1(x1) { } - - R operator()() { return f(x1); } - - template - R operator()(const A1& a1) { return f(x1); } - - template - R operator()(const A1& a1, const A2& a2) { return f(x1); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3) { return f(x1); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { return f(x1); } -}; - -template -class BindAdapter1_fun -{ - F f; - X1 x1; -public: - BindAdapter1_fun(const F& f, X1 x1) : f(f), x1(x1) { } - - void operator()() { f(x1); } - - template - void operator()(const A1& a1) { f(x1); } - - template - void operator()(const A1& a1, const A2& a2) { f(x1); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3) { f(x1); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { f(x1); } -}; - -template -BindAdapter1_fun -Bind(const F& f, X1 x1) -{ - return BindAdapter1_fun(f, x1); -} - -// BindAdapter1_mem -template -class BindAdapter1_mem -{ - R (T::*m)(B1); - T* t; - X1 x1; -public: - template - BindAdapter1_mem(R (T::*m)(B1), T2* t, X1 x1) : m(m), t(t), x1(x1) { } - - R operator()() { return (t->*m)(x1); } - - template - R operator()(const A1& a1) { return (t->*m)(x1); } - - template - R operator()(const A1& a1, const A2& a2) { return (t->*m)(x1); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3) { return (t->*m)(x1); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { return (t->*m)(x1); } -}; - -template -class BindAdapter1_mem -{ - void (T::*m)(B1); - T* t; - X1 x1; -public: - template - BindAdapter1_mem(void (T::*m)(B1), T2* t, X1 x1) : m(m), t(t), x1(x1) { } - - void operator()() { (t->*m)(x1); } - - template - void operator()(const A1& a1) { (t->*m)(x1); } - - template - void operator()(const A1& a1, const A2& a2) { (t->*m)(x1); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3) { (t->*m)(x1); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { (t->*m)(x1); } -}; - -template -BindAdapter1_mem -Bind(R (T::*m)(B1), T2* t, X1 x1) -{ - return BindAdapter1_mem(m, t, x1); -} - -// BindAdapter2_fun -template -class BindAdapter2_fun -{ - F f; - X1 x1; - X2 x2; -public: - BindAdapter2_fun(const F& f, X1 x1, X2 x2) : f(f), x1(x1), x2(x2) { } - - R operator()() { return f(x1, x2); } - - template - R operator()(const A1& a1) { return f(x1, x2); } - - template - R operator()(const A1& a1, const A2& a2) { return f(x1, x2); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3) { return f(x1, x2); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { return f(x1, x2); } -}; - -template -class BindAdapter2_fun -{ - F f; - X1 x1; - X2 x2; -public: - BindAdapter2_fun(const F& f, X1 x1, X2 x2) : f(f), x1(x1), x2(x2) { } - - void operator()() { f(x1, x2); } - - template - void operator()(const A1& a1) { f(x1, x2); } - - template - void operator()(const A1& a1, const A2& a2) { f(x1, x2); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3) { f(x1, x2); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { f(x1, x2); } -}; - -template -BindAdapter2_fun -Bind(const F& f, X1 x1, X2 x2) -{ - return BindAdapter2_fun(f, x1, x2); -} - -// BindAdapter2_mem -template -class BindAdapter2_mem -{ - R (T::*m)(B1, B2); - T* t; - X1 x1; - X2 x2; -public: - template - BindAdapter2_mem(R (T::*m)(B1, B2), T2* t, X1 x1, X2 x2) : m(m), t(t), x1(x1), x2(x2) { } - - R operator()() { return (t->*m)(x1, x2); } - - template - R operator()(const A1& a1) { return (t->*m)(x1, x2); } - - template - R operator()(const A1& a1, const A2& a2) { return (t->*m)(x1, x2); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3) { return (t->*m)(x1, x2); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { return (t->*m)(x1, x2); } -}; - -template -class BindAdapter2_mem -{ - void (T::*m)(B1, B2); - T* t; - X1 x1; - X2 x2; -public: - template - BindAdapter2_mem(void (T::*m)(B1, B2), T2* t, X1 x1, X2 x2) : m(m), t(t), x1(x1), x2(x2) { } - - void operator()() { (t->*m)(x1, x2); } - - template - void operator()(const A1& a1) { (t->*m)(x1, x2); } - - template - void operator()(const A1& a1, const A2& a2) { (t->*m)(x1, x2); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3) { (t->*m)(x1, x2); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { (t->*m)(x1, x2); } -}; - -template -BindAdapter2_mem -Bind(R (T::*m)(B1, B2), T2* t, X1 x1, X2 x2) -{ - return BindAdapter2_mem(m, t, x1, x2); -} - -// BindAdapter3_fun -template -class BindAdapter3_fun -{ - F f; - X1 x1; - X2 x2; - X3 x3; -public: - BindAdapter3_fun(const F& f, X1 x1, X2 x2, X3 x3) : f(f), x1(x1), x2(x2), x3(x3) { } - - R operator()() { return f(x1, x2, x3); } - - template - R operator()(const A1& a1) { return f(x1, x2, x3); } - - template - R operator()(const A1& a1, const A2& a2) { return f(x1, x2, x3); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3) { return f(x1, x2, x3); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { return f(x1, x2, x3); } -}; - -template -class BindAdapter3_fun -{ - F f; - X1 x1; - X2 x2; - X3 x3; -public: - BindAdapter3_fun(const F& f, X1 x1, X2 x2, X3 x3) : f(f), x1(x1), x2(x2), x3(x3) { } - - void operator()() { f(x1, x2, x3); } - - template - void operator()(const A1& a1) { f(x1, x2, x3); } - - template - void operator()(const A1& a1, const A2& a2) { f(x1, x2, x3); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3) { f(x1, x2, x3); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { f(x1, x2, x3); } -}; - -template -BindAdapter3_fun -Bind(const F& f, X1 x1, X2 x2, X3 x3) -{ - return BindAdapter3_fun(f, x1, x2, x3); -} - -// BindAdapter3_mem -template -class BindAdapter3_mem -{ - R (T::*m)(B1, B2, B3); - T* t; - X1 x1; - X2 x2; - X3 x3; -public: - template - BindAdapter3_mem(R (T::*m)(B1, B2, B3), T2* t, X1 x1, X2 x2, X3 x3) : m(m), t(t), x1(x1), x2(x2), x3(x3) { } - - R operator()() { return (t->*m)(x1, x2, x3); } - - template - R operator()(const A1& a1) { return (t->*m)(x1, x2, x3); } - - template - R operator()(const A1& a1, const A2& a2) { return (t->*m)(x1, x2, x3); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3) { return (t->*m)(x1, x2, x3); } - - template - R operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { return (t->*m)(x1, x2, x3); } -}; - -template -class BindAdapter3_mem -{ - void (T::*m)(B1, B2, B3); - T* t; - X1 x1; - X2 x2; - X3 x3; -public: - template - BindAdapter3_mem(void (T::*m)(B1, B2, B3), T2* t, X1 x1, X2 x2) : m(m), t(t), x1(x1), x2(x2) { } - - void operator()() { (t->*m)(x1, x2, x3); } - - template - void operator()(const A1& a1) { (t->*m)(x1, x2, x3); } - - template - void operator()(const A1& a1, const A2& a2) { (t->*m)(x1, x2, x3); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3) { (t->*m)(x1, x2, x3); } - - template - void operator()(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { (t->*m)(x1, x2, x3); } -}; - -template -BindAdapter3_mem -Bind(R (T::*m)(B1, B2, B3), T2* t, X1 x1, X2 x2) -{ - return BindAdapter3_mem(m, t, x1, x2); -} - -// Helper class to holds references as pointers (to avoid copying the -// original object). -template -class RefWrapper -{ - T* ptr; -public: - RefWrapper(T& ref) : ptr(&ref) { } - operator T&() const { return *ptr; } -}; - -// Creates RefWrappers, useful to wrap arguments that have to be -// passed as a reference when you use Bind. -template -RefWrapper Ref(T& ref) -{ - return RefWrapper(ref); -} - -#endif diff --git a/Runtime/Lua/LuaBind/signal/remove_from_container.h b/Runtime/Lua/LuaBind/signal/remove_from_container.h deleted file mode 100644 index 9fdc442..0000000 --- a/Runtime/Lua/LuaBind/signal/remove_from_container.h +++ /dev/null @@ -1,32 +0,0 @@ -// Aseprite Base Library -// Copyright (c) 2001-2013 David Capello -// -// This file is released under the terms of the MIT license. -// Read LICENSE.txt for more information. - -#ifndef BASE_REMOVE_FROM_CONTAINER_H_INCLUDED -#define BASE_REMOVE_FROM_CONTAINER_H_INCLUDED -#pragma once - -namespace base { - -// Removes all ocurrences of the specified element from the STL container. -template -void remove_from_container(ContainerType& container, - typename ContainerType::const_reference element) -{ - for (typename ContainerType::iterator - it = container.begin(), - end = container.end(); it != end; ) { - if (*it == element) { - it = container.erase(it); - end = container.end(); - } - else - ++it; - } -} - -} - -#endif diff --git a/Runtime/Lua/LuaBind/signal/signal.h b/Runtime/Lua/LuaBind/signal/signal.h deleted file mode 100644 index 97882c6..0000000 --- a/Runtime/Lua/LuaBind/signal/signal.h +++ /dev/null @@ -1,451 +0,0 @@ -// Aseprite Base Library -// Copyright (c) 2001-2013 David Capello -// -// This file is released under the terms of the MIT license. -// Read LICENSE.txt for more information. - -#ifndef BASE_SIGNAL_H_INCLUDED -#define BASE_SIGNAL_H_INCLUDED -#pragma once - -//#include "base/slot.h" -//#include "base/remove_from_container.h" - -#include "./slot.h" -#include "./remove_from_container.h" - -#include - -// Signal0_base - Base class to delegate responsibility to -// functions of zero arguments. -template -class Signal0_base -{ -public: - typedef R ReturnType; - typedef Slot0 SlotType; - typedef std::vector SlotList; - -protected: - SlotList m_slots; - -public: - Signal0_base() { } - Signal0_base(const Signal0_base& s) - { - copy(s); - } - ~Signal0_base() - { - disconnectAll(); - } - - SlotType* addSlot(SlotType* slot) - { - m_slots.push_back(slot); - return slot; - } - - template - SlotType* connect(const F& f) - { - return addSlot(new Slot0_fun(f)); - } - - template - SlotType* connect(R (T::*m)(), T* t) - { - return addSlot(new Slot0_mem(m, t)); - } - - const SlotList& getSlots() const - { - return m_slots; - } - - void disconnect(SlotType* slot) - { - base::remove_from_container(m_slots, slot); - } - - void disconnectAll() - { - typename SlotList::iterator end = m_slots.end(); - for (typename SlotList::iterator - it = m_slots.begin(); it != end; ++it) - delete *it; - m_slots.clear(); - } - - bool empty() const - { - return m_slots.empty(); - } - - Signal0_base& operator=(const Signal0_base& s) { - copy(s); - return *this; - } - -private: - - void copy(const Signal0_base& s) - { - typename SlotList::const_iterator end = s.m_slots.end(); - for (typename SlotList::const_iterator - it = s.m_slots.begin(); it != end; ++it) { - m_slots.push_back((*it)->clone()); - } - } - -}; - -// Signal0 -template -class Signal0 : public Signal0_base -{ -public: - Signal0() { } - - Signal0(const Signal0& s) - : Signal0_base(s) { } - - R operator()(R default_result = R()) - { - R result(default_result); - typename Signal0_base::SlotList::iterator end = Signal0_base::m_slots.end(); - for (typename Signal0_base::SlotList::iterator - it = Signal0_base::m_slots.begin(); it != end; ++it) { - typename Signal0_base::SlotType* slot = *it; - result = (*slot)(); - } - return result; - } - - template - R operator()(R default_result, const Merger& m) - { - R result(default_result); - Merger merger(m); - typename Signal0_base::SlotList::iterator end = Signal0_base::m_slots.end(); - for (typename Signal0_base::SlotList::iterator - it = Signal0_base::m_slots.begin(); it != end; ++it) { - typename Signal0_base::SlotType* slot = *it; - result = merger(result, (*slot)()); - } - return result; - } - -}; - -// Signal0 -template<> -class Signal0 : public Signal0_base -{ -public: - Signal0() { } - - Signal0(const Signal0& s) - : Signal0_base(s) { } - - void operator()() - { - SlotList::iterator end = m_slots.end(); - for (SlotList::iterator - it = m_slots.begin(); it != end; ++it) { - SlotType* slot = *it; - (*slot)(); - } - } - -}; - -// Signal1_base - Base class to delegate responsibility to -// functions of one argument. -template -class Signal1_base -{ -public: - typedef R ReturnType; - typedef Slot1 SlotType; - typedef std::vector SlotList; - -protected: - SlotList m_slots; - -public: - Signal1_base() { } - Signal1_base(const Signal1_base& s) - { - copy(s); - } - ~Signal1_base() - { - disconnectAll(); - } - - SlotType* addSlot(SlotType* slot) - { - m_slots.push_back(slot); - return slot; - } - - template - SlotType* connect(const F& f) - { - return addSlot(new Slot1_fun(f)); - } - - template - SlotType* connect(R (T::*m)(A1), T* t) - { - return addSlot(new Slot1_mem(m, t)); - } - - const SlotList& getSlots() const - { - return m_slots; - } - - void disconnect(SlotType* slot) - { - base::remove_from_container(m_slots, slot); - } - - void disconnectAll() - { - typename SlotList::iterator end = m_slots.end(); - for (typename SlotList::iterator - it = m_slots.begin(); it != end; ++it) - delete *it; - m_slots.clear(); - } - - bool empty() const - { - return m_slots.empty(); - } - - Signal1_base& operator=(const Signal1_base& s) { - copy(s); - return *this; - } - -private: - - void copy(const Signal1_base& s) - { - typename SlotList::const_iterator end = s.m_slots.end(); - for (typename SlotList::const_iterator - it = s.m_slots.begin(); it != end; ++it) { - m_slots.push_back((*it)->clone()); - } - } - -}; - -// Signal1 -template -class Signal1 : public Signal1_base -{ -public: - Signal1() { } - - Signal1(const Signal1& s) - : Signal1_base(s) { } - - R operator()(A1 a1, R default_result = R()) - { - R result(default_result); - typename Signal1_base::SlotList::iterator end = Signal1_base::m_slots.end(); - for (typename Signal1_base::SlotList::iterator - it = Signal1_base::m_slots.begin(); it != end; ++it) { - typename Signal1_base::SlotType* slot = *it; - result = (*slot)(a1); - } - return result; - } - - template - R operator()(A1 a1, R default_result, const Merger& m) - { - R result(default_result); - Merger merger(m); - typename Signal1_base::SlotList::iterator end = Signal1_base::m_slots.end(); - for (typename Signal1_base::SlotList::iterator - it = Signal1_base::m_slots.begin(); it != end; ++it) { - typename Signal1_base::SlotType* slot = *it; - result = merger(result, (*slot)(a1)); - } - return result; - } - -}; - -// Signal1 -template -class Signal1 : public Signal1_base -{ -public: - Signal1() { } - - Signal1(const Signal1& s) - : Signal1_base(s) { } - - void operator()(A1 a1) - { - typename Signal1_base::SlotList::iterator end = Signal1_base::m_slots.end(); - for (typename Signal1_base::SlotList::iterator - it = Signal1_base::m_slots.begin(); it != end; ++it) { - typename Signal1_base::SlotType* slot = *it; - (*slot)(a1); - } - } - -}; - -// Signal2_base - Base class to delegate responsibility to -// functions of two arguments. -template -class Signal2_base -{ -public: - typedef R ReturnType; - typedef Slot2 SlotType; - typedef std::vector SlotList; - -protected: - SlotList m_slots; - -public: - Signal2_base() { } - Signal2_base(const Signal2_base& s) - { - copy(s); - } - ~Signal2_base() - { - disconnectAll(); - } - - SlotType* addSlot(SlotType* slot) - { - m_slots.push_back(slot); - return slot; - } - - template - SlotType* connect(const F& f) - { - return addSlot(new Slot2_fun(f)); - } - - template - SlotType* connect(R (T::*m)(A1, A2), T* t) - { - return addSlot(new Slot2_mem(m, t)); - } - - const SlotList& getSlots() const - { - return m_slots; - } - - void disconnect(SlotType* slot) - { - base::remove_from_container(m_slots, slot); - } - - void disconnectAll() - { - typename SlotList::iterator end = m_slots.end(); - for (typename SlotList::iterator - it = m_slots.begin(); it != end; ++it) - delete *it; - m_slots.clear(); - } - - bool empty() const - { - return m_slots.empty(); - } - - Signal2_base& operator=(const Signal2_base& s) { - copy(s); - return *this; - } - -private: - - void copy(const Signal2_base& s) - { - typename SlotList::const_iterator end = s.m_slots.end(); - for (typename SlotList::const_iterator - it = s.m_slots.begin(); it != end; ++it) { - m_slots.push_back((*it)->clone()); - } - } - -}; - -// Signal2 -template -class Signal2 : public Signal2_base -{ -public: - Signal2() { } - - Signal2(const Signal2& s) - : Signal2_base(s) { } - - R operator()(A1 a1, A2 a2, R default_result = R()) - { - R result(default_result); - typename Signal2_base::SlotList::iterator end = Signal2_base::m_slots.end(); - for (typename Signal2_base::SlotList::iterator - it = Signal2_base::m_slots.begin(); it != end; ++it) { - typename Signal2_base::SlotType* slot = *it; - result = (*slot)(a1, a2); - } - return result; - } - - template - R operator()(A1 a1, A2 a2, R default_result, const Merger& m) - { - R result(default_result); - Merger merger(m); - typename Signal2_base::SlotList::iterator end = Signal2_base::m_slots.end(); - for (typename Signal2_base::SlotList::iterator - it = Signal2_base::m_slots.begin(); it != end; ++it) { - typename Signal2_base::SlotType* slot = *it; - result = merger(result, (*slot)(a1, a2)); - } - return result; - } - -}; - -// Signal2 -template -class Signal2 : public Signal2_base -{ -public: - Signal2() { } - - Signal2(const Signal2& s) - : Signal2_base(s) { } - - void operator()(A1 a1, A2 a2) - { - typename Signal2_base::SlotList::iterator end = Signal2_base::m_slots.end(); - for (typename Signal2_base::SlotList::iterator - it = Signal2_base::m_slots.begin(); it != end; ++it) { - typename Signal2_base::SlotType* slot = *it; - (*slot)(a1, a2); - } - } - -}; - -#endif diff --git a/Runtime/Lua/LuaBind/signal/slot.h b/Runtime/Lua/LuaBind/signal/slot.h deleted file mode 100644 index 226d382..0000000 --- a/Runtime/Lua/LuaBind/signal/slot.h +++ /dev/null @@ -1,203 +0,0 @@ -// Aseprite Base Library -// Copyright (c) 2001-2013 David Capello -// -// This file is released under the terms of the MIT license. -// Read LICENSE.txt for more information. - -#ifndef BASE_SLOT_H_INCLUDED -#define BASE_SLOT_H_INCLUDED -#pragma once - -// Slot0 - Base class for delegates of zero arguments. -template -class Slot0 -{ -public: - Slot0() { } - Slot0(const Slot0& s) { (void)s; } - virtual ~Slot0() { } - virtual R operator()() = 0; - virtual Slot0* clone() const = 0; -}; - -// Slot0_fun - hold a F instance and use the function call operator -template -class Slot0_fun : public Slot0 -{ - F f; -public: - Slot0_fun(const F& f) : f(f) { } - Slot0_fun(const Slot0_fun& s) : Slot0(s), f(s.f) { } - ~Slot0_fun() { } - R operator()() { return f(); } - Slot0_fun* clone() const { return new Slot0_fun(*this); } -}; - -template -class Slot0_fun : public Slot0 -{ - F f; -public: - Slot0_fun(const F& f) : f(f) { } - Slot0_fun(const Slot0_fun& s) : Slot0(s), f(s.f) { } - ~Slot0_fun() { } - void operator()() { f(); } - Slot0_fun* clone() const { return new Slot0_fun(*this); } -}; - -// Slot0_mem - pointer to a member function of the T class -template -class Slot0_mem : public Slot0 -{ - R (T::*m)(); - T* t; -public: - Slot0_mem(R (T::*m)(), T* t) : m(m), t(t) { } - Slot0_mem(const Slot0_mem& s) : Slot0(s), m(s.m), t(s.t) { } - ~Slot0_mem() { } - R operator()() { return (t->*m)(); } - Slot0_mem* clone() const { return new Slot0_mem(*this); } -}; - -template -class Slot0_mem : public Slot0 -{ - void (T::*m)(); - T* t; -public: - Slot0_mem(void (T::*m)(), T* t) : m(m), t(t) { } - Slot0_mem(const Slot0_mem& s) : Slot0(s), m(s.m), t(s.t) { } - ~Slot0_mem() { } - void operator()() { (t->*m)(); } - Slot0_mem* clone() const { return new Slot0_mem(*this); } -}; - -// Slot1 - Base class for delegates of one argument. -template -class Slot1 -{ -public: - Slot1() { } - Slot1(const Slot1& s) { (void)s; } - virtual ~Slot1() { } - virtual R operator()(A1 a1) = 0; - virtual Slot1* clone() const = 0; -}; - -// Slot1_fun - hold a F instance and use the function call operator -template -class Slot1_fun : public Slot1 -{ - F f; -public: - Slot1_fun(const F& f) : f(f) { } - Slot1_fun(const Slot1_fun& s) : Slot1(s), f(s.f) { } - ~Slot1_fun() { } - R operator()(A1 a1) { return f(a1); } - Slot1_fun* clone() const { return new Slot1_fun(*this); } -}; - -template -class Slot1_fun : public Slot1 -{ - F f; -public: - Slot1_fun(const F& f) : f(f) { } - Slot1_fun(const Slot1_fun& s) : Slot1(s), f(s.f) { } - ~Slot1_fun() { } - void operator()(A1 a1) { f(a1); } - Slot1_fun* clone() const { return new Slot1_fun(*this); } -}; - -// Slot1_mem - pointer to a member function of the T class -template -class Slot1_mem : public Slot1 -{ - R (T::*m)(A1); - T* t; -public: - Slot1_mem(R (T::*m)(A1), T* t) : m(m), t(t) { } - Slot1_mem(const Slot1_mem& s) : Slot1(s), m(s.m), t(s.t) { } - ~Slot1_mem() { } - R operator()(A1 a1) { return (t->*m)(a1); } - Slot1_mem* clone() const { return new Slot1_mem(*this); } -}; - -template -class Slot1_mem : public Slot1 -{ - void (T::*m)(A1); - T* t; -public: - Slot1_mem(void (T::*m)(A1), T* t) : m(m), t(t) { } - Slot1_mem(const Slot1_mem& s) : Slot1(s), m(s.m), t(s.t) { } - ~Slot1_mem() { } - void operator()(A1 a1) { (t->*m)(a1); } - Slot1_mem* clone() const { return new Slot1_mem(*this); } -}; - -// Slot2 - Base class for delegates of two arguments. -template -class Slot2 -{ -public: - Slot2() { } - Slot2(const Slot2& s) { (void)s; } - virtual ~Slot2() { } - virtual R operator()(A1 a1, A2 a2) = 0; - virtual Slot2* clone() const = 0; -}; - -// Slot2_fun - hold a F instance and use the function call operator -template -class Slot2_fun : public Slot2 -{ - F f; -public: - Slot2_fun(const F& f) : f(f) { } - Slot2_fun(const Slot2_fun& s) : Slot2(s), f(s.f) { } - ~Slot2_fun() { } - R operator()(A1 a1, A2 a2) { return f(a1, a2); } - Slot2_fun* clone() const { return new Slot2_fun(*this); } -}; - -template -class Slot2_fun : public Slot2 -{ - F f; -public: - Slot2_fun(const F& f) : f(f) { } - Slot2_fun(const Slot2_fun& s) : Slot2(s), f(s.f) { } - ~Slot2_fun() { } - void operator()(A1 a1, A2 a2) { f(a1, a2); } - Slot2_fun* clone() const { return new Slot2_fun(*this); } -}; - -// Slot2_mem - pointer to a member function of the T class -template -class Slot2_mem : public Slot2 -{ - R (T::*m)(A1, A2); - T* t; -public: - Slot2_mem(R (T::*m)(A1, A2), T* t) : m(m), t(t) { } - Slot2_mem(const Slot2_mem& s) : Slot2(s), m(s.m), t(s.t) { } - ~Slot2_mem() { } - R operator()(A1 a1, A2 a2) { return (t->*m)(a1, a2); } - Slot2_mem* clone() const { return new Slot2_mem(*this); } -}; - -template -class Slot2_mem : public Slot2 -{ - void (T::*m)(A1, A2); - T* t; -public: - Slot2_mem(void (T::*m)(A1, A2), T* t) : m(m), t(t) { } - Slot2_mem(const Slot2_mem& s) : Slot2(s), m(s.m), t(s.t) { } - ~Slot2_mem() { } - void operator()(A1 a1, A2 a2) { return (t->*m)(a1, a2); } - Slot2_mem* clone() const { return new Slot2_mem(*this); } -}; - -#endif -- cgit v1.1-26-g67d0