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
>
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
);
35 static int destroy(lua_State
* L
)
37 iterator
* self
= static_cast<iterator
*>(lua_touserdata(L
, 1));
42 iterator(Iterator first
, 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
>));
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
);
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
>
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