From fb2e791f08f81cf01624b5790478b732f886c8f9 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 13 Aug 2018 23:25:39 +0200 Subject: [PATCH] [DEV] continue rework --- luaWrapper/luaWrapper.hpp | 67 +++++++++++++----------- luaWrapper/luaWrapperUtil.hpp | 67 +++++++++++++----------- sample/sample_1/LuaBankAccount.cpp | 68 ++++++++++++------------- sample/sample_1/LuaBankAccount.hpp | 2 +- sample/sample_1/main.cpp | 6 +-- sample/sample_2/Example.cpp | 43 ++++++++-------- sample/sample_2/Example.hpp | 43 +++++++++------- sample/sample_2/LuaExample.cpp | 82 +++++++++++++++--------------- sample/sample_2/LuaExample.hpp | 2 +- sample/sample_2/example2.lua | 4 +- sample/sample_2/main.cpp | 6 +-- 11 files changed, 206 insertions(+), 184 deletions(-) diff --git a/luaWrapper/luaWrapper.hpp b/luaWrapper/luaWrapper.hpp index 756d924..f5eb652 100644 --- a/luaWrapper/luaWrapper.hpp +++ b/luaWrapper/luaWrapper.hpp @@ -41,6 +41,7 @@ #include #include #include +#include #include @@ -73,7 +74,13 @@ namespace luaWrapper { } } void executeFile(const etk::String& _fileName) { - if (luaL_dofile(m_luaState, _fileName.c_str())) { + etk::String data = etk::FSNodeReadAllData(_fileName); + if (luaL_dostring(m_luaState, &data[0])) { + LUAW_PRINT(lua_tostring(m_luaState, -1)); + } + } + void executeString(const etk::String& _rawData) { + if (luaL_dostring(m_luaState, &_rawData[0])) { LUAW_PRINT(lua_tostring(m_luaState, -1)); } } @@ -122,7 +129,7 @@ namespace luaWrapper { * when and object is the type I want. This is only used internally. */ struct Userdata { - Userdata(ememory::SharedPtr _vptr = null, size_t _typeId = 0) : + Userdata(ememory::SharedPtr _vptr = null, size_t _typeId = 0): m_data(etk::move(_vptr)), m_typeId(_typeId) { // nothing to do ... @@ -174,7 +181,7 @@ namespace luaWrapper { } template - inline void wrapperfield(lua_State* _luaState, const char* _field) { + inline void wrapperField(lua_State* _luaState, const char* _field) { lua_getfield(_luaState, LUA_REGISTRYINDEX, LUAW_WRAPPER_KEY); // ... LuaWrapper lua_getfield(_luaState, -1, _field); // ... LuaWrapper LuaWrapper.field lua_getfield(_luaState, -1, LuaWrapper::classname); // ... LuaWrapper LuaWrapper.field LuaWrapper.field.class @@ -274,7 +281,7 @@ namespace luaWrapper { ememory::SharedPtr _obj) { if (_obj != null) { LuaWrapper::identifier(_luaState, _obj); // ... id - wrapperfield(_luaState, LUAW_CACHE_KEY); // ... id cache + wrapperField(_luaState, LUAW_CACHE_KEY); // ... id cache lua_pushvalue(_luaState, -2); // ... id cache id lua_gettable(_luaState, -2); // ... id cache obj if (lua_isnil(_luaState, -1)) { @@ -309,7 +316,7 @@ namespace luaWrapper { template bool hold(lua_State* _luaState, ememory::SharedPtr _obj) { - wrapperfield(_luaState, LUAW_HOLDS_KEY); // ... holds + wrapperField(_luaState, LUAW_HOLDS_KEY); // ... holds LuaWrapper::identifier(_luaState, _obj); // ... holds id lua_pushvalue(_luaState, -1); // ... holds id id lua_gettable(_luaState, -3); // ... holds id hold @@ -339,7 +346,7 @@ namespace luaWrapper { template void release(lua_State* _luaState, int _index) { - wrapperfield(_luaState, LUAW_HOLDS_KEY); // ... id ... holds + wrapperField(_luaState, LUAW_HOLDS_KEY); // ... id ... holds lua_pushvalue(_luaState, correctindex(_luaState, _index, 1)); // ... id ... holds id lua_pushnil(_luaState); // ... id ... holds id nil lua_settable(_luaState, -3); // ... id ... holds @@ -427,7 +434,7 @@ namespace luaWrapper { int index(lua_State* _luaState) { // obj key ememory::SharedPtr obj = to(_luaState, 1); - wrapperfield(_luaState, LUAW_STORAGE_KEY); // obj key storage + wrapperField(_luaState, LUAW_STORAGE_KEY); // obj key storage LuaWrapper::identifier(_luaState, obj); // obj key storage id lua_gettable(_luaState, -2); // obj key storage store // Check if storage table exists @@ -459,7 +466,7 @@ namespace luaWrapper { int newindex(lua_State* _luaState) { // obj key value ememory::SharedPtr obj = check(_luaState, 1); - wrapperfield(_luaState, LUAW_STORAGE_KEY); // obj key value storage + wrapperField(_luaState, LUAW_STORAGE_KEY); // obj key value storage LuaWrapper::identifier(_luaState, obj); // obj key value storage id lua_pushvalue(_luaState, -1); // obj key value storage id id lua_gettable(_luaState, -3); // obj key value storage id store @@ -490,13 +497,13 @@ namespace luaWrapper { /* ememory::SharedPtr obj = to(_luaState, 1); LuaWrapper::identifier(_luaState, obj); // obj key value storage id - wrapperfield(_luaState, LUAW_HOLDS_KEY); // obj id counts count holds + wrapperField(_luaState, LUAW_HOLDS_KEY); // obj id counts count holds lua_pushvalue(_luaState, 2); // obj id counts count holds id lua_gettable(_luaState, -2); // obj id counts count holds hold if (lua_toboolean(_luaState, -1)) { obj.reset(); } - wrapperfield(_luaState, LUAW_STORAGE_KEY); // obj id counts count holds hold storage + wrapperField(_luaState, LUAW_STORAGE_KEY); // obj id counts count holds hold storage lua_pushvalue(_luaState, 2); // obj id counts count holds hold storage id lua_pushnil(_luaState); // obj id counts count holds hold storage id nil lua_settable(_luaState, -3); // obj id counts count holds hold storage @@ -630,7 +637,7 @@ namespace luaWrapper { lua_pop(_luaState, 1); // ... LuaWrapper lua_getfield(_luaState, -1, LUAW_CACHE_KEY); // ... LuaWrapper LuaWrapper.cache lua_newtable(_luaState); // ... LuaWrapper LuaWrapper.cache {} - wrapperfield(_luaState, LUAW_CACHE_METATABLE_KEY); // ... LuaWrapper LuaWrapper.cache {} cmt + wrapperField(_luaState, LUAW_CACHE_METATABLE_KEY); // ... LuaWrapper LuaWrapper.cache {} cmt lua_setmetatable(_luaState, -2); // ... LuaWrapper LuaWrapper.cache {} lua_setfield(_luaState, -2, LuaWrapper::classname); // ... LuaWrapper LuaWrapper.cache lua_pop(_luaState, 2); // ... @@ -646,27 +653,27 @@ namespace luaWrapper { } template void setfuncs(lua_State* _luaState, - const char* _classname, - const luaL_Reg* _table, - const luaL_Reg* _metatable, - ememory::SharedPtr (*_allocator)(lua_State*)) { + const char* _classname, + const luaL_Reg* _table, + const luaL_Reg* _metatable, + ememory::SharedPtr (*_allocator)(lua_State*)) { setfuncs(_luaState, _classname, _table, _metatable, _allocator, defaultidentifier); } template void setfuncs(lua_State* _luaState, - const char* _classname, - const luaL_Reg* _table, - const luaL_Reg* _metatable) { + const char* _classname, + const luaL_Reg* _table, + const luaL_Reg* _metatable) { setfuncs(_luaState, _classname, _table, _metatable, defaultallocator, defaultidentifier); } template void registerElement(lua_State* _luaState, - const char* _classname, - const luaL_Reg* _table, - const luaL_Reg* _metatable, - ememory::SharedPtr (*_allocator)(lua_State*), - void (*_identifier)(lua_State*, ememory::SharedPtr)) { + const char* _classname, + const luaL_Reg* _table, + const luaL_Reg* _metatable, + ememory::SharedPtr (*_allocator)(lua_State*), + void (*_identifier)(lua_State*, ememory::SharedPtr)) { setfuncs(_luaState, _classname, _table, _metatable, _allocator, _identifier); // ... T lua_pushvalue(_luaState, -1); // ... LUAW_TYPE LUAW_TYPE lua_setglobal(_luaState, _classname); // ... LUAW_TYPE @@ -674,10 +681,10 @@ namespace luaWrapper { template void registerElement(lua_State* _luaState, - const char* _classname, - const luaL_Reg* _table, - const luaL_Reg* _metatable, - ememory::SharedPtr (*_allocator)(lua_State*)) { + const char* _classname, + const luaL_Reg* _table, + const luaL_Reg* _metatable, + ememory::SharedPtr (*_allocator)(lua_State*)) { setfuncs(_luaState, _classname, _table, _metatable, _allocator, defaultidentifier); lua_pushvalue(_luaState, -1); // ... LUAW_TYPE LUAW_TYPE lua_setglobal(_luaState, _classname); // ... LUAW_TYPE @@ -685,9 +692,9 @@ namespace luaWrapper { template void registerElement(lua_State* _luaState, - const char* _classname, - const luaL_Reg* _table, - const luaL_Reg* _metatable) { + const char* _classname, + const luaL_Reg* _table, + const luaL_Reg* _metatable) { setfuncs(_luaState, _classname, _table, _metatable, defaultallocator, defaultidentifier); // ... T lua_pushvalue(_luaState, -1); // ... T T lua_setglobal(_luaState, _classname); // ... T diff --git a/luaWrapper/luaWrapperUtil.hpp b/luaWrapper/luaWrapperUtil.hpp index b959a96..bec5372 100644 --- a/luaWrapper/luaWrapperUtil.hpp +++ b/luaWrapper/luaWrapperUtil.hpp @@ -107,7 +107,7 @@ namespace luaWrapper { * object through a getter or setter * * The interface to all of them is the same however. In addition to plain - * getter and setter functions, there is a getset which does both - if an + * getter and setter functions, there is a getSet which does both - if an * argument is supplied it attempts to set the value, and in either case it * returns the value. In your lua table declaration in C++ rather than write * individiual wrappers for each getter and setter you may do the following: @@ -116,7 +116,7 @@ namespace luaWrapper { * { * { "GetBar", luaWrapper::utils::get }, * { "SetBar", luaWrapper::utils::set }, - * { "Bar", luaWrapper::utils::getset }, + * { "Bar", luaWrapper::utils::getSet }, * { NULL, NULL } * } * @@ -135,7 +135,7 @@ namespace luaWrapper { * { * { "GetBar", luaWrapper::utils::get }, * { "SetBar", luaWrapper::utils::set }, - * { "Bar", luaWrapper::utils::getset }, + * { "Bar", luaWrapper::utils::getSet }, * } * * In a Lua script, you can now use foo:GetBar(), foo:SetBar() and foo:Bar() @@ -197,7 +197,7 @@ namespace luaWrapper { return 0; } - template int setandrelease(lua_State* _L) { + template int setAndRelease(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if (obj != null) { ememory::SharedPtr member = luaWrapper::opt(_L, 2); @@ -234,7 +234,7 @@ namespace luaWrapper { return 0; } - template int setandrelease(lua_State* _L) { + template int setAndRelease(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if (obj != null) { ememory::SharedPtr member = luaWrapper::opt(_L, 2); @@ -246,7 +246,7 @@ namespace luaWrapper { return 0; } - template int getset(lua_State* _L) { + template int getSet(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if ( obj != null && lua_gettop(_L) >= 2) { @@ -258,7 +258,7 @@ namespace luaWrapper { } } - template int getset(lua_State* _L) { + template int getSet(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if ( obj != null && lua_gettop(_L) >= 2) { @@ -271,7 +271,7 @@ namespace luaWrapper { } } - template int getsetandrelease(lua_State* _L) { + template int getSetAndRelease(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if ( obj != null && lua_gettop(_L) >= 2) { @@ -286,7 +286,7 @@ namespace luaWrapper { } } - template int getset(lua_State* _L) { + template int getSet(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if ( obj != null && lua_gettop(_L) >= 2) { @@ -298,7 +298,7 @@ namespace luaWrapper { } } - template int getset(lua_State* _L) { + template int getSet(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if ( obj != null && lua_gettop(_L) >= 2) { @@ -310,7 +310,7 @@ namespace luaWrapper { } } - template int getset(lua_State* _L) { + template int getSet(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if ( obj != null && lua_gettop(_L) >= 2) { @@ -322,7 +322,7 @@ namespace luaWrapper { } } - template int getset(lua_State* _L) { + template int getSet(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if ( obj != null && lua_gettop(_L) >= 2) { @@ -335,7 +335,7 @@ namespace luaWrapper { } } - template int getsetandrelease(lua_State* _L) { + template int getSetAndRelease(lua_State* _L) { ememory::SharedPtr obj = luaWrapper::check(_L, 1); if ( obj != null && lua_gettop(_L) >= 2) { @@ -359,12 +359,12 @@ namespace luaWrapper { * * struct Foo * { - * int DoSomething(int, const char*); + * int doSomething(int, const char*); * }; * * static luaL_reg Foo_metatable[] = * { - * { "DoSomething", luaWrapper::utils::func(&Foo::DoSomething) }, + * { "doSomething", luaWrapper::utils::func(&Foo::doSomething) }, * { NULL, NULL } * } * @@ -376,7 +376,7 @@ namespace luaWrapper { * This macro will expand based on the function signature of Foo::DoSomething * In this example, it would expand into the following wrapper: * - * luaWrapper::utils::push(luaWrapper::check(L, 1)->DoSomething(luaWrapper::utils::check(L, 2), luaWrapper::utils::check(L, 3))); + * luaWrapper::utils::push(luaWrapper::check(L, 1)->doSomething(luaWrapper::utils::check(L, 2), luaWrapper::utils::check(L, 3))); * return 1; * * In this example there is only one member function called DoSomething. In some @@ -457,27 +457,31 @@ namespace luaWrapper { /** * Member function wrapper */ - template struct MemberFuncWrapper; + template + struct MemberFuncWrapper; - template struct MemberFuncWrapper { + template + struct MemberFuncWrapper { public: static int call(lua_State* _L) { return callImpl(_L, makeIntRange<2,sizeof...(Args)>()); } private: template static int callImpl(lua_State* _L, IntPack) { - luaWrapper::utils::push(_L, ((*luaWrapper::check(_L, 1)).*MemberFunc)(luaWrapper::utils::check::type>(_L, indices)...)); + luaWrapper::utils::push(_L, ((*luaWrapper::check(_L, 1)).*MemberFunc)(luaWrapper::utils::check::type>(_L, indices)...)); return 1; } }; - template struct MemberFuncWrapper { + template + struct MemberFuncWrapper { public: static int call(lua_State* _L) { return callImpl(_L, luaWrapper::utils::makeIntRange<2, sizeof...(Args)>()); } private: - template static int callImpl(lua_State* _L, IntPack) { + template + static int callImpl(lua_State* _L, IntPack) { ((*luaWrapper::check(_L, 1)).*MemberFunc)(luaWrapper::utils::check::type>(_L, indices)...); return 0; } @@ -488,21 +492,24 @@ namespace luaWrapper { * static function wrapper */ - template struct StaticFuncWrapper; + template + struct StaticFuncWrapper; - template struct StaticFuncWrapper { + template + struct StaticFuncWrapper { public: static int call(lua_State* _L) { return callImpl(_L, luaWrapper::utils::makeIntRange<2,sizeof...(Args)>()); } private: template static int callImpl(lua_State* _L, IntPack) { - luaWrapper::utils::push(_L, (*Func)(luaWrapper::utils::check::type>(_L, indices)...)); + luaWrapper::utils::push(_L, (*LUAW_FUNCTION)(luaWrapper::utils::check::type>(_L, indices)...)); return 1; } }; - template struct StaticFuncWrapper { + template + struct StaticFuncWrapper { public: static int call(lua_State* _L) { return callImpl(_L, luaWrapper::utils::makeIntRange<2, sizeof...(Args)>()); @@ -510,7 +517,7 @@ namespace luaWrapper { private: template static int callImpl(lua_State* _L, luaWrapper::utils::IntPack) { - (*Func)(luaWrapper::utils::check::type>(_L, indices)...); + (*LUAW_FUNCTION)(luaWrapper::utils::check::type>(_L, indices)...); return 0; } }; @@ -532,13 +539,13 @@ namespace luaWrapper { * foo = Foo.new() * foo2 = foo:clone() */ - template int clone(lua_State* _L) { - T* obj = new T(*luaWrapper::check(_L, 1)); + template int clone(lua_State* _L) { + T* obj = new T(*luaWrapper::check(_L, 1)); lua_remove(_L, 1); // ... int numargs = lua_gettop(_L); luaWrapper::push(_L, obj); // ... clone luaWrapper::hold(_L, obj); - luaWrapper::postconstructor(_L, numargs); + luaWrapper::postconstructor(_L, numargs); return 1; } @@ -560,7 +567,7 @@ namespace luaWrapper { * After the object is constructed, luaWrapper::utils::build will do the equivalent of * calling f:X(10) and f:Y(20). */ - template int build(lua_State* _L) { + template int build(lua_State* _L) { // obj {} lua_insert(_L, -2); // {} obj if (lua_type(_L, 1) == LUA_TTABLE) { diff --git a/sample/sample_1/LuaBankAccount.cpp b/sample/sample_1/LuaBankAccount.cpp index 63743a8..e5a7742 100644 --- a/sample/sample_1/LuaBankAccount.cpp +++ b/sample/sample_1/LuaBankAccount.cpp @@ -18,9 +18,9 @@ ETK_DECLARE_TYPE(BankAccount); * This function is passed to luaWrapper::register. */ ememory::SharedPtr BankAccount_new(lua_State *_luaState) { - const char* owner = luaL_checkstring(_luaState, 1); - float balance = luaL_checknumber(_luaState, 2); - return ememory::makeShared(owner, balance); + const char* owner = luaL_checkstring(_luaState, 1); + float balance = luaL_checknumber(_luaState, 2); + return ememory::makeShared(owner, balance); } /** @@ -30,8 +30,8 @@ ememory::SharedPtr BankAccount_new(lua_State *_luaState) { */ int BankAccount_checkTotalMoneyInBank(lua_State *_luaState) { - lua_pushnumber(_luaState, BankAccount::checkTotalMoneyInBank()); - return 1; + lua_pushnumber(_luaState, BankAccount::checkTotalMoneyInBank()); + return 1; } /** @@ -43,52 +43,50 @@ int BankAccount_checkTotalMoneyInBank(lua_State *_luaState) { */ int BankAccount_getOwnerName(lua_State *_luaState) { - auto account = luaWrapper::check(_luaState, 1); - lua_pushstring(_luaState, account->getOwnerName()); - return 1; + auto account = luaWrapper::check(_luaState, 1); + lua_pushstring(_luaState, account->getOwnerName()); + return 1; } int BankAccount_deposit(lua_State* _luaState) { - auto account = luaWrapper::check(_luaState, 1); - float amount = luaL_checknumber(_luaState, 2); - account->deposit(amount); - return 0; + auto account = luaWrapper::check(_luaState, 1); + float amount = luaL_checknumber(_luaState, 2); + account->deposit(amount); + return 0; } int BankAccount_withdraw(lua_State* _luaState) { - auto account = luaWrapper::check(_luaState, 1); - float amount = luaL_checknumber(_luaState, 2); - account->withdraw(amount); - return 0; + auto account = luaWrapper::check(_luaState, 1); + float amount = luaL_checknumber(_luaState, 2); + account->withdraw(amount); + return 0; } int BankAccount_checkBalance(lua_State* _luaState) { - auto account = luaWrapper::check(_luaState, 1); - lua_pushnumber(_luaState, account->checkBalance()); - return 1; + auto account = luaWrapper::check(_luaState, 1); + lua_pushnumber(_luaState, account->checkBalance()); + return 1; } static luaL_Reg BankAccount_table[] = { - { "checkTotalMoneyInBank", BankAccount_checkTotalMoneyInBank }, - { NULL, NULL } + { "checkTotalMoneyInBank", BankAccount_checkTotalMoneyInBank }, + { NULL, NULL } }; static luaL_Reg BankAccount_metatable[] = { - { "getOwnerName", BankAccount_getOwnerName }, - { "deposit", BankAccount_deposit }, - { "withdraw", BankAccount_withdraw }, - { "checkBalance", BankAccount_checkBalance }, - { NULL, NULL } + { "getOwnerName", BankAccount_getOwnerName }, + { "deposit", BankAccount_deposit }, + { "withdraw", BankAccount_withdraw }, + { "checkBalance", BankAccount_checkBalance }, + { NULL, NULL } }; -int luaopen_BankAccount(lua_State* _luaState) { - luaWrapper::registerElement(_luaState, - "BankAccount", - BankAccount_table, - BankAccount_metatable, - BankAccount_new // If your class has a default constructor you can omit this argument, - // LuaWrapper will generate a default allocator for you. - ); - return 1; +int luaopen_BankAccount(luaWrapper:Lua& _lua) { + _lua.registerElement("BankAccount", + BankAccount_table, + BankAccount_metatable, + BankAccount_new // If your class has a default constructor you can omit this argument, LuaWrapper will generate a default allocator for you. + ); + return 1; } diff --git a/sample/sample_1/LuaBankAccount.hpp b/sample/sample_1/LuaBankAccount.hpp index b2d4c80..2019154 100644 --- a/sample/sample_1/LuaBankAccount.hpp +++ b/sample/sample_1/LuaBankAccount.hpp @@ -1,4 +1,4 @@ #pragma once struct lua_State; -int luaopen_BankAccount(lua_State* _luaState); +int luaopen_BankAccount(luaWrapper:Lua& _lua); diff --git a/sample/sample_1/main.cpp b/sample/sample_1/main.cpp index 0a8f89c..a9d5c71 100644 --- a/sample/sample_1/main.cpp +++ b/sample/sample_1/main.cpp @@ -26,12 +26,12 @@ int main(int _argc, const char *_argv[]) { } } if (inputFileName.empty() == true) { - TEST_ERROR("missing file..."); - usage(); + TEST_WARNING("missing file... ==> select default one"); + inputFileName = "DATA:example1.lua"; } { luaWrapper::Lua lua; - luaopen_BankAccount(lua.getState()); + luaopen_BankAccount(lua); lua.executeFile(inputFileName); } TEST_PRINT("END SAMPLE 1"); diff --git a/sample/sample_2/Example.cpp b/sample/sample_2/Example.cpp index df71d53..2911312 100644 --- a/sample/sample_2/Example.cpp +++ b/sample/sample_2/Example.cpp @@ -2,96 +2,97 @@ #include #include #include +#include ETK_DECLARE_TYPE(Example); // These are just a bunch of generic getters and setters for // values in the Example class -bool Example::GetBoolean() const { +bool Example::getBoolean() const { return m_boolean; } -int Example::GetInteger() const { +int Example::getInteger() const { return m_integer; } -unsigned int Example::GetUInteger() const { +unsigned int Example::getUInteger() const { return m_uinteger; } -const char* Example::GetCString() const { +const char* Example::getCString() const { return m_cstring; } -const etk::String& Example::GetCPPString() const { +const etk::String& Example::getCPPString() const { return m_cppstring; } -const Vector2D& Example::GetVec() const { +const Vector2D& Example::getVec() const { return m_vec; } -double Example::GetNumber() const { +double Example::getNumber() const { return m_number; } -float Example::GetFloatNumber() const { +float Example::getFloatNumber() const { return m_floatnumber; } -Example* Example::GetPtr() const { +Example* Example::getPtr() const { return m_ptr; } -void Example::SetBoolean(bool _val) { +void Example::setBoolean(bool _val) { m_boolean = _val; } -void Example::SetInteger(int _val) { +void Example::setInteger(int _val) { m_integer = _val; } -void Example::SetUInteger(unsigned int _val) { +void Example::setUInteger(unsigned int _val) { m_uinteger = _val; } -void Example::SetCString(const char* _val) { +void Example::setCString(const char* _val) { m_cstring = _val; } -void Example::SetCPPString(const etk::String& _val) { +void Example::setCPPString(const etk::String& _val) { m_cppstring = _val; } -void Example::SetVec(const Vector2D& _val) { +void Example::setVec(const Vector2D& _val) { m_vec = _val; } -void Example::SetNumber(double _val) { +void Example::setNumber(double _val) { m_number = _val; } -void Example::SetFloatNumber(float _val) { +void Example::setFloatNumber(float _val) { m_floatnumber = _val; } -void Example::SetPtr(Example* _val) { +void Example::setPtr(Example* _val) { m_ptr = _val; } int Example::DoSomething(bool _b) { - std::cout << "b = " << _b << std::endl; + TEST_PRINT("b = " << _b); return 0; } int Example::DoSomethingElse(int _i, int _j) { - std::cout << "i = " << _i << ", j = " << _j << std::endl; + TEST_PRINT("i = " << _i << ", j = " << _j); return 0; } int Example::DoSomethingElse(float _f) { - std::cout << "f = " << _f << std::endl; + TEST_PRINT("f = " << _f); return 0; } diff --git a/sample/sample_2/Example.hpp b/sample/sample_2/Example.hpp index 09e2ce6..35b011b 100644 --- a/sample/sample_2/Example.hpp +++ b/sample/sample_2/Example.hpp @@ -18,25 +18,32 @@ class Example { Example* m_ptr = null; Vector2D m_vec = Vector2D(4,6); - bool GetBoolean() const; - int GetInteger() const; - unsigned int GetUInteger() const; - const char* GetCString() const; - const etk::String& GetCPPString() const; - double GetNumber() const; - float GetFloatNumber() const; - Example* GetPtr() const; - const Vector2D& GetVec() const; + bool getBoolean() const; + void setBoolean(bool _val); - void SetBoolean(bool _val); - void SetInteger(int _val); - void SetUInteger(unsigned int _val); - void SetCString(const char* _val); - void SetCPPString(const etk::String& _val); - void SetNumber(double _val); - void SetFloatNumber(float _val); - void SetPtr(Example* _val); - void SetVec(const Vector2D& _Member); + int getInteger() const; + void setInteger(int _val); + + unsigned int getUInteger() const; + void setUInteger(unsigned int _val); + + const char* getCString() const; + void setCString(const char* _val); + + const etk::String& getCPPString() const; + void setCPPString(const etk::String& _val); + + double getNumber() const; + void setNumber(double _val); + + float getFloatNumber() const; + void setFloatNumber(float _val); + + Example* getPtr() const; + void setPtr(Example* _val); + + const Vector2D& getVec() const; + void setVec(const Vector2D& _Member); int DoSomething(bool _b); void DoSomething2(bool _b, int _p) {}; diff --git a/sample/sample_2/LuaExample.cpp b/sample/sample_2/LuaExample.cpp index e7ebee8..5d3472f 100644 --- a/sample/sample_2/LuaExample.cpp +++ b/sample/sample_2/LuaExample.cpp @@ -38,43 +38,43 @@ static luaL_Reg Example_metatable[] = { // Class | data type | class member { "getBoolean", luaWrapper::utils::get }, { "setBoolean", luaWrapper::utils::set }, - { "m_boolean", luaWrapper::utils::getset }, + { "m_boolean", luaWrapper::utils::getSet }, { "getInteger", luaWrapper::utils::get }, { "setInteger", luaWrapper::utils::set }, - { "m_integer", luaWrapper::utils::getset }, + { "m_integer", luaWrapper::utils::getSet }, { "getUInteger", luaWrapper::utils::get }, { "setUInteger", luaWrapper::utils::set }, - { "m_uinteger", luaWrapper::utils::getset }, + { "m_uinteger", luaWrapper::utils::getSet }, { "getCString", luaWrapper::utils::get }, { "setCString", luaWrapper::utils::set }, - { "m_string", luaWrapper::utils::getset }, + { "m_string", luaWrapper::utils::getSet }, { "getCPPString", luaWrapper::utils::get }, { "setCPPString", luaWrapper::utils::set }, - { "m_cppstring", luaWrapper::utils::getset }, + { "m_cppstring", luaWrapper::utils::getSet }, { "getVec", luaWrapper::utils::get }, { "setVec", luaWrapper::utils::set }, - { "m_vec", luaWrapper::utils::getset }, + { "m_vec", luaWrapper::utils::getSet }, { "getNumber", luaWrapper::utils::get }, { "setNumber", luaWrapper::utils::set }, - { "m_number", luaWrapper::utils::getset }, + { "m_number", luaWrapper::utils::getSet }, { "getFloatNumber", luaWrapper::utils::get }, { "setFloatNumber", luaWrapper::utils::set }, - { "m_floatnumber", luaWrapper::utils::getset }, + { "m_floatnumber", luaWrapper::utils::getSet }, { "getPtr", luaWrapper::utils::get }, { "setPtr", luaWrapper::utils::set }, - { "Ptr", luaWrapper::utils::getset }, + { "Ptr", luaWrapper::utils::getSet }, // The getters and setters above work on member variables directly, but // sometimes all you have are getter and setter functions instead of - // variables. You can still automate the creation of Getter and Setter Lua + // variables. You can still automate the creation of getter and setter Lua // function wrappers. // Normally it would be silly to have getter and setter functions for both @@ -85,55 +85,55 @@ static luaL_Reg Example_metatable[] = { // Class | data type | getter // Class | data type | setter // Class | data type | getter | setter - { "getBooleanFunc", luaWrapper::utils::get }, - { "setBooleanFunc", luaWrapper::utils::set }, - { "BooleanFunc", luaWrapper::utils::getset }, + { "getBooleanFunc", luaWrapper::utils::get }, + { "setBooleanFunc", luaWrapper::utils::set }, + { "BooleanFunc", luaWrapper::utils::getSet }, - { "getIntegerFunc", luaWrapper::utils::get }, - { "setIntegerFunc", luaWrapper::utils::set }, - { "IntegerFunc", luaWrapper::utils::getset }, + { "getIntegerFunc", luaWrapper::utils::get }, + { "setIntegerFunc", luaWrapper::utils::set }, + { "IntegerFunc", luaWrapper::utils::getSet }, - { "getUIntegerFunc", luaWrapper::utils::get }, - { "setUIntegerFunc", luaWrapper::utils::set }, - { "UIntegerFunc", luaWrapper::utils::getset }, + { "getUIntegerFunc", luaWrapper::utils::get }, + { "setUIntegerFunc", luaWrapper::utils::set }, + { "UIntegerFunc", luaWrapper::utils::getSet }, - { "getCStringFunc", luaWrapper::utils::get }, - { "setCStringFunc", luaWrapper::utils::set }, - { "CStringFunc", luaWrapper::utils::getset }, + { "getCStringFunc", luaWrapper::utils::get }, + { "setCStringFunc", luaWrapper::utils::set }, + { "CStringFunc", luaWrapper::utils::getSet }, - { "getNumberFunc", luaWrapper::utils::get }, - { "setNumberFunc", luaWrapper::utils::set }, - { "NumberFunc", luaWrapper::utils::getset }, + { "getNumberFunc", luaWrapper::utils::get }, + { "setNumberFunc", luaWrapper::utils::set }, + { "NumberFunc", luaWrapper::utils::getSet }, - { "getFloatNumberFunc", luaWrapper::utils::get }, - { "setFloatNumberFunc", luaWrapper::utils::set }, - { "FloatNumberFunc", luaWrapper::utils::getset }, + { "getFloatNumberFunc", luaWrapper::utils::get }, + { "setFloatNumberFunc", luaWrapper::utils::set }, + { "FloatNumberFunc", luaWrapper::utils::getSet }, - { "getPtrFunc", luaWrapper::utils::get }, - { "setPtrFunc", luaWrapper::utils::set }, - { "PtrFunc", luaWrapper::utils::getset }, + { "getPtrFunc", luaWrapper::utils::get }, + { "setPtrFunc", luaWrapper::utils::set }, + { "PtrFunc", luaWrapper::utils::getSet }, // In order to use luaWrapper::utils::get and luaWrapper::utils::set on non-primitive types, you must define luaWrapper::utils::to // and luaWrapper::utils::check for that type. // See LuaCustomTypes.hpp for an example involving etk::String and Vector2D - { "getCPPStringFunc", luaWrapper::utils::get }, - { "setCPPStringFunc", luaWrapper::utils::set }, - { "CPPStringFunc", luaWrapper::utils::getset }, + { "getCPPStringFunc", luaWrapper::utils::get }, + { "setCPPStringFunc", luaWrapper::utils::set }, + { "CPPStringFunc", luaWrapper::utils::getSet }, - { "getVecFunc", luaWrapper::utils::get }, - { "setSetFunc", luaWrapper::utils::set }, - { "VecFunc", luaWrapper::utils::getset }, + { "getVecFunc", luaWrapper::utils::get }, + { "setsetFunc", luaWrapper::utils::set }, + { "VecFunc", luaWrapper::utils::getSet }, - { "DoSomething", luaWrapperUtils_func(&Example::DoSomething) }, - { "DoSomething2", luaWrapperUtils_func(&Example::DoSomething2) }, + { "doSomething", luaWrapperUtils_func(&Example::doSomething) }, + { "doSomething2", luaWrapperUtils_func(&Example::doSomething2) }, //{ "DoSomethingElse1", luaWrapper::utils::funcsig(int, Example, DoSomethingElse, int, int) }, //{ "DoSomethingElse2", luaWrapper::utils::funcsig(int, Example, DoSomethingElse, float) }, { NULL, NULL } }; -int luaopen_Example(lua_State* _L) { - luaWrapper::registerElement(_L, "Example", NULL, Example_metatable); +int luaopen_Example(luaWrapper:Lua& _lua) { + _lua.registerElement("Example", NULL, Example_metatable); return 1; } diff --git a/sample/sample_2/LuaExample.hpp b/sample/sample_2/LuaExample.hpp index 716ad6f..d1d3d15 100644 --- a/sample/sample_2/LuaExample.hpp +++ b/sample/sample_2/LuaExample.hpp @@ -1,5 +1,5 @@ #pragma once struct lua_State; -int luaopen_Example(lua_State* _L); +int luaopen_Example(luaWrapper:Lua& _lua); diff --git a/sample/sample_2/example2.lua b/sample/sample_2/example2.lua index 88de036..621bf24 100644 --- a/sample/sample_2/example2.lua +++ b/sample/sample_2/example2.lua @@ -28,4 +28,6 @@ for k, v in pairs(vec2) do print(k, v) end -ex:printMe() \ No newline at end of file +ex:printMe() + +ex:doSomething(true, 12432) diff --git a/sample/sample_2/main.cpp b/sample/sample_2/main.cpp index d10ae28..2436c9d 100644 --- a/sample/sample_2/main.cpp +++ b/sample/sample_2/main.cpp @@ -31,12 +31,12 @@ int main(int _argc, const char *_argv[]) { } } if (inputFileName.empty() == true) { - TEST_ERROR("missing file..."); - usage(); + TEST_WARNING("missing file... ==> select default one"); + inputFileName = "DATA:example2.lua"; } { luaWrapper::Lua lua; - luaopen_Example(lua.getState()); + luaopen_Example(lua); lua.executeFile(inputFileName); } TEST_PRINT("END SAMPLE 2");