]> git.saurik.com Git - bison.git/blob - data/stack.hh
529337740eb5398a9ef7b58d3ba89e6a919c9ac8
[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 /**
29 ** \file ]b4_dir_prefix[stack.hh
30 ** Define the ]b4_namespace_ref[::stack class.
31 */
32
33 #ifndef BISON_STACK_HH
34 # define BISON_STACK_HH
35
36 # include <deque>
37
38 ]b4_namespace_open[
39 template <class T, class S = std::deque<T> >
40 class stack
41 {
42 public:
43 // Hide our reversed order.
44 typedef typename S::reverse_iterator iterator;
45 typedef typename S::const_reverse_iterator const_iterator;
46
47 stack () : seq_ ()
48 {
49 }
50
51 stack (unsigned int n) : seq_ (n)
52 {
53 }
54
55 inline
56 T&
57 operator [] (unsigned int i)
58 {
59 return seq_[i];
60 }
61
62 inline
63 const T&
64 operator [] (unsigned int i) const
65 {
66 return seq_[i];
67 }
68
69 inline
70 void
71 push (const T& t)
72 {
73 seq_.push_front (t);
74 }
75
76 inline
77 void
78 pop (unsigned int n = 1)
79 {
80 for (; n; --n)
81 seq_.pop_front ();
82 }
83
84 inline
85 unsigned int
86 height () const
87 {
88 return seq_.size ();
89 }
90
91 inline const_iterator begin () const { return seq_.rbegin (); }
92 inline const_iterator end () const { return seq_.rend (); }
93
94 private:
95 S seq_;
96 };
97
98 /// Present a slice of the top of a stack.
99 template <class T, class S = stack<T> >
100 class slice
101 {
102 public:
103 slice (const S& stack, unsigned int range)
104 : stack_ (stack)
105 , range_ (range)
106 {
107 }
108
109 inline
110 const T&
111 operator [] (unsigned int i) const
112 {
113 return stack_[range_ - i];
114 }
115
116 private:
117 const S& stack_;
118 unsigned int range_;
119 };
120 ]b4_namespace_close[
121
122 #endif // not BISON_STACK_HH[]dnl
123 ]
124 m4_divert_pop(0)
125 m4_popdef([b4_copyright_years])dnl
126 m4_changecom([#])