ソースを参照

Clean up method wrapping

Ole 10 年 前
コミット
aac8a5af39
共有2 個のファイルを変更した21 個の追加81 個の削除を含む
  1. 7 14
      lib/luwra/functions.hpp
  2. 14 67
      lib/luwra/usertypes.hpp

+ 7 - 14
lib/luwra/functions.hpp

@@ -23,8 +23,8 @@ namespace internal {
 	};
 
 	template <typename... A>
-	struct FunctionWrapper<void(A...)> {
-		template <void (*function_pointer)(A...)> static inline
+	struct FunctionWrapper<void (A...)> {
+		template <void (* function_pointer)(A...)> static inline
 		int invoke(State* state) {
 			apply(state, function_pointer);
 			return 0;
@@ -32,8 +32,8 @@ namespace internal {
 	};
 
 	template <typename R, typename... A>
-	struct FunctionWrapper<R(A...)> {
-		template <R (*function_pointer)(A...)> static inline
+	struct FunctionWrapper<R (A...)> {
+		template <R (* function_pointer)(A...)> static inline
 		int invoke(State* state) {
 			return push(
 				state,
@@ -42,24 +42,17 @@ namespace internal {
 		}
 	};
 
+	// We need an alias, because function pointers are weird
 	template <typename R, typename... A>
 	struct FunctionWrapper<R(*)(A...)>: FunctionWrapper<R(A...)> {};
 }
 
-/**
- * \deprecated
- */
-template <
-	typename S,
-	S function_pointer
->
-constexpr CFunction wrap_function =
-	&internal::FunctionWrapper<S>::template invoke<function_pointer>;
-
 LUWRA_NS_END
 
 /**
  * Generate a `lua_CFunction` wrapper for a function.
+ * \param fun Fully qualified function name (Do not supply a pointer)
+ * \return Wrapped function as `lua_CFunction`
  */
 #define LUWRA_WRAP_FUNCTION(fun) \
 	(&luwra::internal::FunctionWrapper<decltype(&fun)>::template invoke<&fun>)

+ 14 - 67
lib/luwra/usertypes.hpp

@@ -151,7 +151,7 @@ namespace internal {
 	/**
 	 * Helper struct for wrapping user type methods
 	 */
-	template <typename T, typename S>
+	template <typename T>
 	struct MethodWrapper {
 		static_assert(
 			sizeof(T) == -1,
@@ -161,7 +161,7 @@ namespace internal {
 
 	// 'const volatile'-qualified methods
 	template <typename T, typename R, typename... A>
-	struct MethodWrapper<const volatile T, R(A...)> {
+	struct MethodWrapper<R (T::*)(A...) const volatile> {
 		using MethodPointerType = R (T::*)(A...) const volatile;
 		using FunctionSignature = R (const volatile T*, A...);
 
@@ -178,7 +178,7 @@ namespace internal {
 
 	// 'const'-qualified methods
 	template <typename T, typename R, typename... A>
-	struct MethodWrapper<const T, R(A...)> {
+	struct MethodWrapper<R (T::*)(A...) const> {
 		using MethodPointerType = R (T::*)(A...) const;
 		using FunctionSignature = R (const T*, A...);
 
@@ -195,7 +195,7 @@ namespace internal {
 
 	// 'volatile'-qualified methods
 	template <typename T, typename R, typename... A>
-	struct MethodWrapper<volatile T, R(A...)> {
+	struct MethodWrapper<R (T::*)(A...) volatile> {
 		using MethodPointerType = R (T::*)(A...) volatile;
 		using FunctionSignature = R (volatile T*, A...);
 
@@ -212,7 +212,7 @@ namespace internal {
 
 	// unqualified methods
 	template <typename T, typename R, typename... A>
-	struct MethodWrapper<T, R(A...)> {
+	struct MethodWrapper<R (T::*)(A...)> {
 		using MethodPointerType = R (T::*)(A...);
 		using FunctionSignature = R (T*, A...);
 
@@ -226,34 +226,6 @@ namespace internal {
 			return FunctionWrapper<FunctionSignature>::template invoke<call<method_pointer>>(state);
 		}
 	};
-
-	template <typename T>
-	struct MethodWrapperHelper {
-		static_assert(
-			sizeof(T) == -1,
-			"Parameter to MethodWrapperHelper is not a function pointer"
-		);
-	};
-
-	template <typename T, typename R, typename... A>
-	struct MethodWrapperHelper<R (T::*)(A...) const volatile>:
-		MethodWrapper<const volatile T, R(A...)>
-	{};
-
-	template <typename T, typename R, typename... A>
-	struct MethodWrapperHelper<R (T::*)(A...) const>:
-		MethodWrapper<const T, R(A...)>
-	{};
-
-	template <typename T, typename R, typename... A>
-	struct MethodWrapperHelper<R (T::*)(A...) volatile>:
-		MethodWrapper<volatile T, R(A...)>
-	{};
-
-	template <typename T, typename R, typename... A>
-	struct MethodWrapperHelper<R (T::*)(A...)>:
-		MethodWrapper<T, R(A...)>
-	{};
 }
 
 /**
@@ -335,40 +307,6 @@ constexpr CFunction wrap_constructor =
 #define LUWRA_WRAP_CONSTRUCTOR(type, ...) \
 	(luwra::wrap_constructor<type, __VA_ARGS__>)
 
-/**
- * Works similiar to `wrap_function`. Given a class or struct declaration as follows:
- *
- *   struct T {
- *     R my_method(A0, A1 ... An);
- *   };
- *
- * You might wrap this method easily:
- *
- *   CFunction wrapped_meth = wrap_method<T, R(A0, A1 ... An), &T::my_method>;
- *
- * In Lua, assuming `instance` is a userdata instance of type `T`, x0, x1 ... xn are instances
- * of A0, A1 ... An, and the method has been bound as `my_method`; it is possible to invoke the
- * method like so:
- *
- *   instance:my_method(x0, x1 ... xn)
- */
-template <
-	typename T,
-	typename S,
-	typename internal::MethodWrapper<T, S>::MethodPointerType method_pointer
->
-constexpr CFunction wrap_method =
-	wrap_function<
-		typename internal::MethodWrapper<T, S>::FunctionSignature,
-		internal::MethodWrapper<T, S>::template call<method_pointer>
-	>;
-
-/**
- * This macro allows you to wrap methods without supplying template parameters.
- */
-#define LUWRA_WRAP_METHOD(meth) \
-	(&luwra::internal::MethodWrapperHelper<decltype(&meth)>::template invoke<&meth>)
-
 /**
  * Property accessor method
  *
@@ -443,4 +381,13 @@ void register_user_type(
 
 LUWRA_NS_END
 
+/**
+ * Generate a `lua_CFunction` wrapper for a method.
+ * \param fun Fully qualified method name (Do not supply a pointer)
+ * \return Wrapped function as `lua_CFunction`
+ */
+#define LUWRA_WRAP_METHOD(meth) \
+	(&luwra::internal::MethodWrapper<decltype(&meth)>::template invoke<&meth>)
+
+
 #endif