split_functor_input_policy.hpp 7.1 KB

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