Add has_policy<> and remove has_yield<>.
[luabind.git] / luabind / iterator_policy.hpp
blobb1f827fe3a69efe193e0070d479eab3860b2d3f1
1 // Copyright Daniel Wallin 2007. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef LUABIND_ITERATOR_POLICY__071111_HPP
6 # define LUABIND_ITERATOR_POLICY__071111_HPP
8 # include <luabind/config.hpp>
9 # include <luabind/detail/policy.hpp>
10 # include <luabind/detail/convert_to_lua.hpp>
12 namespace luabind { namespace detail {
14 template <class Iterator>
15 struct iterator
17 static int next(lua_State* L)
19 iterator* self = static_cast<iterator*>(
20 lua_touserdata(L, lua_upvalueindex(1)));
22 if (self->first != self->last)
24 convert_to_lua(L, *self->first);
25 ++self->first;
27 else
29 lua_pushnil(L);
32 return 1;
35 static int destroy(lua_State* L)
37 iterator* self = static_cast<iterator*>(lua_touserdata(L, 1));
38 self->~iterator();
39 return 0;
42 iterator(Iterator first, Iterator last)
43 : first(first)
44 , last(last)
47 Iterator first;
48 Iterator last;
51 template <class Iterator>
52 int make_range(lua_State* L, Iterator first, Iterator last)
54 void* storage = lua_newuserdata(L, sizeof(iterator<Iterator>));
55 lua_newtable(L);
56 lua_pushcclosure(L, iterator<Iterator>::destroy, 0);
57 lua_setfield(L, -2, "__gc");
58 lua_setmetatable(L, -2);
59 lua_pushcclosure(L, iterator<Iterator>::next, 1);
60 new (storage) iterator<Iterator>(first, last);
61 return 1;
64 template <class Container>
65 int make_range(lua_State* L, Container& container)
67 return make_range(L, container.begin(), container.end());
70 struct iterator_converter
72 typedef iterator_converter type;
74 template <class Container>
75 void apply(lua_State* L, Container& container)
77 make_range(L, container);
80 template <class Container>
81 void apply(lua_State* L, Container const& container)
83 make_range(L, container);
87 struct iterator_policy : conversion_policy<0>
89 static void precall(lua_State*, index_map const&)
92 static void postcall(lua_State*, index_map const&)
95 template <class T, class Direction>
96 struct apply
98 typedef iterator_converter type;
102 }} // namespace luabind::detail
104 namespace luabind { namespace {
106 LUABIND_ANONYMOUS_FIX detail::policy_cons<
107 detail::iterator_policy, detail::null_type> return_stl_iterator;
109 }} // namespace luabind::unnamed
111 #endif // LUABIND_ITERATOR_POLICY__071111_HPP