]> 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 20
5de5b987
AD
21# b4_stack_define
22# ---------------
23m4_define([b4_stack_define],
24[[ template <class T, class S = std::deque<T> >
51bacae6
AD
25 class stack
26 {
27 public:
28 // Hide our reversed order.
29 typedef typename S::reverse_iterator iterator;
30 typedef typename S::const_reverse_iterator const_iterator;
31
32 stack ()
33 : seq_ ()
34 {
35 }
36
37 stack (unsigned int n)
38 : seq_ (n)
39 {
40 }
41
42 inline
43 T&
44 operator [] (unsigned int i)
45 {
46 return seq_[i];
47 }
48
49 inline
50 const T&
51 operator [] (unsigned int i) const
52 {
53 return seq_[i];
54 }
55
56 inline
57 void
58 push (const T& t)
59 {
60 seq_.push_front (t);
61 }
62
63 inline
64 void
65 pop (unsigned int n = 1)
66 {
67 for (; n; --n)
e9690142 68 seq_.pop_front ();
51bacae6
AD
69 }
70
71 inline
72 typename S::size_type
73 size () const
74 {
75 return seq_.size ();
76 }
77
78 inline
79 const_iterator
80 begin () const
81 {
82 return seq_.rbegin ();
83 }
84
85 inline
86 const_iterator
87 end () const
88 {
89 return seq_.rend ();
90 }
91
92 private:
93 /// The wrapped container.
94 S seq_;
95 };
96
97 /// Present a slice of the top of a stack.
98 template <class T, class S = stack<T> >
99 class slice
100 {
101 public:
102 slice (const S& stack, unsigned int range)
a9b67b5b
AD
103 : stack_ (stack)
104 , range_ (range)
51bacae6
AD
105 {
106 }
107
108 inline
109 const T&
110 operator [] (unsigned int i) const
111 {
112 return stack_[range_ - i];
113 }
114
115 private:
116 const S& stack_;
117 unsigned int range_;
118 };
5de5b987
AD
119]])
120
121b4_defines_if(
064e42b0 122[b4_output_begin([b4_dir_prefix[]stack.hh])
5de5b987
AD
123b4_copyright([Stack handling for Bison parsers in C++])[
124
125/**
126 ** \file ]b4_dir_prefix[stack.hh
127 ** Define the ]b4_namespace_ref[::stack class.
128 */
129
130]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
131
132# include <deque>
133
134]b4_namespace_open[
135]b4_stack_define[
51bacae6
AD
136]b4_namespace_close[
137
22172d47 138]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
1c7ec959 139b4_output_end()
5de5b987 140])
1c7ec959
AD
141
142m4_popdef([b4_copyright_years])