transform_iterator.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(TRANSFORM_ITERATOR_HPP_D492C659_88C7_4258_8C42_192F9AE80EC0_INCLUDED)
  9. #define TRANSFORM_ITERATOR_HPP_D492C659_88C7_4258_8C42_192F9AE80EC0_INCLUDED
  10. #include <boost/config.hpp>
  11. #include <boost/iterator_adaptors.hpp>
  12. #include <boost/iterator/transform_iterator.hpp>
  13. #include <boost/assert.hpp>
  14. // this must occur after all of the includes and before any code appears
  15. #ifdef BOOST_HAS_ABI_HEADERS
  16. #include BOOST_ABI_PREFIX
  17. #endif
  18. ///////////////////////////////////////////////////////////////////////////////
  19. namespace boost {
  20. namespace wave {
  21. namespace impl {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. //
  24. // The new Boost.Iterator library already conatins a transform_iterator usable
  25. // for our needs. The code below wraps this up.
  26. //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. template <class AdaptableUnaryFunctionT, class IteratorT>
  29. class ref_transform_iterator_generator
  30. {
  31. typedef typename AdaptableUnaryFunctionT::result_type return_type;
  32. typedef typename AdaptableUnaryFunctionT::argument_type argument_type;
  33. public:
  34. typedef boost::transform_iterator<
  35. return_type (*)(argument_type), IteratorT, return_type>
  36. type;
  37. };
  38. template <class AdaptableUnaryFunctionT, class IteratorT>
  39. inline
  40. typename ref_transform_iterator_generator<
  41. AdaptableUnaryFunctionT, IteratorT>::type
  42. make_ref_transform_iterator(
  43. IteratorT base, AdaptableUnaryFunctionT const &f)
  44. {
  45. typedef typename ref_transform_iterator_generator<
  46. AdaptableUnaryFunctionT, IteratorT>::type
  47. iterator_type;
  48. return iterator_type(base, f.transform);
  49. }
  50. // Retrieve the token value given a parse node
  51. // This is used in conjunction with the ref_transform_iterator above, to
  52. // get the token values while iterating directly over the parse tree.
  53. template <typename TokenT, typename ParseTreeNodeT>
  54. struct get_token_value {
  55. typedef TokenT const &result_type;
  56. typedef ParseTreeNodeT const &argument_type;
  57. static result_type
  58. transform (argument_type node)
  59. {
  60. BOOST_ASSERT(1 == std::distance(node.value.begin(),
  61. node.value.end()));
  62. return *node.value.begin();
  63. }
  64. };
  65. ///////////////////////////////////////////////////////////////////////////////
  66. } // namespace impl
  67. } // namespace wave
  68. } // namespace boost
  69. // the suffix header occurs after all of the code
  70. #ifdef BOOST_HAS_ABI_HEADERS
  71. #include BOOST_ABI_SUFFIX
  72. #endif
  73. #endif // !defined(TRANSFORM_ITERATOR_HPP_D492C659_88C7_4258_8C42_192F9AE80EC0_INCLUDED)