]> git.saurik.com Git - bison.git/blob - data/stack.hh
maint: run "make update-copyright".
[bison.git] / data / stack.hh
1 # C++ skeleton for Bison
2
3 # Copyright (C) 2002-2012 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-2012])
20
21 # We do want M4 expansion after # for CPP macros.
22 m4_changecom()
23 m4_divert_push(0)dnl
24 @output(b4_dir_prefix[]stack.hh@)@
25 b4_copyright([Stack handling for Bison parsers in C++])[
26
27 #ifndef BISON_STACK_HH
28 # define BISON_STACK_HH
29
30 #include <deque>
31
32 ]b4_namespace_open[
33 template <class T, class S = std::deque<T> >
34 class stack
35 {
36 public:
37 // Hide our reversed order.
38 typedef typename S::reverse_iterator iterator;
39 typedef typename S::const_reverse_iterator const_iterator;
40
41 stack ()
42 : seq_ ()
43 {
44 }
45
46 stack (unsigned int n)
47 : seq_ (n)
48 {
49 }
50
51 inline
52 T&
53 operator [] (unsigned int i)
54 {
55 return seq_[i];
56 }
57
58 inline
59 const T&
60 operator [] (unsigned int i) const
61 {
62 return seq_[i];
63 }
64
65 inline
66 void
67 push (const T& t)
68 {
69 seq_.push_front (t);
70 }
71
72 inline
73 void
74 pop (unsigned int n = 1)
75 {
76 for (; n; --n)
77 seq_.pop_front ();
78 }
79
80 inline
81 typename S::size_type
82 size () const
83 {
84 return seq_.size ();
85 }
86
87 inline
88 const_iterator
89 begin () const
90 {
91 return seq_.rbegin ();
92 }
93
94 inline
95 const_iterator
96 end () const
97 {
98 return seq_.rend ();
99 }
100
101 private:
102 /// The wrapped container.
103 S seq_;
104 };
105
106 /// Present a slice of the top of a stack.
107 template <class T, class S = stack<T> >
108 class slice
109 {
110 public:
111 slice (const S& stack, unsigned int range)
112 : stack_ (stack),
113 range_ (range)
114 {
115 }
116
117 inline
118 const T&
119 operator [] (unsigned int i) const
120 {
121 return stack_[range_ - i];
122 }
123
124 private:
125 const S& stack_;
126 unsigned int range_;
127 };
128 ]b4_namespace_close[
129
130 #endif // not BISON_STACK_HH[]dnl
131 ]
132 m4_divert_pop(0)
133 m4_popdef([b4_copyright_years])dnl
134 m4_changecom([#])