stack.cpp 767 B

1234567891011121314151617181920212223242526272829303132
  1. #include <lua.hpp>
  2. #include <luwra.hpp>
  3. #include <iostream>
  4. static
  5. double sum3(int a, int b, double c) {
  6. return (a + b) * c;
  7. }
  8. int main() {
  9. lua_State* state = luaL_newstate();
  10. luaL_openlibs(state);
  11. // Build stack
  12. luwra::push(state, 13);
  13. luwra::push(state, 37);
  14. luwra::push(state, 42.2);
  15. // Each value can be retrieved individually.
  16. std::cout << "a = " << luwra::read<int>(state, 1) << std::endl;
  17. std::cout << "b = " << luwra::read<int>(state, 2) << std::endl;
  18. std::cout << "c = " << luwra::read<double>(state, 3) << std::endl;
  19. // ... which is a little cumbersome. Instead we might apply a fitting function to our stack.
  20. std::cout << "(a + b) * c = "
  21. << luwra::apply(state, sum3)
  22. << std::endl;
  23. lua_close(state);
  24. return 0;
  25. }