| 1 | // Copyright (c) 2001 Daniel C. Nuffer |
| 2 | // Copyright (c) 2001-2011 Hartmut Kaiser |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #if !defined(BOOST_SPIRIT_ITERATOR_FIXED_SIZE_QUEUE_POLICY_MAR_16_2007_1134AM) |
| 8 | #define BOOST_SPIRIT_ITERATOR_FIXED_SIZE_QUEUE_POLICY_MAR_16_2007_1134AM |
| 9 | |
| 10 | #include <boost/spirit/home/support/iterators/detail/multi_pass.hpp> |
| 11 | #include <boost/spirit/home/support/iterators/detail/fixed_size_queue.hpp> |
| 12 | #include <boost/core/invoke_swap.hpp> |
| 13 | #include <boost/assert.hpp> |
| 14 | #include <cstdlib> |
| 15 | |
| 16 | namespace boost { namespace spirit { namespace iterator_policies |
| 17 | { |
| 18 | /////////////////////////////////////////////////////////////////////////// |
| 19 | // class fixed_size_queue |
| 20 | // Implementation of the StoragePolicy used by multi_pass |
| 21 | // fixed_size_queue keeps a circular buffer (implemented by |
| 22 | // boost::spirit::fixed_size_queue class) that is size N+1 and stores N |
| 23 | // elements. |
| 24 | // |
| 25 | // It is up to the user to ensure that there is enough look ahead for |
| 26 | // their grammar. Currently there is no way to tell if an iterator is |
| 27 | // pointing to forgotten data. The leading iterator will put an item in |
| 28 | // the queue and remove one when it is incremented. No dynamic allocation |
| 29 | // is done, except on creation of the queue (fixed_size_queue constructor). |
| 30 | /////////////////////////////////////////////////////////////////////////// |
| 31 | template <std::size_t N> |
| 32 | struct fixed_size_queue |
| 33 | { |
| 34 | /////////////////////////////////////////////////////////////////////// |
| 35 | template <typename Value> |
| 36 | class unique : public detail::default_storage_policy |
| 37 | { |
| 38 | private: |
| 39 | typedef detail::fixed_size_queue<Value, N> queue_type; |
| 40 | |
| 41 | protected: |
| 42 | unique() {} |
| 43 | |
| 44 | unique(unique const& x) |
| 45 | : queued_position(x.queued_position) {} |
| 46 | |
| 47 | void swap(unique& x) |
| 48 | { |
| 49 | boost::core::invoke_swap(queued_position, x.queued_position); |
| 50 | } |
| 51 | |
| 52 | // This is called when the iterator is dereferenced. It's a |
| 53 | // template method so we can recover the type of the multi_pass |
| 54 | // iterator and access the m_input data member. |
| 55 | template <typename MultiPass> |
| 56 | static typename MultiPass::reference |
| 57 | dereference(MultiPass const& mp) |
| 58 | { |
| 59 | if (!mp.queued_position.get_position().is_initialized()) |
| 60 | mp.queued_position.get_position().set_queue(&mp.shared()->queued_elements); |
| 61 | |
| 62 | if (mp.queued_position == mp.shared()->queued_elements.end()) |
| 63 | return MultiPass::get_input(mp); |
| 64 | |
| 65 | return *mp.queued_position; |
| 66 | } |
| 67 | |
| 68 | // This is called when the iterator is incremented. It's a |
| 69 | // template method so we can recover the type of the multi_pass |
| 70 | // iterator and access the m_input data member. |
| 71 | template <typename MultiPass> |
| 72 | static void increment(MultiPass& mp) |
| 73 | { |
| 74 | if (!mp.queued_position.get_position().is_initialized()) |
| 75 | mp.queued_position.get_position().set_queue(&mp.shared()->queued_elements); |
| 76 | |
| 77 | if (mp.queued_position == mp.shared()->queued_elements.end()) |
| 78 | { |
| 79 | // don't let the queue get larger than N |
| 80 | if (mp.shared()->queued_elements.size() >= N) |
| 81 | mp.shared()->queued_elements.pop_front(); |
| 82 | |
| 83 | mp.shared()->queued_elements.push_back( |
| 84 | MultiPass::get_input(mp)); |
| 85 | MultiPass::advance_input(mp); |
| 86 | } |
| 87 | ++mp.queued_position; |
| 88 | } |
| 89 | |
| 90 | // clear_queue is a no-op |
| 91 | |
| 92 | // called to determine whether the iterator is an eof iterator |
| 93 | template <typename MultiPass> |
| 94 | static bool is_eof(MultiPass const& mp) |
| 95 | { |
| 96 | return mp.queued_position == mp.shared()->queued_elements.end() && |
| 97 | MultiPass::input_at_eof(mp); |
| 98 | } |
| 99 | |
| 100 | // called by operator== |
| 101 | template <typename MultiPass> |
| 102 | static bool equal_to(MultiPass const& mp, MultiPass const& x) |
| 103 | { |
| 104 | return mp.queued_position == x.queued_position; |
| 105 | } |
| 106 | |
| 107 | // called by operator< |
| 108 | template <typename MultiPass> |
| 109 | static bool less_than(MultiPass const& mp, MultiPass const& x) |
| 110 | { |
| 111 | return mp.queued_position < x.queued_position; |
| 112 | } |
| 113 | |
| 114 | protected: |
| 115 | mutable typename queue_type::iterator queued_position; |
| 116 | }; |
| 117 | |
| 118 | /////////////////////////////////////////////////////////////////////// |
| 119 | template <typename Value> |
| 120 | struct shared |
| 121 | { |
| 122 | typedef detail::fixed_size_queue<Value, N> queue_type; |
| 123 | queue_type queued_elements; |
| 124 | }; |
| 125 | }; |
| 126 | |
| 127 | }}} |
| 128 | |
| 129 | #endif |
| 130 | |