stack.hh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # C++ skeleton for Bison
  2. # Copyright (C) 2002-2013 Free Software Foundation, Inc.
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. m4_pushdef([b4_copyright_years],
  16. [2002-2013])
  17. # b4_stack_define
  18. # ---------------
  19. m4_define([b4_stack_define],
  20. [[ template <class T, class S = std::vector<T> >
  21. class stack
  22. {
  23. public:
  24. // Hide our reversed order.
  25. typedef typename S::reverse_iterator iterator;
  26. typedef typename S::const_reverse_iterator const_iterator;
  27. stack ()
  28. : seq_ ()
  29. {
  30. }
  31. stack (unsigned int n)
  32. : seq_ (n)
  33. {
  34. }
  35. inline
  36. T&
  37. operator[] (unsigned int i)
  38. {
  39. return seq_[seq_.size () - 1 - i];
  40. }
  41. inline
  42. const T&
  43. operator[] (unsigned int i) const
  44. {
  45. return seq_[seq_.size () - 1 - i];
  46. }
  47. /// Steal the contents of \a t.
  48. ///
  49. /// Close to move-semantics.
  50. inline
  51. void
  52. push (T& t)
  53. {
  54. seq_.push_back (T());
  55. operator[](0).move (t);
  56. }
  57. inline
  58. void
  59. pop (unsigned int n = 1)
  60. {
  61. for (; n; --n)
  62. seq_.pop_back ();
  63. }
  64. void
  65. clear ()
  66. {
  67. seq_.clear ();
  68. }
  69. inline
  70. typename S::size_type
  71. size () const
  72. {
  73. return seq_.size ();
  74. }
  75. inline
  76. const_iterator
  77. begin () const
  78. {
  79. return seq_.rbegin ();
  80. }
  81. inline
  82. const_iterator
  83. end () const
  84. {
  85. return seq_.rend ();
  86. }
  87. private:
  88. stack (const stack&);
  89. stack& operator= (const stack&);
  90. /// The wrapped container.
  91. S seq_;
  92. };
  93. /// Present a slice of the top of a stack.
  94. template <class T, class S = stack<T> >
  95. class slice
  96. {
  97. public:
  98. slice (const S& stack, unsigned int range)
  99. : stack_ (stack)
  100. , range_ (range)
  101. {
  102. }
  103. inline
  104. const T&
  105. operator [] (unsigned int i) const
  106. {
  107. return stack_[range_ - i];
  108. }
  109. private:
  110. const S& stack_;
  111. unsigned int range_;
  112. };
  113. ]])
  114. b4_defines_if(
  115. [b4_output_begin([b4_dir_prefix[]stack.hh])
  116. b4_copyright([Stack handling for Bison parsers in C++])[
  117. /**
  118. ** \file ]b4_dir_prefix[stack.hh
  119. ** Define the ]b4_namespace_ref[::stack class.
  120. */
  121. ]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
  122. # include <vector>
  123. ]b4_namespace_open[
  124. ]b4_stack_define[
  125. ]b4_namespace_close[
  126. ]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
  127. b4_output_end()
  128. ])
  129. m4_popdef([b4_copyright_years])