test_collapse_converter.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright Daniel Wallin 2009. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "test.hpp"
  5. #include <luabind/luabind.hpp>
  6. struct X
  7. {
  8. X(int x, int y)
  9. : x(x)
  10. , y(y)
  11. {}
  12. int x;
  13. int y;
  14. };
  15. namespace luabind {
  16. int combine_score(int s1, int s2)
  17. {
  18. if (s1 < 0 || s2 < 0) return -1;
  19. return s1 + s2;
  20. }
  21. template <>
  22. struct default_converter<X>
  23. : native_converter_base<X>
  24. {
  25. int const consumed_args(...)
  26. {
  27. return 2;
  28. }
  29. int compute_score(lua_State* L, int index)
  30. {
  31. return combine_score(
  32. c1.compute_score(L, index), c2.compute_score(L, index + 1));
  33. }
  34. X from(lua_State* L, int index)
  35. {
  36. return X(lua_tonumber(L, index), lua_tonumber(L, index + 1));
  37. }
  38. default_converter<int> c1;
  39. default_converter<int> c2;
  40. };
  41. } // namespace luabind
  42. int take(X x)
  43. {
  44. return x.x + x.y;
  45. }
  46. void test_main(lua_State* L)
  47. {
  48. using namespace luabind;
  49. module(L) [
  50. def("take", &take)
  51. ];
  52. DOSTRING(L,
  53. "assert(take(1,1) == 2)\n"
  54. "assert(take(2,3) == 5)\n"
  55. );
  56. }