tst.hpp 3.0 KB

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