moved around some code
[luabind.git] / luabind / iterator_policy.hpp
blob49a4e0bd6b975e5dbd983a531305ba9eeeb7f729
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>
32 namespace luabind { namespace detail
34 template<class Iter>
35 struct iterator_state
37 typedef iterator_state<Iter> self_t;
39 static int step(lua_State* L)
41 self_t& state = *static_cast<self_t*>(lua_touserdata(L, lua_upvalueindex(1)));
43 if (state.start == state.end)
45 lua_pushnil(L);
47 else
49 convert_to_lua(L, *state.start);
50 ++state.start;
53 return 1;
56 iterator_state(const Iter& s, const Iter& e)
57 : start(s)
58 , end(e)
61 Iter start;
62 Iter end;
65 struct iterator_converter
67 template<class T>
68 void apply(lua_State* L, const T& c)
70 typedef typename T::const_iterator iter_t;
71 typedef iterator_state<iter_t> state_t;
73 // note that this should be destructed, for now.. just hope that iterator
74 // is a pod
75 void* iter = lua_newuserdata(L, sizeof(state_t));
76 new (iter) state_t(c.begin(), c.end());
77 lua_pushcclosure(L, state_t::step, 1);
80 template<class T>
81 void apply(lua_State* L, T& c)
83 typedef typename T::iterator iter_t;
84 typedef iterator_state<iter_t> state_t;
86 // note that this should be destructed, for now.. just hope that iterator
87 // is a pod
88 void* iter = lua_newuserdata(L, sizeof(state_t));
89 new (iter) state_t(c.begin(), c.end());
90 lua_pushcclosure(L, state_t::step, 1);
94 struct iterator_policy : conversion_policy<0>
96 static void precall(lua_State*, const index_map&) {}
97 static void postcall(lua_State*, const index_map&) {}
99 template<class T, class Direction>
100 struct generate_converter
102 typedef iterator_converter type;
108 namespace luabind
110 namespace
112 LUABIND_ANONYMOUS_FIX detail::policy_cons<detail::iterator_policy, detail::null_type> return_stl_iterator;
116 #endif // LUABIND_ITERATOR_POLICY_HPP_INCLUDED