OSDN Git Service

build moflib1.0 on cmake-base system
[moflib/moflib.git] / moflib-1.0 / extlib / luabind-0.8 / luabind / container_policy.hpp
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
2
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:
9
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12
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.
22
23
24 #ifndef LUABIND_CONTAINER_POLICY_HPP_INCLUDED
25 #define LUABIND_CONTAINER_POLICY_HPP_INCLUDED
26
27 #include <luabind/config.hpp>
28 #include <luabind/detail/policy.hpp>
29 #include <boost/mpl/apply_wrap.hpp>
30
31 namespace luabind { namespace detail {
32
33         namespace mpl = boost::mpl;
34
35         template<class Policies>
36         struct container_converter_lua_to_cpp
37         {
38                 template<class T>
39                 T apply(lua_State* L, by_const_reference<T>, int index)
40                 {
41                         typedef typename T::value_type value_type;
42
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;
45
46                         T container;
47
48                         lua_pushnil(L);
49                         while (lua_next(L, index))
50                         {
51                                 container.push_back(converter.apply(L, LUABIND_DECORATE_TYPE(value_type), -1));
52                                 lua_pop(L, 1); // pop value
53                         }
54
55                         return container;
56                 }
57
58                 template<class T>
59                 T apply(lua_State* L, by_value<T>, int index)
60                 {
61                         return apply(L, by_const_reference<T>(), index);
62                 }
63
64                 template<class T>
65                 static int match(lua_State* L, by_const_reference<T>, int index)
66                 {
67                         if (lua_istable(L, index)) return 0; else return -1;
68                 }
69
70                 template<class T>
71                 void converter_postcall(lua_State*, T, int) {}
72         };
73
74         template<class Policies>
75         struct container_converter_cpp_to_lua
76         {
77                 template<class T>
78                 void apply(lua_State* L, const T& container)
79                 {
80                         typedef typename T::value_type value_type;
81
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;
84
85                         lua_newtable(L);
86
87                         int index = 1;
88
89                         for (typename T::const_iterator i = container.begin(); i != container.end(); ++i)
90                         {
91                                 converter.apply(L, *i);
92                                 lua_rawseti(L, -2, index);
93                                 ++index;
94                         }
95                 }
96         };
97
98         template<int N, class Policies>
99 //      struct container_policy : converter_policy_tag
100         struct container_policy : conversion_policy<N>
101         {
102 //              BOOST_STATIC_CONSTANT(int, index = N);
103
104                 static void precall(lua_State*, const index_map&) {}
105                 static void postcall(lua_State*, const index_map&) {}
106
107                 struct only_accepts_nonconst_pointers {};
108
109                 template<class T, class Direction>
110                 struct apply
111                 {
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;
116                 };
117         };
118
119 }}
120
121 namespace luabind
122 {
123         template<int N>
124         detail::policy_cons<detail::container_policy<N, detail::null_type>, detail::null_type> 
125         container(LUABIND_PLACEHOLDER_ARG(N)) 
126         { 
127                 return detail::policy_cons<detail::container_policy<N, detail::null_type>, detail::null_type>(); 
128         }
129
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&) 
133         { 
134                 return detail::policy_cons<detail::container_policy<N, Policies>, detail::null_type>(); 
135         }
136 }
137
138 #endif // LUABIND_CONTAINER_POLICY_HPP_INCLUDED