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