*** empty log message ***
[luabind.git] / examples / filesystem / directory_iterator.hpp
blobc8958c79f89c2bd6b5e240df1216bd4677860aee
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_DIRECTORY_ITERATOR_HPP_INCLUDED
25 #define LUABIND_DIRECTORY_ITERATOR_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 dir_iterator_state
37 typedef dir_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);
46 return 1;
48 else
50 convert_to_lua(L, *state.start);
53 ++state.start;
55 return 1;
58 dir_iterator_state(const Iter& s, const Iter& e)
59 : start(s)
60 , end(e)
63 Iter start;
64 Iter end;
67 struct dir_iterator_converter
69 template<class T>
70 void apply(lua_State* L, const T& c)
72 typedef boost::filesystem::directory_iterator iter_t;
73 typedef dir_iterator_state<iter_t> state_t;
75 // note that this should be destructed, for now.. just hope that iterator
76 // is a pod
77 void* iter = lua_newuserdata(L, sizeof(state_t));
78 new (iter) state_t(iter_t(c), iter_t());
79 lua_pushcclosure(L, state_t::step, 1);
82 template<class T>
83 void apply(lua_State* L, T& c)
85 typedef boost::filesystem::directory_iterator iter_t;
86 typedef dir_iterator_state<iter_t> state_t;
88 // note that this should be destructed, for now.. just hope that iterator
89 // is a pod
90 void* iter = lua_newuserdata(L, sizeof(state_t));
91 new (iter) state_t(iter_t(c), iter_t());
92 lua_pushcclosure(L, state_t::step, 1);
96 struct dir_iterator_policy : conversion_policy<0>
98 static void precall(lua_State*, const index_map&) {}
99 static void postcall(lua_State*, const index_map&) {}
101 template<class T, class Direction>
102 struct generate_converter
104 typedef dir_iterator_converter type;
110 namespace luabind
112 namespace
114 LUABIND_ANONYMOUS_FIX detail::policy_cons<detail::dir_iterator_policy, detail::null_type> return_directory_iterator;
118 #endif // LUABIND_ITERATOR_POLICY_HPP_INCLUDED