types.cpp 1015 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <lua.hpp>
  2. #include <luwra.hpp>
  3. #include <iostream>
  4. using namespace luwra;
  5. // You may add custom specializations of luwra::Value in order to retrieve them from the stack.
  6. template <>
  7. struct Value<char> {
  8. /**
  9. * Retrieve the value at position `n`.
  10. */
  11. static inline
  12. char Read(State* state, int n) {
  13. auto str = Value<std::string>::Read(state, n);
  14. if (str.length() < 1) {
  15. luaL_argerror(state, n, "Given empty string instead of character");
  16. }
  17. return str[0];
  18. }
  19. /**
  20. * Push the value onto the stack.
  21. */
  22. static inline
  23. int Push(State* state, char val) {
  24. if (val == 0)
  25. return 0;
  26. lua_pushlstring(state, &val, 1);
  27. return 1;
  28. }
  29. };
  30. static
  31. void read_chars(char a, char b) {
  32. std::cout << "Got '" << a << b << "'" << std::endl;
  33. }
  34. int main() {
  35. lua_State* state = luaL_newstate();
  36. luaL_openlibs(state);
  37. // Build stack
  38. Value<char>::Push(state, 'H');
  39. Value<char>::Push(state, 'i');
  40. // Apply function to stack values
  41. Apply(state, read_chars);
  42. lua_close(state);
  43. return 0;
  44. }