| 1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #if !defined(BOOST_SPIRIT_ITERATOR_SPLIT_FUNCTOR_INPUT_POLICY_JAN_17_2008_0103PM) |
| 7 | #define BOOST_SPIRIT_ITERATOR_SPLIT_FUNCTOR_INPUT_POLICY_JAN_17_2008_0103PM |
| 8 | |
| 9 | #include <boost/spirit/home/support/iterators/multi_pass_fwd.hpp> |
| 10 | #include <boost/spirit/home/support/iterators/detail/multi_pass.hpp> |
| 11 | #include <boost/core/invoke_swap.hpp> |
| 12 | #include <boost/assert.hpp> |
| 13 | #include <boost/type_traits/is_empty.hpp> |
| 14 | |
| 15 | namespace boost { namespace spirit { namespace iterator_policies |
| 16 | { |
| 17 | namespace split_functor_input_is_valid_test_ |
| 18 | { |
| 19 | template <typename Token> |
| 20 | inline bool token_is_valid(Token const&) |
| 21 | { |
| 22 | return true; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////// |
| 27 | // class split_functor_input |
| 28 | // Implementation of the InputPolicy used by multi_pass |
| 29 | // split_functor_input gets tokens from a functor |
| 30 | // |
| 31 | // This policy should be used when the functor holds two parts of data: a |
| 32 | // unique part (unique for each instance of the iterator) and a shared |
| 33 | // part (to be shared between the different copies of the same iterator). |
| 34 | // Using this policy allows to merge the shared part of the functor with |
| 35 | // the shared part of the iterator data, saving one pointer and one |
| 36 | // allocation per iterator instance. |
| 37 | // |
| 38 | // The Functor template parameter of this policy is expected to be a |
| 39 | // std::pair<unique, shared>, where 'unique' and 'shared' represent the |
| 40 | // respective parts of the functor itself. |
| 41 | // |
| 42 | // Note: the unique part of the functor must have a typedef for result_type |
| 43 | // It also must have a static variable of type result_type defined |
| 44 | // to represent EOF that is called eof. |
| 45 | // |
| 46 | /////////////////////////////////////////////////////////////////////////// |
| 47 | struct split_functor_input |
| 48 | { |
| 49 | /////////////////////////////////////////////////////////////////////// |
| 50 | template <typename Functor |
| 51 | , bool FunctorIsEmpty = is_empty<typename Functor::first_type>::value> |
| 52 | class unique; |
| 53 | |
| 54 | // the unique part of the functor is empty, do not include the functor |
| 55 | // as a member at all to avoid unnecessary padding bytes to be included |
| 56 | // into the generated structure |
| 57 | template <typename Functor> |
| 58 | class unique<Functor, true> // : public detail::default_input_policy |
| 59 | { |
| 60 | protected: |
| 61 | typedef typename Functor::first_type functor_type; |
| 62 | typedef typename functor_type::result_type result_type; |
| 63 | |
| 64 | public: |
| 65 | typedef result_type value_type; |
| 66 | typedef std::ptrdiff_t difference_type; |
| 67 | typedef std::ptrdiff_t distance_type; |
| 68 | typedef result_type const* pointer; |
| 69 | typedef result_type const& reference; |
| 70 | |
| 71 | protected: |
| 72 | unique() {} |
| 73 | explicit unique(Functor const&) {} |
| 74 | |
| 75 | public: |
| 76 | void swap(unique&) {} |
| 77 | |
| 78 | // get the next token |
| 79 | template <typename MultiPass> |
| 80 | static typename MultiPass::reference get_input(MultiPass& mp) |
| 81 | { |
| 82 | value_type& curtok = mp.shared()->curtok; |
| 83 | using namespace split_functor_input_is_valid_test_; |
| 84 | if (!token_is_valid(curtok)) |
| 85 | functor_type::get_next(mp, curtok); |
| 86 | return curtok; |
| 87 | } |
| 88 | |
| 89 | template <typename MultiPass> |
| 90 | static void advance_input(MultiPass& mp) |
| 91 | { |
| 92 | functor_type::get_next(mp, mp.shared()->curtok); |
| 93 | } |
| 94 | |
| 95 | // test, whether we reached the end of the underlying stream |
| 96 | template <typename MultiPass> |
| 97 | static bool input_at_eof(MultiPass const& mp) |
| 98 | { |
| 99 | return mp.shared()->curtok == functor_type::eof; |
| 100 | } |
| 101 | |
| 102 | template <typename MultiPass> |
| 103 | static bool input_is_valid(MultiPass const&, value_type const& t) |
| 104 | { |
| 105 | using namespace split_functor_input_is_valid_test_; |
| 106 | return token_is_valid(t); |
| 107 | } |
| 108 | |
| 109 | template <typename MultiPass> |
| 110 | static void destroy(MultiPass& mp) |
| 111 | { |
| 112 | functor_type::destroy(mp); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | // the unique part of the functor is non-empty |
| 117 | template <typename Functor> |
| 118 | class unique<Functor, false> : public unique<Functor, true> |
| 119 | { |
| 120 | protected: |
| 121 | typedef typename Functor::first_type functor_type; |
| 122 | typedef typename functor_type::result_type result_type; |
| 123 | |
| 124 | protected: |
| 125 | unique() {} |
| 126 | explicit unique(Functor const& x) : ftor(x.first) {} |
| 127 | |
| 128 | void swap(unique& x) |
| 129 | { |
| 130 | boost::core::invoke_swap(ftor, x.ftor); |
| 131 | } |
| 132 | |
| 133 | public: |
| 134 | typedef result_type value_type; |
| 135 | typedef std::ptrdiff_t difference_type; |
| 136 | typedef std::ptrdiff_t distance_type; |
| 137 | typedef result_type const* pointer; |
| 138 | typedef result_type const& reference; |
| 139 | |
| 140 | public: |
| 141 | // get the next token |
| 142 | template <typename MultiPass> |
| 143 | static typename MultiPass::reference get_input(MultiPass& mp) |
| 144 | { |
| 145 | value_type& curtok = mp.shared()->curtok; |
| 146 | using namespace split_functor_input_is_valid_test_; |
| 147 | if (!token_is_valid(curtok)) |
| 148 | functor_type::get_next(mp, curtok); |
| 149 | return curtok; |
| 150 | } |
| 151 | |
| 152 | template <typename MultiPass> |
| 153 | static void advance_input(MultiPass& mp) |
| 154 | { |
| 155 | mp.ftor.get_next(mp, mp.shared()->curtok); |
| 156 | } |
| 157 | |
| 158 | template <typename MultiPass> |
| 159 | static bool input_is_valid(MultiPass const&, value_type const& t) |
| 160 | { |
| 161 | using namespace split_functor_input_is_valid_test_; |
| 162 | return token_is_valid(t); |
| 163 | } |
| 164 | |
| 165 | // test, whether we reached the end of the underlying stream |
| 166 | template <typename MultiPass> |
| 167 | static bool input_at_eof(MultiPass const& mp) |
| 168 | { |
| 169 | return mp.shared()->curtok == mp.ftor.eof; |
| 170 | } |
| 171 | |
| 172 | typename Functor::first_type& get_functor() const |
| 173 | { |
| 174 | return ftor; |
| 175 | } |
| 176 | |
| 177 | mutable functor_type ftor; |
| 178 | }; |
| 179 | |
| 180 | /////////////////////////////////////////////////////////////////////// |
| 181 | template <typename Functor> |
| 182 | struct shared |
| 183 | { |
| 184 | protected: |
| 185 | typedef typename Functor::first_type functor_type; |
| 186 | typedef typename functor_type::result_type result_type; |
| 187 | |
| 188 | public: |
| 189 | explicit shared(Functor const& x) : ftor(x.second), curtok(0) {} |
| 190 | |
| 191 | mutable typename Functor::second_type ftor; |
| 192 | result_type curtok; |
| 193 | }; |
| 194 | }; |
| 195 | |
| 196 | }}} |
| 197 | |
| 198 | #endif |
| 199 | |