stack.hh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # C++ skeleton for Bison
  2. # Copyright (C) 2002-2015, 2018-2021 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 <https://www.gnu.org/licenses/>.
  15. # b4_stack_file
  16. # -------------
  17. # Name of the file containing the stack class, if we want this file.
  18. b4_defines_if([b4_required_version_if([30200], [],
  19. [m4_define([b4_stack_file], [stack.hh])])])
  20. # b4_stack_define
  21. # ---------------
  22. m4_define([b4_stack_define],
  23. [[ /// A stack with random access from its top.
  24. template <typename T, typename S = std::vector<T> >
  25. class stack
  26. {
  27. public:
  28. // Hide our reversed order.
  29. typedef typename S::iterator iterator;
  30. typedef typename S::const_iterator const_iterator;
  31. typedef typename S::size_type size_type;
  32. typedef typename std::ptrdiff_t index_type;
  33. stack (size_type n = 200)
  34. : seq_ (n)
  35. {}
  36. #if 201103L <= YY_CPLUSPLUS
  37. /// Non copyable.
  38. stack (const stack&) = delete;
  39. /// Non copyable.
  40. stack& operator= (const stack&) = delete;
  41. #endif
  42. /// Random access.
  43. ///
  44. /// Index 0 returns the topmost element.
  45. const T&
  46. operator[] (index_type i) const
  47. {
  48. return seq_[size_type (size () - 1 - i)];
  49. }
  50. /// Random access.
  51. ///
  52. /// Index 0 returns the topmost element.
  53. T&
  54. operator[] (index_type i)
  55. {
  56. return seq_[size_type (size () - 1 - i)];
  57. }
  58. /// Steal the contents of \a t.
  59. ///
  60. /// Close to move-semantics.
  61. void
  62. push (YY_MOVE_REF (T) t)
  63. {
  64. seq_.push_back (T ());
  65. operator[] (0).move (t);
  66. }
  67. /// Pop elements from the stack.
  68. void
  69. pop (std::ptrdiff_t n = 1) YY_NOEXCEPT
  70. {
  71. for (; 0 < n; --n)
  72. seq_.pop_back ();
  73. }
  74. /// Pop all elements from the stack.
  75. void
  76. clear () YY_NOEXCEPT
  77. {
  78. seq_.clear ();
  79. }
  80. /// Number of elements on the stack.
  81. index_type
  82. size () const YY_NOEXCEPT
  83. {
  84. return index_type (seq_.size ());
  85. }
  86. /// Iterator on top of the stack (going downwards).
  87. const_iterator
  88. begin () const YY_NOEXCEPT
  89. {
  90. return seq_.begin ();
  91. }
  92. /// Bottom of the stack.
  93. const_iterator
  94. end () const YY_NOEXCEPT
  95. {
  96. return seq_.end ();
  97. }
  98. /// Present a slice of the top of a stack.
  99. class slice
  100. {
  101. public:
  102. slice (const stack& stack, index_type range)
  103. : stack_ (stack)
  104. , range_ (range)
  105. {}
  106. const T&
  107. operator[] (index_type i) const
  108. {
  109. return stack_[range_ - i];
  110. }
  111. private:
  112. const stack& stack_;
  113. index_type range_;
  114. };
  115. private:
  116. #if YY_CPLUSPLUS < 201103L
  117. /// Non copyable.
  118. stack (const stack&);
  119. /// Non copyable.
  120. stack& operator= (const stack&);
  121. #endif
  122. /// The wrapped container.
  123. S seq_;
  124. };
  125. ]])
  126. m4_ifdef([b4_stack_file],
  127. [b4_output_begin([b4_dir_prefix], [b4_stack_file])[
  128. ]b4_generated_by[
  129. // Starting with Bison 3.2, this file is useless: the structure it
  130. // used to define is now defined with the parser itself.
  131. //
  132. // To get rid of this file:
  133. // 1. add '%require "3.2"' (or newer) to your grammar file
  134. // 2. remove references to this file from your build system.
  135. ]b4_output_end[
  136. ]])