Bläddra i källkod

Add generic wrapping

Ole 10 år sedan
förälder
incheckning
0b289a7d5b
5 ändrade filer med 78 tillägg och 15 borttagningar
  1. 3 3
      examples/functions.cpp
  2. 4 4
      examples/usertypes.cpp
  3. 1 0
      lib/luwra.hpp
  4. 62 0
      lib/luwra/generic.hpp
  5. 8 8
      tests/usertypes.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(my_function_1);
+	auto wrapped_1 = LUWRA_WRAP(my_function_1);
 	luwra::setGlobal(state, "my_function_1", wrapped_1);
 
 	// Register 'my_function_2'
-	auto wrapped_2 = LUWRA_WRAP_FUNCTION(my_function_2);
+	auto wrapped_2 = LUWRA_WRAP(my_function_2);
 	luwra::setGlobal(state, "my_function_2", wrapped_2);
 
 	// Register 'my_function_3'
-	auto wrapped_3 = LUWRA_WRAP_FUNCTION(my_function_3);
+	auto wrapped_3 = LUWRA_WRAP(my_function_3);
 	luwra::setGlobal(state, "my_function_3", wrapped_3);
 
 	// Load Lua code

+ 4 - 4
examples/usertypes.cpp

@@ -37,13 +37,13 @@ int main() {
 		state,
 		// Methods which shall be availabe in the Lua user data, need to be declared here
 		{
-			LUWRA_METHOD(Point, scale),
-			LUWRA_FIELD(Point, x),
-			LUWRA_FIELD(Point, y)
+			LUWRA_MEMBER(Point, scale),
+			LUWRA_MEMBER(Point, x),
+			LUWRA_MEMBER(Point, y)
 		},
 		// Meta methods may be registered aswell
 		{
-			LUWRA_METHOD(Point, __tostring)
+			LUWRA_MEMBER(Point, __tostring)
 		}
 	);
 

+ 1 - 0
lib/luwra.hpp

@@ -9,6 +9,7 @@
 #include "luwra/methods.hpp"
 #include "luwra/fields.hpp"
 #include "luwra/usertypes.hpp"
+#include "luwra/generic.hpp"
 #include "luwra/state.hpp"
 
 #endif

+ 62 - 0
lib/luwra/generic.hpp

@@ -0,0 +1,62 @@
+#ifndef LUWRA_GENERIC_H_
+#define LUWRA_GENERIC_H_
+
+#include "common.hpp"
+#include "functions.hpp"
+#include "methods.hpp"
+#include "fields.hpp"
+
+LUWRA_NS_BEGIN
+
+namespace internal {
+	template <typename T>
+	struct GenericWrapper {
+		static_assert(
+			sizeof(T) == -1,
+			"Parameter to GenericWrapper is not a valid type"
+		);
+	};
+
+	// Functions
+	template <typename R, typename... A>
+	struct GenericWrapper<R (A...)>:
+		FunctionWrapper<R (A...)> {};
+
+	template <typename R, typename... A>
+	struct GenericWrapper<R (*)(A...)>:
+		FunctionWrapper<R (*)(A...)> {};
+
+	// Methods
+	template <typename T, typename R, typename... A>
+	struct GenericWrapper<R (T::*)(A...) const volatile>:
+		MethodWrapper<R (T::*)(A...) const volatile> {};
+
+	template <typename T, typename R, typename... A>
+	struct GenericWrapper<R (T::*)(A...) const>:
+		MethodWrapper<R (T::*)(A...) const> {};
+
+	template <typename T, typename R, typename... A>
+	struct GenericWrapper<R (T::*)(A...) volatile>:
+		MethodWrapper<R (T::*)(A...) volatile> {};
+
+	template <typename T, typename R, typename... A>
+	struct GenericWrapper<R (T::*)(A...)>:
+		MethodWrapper<R (T::*)(A...)> {};
+
+	// Fields
+	template <typename T, typename R>
+	struct GenericWrapper<R T::*>: FieldWrapper<R T::*> {};
+
+	template <typename T, typename R>
+	struct GenericWrapper<const R T::*>: FieldWrapper<const R T::*> {};
+}
+
+LUWRA_NS_END
+
+#define LUWRA_WRAP(entity) \
+	(&luwra::internal::GenericWrapper<decltype(&entity)>::template invoke<&entity>)
+
+#define LUWRA_MEMBER(type, name) \
+	{__STRING(name), LUWRA_WRAP(type::name)}
+
+#endif

+ 8 - 8
tests/usertypes.cpp

@@ -68,10 +68,10 @@ TEST_CASE("usertypes_wrap_fields") {
 	luwra::registerUserType<B>(
 		state,
 		{
-			{"n", LUWRA_WRAP_FIELD(B::n)},
-			{"cn", LUWRA_WRAP_FIELD(B::cn)},
-			{"vn", LUWRA_WRAP_FIELD(B::vn)},
-			{"cvn", LUWRA_WRAP_FIELD(B::cvn)}
+			LUWRA_MEMBER(B, n),
+			LUWRA_MEMBER(B, cn),
+			LUWRA_MEMBER(B, vn),
+			LUWRA_MEMBER(B, cvn)
 		}
 	);
 
@@ -145,10 +145,10 @@ TEST_CASE("usertypes_wrap_methods") {
 	luwra::registerUserType<C>(
 		state,
 		{
-			{"foo1", LUWRA_WRAP_METHOD(C::foo1)},
-			{"foo2", LUWRA_WRAP_METHOD(C::foo2)},
-			{"foo3", LUWRA_WRAP_METHOD(C::foo3)},
-			{"foo4", LUWRA_WRAP_METHOD(C::foo4)}
+			LUWRA_MEMBER(C, foo1),
+			LUWRA_MEMBER(C, foo2),
+			LUWRA_MEMBER(C, foo3),
+			LUWRA_MEMBER(C, foo4)
 		}
 	);