From b68a57c65cdc27bc96bfd07f706bac32302e5fd8 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sun, 25 May 2014 12:30:44 -0400 Subject: [PATCH 1/4] Fixed rename mishap --- sol/error.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sol/error.hpp b/sol/error.hpp index 205b2832..e2eaa15c 100644 --- a/sol/error.hpp +++ b/sol/error.hpp @@ -19,8 +19,8 @@ // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#ifndef error_HPP -#define error_HPP +#ifndef SOL_ERROR_HPP +#define SOL_ERROR_HPP #include #include @@ -32,4 +32,4 @@ public: }; } // sol -#endif // error_HPP \ No newline at end of file +#endif // SOL_ERROR_HPP \ No newline at end of file From 41e1ca2baa7d361e718b206e69bf8a6e347540b5 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sun, 25 May 2014 13:46:23 -0400 Subject: [PATCH 2/4] Additions for gitignore for the new files that are necessary to kick around qtCreator Fix for userdata to accept base classes where the derived class uses the name of a base member function to access it (Derived::get_num, where get_num is only implemented in Base::get_num) VC++ makes this acceptance easy by taking the name as "Derived", but GCC and Clang --- .gitignore | 6 ++++++ sol/userdata.hpp | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6ef4ae8d..80f44be7 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,9 @@ liblua.a sol/include/ .ninja* include/ +lib/liblua5.2.a +*.config +*.creator +*.files +*.includes +main.cpp diff --git a/sol/userdata.hpp b/sol/userdata.hpp index 117cfa4f..d6c8af02 100644 --- a/sol/userdata.hpp +++ b/sol/userdata.hpp @@ -110,8 +110,9 @@ private: template void build_function_tables() {} - template - void build_function_tables(std::string name, Ret T::* func, Args&&... args) { + template + void build_function_tables(std::string name, Ret TBase::* func, Args&&... args) { + static_assert(std::is_base_of::value, "Any registered function must be part of the class"); typedef typename std::decay::type function_type; functionnames.push_back(std::move(name)); functions.emplace_back(detail::make_unique>(std::move(func))); From 936d52b6e691ecff5d009dcc1f72a6b4895e601b Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sun, 25 May 2014 14:25:08 -0400 Subject: [PATCH 3/4] main.ninja for compiling with QtCreator on Windows. Really need Shogun... --- main.ninja | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 main.ninja diff --git a/main.ninja b/main.ninja new file mode 100644 index 00000000..004164bf --- /dev/null +++ b/main.ninja @@ -0,0 +1,27 @@ +# This file has been generated by shinobi version 0.9.5 + +ninja_required_version = 1.3 +builddir = bin +objdir = obj +cxx = g++ +cxxflags = -std=c++11 -pedantic -pedantic-errors -Wextra -Wall -O2 -DNDEBUG +incflags = -I"." -I"./include" -I"." -I"/usr/include/lua5.2" -I"./lua-5.2.2/src/" -I"./Catch/include/" +linkflags = -static -L"./lib" -llua5.2 + +rule compile + command = $cxx -MMD -MF $out.d $cxxflags -c $in -o $out $incflags + deps = gcc + depfile = $out.d + description = Compiling $in + +rule link + command = $cxx $in -o $out $linkflags + description = Creating $out + +build $objdir/main.o: compile main.cpp + +build $builddir/tests: link $objdir/main.o + +build install: phony $builddir/tests + +default install From b153b3433447cc755f3f2dd93bd8124abedb0c46 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sun, 25 May 2014 14:41:06 -0400 Subject: [PATCH 4/4] Added tests to ensure the derived/base case doesn't slip by us again! --- tests.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/tests.cpp b/tests.cpp index 89de8659..c6ac269a 100644 --- a/tests.cpp +++ b/tests.cpp @@ -50,6 +50,27 @@ int plop_xyz(int x, int y, std::string z) { return 11; } +class Base { +public: + Base(int a_num) : m_num(a_num) { } + + int get_num() { + return m_num; + } + +protected: + int m_num; +}; + +class Derived : public Base { +public: + Derived(int a_num) : Base(a_num) { } + + int get_num_10() { + return 10 * m_num; + } +}; + TEST_CASE("simple/set_global", "Check if the set_global works properly.") { sol::state lua; @@ -254,7 +275,7 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work std::cout << "stateless lambda()" << std::endl; return "test"; } - ); + ); REQUIRE_NOTHROW(run_script(lua)); lua.get("os").set_function("fun", &free_function); @@ -272,7 +293,7 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work std::cout << "stateless lambda()" << std::endl; return "test"; } - ); + ); REQUIRE_NOTHROW(run_script(lua)); // r-value, cannot optimise @@ -430,8 +451,8 @@ TEST_CASE("tables/userdata utility", "Show internal management of classes regist lua.new_userdata("fuser", "add", &fuser::add, "add2", &fuser::add2); lua.script("a = fuser.new()\n" - "b = a:add(1)\n" - "c = a:add2(1)\n"); + "b = a:add(1)\n" + "c = a:add2(1)\n"); sol::object a = lua.get("a"); sol::object b = lua.get("b"); @@ -448,3 +469,27 @@ TEST_CASE("tables/userdata utility", "Show internal management of classes regist REQUIRE(bresult == 1); REQUIRE(cresult == 3); } + +TEST_CASE("tables/userdata utility derived", "userdata classes must play nice when a derived class does not overload a publically visible base function") { + sol::state lua; + lua.open_libraries(sol::lib::base); + sol::constructors> basector; + sol::userdata baseuserdata("Base", basector, "get_num", &Base::get_num); + + lua.set_userdata(baseuserdata); + + lua.script("base = Base.new(5)"); + lua.script("print(base:get_num())"); + + sol::constructors> derivedctor; + sol::userdata deriveduserdata("Derived", derivedctor, "get_num", &Derived::get_num, "get_num_10", &Derived::get_num_10); + + lua.set_userdata(deriveduserdata); + + lua.script("derived = Derived.new(7)"); + lua.script("dgn10 = derived:get_num_10()\nprint(dgn10)"); + lua.script("dgn = derived:get_num()\nprint(dgn)"); + + REQUIRE((lua.get("dgn10") == 70)); + REQUIRE((lua.get("dgn") == 7)); +}