container_policy.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef LUABIND_CONTAINER_POLICY_HPP_INCLUDED
  20. #define LUABIND_CONTAINER_POLICY_HPP_INCLUDED
  21. #include <luabind/config.hpp>
  22. #include <luabind/detail/policy.hpp>
  23. #include <boost/mpl/apply_wrap.hpp>
  24. namespace luabind { namespace detail {
  25. namespace mpl = boost::mpl;
  26. template<class Policies>
  27. struct container_converter_lua_to_cpp
  28. {
  29. int const consumed_args(...)
  30. {
  31. return 1;
  32. }
  33. template<class T>
  34. T apply(lua_State* L, by_const_reference<T>, int index)
  35. {
  36. typedef typename T::value_type value_type;
  37. typedef typename find_conversion_policy<1, Policies>::type converter_policy;
  38. typename mpl::apply_wrap2<converter_policy,value_type,lua_to_cpp>::type converter;
  39. T container;
  40. lua_pushnil(L);
  41. while (lua_next(L, index))
  42. {
  43. container.push_back(converter.apply(L, LUABIND_DECORATE_TYPE(value_type), -1));
  44. lua_pop(L, 1); // pop value
  45. }
  46. return container;
  47. }
  48. template<class T>
  49. T apply(lua_State* L, by_value<T>, int index)
  50. {
  51. return apply(L, by_const_reference<T>(), index);
  52. }
  53. template<class T>
  54. static int match(lua_State* L, by_const_reference<T>, int index)
  55. {
  56. if (lua_istable(L, index)) return 0; else return -1;
  57. }
  58. template<class T>
  59. void converter_postcall(lua_State*, T, int) {}
  60. };
  61. template<class Policies>
  62. struct container_converter_cpp_to_lua
  63. {
  64. template<class T>
  65. void apply(lua_State* L, const T& container)
  66. {
  67. typedef typename T::value_type value_type;
  68. typedef typename find_conversion_policy<1, Policies>::type converter_policy;
  69. typename mpl::apply_wrap2<converter_policy,value_type,lua_to_cpp>::type converter;
  70. lua_newtable(L);
  71. int index = 1;
  72. for (typename T::const_iterator i = container.begin(); i != container.end(); ++i)
  73. {
  74. converter.apply(L, *i);
  75. lua_rawseti(L, -2, index);
  76. ++index;
  77. }
  78. }
  79. };
  80. template<int N, class Policies>
  81. // struct container_policy : converter_policy_tag
  82. struct container_policy : conversion_policy<N>
  83. {
  84. // BOOST_STATIC_CONSTANT(int, index = N);
  85. static void precall(lua_State*, const index_map&) {}
  86. static void postcall(lua_State*, const index_map&) {}
  87. struct only_accepts_nonconst_pointers {};
  88. template<class T, class Direction>
  89. struct apply
  90. {
  91. typedef typename boost::mpl::if_<boost::is_same<lua_to_cpp, Direction>
  92. , container_converter_lua_to_cpp<Policies>
  93. , container_converter_cpp_to_lua<Policies>
  94. >::type type;
  95. };
  96. };
  97. }}
  98. namespace luabind
  99. {
  100. template<int N>
  101. detail::policy_cons<detail::container_policy<N, detail::null_type>, detail::null_type>
  102. container(LUABIND_PLACEHOLDER_ARG(N))
  103. {
  104. return detail::policy_cons<detail::container_policy<N, detail::null_type>, detail::null_type>();
  105. }
  106. template<int N, class Policies>
  107. detail::policy_cons<detail::container_policy<N, Policies>, detail::null_type>
  108. container(LUABIND_PLACEHOLDER_ARG(N), const Policies&)
  109. {
  110. return detail::policy_cons<detail::container_policy<N, Policies>, detail::null_type>();
  111. }
  112. }
  113. #endif // LUABIND_CONTAINER_POLICY_HPP_INCLUDED