Sfoglia il codice sorgente

Add 'LUWRA_WRAP_FUNCTION' to make function wrapping easier

Ole 10 anni fa
parent
commit
50d52f1b66
3 ha cambiato i file con 34 aggiunte e 7 eliminazioni
  1. 3 3
      examples/functions.cpp
  2. 23 0
      lib/luwra/functions.hpp
  3. 8 4
      tests/functions.cpp

+ 3 - 3
examples/functions.cpp

@@ -23,15 +23,15 @@ int main() {
 	luaL_openlibs(state);
 
 	// Register 'my_function_1'
-	auto wrapped_1 = luwra::wrap_function<void(float, const char*), my_function_1>;
+	auto wrapped_1 = LUWRA_WRAP_FUNCTION(my_function_1);
 	luwra::register_global(state, "my_function_1", wrapped_1);
 
 	// Register 'my_function_2'
-	auto wrapped_2 = luwra::wrap_function<std::string(), my_function_2>;
+	auto wrapped_2 = LUWRA_WRAP_FUNCTION(my_function_2);
 	luwra::register_global(state, "my_function_2", wrapped_2);
 
 	// Register 'my_function_3'
-	auto wrapped_3 = luwra::wrap_function<int(int, int), my_function_3>;
+	auto wrapped_3 = LUWRA_WRAP_FUNCTION(my_function_3);
 	luwra::register_global(state, "my_function_3", wrapped_3);
 
 	// Load Lua code

+ 23 - 0
lib/luwra/functions.hpp

@@ -58,6 +58,23 @@ namespace internal {
 			);
 		}
 	};
+
+	template <typename T>
+	struct FunctionWrapperHelper {
+		static_assert(
+			sizeof(T) == -1,
+			"Parameter to FunctionWrapperHelper is not a function pointer"
+		);
+	};
+
+	template <typename R, typename... A>
+	struct FunctionWrapperHelper<R(*)(A...)> {
+		using Signature = R(A...);
+
+		template <R(*function_pointer)(A...)> static
+		constexpr CFunction wrapped =
+			&internal::FunctionWrapper<Signature>::template invoke<function_pointer>;
+	};
 }
 
 /**
@@ -80,6 +97,12 @@ template <
 constexpr CFunction wrap_function =
 	&internal::FunctionWrapper<S>::template invoke<function_pointer>;
 
+/**
+ * This macros allows you to wrap functions without providing a type signature.
+ */
+#define LUWRA_WRAP_FUNCTION(fun) \
+	(luwra::internal::FunctionWrapperHelper<decltype(&fun)>::wrapped<&fun>)
+
 LUWRA_NS_END
 
 #endif

+ 8 - 4
tests/functions.cpp

@@ -18,7 +18,8 @@ TEST_CASE("wrap_function_noret_noparams") {
 	noret_environment = 1337;
 
 	// Wrap function
-	lua_CFunction cfun = luwra::wrap_function<void(), test_function_noret_noparams>;
+	// lua_CFunction cfun = luwra::wrap_function<void(), test_function_noret_noparams>;
+	lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noret_noparams);
 	REQUIRE(cfun != nullptr);
 
 	// Register function
@@ -45,7 +46,8 @@ TEST_CASE("wrap_function_noret") {
 	int req_environemt = noret_environment;
 
 	// Wrap function
-	lua_CFunction cfun = luwra::wrap_function<void(int, int), test_function_noret>;
+	// lua_CFunction cfun = luwra::wrap_function<void(int, int), test_function_noret>;
+	lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noret);
 	REQUIRE(cfun != nullptr);
 
 	// Register function
@@ -68,7 +70,8 @@ TEST_CASE("wrap_function_noparams") {
 	lua_State* state = luaL_newstate();
 
 	// Wrap function
-	lua_CFunction cfun = luwra::wrap_function<int(), test_function_noparams>;
+	// lua_CFunction cfun = luwra::wrap_function<int(), test_function_noparams>;
+	lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noparams);
 	REQUIRE(cfun != nullptr);
 
 	// Register function
@@ -91,7 +94,8 @@ TEST_CASE("wrap_function") {
 	lua_State* state = luaL_newstate();
 
 	// Wrap function
-	lua_CFunction cfun = luwra::wrap_function<int(int, int), test_function>;
+	// lua_CFunction cfun = luwra::wrap_function<int(int, int), test_function>;
+	lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function);
 	REQUIRE(cfun != nullptr);
 
 	// Register function