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