浏览代码

Add stack example

Ole Krüger 10 年之前
父节点
当前提交
1b6fe632b8
共有 2 个文件被更改,包括 35 次插入1 次删除
  1. 1 1
      examples/Makefile
  2. 34 0
      examples/stack.cpp

+ 1 - 1
examples/Makefile

@@ -3,7 +3,7 @@ RM              = rm -rf
 EXEC            = exec
 
 # Artifacts
-OBJS            = functions.out methods.out
+OBJS            = functions.out methods.out stack.out
 DEPS            = $(OBJS:%=%.d)
 
 # Compiler

+ 34 - 0
examples/stack.cpp

@@ -0,0 +1,34 @@
+#include <lua.hpp>
+#include <luwra.hpp>
+
+#include <iostream>
+
+using namespace luwra;
+
+static
+lua_Integer sum3(lua_Integer a, lua_Integer b, lua_Integer c) {
+	return a + b + c;
+}
+
+int main() {
+	lua_State* state = luaL_newstate();
+	luaL_openlibs(state);
+
+	// Build stack
+	lua_pushinteger(state, 13);
+	lua_pushinteger(state, 37);
+	lua_pushinteger(state, 42);
+
+	// Each value can be retrieved individually.
+	std::cout << "a = " << Value<lua_Integer>::read(state, 1) << std::endl;
+	std::cout << "b = " << Value<lua_Integer>::read(state, 2) << std::endl;
+	std::cout << "c = " << Value<lua_Integer>::read(state, 3) << std::endl;
+
+	// ... which is a little cumbersome. Instead we might apply a fitting function to our stack.
+	std::cout << "a + b + c = "
+	          << apply(state, sum3)
+	          << std::endl;
+
+	lua_close(state);
+	return 0;
+}