Merge branch 'dw/cast-graph'
[luabind.git] / test / test_iterator.cpp
blobd1fae47461baad14d0a34a91096b1b5653ba93e4
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 #include "test.hpp"
7 #include <luabind/luabind.hpp>
8 #include <luabind/iterator_policy.hpp>
9 #include <boost/iterator/iterator_adaptor.hpp>
11 struct container
13 container()
15 for (int i = 0; i < 5; ++i)
16 data[i] = i + 1;
19 struct iterator
20 : boost::iterator_adaptor<iterator, int*>
22 static std::size_t alive;
24 iterator(int* p)
25 : iterator::iterator_adaptor_(p)
27 ++alive;
30 iterator(iterator const& other)
31 : iterator::iterator_adaptor_(other)
33 ++alive;
36 ~iterator()
38 --alive;
42 iterator begin()
44 return iterator(data);
47 iterator end()
49 return iterator(data + 5);
52 int data[5];
55 std::size_t container::iterator::alive = 0;
57 struct cls
59 container iterable;
62 void test_main(lua_State* L)
64 using namespace luabind;
66 module(L)
68 class_<cls>("cls")
69 .def(constructor<>())
70 .def_readonly("iterable", &cls::iterable, return_stl_iterator)
73 DOSTRING(L,
74 "x = cls()\n"
75 "sum = 0\n"
76 "for i in x.iterable do\n"
77 " sum = sum + i\n"
78 "end\n"
79 "assert(sum == 15)\n"
80 "collectgarbage('collect')\n"
83 assert(container::iterator::alive == 0);