directory_iterator.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef LUABIND_DIRECTORY_ITERATOR_HPP_INCLUDED
  20. #define LUABIND_DIRECTORY_ITERATOR_HPP_INCLUDED
  21. #include <luabind/config.hpp>
  22. #include <luabind/detail/policy.hpp>
  23. #include <luabind/detail/implicit_cast.hpp>
  24. #include <luabind/detail/convert_to_lua.hpp>
  25. namespace luabind { namespace detail
  26. {
  27. template<class Iter>
  28. struct dir_iterator_state
  29. {
  30. typedef dir_iterator_state<Iter> self_t;
  31. static int step(lua_State* L)
  32. {
  33. self_t& state = *static_cast<self_t*>(lua_touserdata(L, lua_upvalueindex(1)));
  34. if (state.start == state.end)
  35. {
  36. lua_pushnil(L);
  37. return 1;
  38. }
  39. else
  40. {
  41. convert_to_lua(L, *state.start);
  42. }
  43. ++state.start;
  44. return 1;
  45. }
  46. dir_iterator_state(const Iter& s, const Iter& e)
  47. : start(s)
  48. , end(e)
  49. {}
  50. Iter start;
  51. Iter end;
  52. };
  53. struct dir_iterator_converter
  54. {
  55. template<class T>
  56. void apply(lua_State* L, const T& c)
  57. {
  58. typedef boost::filesystem::directory_iterator iter_t;
  59. typedef dir_iterator_state<iter_t> state_t;
  60. // note that this should be destructed, for now.. just hope that iterator
  61. // is a pod
  62. void* iter = lua_newuserdata(L, sizeof(state_t));
  63. new (iter) state_t(iter_t(c), iter_t());
  64. lua_pushcclosure(L, state_t::step, 1);
  65. }
  66. template<class T>
  67. void apply(lua_State* L, T& c)
  68. {
  69. typedef boost::filesystem::directory_iterator iter_t;
  70. typedef dir_iterator_state<iter_t> state_t;
  71. // note that this should be destructed, for now.. just hope that iterator
  72. // is a pod
  73. void* iter = lua_newuserdata(L, sizeof(state_t));
  74. new (iter) state_t(iter_t(c), iter_t());
  75. lua_pushcclosure(L, state_t::step, 1);
  76. }
  77. };
  78. struct dir_iterator_policy : conversion_policy<0>
  79. {
  80. static void precall(lua_State*, const index_map&) {}
  81. static void postcall(lua_State*, const index_map&) {}
  82. template<class T, class Direction>
  83. struct generate_converter
  84. {
  85. typedef dir_iterator_converter type;
  86. };
  87. };
  88. }}
  89. namespace luabind
  90. {
  91. namespace
  92. {
  93. LUABIND_ANONYMOUS_FIX detail::policy_cons<detail::dir_iterator_policy, detail::null_type> return_directory_iterator;
  94. }
  95. }
  96. #endif // LUABIND_ITERATOR_POLICY_HPP_INCLUDED