Procházet zdrojové kódy

Add new stack interaction function 'call'

Ole před 10 roky
rodič
revize
87a4b3e308
2 změnil soubory, kde provedl 58 přidání a 14 odebrání
  1. 2 14
      lib/luwra/functions.hpp
  2. 56 0
      lib/luwra/stack.hpp

+ 2 - 14
lib/luwra/functions.hpp

@@ -18,27 +18,15 @@ namespace internal {
 	struct FunctionWrapper {
 		static_assert(
 			sizeof(T) == -1,
-			"Parameter to FunctionWrapper is not a function signature"
+			"Parameter to FunctionWrapper is not a valid signature"
 		);
 	};
 
-	template <typename... A>
-	struct FunctionWrapper<void (A...)> {
-		template <void (* fun)(A...)> static inline
-		int invoke(State* state) {
-			direct<void (A...)>(state, fun);
-			return 0;
-		}
-	};
-
 	template <typename R, typename... A>
 	struct FunctionWrapper<R (A...)> {
 		template <R (* fun)(A...)> static inline
 		int invoke(State* state) {
-			return push(
-				state,
-				direct<R (A...)>(state, fun)
-			);
+			return call<R (A...)>(state, fun);
 		}
 	};
 

+ 56 - 0
lib/luwra/stack.hpp

@@ -129,6 +129,62 @@ R apply(State* state, const std::function<R(A...)>& fun) {
 	return apply(state, 1, fun);
 }
 
+namespace internal {
+	template <typename T>
+	struct LayoutCaller {
+		static_assert(
+			sizeof(T) == -1,
+			"Parameter to LayoutCaller is not a valid signature"
+		);
+	};
+
+	template <typename... A>
+	struct LayoutCaller<void (A...)> {
+		template <typename F, typename... X> static inline
+		int call(State* state, int n, F&& hook, X&&... args) {
+			direct<void (A...)>(
+				state,
+				n,
+				std::forward<F>(hook),
+						std::forward<X>(args)...
+			);
+			return 0;
+		}
+	};
+
+	template <typename R, typename... A>
+	struct LayoutCaller<R (A...)> {
+		template <typename F, typename... X> static inline
+		int call(State* state, int n, F&& hook, X&&... args) {
+			return push(
+				state,
+				direct<R (A...)>(
+					state,
+					n,
+					std::forward<F>(hook),
+					std::forward<X>(args)...
+				)
+			);
+		}
+	};
+}
+
+/**
+ * \todo Document me
+ */
+template <typename S, typename F> static inline
+int call(State* state, int pos, F&& hook) {
+	return internal::LayoutCaller<S>::call(state, pos, std::forward<F>(hook));
+}
+
+/**
+ * \todo Document me
+ */
+template <typename S, typename F> static inline
+int call(State* state, F&& hook) {
+	return call<S>(state, 1, std::forward<F>(hook));
+}
+
 LUWRA_NS_END
 
 #endif