]> git.saurik.com Git - bison.git/blame - data/stack.hh
Merge remote-tracking branch 'origin/maint'
[bison.git] / data / stack.hh
CommitLineData
51bacae6
AD
1# C++ skeleton for Bison
2
34136e65 3# Copyright (C) 2002-2012 Free Software Foundation, Inc.
51bacae6
AD
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
18m4_pushdef([b4_copyright_years],
34136e65 19 [2002-2012])
51bacae6
AD
20
21# We do want M4 expansion after # for CPP macros.
22m4_changecom()
23m4_divert_push(0)dnl
24@output(b4_dir_prefix[]stack.hh@)@
25b4_copyright([Stack handling for Bison parsers in C++])[
26
03f1b545
AD
27/**
28 ** \file ]b4_dir_prefix[stack.hh
29 ** Define the ]b4_namespace_ref[::stack class.
30 */
51bacae6 31
22172d47 32]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
a9b67b5b
AD
33
34# include <deque>
51bacae6
AD
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)
e9690142 81 seq_.pop_front ();
51bacae6
AD
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)
a9b67b5b
AD
116 : stack_ (stack)
117 , range_ (range)
51bacae6
AD
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
22172d47 134]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
51bacae6
AD
135m4_divert_pop(0)
136m4_popdef([b4_copyright_years])dnl
137m4_changecom([#])