Parcourir la Source

Add overload for 'push' which allows you to push multiple values at once

Ole il y a 10 ans
Parent
commit
5b02184c41
2 fichiers modifiés avec 25 ajouts et 0 suppressions
  1. 5 0
      examples/types.cpp
  2. 20 0
      lib/luwra/types.hpp

+ 5 - 0
examples/types.cpp

@@ -51,6 +51,11 @@ int main() {
 
 	// apply function to stack values
 	luwra::apply(state, read_chars);
+	lua_pop(state, 2);
+
+	// Build stack again
+	luwra::push(state, 'Y', 'o');
+	luwra::apply(state, read_chars);
 
 	lua_close(state);
 	return 0;

+ 20 - 0
lib/luwra/types.hpp

@@ -79,6 +79,26 @@ int push(State* state, T value) {
 	return Value<T>::push(state, value);
 }
 
+/**
+ * Allows you to push multiple values at once.
+ */
+template <typename T1, typename T2, typename... TR>
+int push(State* state, T1 value, T2&& head, TR&&... rest) {
+	int result = push(state, value);
+
+	if (result < 0)
+		return result;
+
+	int other = push(state, std::forward<T2>(head), std::forward<TR>(rest)...);
+
+	if (other < 0) {
+		lua_pop(state, result);
+		return other;
+	} else {
+		return result + other;
+	}
+}
+
 /**
  * Convenient wrapper for [Value<T>::read](@ref Value<T>::read).
  */