more holder type features
[luabind.git] / luabind / iterator_policy.hpp
blob7e408b71452b9281840bd809da40a2cb2b731395
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);
52 ++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 template<class T>
69 void apply(lua_State* L, const T& c)
71 typedef typename T::const_iterator iter_t;
72 typedef iterator_state<iter_t> state_t;
74 // note that this should be destructed, for now.. just hope that iterator
75 // is a pod
76 void* iter = lua_newuserdata(L, sizeof(state_t));
77 new (iter) state_t(c.begin(), c.end());
78 lua_pushcclosure(L, state_t::step, 1);
81 template<class T>
82 void apply(lua_State* L, T& c)
84 typedef typename T::iterator iter_t;
85 typedef iterator_state<iter_t> state_t;
87 // note that this should be destructed, for now.. just hope that iterator
88 // is a pod
89 void* iter = lua_newuserdata(L, sizeof(state_t));
90 new (iter) state_t(c.begin(), c.end());
91 lua_pushcclosure(L, state_t::step, 1);
95 struct iterator_policy : conversion_policy<0>
97 static void precall(lua_State*, const index_map&) {}
98 static void postcall(lua_State*, const index_map&) {}
100 template<class T, class Direction>
101 struct generate_converter
103 typedef iterator_converter type;
109 namespace luabind
111 namespace
113 LUABIND_ANONYMOUS_FIX detail::policy_cons<detail::iterator_policy, detail::null_type> return_stl_iterator;
117 #endif // LUABIND_ITERATOR_POLICY_HPP_INCLUDED