]> git.saurik.com Git - bison.git/blob - data/stack.hh
cb8e6964de64f7b4095cf9826119a3beb0f96115
[bison.git] / data / stack.hh
1 # C++ skeleton for Bison
2
3 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 # Free Software Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 m4_pushdef([b4_copyright_years],
20 [2002, 2003, 2004, 2005, 2006, 2007, 2008])
21
22 # We do want M4 expansion after # for CPP macros.
23 m4_changecom()
24 m4_divert_push(0)dnl
25 @output(b4_dir_prefix[]stack.hh@)@
26 b4_copyright([Stack handling for Bison parsers in C++])[
27
28 #ifndef BISON_STACK_HH
29 # define BISON_STACK_HH
30
31 #include <deque>
32
33 ]b4_namespace_open[
34 template <class T, class S = std::deque<T> >
35 class stack
36 {
37 public:
38 // Hide our reversed order.
39 typedef typename S::reverse_iterator iterator;
40 typedef typename S::const_reverse_iterator const_iterator;
41
42 stack ()
43 : seq_ ()
44 {
45 }
46
47 stack (unsigned int n)
48 : seq_ (n)
49 {
50 }
51
52 inline
53 T&
54 operator [] (unsigned int i)
55 {
56 return seq_[i];
57 }
58
59 inline
60 const T&
61 operator [] (unsigned int i) const
62 {
63 return seq_[i];
64 }
65
66 inline
67 void
68 push (const T& t)
69 {
70 seq_.push_front (t);
71 }
72
73 inline
74 void
75 pop (unsigned int n = 1)
76 {
77 for (; n; --n)
78 seq_.pop_front ();
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 /// The wrapped container.
104 S seq_;
105 };
106
107 /// Present a slice of the top of a stack.
108 template <class T, class S = stack<T> >
109 class slice
110 {
111 public:
112 slice (const S& stack, unsigned int range)
113 : stack_ (stack),
114 range_ (range)
115 {
116 }
117
118 inline
119 const T&
120 operator [] (unsigned int i) const
121 {
122 return stack_[range_ - i];
123 }
124
125 private:
126 const S& stack_;
127 unsigned int range_;
128 };
129 ]b4_namespace_close[
130
131 #endif // not BISON_STACK_HH[]dnl
132 ]
133 m4_divert_pop(0)
134 m4_popdef([b4_copyright_years])dnl
135 m4_changecom([#])