New inheritance graph code.
[luabind.git] / luabind / container_policy.hpp
blob68ad0a90f483c1a42e0e010a8d64693812188ace
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>
29 #include <boost/mpl/apply_wrap.hpp>
31 namespace luabind { namespace detail {
33 namespace mpl = boost::mpl;
35 template<class Policies>
36 struct container_converter_lua_to_cpp
38 int const consumed_args(...)
40 return 1;
43 template<class T>
44 T apply(lua_State* L, by_const_reference<T>, int index)
46 typedef typename T::value_type value_type;
48 typedef typename find_conversion_policy<1, Policies>::type converter_policy;
49 typename mpl::apply_wrap2<converter_policy,value_type,lua_to_cpp>::type converter;
51 T container;
53 lua_pushnil(L);
54 while (lua_next(L, index))
56 container.push_back(converter.apply(L, LUABIND_DECORATE_TYPE(value_type), -1));
57 lua_pop(L, 1); // pop value
60 return container;
63 template<class T>
64 T apply(lua_State* L, by_value<T>, int index)
66 return apply(L, by_const_reference<T>(), index);
69 template<class T>
70 static int match(lua_State* L, by_const_reference<T>, int index)
72 if (lua_istable(L, index)) return 0; else return -1;
75 template<class T>
76 void converter_postcall(lua_State*, T, int) {}
79 template<class Policies>
80 struct container_converter_cpp_to_lua
82 template<class T>
83 void apply(lua_State* L, const T& container)
85 typedef typename T::value_type value_type;
87 typedef typename find_conversion_policy<1, Policies>::type converter_policy;
88 typename mpl::apply_wrap2<converter_policy,value_type,lua_to_cpp>::type converter;
90 lua_newtable(L);
92 int index = 1;
94 for (typename T::const_iterator i = container.begin(); i != container.end(); ++i)
96 converter.apply(L, *i);
97 lua_rawseti(L, -2, index);
98 ++index;
103 template<int N, class Policies>
104 // struct container_policy : converter_policy_tag
105 struct container_policy : conversion_policy<N>
107 // BOOST_STATIC_CONSTANT(int, index = N);
109 static void precall(lua_State*, const index_map&) {}
110 static void postcall(lua_State*, const index_map&) {}
112 struct only_accepts_nonconst_pointers {};
114 template<class T, class Direction>
115 struct apply
117 typedef typename boost::mpl::if_<boost::is_same<lua_to_cpp, Direction>
118 , container_converter_lua_to_cpp<Policies>
119 , container_converter_cpp_to_lua<Policies>
120 >::type type;
126 namespace luabind
128 template<int N>
129 detail::policy_cons<detail::container_policy<N, detail::null_type>, detail::null_type>
130 container(LUABIND_PLACEHOLDER_ARG(N))
132 return detail::policy_cons<detail::container_policy<N, detail::null_type>, detail::null_type>();
135 template<int N, class Policies>
136 detail::policy_cons<detail::container_policy<N, Policies>, detail::null_type>
137 container(LUABIND_PLACEHOLDER_ARG(N), const Policies&)
139 return detail::policy_cons<detail::container_policy<N, Policies>, detail::null_type>();
143 #endif // LUABIND_CONTAINER_POLICY_HPP_INCLUDED