]> git.saurik.com Git - bison.git/blob - data/stack.hh
Merge remote-tracking branch 'origin/maint'
[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 /**
28 ** \file ]b4_dir_prefix[stack.hh
29 ** Define the ]b4_namespace_ref[::stack class.
30 */
31
32 ]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
33
34 # include <deque>
35
36 ]b4_namespace_open[
37 template <class T, class S = std::deque<T> >
38 class stack
39 {
40 public:
41 // Hide our reversed order.
42 typedef typename S::reverse_iterator iterator;
43 typedef typename S::const_reverse_iterator const_iterator;
44
45 stack ()
46 : seq_ ()
47 {
48 }
49
50 stack (unsigned int n)
51 : 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 typename S::size_type
86 size () const
87 {
88 return seq_.size ();
89 }
90
91 inline
92 const_iterator
93 begin () const
94 {
95 return seq_.rbegin ();
96 }
97
98 inline
99 const_iterator
100 end () const
101 {
102 return seq_.rend ();
103 }
104
105 private:
106 /// The wrapped container.
107 S seq_;
108 };
109
110 /// Present a slice of the top of a stack.
111 template <class T, class S = stack<T> >
112 class slice
113 {
114 public:
115 slice (const S& stack, unsigned int range)
116 : stack_ (stack)
117 , range_ (range)
118 {
119 }
120
121 inline
122 const T&
123 operator [] (unsigned int i) const
124 {
125 return stack_[range_ - i];
126 }
127
128 private:
129 const S& stack_;
130 unsigned int range_;
131 };
132 ]b4_namespace_close[
133
134 ]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
135 m4_divert_pop(0)
136 m4_popdef([b4_copyright_years])dnl
137 m4_changecom([#])