1 | #include <cstdlib>
|
---|
2 | #include <iostream>
|
---|
3 | #include <boost/spirit/home/qi.hpp>
|
---|
4 | #include <boost/spirit/home/support.hpp>
|
---|
5 | #include <boost/spirit/home/support/multi_pass.hpp>
|
---|
6 | #include <boost/spirit/home/support/iterators/detail/functor_input_policy.hpp>
|
---|
7 |
|
---|
8 |
|
---|
9 | class iterate_rnd {
|
---|
10 | private:
|
---|
11 | size_t limit_;
|
---|
12 | public:
|
---|
13 | typedef char result_type;
|
---|
14 |
|
---|
15 | static result_type eof;
|
---|
16 |
|
---|
17 | iterate_rnd() : limit_(104857600) {}
|
---|
18 | iterate_rnd(size_t limit) : limit_(limit) {}
|
---|
19 |
|
---|
20 | result_type operator()() {
|
---|
21 | if (limit_ == 0) return eof;
|
---|
22 | limit_--;
|
---|
23 | return rand() & 0x7F;
|
---|
24 | }
|
---|
25 | };
|
---|
26 |
|
---|
27 |
|
---|
28 | iterate_rnd::result_type iterate_rnd::eof = iterate_rnd::result_type(-1);
|
---|
29 |
|
---|
30 |
|
---|
31 | using namespace boost::spirit;
|
---|
32 |
|
---|
33 |
|
---|
34 | typedef multi_pass<
|
---|
35 | iterate_rnd,
|
---|
36 | iterator_policies::default_policy<
|
---|
37 | iterator_policies::ref_counted,
|
---|
38 | iterator_policies::no_check,
|
---|
39 | iterator_policies::functor_input,
|
---|
40 | iterator_policies::split_std_deque
|
---|
41 | >
|
---|
42 | > functor_multi_pass_type;
|
---|
43 |
|
---|
44 |
|
---|
45 | int main() {
|
---|
46 | functor_multi_pass_type first = functor_multi_pass_type(iterate_rnd());
|
---|
47 | functor_multi_pass_type last;
|
---|
48 | std::string input;
|
---|
49 |
|
---|
50 | {
|
---|
51 | functor_multi_pass_type copy(first);
|
---|
52 | while (first != last) {
|
---|
53 | ++first;
|
---|
54 | }
|
---|
55 | std::cout << "Iterator offset at this point is " << std::distance(copy, first) << std::endl;
|
---|
56 | }
|
---|
57 |
|
---|
58 | first.clear_queue();
|
---|
59 | /* only one reference to the shared memory object is
|
---|
60 | * left, but no memory reset is triggered within the iterator */
|
---|
61 | std::cout << "Press enter to exit. Check memory usage at this point.";
|
---|
62 | std::getline(std::cin, input);
|
---|
63 |
|
---|
64 | return 0;
|
---|
65 | }
|
---|