Made it work on VC6.5 again. This involves changing generate_converter
[luabind.git] / luabind / iterator_policy.hpp
blobce50190bdc993875f6157ae0a799bcbab4f61414
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_ITERATOR_POLICY_HPP_INCLUDED
25 #define LUABIND_ITERATOR_POLICY_HPP_INCLUDED
27 #include <luabind/config.hpp>
28 #include <luabind/detail/policy.hpp>
29 #include <luabind/detail/implicit_cast.hpp>
30 #include <luabind/detail/convert_to_lua.hpp>
31 #include <boost/mpl/bool.hpp>
33 namespace luabind { namespace detail
35 template<class Iter>
36 struct iterator_state
38 typedef iterator_state<Iter> self_t;
40 static int step(lua_State* L)
42 self_t& state = *static_cast<self_t*>(lua_touserdata(L, lua_upvalueindex(1)));
44 if (state.start == state.end)
46 lua_pushnil(L);
48 else
50 convert_to_lua(L, *state.start);
51 ++state.start;
54 return 1;
57 iterator_state(const Iter& s, const Iter& e)
58 : start(s)
59 , end(e)
62 Iter start;
63 Iter end;
66 struct iterator_converter
68 typedef boost::mpl::bool_<false> is_value_converter;
69 typedef iterator_converter type;
71 template<class T>
72 void apply(lua_State* L, const T& c)
74 typedef typename T::const_iterator iter_t;
75 typedef iterator_state<iter_t> state_t;
77 // note that this should be destructed, for now.. just hope that iterator
78 // is a pod
79 void* iter = lua_newuserdata(L, sizeof(state_t));
80 new (iter) state_t(c.begin(), c.end());
81 lua_pushcclosure(L, state_t::step, 1);
84 template<class T>
85 void apply(lua_State* L, T& c)
87 typedef typename T::iterator iter_t;
88 typedef iterator_state<iter_t> state_t;
90 // note that this should be destructed, for now.. just hope that iterator
91 // is a pod
92 void* iter = lua_newuserdata(L, sizeof(state_t));
93 new (iter) state_t(c.begin(), c.end());
94 lua_pushcclosure(L, state_t::step, 1);
98 struct iterator_policy : conversion_policy<0>
100 static void precall(lua_State*, const index_map&) {}
101 static void postcall(lua_State*, const index_map&) {}
103 template<class T, class Direction>
104 struct apply
106 typedef iterator_converter type;
112 namespace luabind
114 namespace
116 LUABIND_ANONYMOUS_FIX detail::policy_cons<detail::iterator_policy, detail::null_type> return_stl_iterator;
120 #endif // LUABIND_ITERATOR_POLICY_HPP_INCLUDED