tst.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  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_TST_JUNE_03_2007_1031AM)
  7. #define BOOST_SPIRIT_TST_JUNE_03_2007_1031AM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/string/detail/tst.hpp>
  12. namespace boost { namespace spirit { namespace qi
  13. {
  14. struct tst_pass_through
  15. {
  16. template <typename Char>
  17. Char operator()(Char ch) const
  18. {
  19. return ch;
  20. }
  21. };
  22. template <typename Char, typename T>
  23. struct tst
  24. {
  25. typedef Char char_type; // the character type
  26. typedef T value_type; // the value associated with each entry
  27. typedef detail::tst_node<Char, T> node;
  28. tst()
  29. : root(0)
  30. {
  31. }
  32. ~tst()
  33. {
  34. clear();
  35. }
  36. tst(tst const& rhs)
  37. : root(0)
  38. {
  39. copy(rhs);
  40. }
  41. tst& operator=(tst const& rhs)
  42. {
  43. return assign(rhs);
  44. }
  45. template <typename Iterator, typename Filter>
  46. T* find(Iterator& first, Iterator last, Filter filter) const
  47. {
  48. return node::find(root, first, last, filter);
  49. }
  50. template <typename Iterator>
  51. T* find(Iterator& first, Iterator last) const
  52. {
  53. return find(first, last, tst_pass_through());
  54. }
  55. template <typename Iterator>
  56. T* add(
  57. Iterator first
  58. , Iterator last
  59. , typename boost::call_traits<T>::param_type val)
  60. {
  61. return node::add(root, first, last, val, this);
  62. }
  63. template <typename Iterator>
  64. void remove(Iterator first, Iterator last)
  65. {
  66. node::remove(root, first, last, this);
  67. }
  68. void clear()
  69. {
  70. node::destruct_node(root, this);
  71. root = 0;
  72. }
  73. template <typename F>
  74. void for_each(F f) const
  75. {
  76. node::for_each(root, std::basic_string<Char>(), f);
  77. }
  78. private:
  79. friend struct detail::tst_node<Char, T>;
  80. void copy(tst const& rhs)
  81. {
  82. root = node::clone_node(rhs.root, this);
  83. }
  84. tst& assign(tst const& rhs)
  85. {
  86. if (this != &rhs)
  87. {
  88. clear();
  89. copy(rhs);
  90. }
  91. return *this;
  92. }
  93. node* root;
  94. node* new_node(Char id)
  95. {
  96. return new node(id);
  97. }
  98. T* new_data(typename boost::call_traits<T>::param_type val)
  99. {
  100. return new T(val);
  101. }
  102. void delete_node(node* p)
  103. {
  104. delete p;
  105. }
  106. void delete_data(T* p)
  107. {
  108. delete p;
  109. }
  110. };
  111. }}}
  112. #endif