]> git.saurik.com Git - bison.git/blob - data/stack.hh
e785e8081895d5a5734eba9fe6996edb5952f359
[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 [2002-2012])[
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 () : seq_ ()
43 {
44 }
45
46 stack (unsigned int n) : seq_ (n)
47 {
48 }
49
50 inline
51 T&
52 operator [] (unsigned int i)
53 {
54 return seq_[i];
55 }
56
57 inline
58 const T&
59 operator [] (unsigned int i) const
60 {
61 return seq_[i];
62 }
63
64 inline
65 void
66 push (const T& t)
67 {
68 seq_.push_front (t);
69 }
70
71 inline
72 void
73 pop (unsigned int n = 1)
74 {
75 for (; n; --n)
76 seq_.pop_front ();
77 }
78
79 inline
80 unsigned int
81 height () const
82 {
83 return seq_.size ();
84 }
85
86 inline const_iterator begin () const { return seq_.rbegin (); }
87 inline const_iterator end () const { return seq_.rend (); }
88
89 private:
90 S seq_;
91 };
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
104 inline
105 const T&
106 operator [] (unsigned int i) const
107 {
108 return stack_[range_ - i];
109 }
110
111 private:
112 const S& stack_;
113 unsigned int range_;
114 };
115 ]b4_namespace_close[
116
117 #endif // not BISON_STACK_HH[]dnl
118 ]
119 m4_divert_pop(0)
120 m4_popdef([b4_copyright_years])dnl
121 m4_changecom([#])