From 4d78d62a70e78e3db6c4ea2a5a87ef2479b459ab Mon Sep 17 00:00:00 2001 From: touky Date: Sat, 29 Oct 2016 23:33:08 -0400 Subject: [PATCH] Lolua refactor 2nd pass: New version is much simpler and practical Next pass is clean-up and fix of all the old usage --- doc/samples/meshviewer/scenesetup.cpp | 6 +- doc/samples/meshviewer/scenesetup.h | 2 +- doc/tutorial/14_lol_lua.cpp | 80 ++-- src/easymesh/easymeshlua.cpp | 6 +- src/easymesh/easymeshlua.h | 2 +- src/lolimgui.cpp | 2 +- src/lolua/baselua.h | 501 +++++++++++++++++++++----- src/utils.h | 10 +- 8 files changed, 465 insertions(+), 144 deletions(-) diff --git a/doc/samples/meshviewer/scenesetup.cpp b/doc/samples/meshviewer/scenesetup.cpp index 46806349..74e942dd 100644 --- a/doc/samples/meshviewer/scenesetup.cpp +++ b/doc/samples/meshviewer/scenesetup.cpp @@ -242,10 +242,10 @@ int SceneSetupLuaObject::Toggle(lua_State* l) } //----------------------------------------------------------------------------- -const LuaObjectLib* SceneSetupLuaObject::GetLib() +const LuaObjectLibrary* SceneSetupLuaObject::GetLib() { typedef SceneSetupLuaObject SSLO; - static const LuaObjectLib lib = LuaObjectLib( + static const LuaObjectLibrary lib = LuaObjectLibrary( "SceneSetup", //Statics { { nullptr, nullptr } }, @@ -273,7 +273,7 @@ SceneSetupLuaLoader::SceneSetupLuaLoader() : LuaLoader() { lua_State* l = GetLuaState(); - LuaObjectDef::Register(l); + LuaObjectHelper::Register(l); } //----------------------------------------------------------------------------- diff --git a/doc/samples/meshviewer/scenesetup.h b/doc/samples/meshviewer/scenesetup.h index 9b6ffc29..39165715 100644 --- a/doc/samples/meshviewer/scenesetup.h +++ b/doc/samples/meshviewer/scenesetup.h @@ -116,7 +116,7 @@ public: //------------------------------------------------------------------------- static SceneSetupLuaObject* New(lua_State* l, int arg_nb); - static const LuaObjectLib* GetLib(); + static const LuaObjectLibrary* GetLib(); //------------------------------------------------------------------------- public: diff --git a/doc/tutorial/14_lol_lua.cpp b/doc/tutorial/14_lol_lua.cpp index e9311ac3..de5b5cd0 100644 --- a/doc/tutorial/14_lol_lua.cpp +++ b/doc/tutorial/14_lol_lua.cpp @@ -37,27 +37,25 @@ public: //------------------------------------------------------------------------- static int AddFive(lua_State* l) { - LuaStack stack(l); - int32_t i = stack.GetVar(); - //LuaInt32 i; i.Get(l, 1); + auto stack = LuaStack::Begin(l); + int32_t i = stack.Get(); + i += 5; - //return i.Return(l); - return stack.SetVar(i).Return(); - } + + return (stack << i).End(); + } + static int AddTenInstance(lua_State* l) { - LuaStack stack(l); - DemoObject* obj = nullptr; // stack.GetPtr(); - float f = stack.GetVar(); + auto stack = LuaStack::Begin(l); + DemoObject* obj = stack.GetPtr(); + float f = stack.Get(); - //LuaDemoObjectPtr obj; - //LuaFloat f; - //stack >> obj >> f; f = obj->AddTenMethod(f); - //return f.Return(l); - return 0;// stack.SetVar(f).SetPtr().Return(); + return (stack << f).End(); } + float AddTenMethod(float f) { return (f + 10); @@ -65,27 +63,30 @@ public: static int GetX(lua_State* l) { - LuaStack stack(l); - LuaDemoObjectPtr obj; - LuaInt32 i; - stack >> obj; + auto stack = LuaStack::Begin(l); + DemoObject* obj = stack.GetPtr(); + auto i = stack.Get(); + i = obj->m_x; - return stack << i; + + return (stack << i).End(); } + static int SetX(lua_State* l) { - LuaStack stack(l); - LuaDemoObjectPtr obj; - LuaInt32 i; - stack >> obj >> i; + auto stack = LuaStack::Begin(l); + DemoObject* obj = stack.GetPtr(); + auto i = stack.Get(); + obj->m_x = i; - return 0; + + return stack.End(); } //------------------------------------------------------------------------- - static const LuaObjectLib* GetLib() + static const LuaObjectLibrary* GetLib() { - static const LuaObjectLib lib = LuaObjectLib( + static const LuaObjectLibrary lib = LuaObjectLibrary( "LoluaDemo", { { "AddFive", &DemoObject::AddFive } }, { { "AddTenInstance", &DemoObject::AddTenInstance } }, @@ -99,9 +100,12 @@ public: //----------------------------------------------------------------------------- static int GlobalAddString(lua_State* l) { - LuaString s; s.Get(l, 1); - s() += "_added"; - return s.Return(l); + auto stack = LuaStack::Begin(l); + auto s = stack.Get(); + + s += "_added"; + + return (stack << s).End(); } //----------------------------------------------------------------------------- @@ -113,7 +117,7 @@ public: lua_State* l = GetLuaState(); //Registering demo object - LuaObjectDef::Register(l); + LuaObjectHelper::Register(l); //Registering function LuaFunction add_string(l, "GlobalAddString", &GlobalAddString); @@ -166,18 +170,18 @@ public: demo_loader->TestStuff(); //Grab global test values - float testvalue_num = demo_loader->GetVar("testvalue_num"); - int32_t testvalue_int = demo_loader->GetVar("testvalue_int"); - uint32_t testvalue_uint = demo_loader->GetVar("testvalue_uint"); - String testvalue_str = demo_loader->GetVar("testvalue_str"); + float testvalue_num = demo_loader->Get("testvalue_num"); + int32_t testvalue_int = demo_loader->Get("testvalue_int"); + uint32_t testvalue_uint = demo_loader->Get("testvalue_uint"); + String testvalue_str = demo_loader->Get("testvalue_str"); //Grab string modified with function - String function_return = demo_loader->GetVar("function_return"); + String function_return = demo_loader->Get("function_return"); //Grab global values modified with DemoObject - int32_t loluademo_return = demo_loader->GetVar("loluademo_return"); - int32_t loluademo_getx = demo_loader->GetVar("loluademo_getx"); - float loluademo_inst_return = demo_loader->GetVar("loluademo_inst_return"); + int32_t loluademo_return = demo_loader->Get("loluademo_return"); + int32_t loluademo_getx = demo_loader->Get("loluademo_getx"); + float loluademo_inst_return = demo_loader->Get("loluademo_inst_return"); DemoObject* loluademo_inst = demo_loader->GetPtr("loluademo_inst"); msg::info("Lua Vars: \ diff --git a/src/easymesh/easymeshlua.cpp b/src/easymesh/easymeshlua.cpp index 258b3182..a68c6a7e 100644 --- a/src/easymesh/easymeshlua.cpp +++ b/src/easymesh/easymeshlua.cpp @@ -27,7 +27,7 @@ EasyMeshLuaLoader::EasyMeshLuaLoader() : LuaLoader() lua_State* l = GetLuaState(); //Registering demo object - LuaObjectDef::Register(l); + LuaObjectHelper::Register(l); } //----------------------------------------------------------------------------- @@ -88,10 +88,10 @@ EasyMeshLuaObject* EasyMeshLuaObject::New(lua_State* l, int arg_nb) } //----------------------------------------------------------------------------- -const LuaObjectLib* EasyMeshLuaObject::GetLib() +const LuaObjectLibrary* EasyMeshLuaObject::GetLib() { typedef EasyMeshLuaObject EMLO; - static const LuaObjectLib lib = LuaObjectLib( + static const LuaObjectLibrary lib = LuaObjectLibrary( "EasyMesh", //Statics { { nullptr, nullptr } }, diff --git a/src/easymesh/easymeshlua.h b/src/easymesh/easymeshlua.h index 3632e2d4..35e76140 100644 --- a/src/easymesh/easymeshlua.h +++ b/src/easymesh/easymeshlua.h @@ -28,7 +28,7 @@ public: //------------------------------------------------------------------------- static EasyMeshLuaObject* New(lua_State* l, int arg_nb); - static const LuaObjectLib* GetLib(); + static const LuaObjectLibrary* GetLib(); //------------------------------------------------------------------------- static int AppendCylinder(lua_State* l) diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index ab47cc89..70a612b1 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -375,7 +375,7 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) m_vdecl->SetStream(vbo, m_attribs[0], m_attribs[1], m_attribs[2]); const ImDrawIdx* idx_buffer_offset = 0; - for (size_t cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[(int)cmd_i]; #ifdef SHOW_IMGUI_DEBUG diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index aaf6cb84..035a641b 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -16,6 +16,9 @@ #pragma once +#define OLD_VAR_SYSTEM true +#define NEW_VAR_SYSTEM true + namespace lol { @@ -33,69 +36,70 @@ typedef struct ClassVar } ClassVar; //----------------------------------------------------------------------------- -struct ObjectLib +class Object { - typedef struct ClassVarStr +public: + //----------------------------------------------------------------------------- + struct Library { - ClassVarStr() { } - ClassVarStr(String var_name, lua_CFunction get, lua_CFunction set) + typedef struct ClassVarStr { - m_get_name = String("Get") + var_name; - m_set_name = String("Set") + var_name; - m_get = get; - m_set = set; - } - String m_get_name = ""; - String m_set_name = ""; - lua_CFunction m_get = nullptr; - lua_CFunction m_set = nullptr; - } ClassVarStr; - - ObjectLib(String class_name, - array const& statics, - array const& methods, - array const& variables) - { - m_class_name = class_name; - m_static_name = class_name + "_lib"; - m_method_name = class_name + "_inst"; - - m_statics = statics; - if (m_statics.count() == 0 - || m_statics.last().name != nullptr - || m_statics.last().func != nullptr) - m_statics.push({ nullptr, nullptr }); - - m_methods = methods; - if (m_methods.count() == 0 - || m_methods.last().name != nullptr - || m_methods.last().func != nullptr) - m_methods.push({ nullptr, nullptr }); - - for (ClassVar const& cv : variables) + ClassVarStr() { } + ClassVarStr(String var_name, lua_CFunction get, lua_CFunction set) + { + m_get_name = String("Get") + var_name; + m_set_name = String("Set") + var_name; + m_get = get; + m_set = set; + } + String m_get_name = ""; + String m_set_name = ""; + lua_CFunction m_get = nullptr; + lua_CFunction m_set = nullptr; + } ClassVarStr; + + Library(String class_name, + array const& statics, + array const& methods, + array const& variables) { - if (cv.name && cv.get && cv.set) + m_class_name = class_name; + m_static_name = class_name + "_lib"; + m_method_name = class_name + "_inst"; + + m_statics = statics; + if (m_statics.count() == 0 + || m_statics.last().name != nullptr + || m_statics.last().func != nullptr) + m_statics.push({ nullptr, nullptr }); + + m_methods = methods; + if (m_methods.count() == 0 + || m_methods.last().name != nullptr + || m_methods.last().func != nullptr) + m_methods.push({ nullptr, nullptr }); + + for (ClassVar const& cv : variables) { - m_variables.push({ cv.name, cv.get, cv.set }); + if (cv.name && cv.get && cv.set) + { + m_variables.push({ cv.name, cv.get, cv.set }); + } } + if (m_variables.count() == 0 + || variables.last().name != nullptr + || variables.last().get != nullptr + || variables.last().set != nullptr) + m_variables.push(ClassVarStr()); } - if (m_variables.count() == 0 - || variables.last().name != nullptr - || variables.last().get != nullptr - || variables.last().set != nullptr) - m_variables.push(ClassVarStr()); - } - String m_class_name = ""; - String m_static_name = ""; - String m_method_name = ""; - array m_statics; - array m_methods; - array m_variables; -}; + String m_class_name = ""; + String m_static_name = ""; + String m_method_name = ""; + array m_statics; + array m_methods; + array m_variables; + }; -//----------------------------------------------------------------------------- -class Object -{ public: Object() { } virtual ~Object() { } @@ -106,19 +110,20 @@ public: ASSERT(false); return nullptr; } - static const ObjectLib* GetLib() { ASSERT(false); return nullptr; } + static const Library* GetLib() { ASSERT(false); return nullptr; } }; //----------------------------------------------------------------------------- // Class available to link C++ class to Lua methods //-- -class ObjectDef +class ObjectHelper { -public: +private: //------------------------------------------------------------------------- - ObjectDef() { } - virtual ~ObjectDef() { } + ObjectHelper() { } + virtual ~ObjectHelper() { } +public: //------------------------------------------------------------------------- template static void Register(lua_State *l) @@ -163,8 +168,8 @@ public: lua_setfield(l, -1, "__index"); //Create variables Get/Set - const array& variables = GetVariables(); - for (const ObjectLib::ClassVarStr& var : variables) + const array& variables = GetVariables(); + for (const Object::Library::ClassVarStr& var : variables) { if (!var.m_get || !var.m_set) continue; @@ -183,27 +188,29 @@ public: } private: + //------------------------------------------------------------------------- template - static const ObjectLib* GetLib() + static const Object::Library* GetLibrary() { - const ObjectLib* lib = TLuaClass::GetLib(); + const Object::Library* lib = TLuaClass::GetLib(); ASSERT(lib); return lib; } public: + //------------------------------------------------------------------------- template - static const char* GetObjectName() { return GetLib()->m_class_name.C(); } + static const char* GetObjectName() { return GetLibrary()->m_class_name.C(); } template - static const char* GetStaticName() { return GetLib()->m_static_name.C(); } + static const char* GetStaticName() { return GetLibrary()->m_static_name.C(); } template - static const char* GetMethodName() { return GetLib()->m_method_name.C(); } + static const char* GetMethodName() { return GetLibrary()->m_method_name.C(); } template - static const ClassMethod* GetStaticMethods() { return GetLib()->m_statics.data(); } + static const ClassMethod* GetStaticMethods() { return GetLibrary()->m_statics.data(); } template - static const ClassMethod* GetInstanceMethods() { return GetLib()->m_methods.data(); } + static const ClassMethod* GetInstanceMethods() { return GetLibrary()->m_methods.data(); } template - static const array& GetVariables() { return GetLib()->m_variables; } + static const array& GetVariables() { return GetLibrary()->m_variables; } protected: //------------------------------------------------------------------------- @@ -241,6 +248,9 @@ public: } }; +//----------------------------------------------------------------------------- +#if OLD_VAR_SYSTEM + //----------------------------------------------------------------------------- template class VarPtr @@ -311,7 +321,7 @@ protected: } virtual void InnerGet(lua_State* l, int& index) { - T** obj = static_cast(luaL_checkudata(l, index++, ObjectDef::GetMethodName())); + T** obj = static_cast(luaL_checkudata(l, index++, ObjectHelper::GetMethodName())); m_value = obj ? *obj : nullptr; } void InnerPush(lua_State* l) @@ -339,6 +349,7 @@ protected: } }; */ + //----------------------------------------------------------------------------- template class Var @@ -431,6 +442,7 @@ protected: }; //----------------------------------------------------------------------------- +#ifndef BASE_TYPES template<> inline bool Var::InnerIsValid(lua_State* l, int index) { return lua_isboolean(l, index); @@ -638,8 +650,10 @@ template<> inline int Var::InnerPush(lua_State* l) Var w = m_value.w; return (x.Return(l) + y.Return(l) + z.Return(l) + w.Return(l)); } +#endif //BASE_TYPES //----------------------------------------------------------------------------- +#ifndef CUSTOM_TYPES class VarColor { protected: @@ -866,42 +880,150 @@ protected: return s.Return(l); } }; +#endif //CUSTOM_TYPES + +#endif //OLD_VAR_SYSTEM +//----------------------------------------------------------------------------- +// Stack: Main class that encapsulates everything ----------------------------- //----------------------------------------------------------------------------- class Stack { public: + +#if NEW_VAR_SYSTEM + + //------------------------------------------------------------------------- + static Stack Begin(lua_State* state, int32_t start_index = 1) + { + return Stack(state, start_index); + } + + //------------------------------------------------------------------------- + int32_t End() + { + return m_result; + } + +#if !OLD_VAR_SYSTEM +protected: +#endif //!OLD_VAR_SYSTEM + +#endif //NEW_VAR_SYSTEM + Stack(lua_State* l, int32_t start_index = 1) { m_state = l; m_index = start_index; } virtual ~Stack() { } - inline operator int32_t() { return m_result; } + int32_t GetArgs() { return lua_gettop(m_state); } - //------------------------------------------------------------------------- - template - T GetVar(T defaultValue = 0, bool isOptional = false) - { - return Var(defaultValue, m_state, m_index, isOptional).GetValue(); - } +#if NEW_VAR_SYSTEM - //------------------------------------------------------------------------- - template - Stack& SetVar(T& value) - { - m_result += Var(value, m_state, m_index).Return(m_state); - return *this; - } +public: + //------------------------------------------------------------------------- + //The encapsulating struct for pointers + template + struct Ptr + { + public: + T* m_value = nullptr; + bool m_throw_error = false; + + //--------------------------------------------------------------------- + //m_value: Your stored value + //throw_error: If true, lua will throw an error if the value get fails + Ptr(T* value, bool throw_error = false) + { + m_value = value; + m_throw_error = throw_error; + } + Ptr(const T*& value) { m_value = value; } + inline operator T*() { return m_value; } + inline Ptr& operator=(T const*& value) { m_value = value; return *this; } + }; //------------------------------------------------------------------------- - int32_t Return() { return m_result; } + template T Get(bool isOptional = false) { return Get(InnerDefault(), isOptional); } + template SafeEnum GetEnum(bool isOptional = false) { return GetEnum(InnerDefaultSafeEnum(), isOptional); } + template Ptr

GetPtr(bool isOptional = false) { return GetPtr(InnerDefaultPtr

(), isOptional); } + + //------------------------------------------------------------------------- +#define DECLARE_STACK_GET(T0, T1, GET_NAME, INNER_IS_VALID, INNER_GET) \ + template \ + T1 GET_NAME(T1 value, bool isOptional = false) \ + { \ + bool is_nil = lua_isnil(m_state, m_index); \ + if (!isOptional || (!is_nil && INNER_IS_VALID())) \ + { \ + ASSERT(!is_nil); /* touky: should assert, though ? */ \ + return INNER_GET(value); \ + } \ + return value; \ + } + DECLARE_STACK_GET(T, T, Get, InnerIsValid, InnerGet); + DECLARE_STACK_GET(E, SafeEnum, GetEnum, InnerIsValidSafeEnum, InnerGetSafeEnum); + DECLARE_STACK_GET(P, Ptr

, GetPtr, InnerIsValidPtr, InnerGetPtr); + +#undef DECLARE_STACK_GET + + //------------------------------------------------------------------------- + template Stack& operator<<(T& value) { m_result += InnerPush(value); return *this; } + template Stack& operator<<(SafeEnum& value) { m_result += InnerPushSafeEnum(value); return *this; } + template Stack& operator<<(Ptr

& value) { m_result += InnerPushPtr

(value); return *this; } + +protected: + #define INNER_ERROR "Your type is not implemented. For pointers, use LuaPtr()" + template T InnerDefault() { return T(0); } + template bool InnerIsValid() { ASSERT(false, INNER_ERROR); return false; } + template T InnerGet(T value) { UNUSED(value); ASSERT(false, INNER_ERROR); return InnerDefault(); } + template int InnerPush(T value) { UNUSED(value); ASSERT(false, INNER_ERROR); return 0; } + +#ifndef INNER_T_T + //template class C> C InnerDefault() { return C(); } + //template bool InnerIsValid() { return InnerIsValid(); } + //template SafeEnum InnerGet(SafeEnum value) { return FindValue >(InnerGet(value.ToString().C())); } + //template int InnerPush(SafeEnum value) { return InnerPush(value.ToString.C()); } +#endif //STACK_STRING + +#ifndef INNER_SAFE_ENUM + template SafeEnum InnerDefaultSafeEnum() { return SafeEnum(); } + template bool InnerIsValidSafeEnum() { return InnerIsValid(); } + template SafeEnum InnerGetSafeEnum(SafeEnum value) { return FindValue >(InnerGet(value.ToString())); } + template int InnerPushSafeEnum(SafeEnum value) { return InnerPush(value.ToString()); } +#endif //STACK_STRING + +#ifndef INNER_PTR + template inline Ptr

InnerDefaultPtr() { return Ptr

(nullptr); } + template inline bool InnerIsValidPtr() { return !!lua_isuserdata(m_state, m_index); } + template inline Ptr

InnerGetPtr(Ptr

value) + { + P** obj = static_cast(value.m_throw_error + ? luaL_checkudata(m_state, m_index++, ObjectHelper::GetMethodName

()) + : luaL_testudata(m_state, m_index++, ObjectHelper::GetMethodName

()) ); + return Ptr

(obj ? *obj : value.m_value); + } + template inline int InnerPushPtr(Ptr

value) + { + P** data = (P**)lua_newuserdata(m_state, sizeof(P*)); + *data = value.m_value; + } +#endif //STACK_STRING + +#endif //NEW_VAR_SYSTEM + + //------------------------------------------------------------------------- +#if OLD_VAR_SYSTEM +public: + inline operator int32_t() { return m_result; } //------------------------------------------------------------------------- + template Stack& operator>>(T& var) { @@ -909,6 +1031,8 @@ public: return *this; } +#if !NEW_VAR_SYSTEM + //------------------------------------------------------------------------- template Stack& operator<<(T& var) @@ -917,16 +1041,152 @@ public: return *this; } +#endif //NEW_VAR_SYSTEM + +#endif //OLD_VAR_SYSTEM + private: lua_State* m_state = nullptr; int32_t m_index = 1; int32_t m_result = 0; }; +//----------------------------------------------------------------------------- +#if NEW_VAR_SYSTEM + +#ifndef REGION_STACK_VAR + +//#if OLD_VAR_SYSTEM && NEW_VAR_SYSTEM +// +////------------------------------------------------------------------------- +//template +//Stack& Stack::operator<<(Var& var) +//{ +// m_result += var.Return(m_state); +// return *this; +//} +// +//#endif //OLD_VAR_SYSTEM && NEW_VAR_SYSTEM + +#ifndef STACK_BOOL +template<> inline bool Stack::InnerIsValid() { return lua_isboolean(m_state, m_index); } +template<> inline bool Stack::InnerGet(bool value) { UNUSED(value); return !!lua_toboolean(m_state, m_index++); } +template<> inline int Stack::InnerPush(bool value) { lua_pushboolean(m_state, value); return 1; } +#endif // STACK_BOOL + +//----------------------------------------------------------------------------- +#ifndef STACK_CHAR_CONST +template<> inline bool Stack::InnerIsValid() { return !!lua_isstring(m_state, m_index); } +template<> inline char const* Stack::InnerGet(char const* value) { UNUSED(value); return lua_tostring(m_state, m_index++); } +template<> inline int Stack::InnerPush(char const* value) { lua_pushstring(m_state, value); return 1; } +#endif // STACK_CHAR_CONST + +//----------------------------------------------------------------------------- +#ifndef STACK_STRING +template<> inline String Stack::InnerDefault() { return String(); } +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline String Stack::InnerGet(String value) { return String(InnerGet(value.C())); } +template<> inline int Stack::InnerPush(String value) { return InnerPush(value.C()); } +#endif //STACK_STRING + +//----------------------------------------------------------------------------- +#ifndef STACK_STRING +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline double Stack::InnerGet(double value) { UNUSED(value); return lua_tonumber(m_state, m_index++); } +template<> inline int Stack::InnerPush(double value) { lua_pushnumber(m_state, value); return 1; } +#endif //STACK_STRING + +//----------------------------------------------------------------------------- +#ifndef STACK_FLOAT +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline float Stack::InnerGet(float value) { return (float)InnerGet((double)value); } +template<> inline int Stack::InnerPush(float value) { return InnerPush((double)value); } +#endif //STACK_FLOAT + +//----------------------------------------------------------------------------- +//DO NOT REMOVE: IT IS THE BASIS FOR INT 32 +#ifndef STACK_INT64 +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline int64_t Stack::InnerGet(int64_t value) { UNUSED(value); return lua_tointeger(m_state, m_index++); } +template<> inline int Stack::InnerPush(int64_t value) { lua_pushinteger(m_state, value); return 1; } +#endif //STACK_INT64 + +//----------------------------------------------------------------------------- +#ifndef STACK_UINT64 +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline uint64_t Stack::InnerGet(uint64_t value) { UNUSED(value); return (uint64_t)lua_tointeger(m_state, m_index++); } +template<> inline int Stack::InnerPush(uint64_t value) { lua_pushinteger(m_state, (lua_Unsigned)value); return 1; } +#endif //STACK_UINT64 + +//----------------------------------------------------------------------------- +#ifndef STACK_INT32 +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline int32_t Stack::InnerGet(int32_t value) { UNUSED(value); return (int32_t)lua_tointeger(m_state, m_index++); } +template<> inline int Stack::InnerPush(int32_t value) { lua_pushinteger(m_state, (lua_Integer)value); return 1; } +#endif STACK_INT32 + +//----------------------------------------------------------------------------- +#ifndef STACK_UINT32 +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline uint32_t Stack::InnerGet(uint32_t value) { UNUSED(value); return (uint32_t)(lua_Unsigned)lua_tointeger(m_state, m_index++); } +template<> inline int Stack::InnerPush(uint32_t value) { lua_pushinteger(m_state, (lua_Unsigned)value); return 1; } +#endif //STACK_UINT32 + +//----------------------------------------------------------------------------- +#ifndef STACK_VEC2 +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline vec2 Stack::InnerGet(vec2 value) { return vec2(InnerGet(value.x), Get(value.y, true)); } +template<> inline int Stack::InnerPush(vec2 value) { return (InnerPush(value.x) + InnerPush(value.y)); } +#endif //STACK_VEC2 + +//----------------------------------------------------------------------------- +#ifndef STACK_VEC3 +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline vec3 Stack::InnerGet(vec3 value) { return vec3(InnerGet(value.x), Get(value.y, true), Get(value.z, true)); } +template<> inline int Stack::InnerPush(vec3 value) { return (InnerPush(value.x) + InnerPush(value.y) + InnerPush(value.z)); } +#endif //STACK_VEC3 + +//----------------------------------------------------------------------------- +#ifndef STACK_VEC4 +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline vec4 Stack::InnerGet(vec4 value) { return vec4(InnerGet(value.x), Get(value.y, true), Get(value.z, true), Get(value.w, true)); } +template<> inline int Stack::InnerPush(vec4 value) { return (InnerPush(value.x) + InnerPush(value.y) + InnerPush(value.z) + InnerPush(value.w)); } +#endif STACK_VEC4 + +//----------------------------------------------------------------------------- +#ifndef STACK_SAFE_ENUM +//template inline SafeEnum Stack::InnerDefault() { return SafeEnum(); } +//template inline bool Stack::InnerIsValid >() { return InnerIsValid(); } +//template inline SafeEnum Stack::InnerGet >(SafeEnum value) { return FindValue >(InnerGet(value.ToString().C())); } +//template inline int Stack::InnerPush >(SafeEnum value) { return InnerPush(value.ToString.C()); } +#endif //STACK_STRING + +//----------------------------------------------------------------------------- +#ifndef STACK_PTR +//template inline Stack::Ptr

Stack::InnerDefault >() { return Stack::Ptr

(nullptr); } +//template inline bool Stack::InnerIsValid >() { return !!lua_isuserdata(m_state, m_index); } +//template inline Stack::Ptr

Stack::InnerGet >(Stack::Ptr

value) +//{ +// P** obj = static_cast(value.m_throw_error +// ? luaL_checkudata(m_state, m_index++, ObjectHelper::GetMethodName

()) +// : luaL_testudata(m_state, m_index++, ObjectHelper::GetMethodName

()) ); +// return Stack::Ptr

(obj ? *obj : value.m_value); +//} +//template inline int Stack::InnerPush >(Stack::Ptr

value) +//{ +// P** data = (P**)lua_newuserdata(m_state, sizeof(P*)); +// *data = value.m_value; +//} +#endif //STACK_STRING + +#endif //REGION_STACK_VAR + +#endif //NEW_VAR_SYSTEM + //----------------------------------------------------------------------------- class Loader { - friend class ObjectDef; + friend class ObjectHelper; public: Loader(); virtual ~Loader(); @@ -934,6 +1194,27 @@ public: bool ExecLuaFile(String const &lua); bool ExecLuaCode(String const &lua); +#if NEW_VAR_SYSTEM + //------------------------------------------------------------------------- +#define DECLARE_LOADER_GET(T0, T1, GET_NAME) \ + template \ + T1 GET_NAME(String const &name) \ + { \ + lua_getglobal(m_lua_state, name.C()); \ + auto stack = LuaStack::Begin(m_lua_state, -1); \ + auto result = stack.GET_NAME(); \ + lua_pop(m_lua_state, 1); \ + return result; \ + } + + DECLARE_LOADER_GET(T, T, Get); + DECLARE_LOADER_GET(E, SafeEnum, GetEnum); + DECLARE_LOADER_GET(P, P*, GetPtr); + +#undef DECLARE_LOADER_GET + +#else //OLD_VAR_SYSTEM + template T GetVar(String const &name) { @@ -951,6 +1232,8 @@ public: return var(); } +#endif //OLD_VAR_SYSTEM + protected: lua_State* GetLuaState(); static void Store(lua_State* l, Loader* loader); @@ -964,10 +1247,33 @@ private: }; //----------------------------------------------------------------------------- -// ObjectDef member implementations that require VarPtr +// ObjectHelper member implementations that require VarPtr +#if NEW_VAR_SYSTEM template -int ObjectDef::Store(lua_State * l) +int ObjectHelper::Store(lua_State * l) +{ + auto stack = LuaStack::Begin(l); + auto obj = stack.Get >(); + ASSERT(obj); + Loader::StoreObject(l, obj); + return 0; +} + +template +int ObjectHelper::Del(lua_State * l) +{ + auto stack = LuaStack::Begin(l); + auto obj = stack.Get >(); + ASSERT(obj); + delete obj; + return 0; +} + +#else //OLD_VAR_SYSTEM + +template +int ObjectHelper::Store(lua_State * l) { VarPtr obj; obj.Get(l, 1); @@ -977,7 +1283,7 @@ int ObjectDef::Store(lua_State * l) } template -int ObjectDef::Del(lua_State * l) +int ObjectHelper::Del(lua_State * l) { VarPtr obj; obj.Get(l, 1); @@ -986,13 +1292,22 @@ int ObjectDef::Del(lua_State * l) return 0; } +#endif //OLD_VAR_SYSTEM + + } /* namespace Lolua */ +//TYPEDEFS typedef Lolua::Function LuaFunction; -typedef Lolua::ObjectDef LuaObjectDef; +typedef Lolua::ObjectHelper LuaObjectHelper; typedef Lolua::Object LuaObject; -typedef Lolua::ObjectLib LuaObjectLib; +typedef Lolua::Object::Library LuaObjectLibrary; typedef Lolua::Loader LuaLoader; +typedef Lolua::Stack LuaStack; +template using LuaPtr = Lolua::Stack::Ptr

; +//template +//typedef Lolua::Stack::Ptr

LuaPtr

; +#if OLD_VAR_SYSTEM typedef Lolua::Var LuaBool; typedef Lolua::Var LuaCharPtr; typedef Lolua::Var LuaString; @@ -1006,6 +1321,6 @@ typedef Lolua::Var LuaVec2; typedef Lolua::Var LuaVec3; typedef Lolua::Var LuaVec4; typedef Lolua::VarColor LuaColor; -typedef Lolua::Stack LuaStack; +#endif //OLD_VAR_SYSTEM } /* namespace lol */ diff --git a/src/utils.h b/src/utils.h index 3faf9d84..5929394e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -61,6 +61,12 @@ template< class T > inline int GetRandom(array src) // Gets the value for the given enum type. template inline T FindValue(const char* name) +{ + auto str = String(name); + return FindValue(str); +} + +template inline T FindValue(String const& name) { String n = name; n.to_lower(); @@ -72,10 +78,6 @@ template inline T FindValue(const char* name) } return T::Max; } -template inline T FindValue(String const& name) -{ - return FindValue(name.C()); -} } /* namespace lol */