Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / include / luabind / container_policy.hpp
blob38cfaec6779d4b3858b27b9e7172b4b685820fac
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 template<class T>
39 T apply(lua_State* L, by_const_reference<T>, int index)
41 typedef typename T::value_type value_type;
43 typedef typename find_conversion_policy<1, Policies>::type converter_policy;
44 typename mpl::apply_wrap2<converter_policy,value_type,lua_to_cpp>::type converter;
46 T container;
48 lua_pushnil(L);
49 while (lua_next(L, index))
51 container.push_back(converter.apply(L, LUABIND_DECORATE_TYPE(value_type), -1));
52 lua_pop(L, 1); // pop value
55 return container;
58 template<class T>
59 T apply(lua_State* L, by_value<T>, int index)
61 return apply(L, by_const_reference<T>(), index);
64 template<class T>
65 static int match(lua_State* L, by_const_reference<T>, int index)
67 if (lua_istable(L, index)) return 0; else return -1;
70 template<class T>
71 void converter_postcall(lua_State*, T, int) {}
74 template<class Policies>
75 struct container_converter_cpp_to_lua
77 template<class T>
78 void apply(lua_State* L, const T& container)
80 typedef typename T::value_type value_type;
82 typedef typename find_conversion_policy<1, Policies>::type converter_policy;
83 typename mpl::apply_wrap2<converter_policy,value_type,lua_to_cpp>::type converter;
85 lua_newtable(L);
87 int index = 1;
89 for (typename T::const_iterator i = container.begin(); i != container.end(); ++i)
91 converter.apply(L, *i);
92 lua_rawseti(L, -2, index);
93 ++index;
98 template<int N, class Policies>
99 // struct container_policy : converter_policy_tag
100 struct container_policy : conversion_policy<N>
102 // BOOST_STATIC_CONSTANT(int, index = N);
104 static void precall(lua_State*, const index_map&) {}
105 static void postcall(lua_State*, const index_map&) {}
107 struct only_accepts_nonconst_pointers {};
109 template<class T, class Direction>
110 struct apply
112 typedef typename boost::mpl::if_<boost::is_same<lua_to_cpp, Direction>
113 , container_converter_lua_to_cpp<Policies>
114 , container_converter_cpp_to_lua<Policies>
115 >::type type;
121 namespace luabind
123 template<int N>
124 detail::policy_cons<detail::container_policy<N, detail::null_type>, detail::null_type>
125 container(LUABIND_PLACEHOLDER_ARG(N))
127 return detail::policy_cons<detail::container_policy<N, detail::null_type>, detail::null_type>();
130 template<int N, class Policies>
131 detail::policy_cons<detail::container_policy<N, Policies>, detail::null_type>
132 container(LUABIND_PLACEHOLDER_ARG(N), const Policies&)
134 return detail::policy_cons<detail::container_policy<N, Policies>, detail::null_type>();
138 #endif // LUABIND_CONTAINER_POLICY_HPP_INCLUDED