]> git.saurik.com Git - bison.git/blob - data/stack.hh
Merge branch '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 # b4_stack_define
22 # ---------------
23 m4_define([b4_stack_define],
24 [[ template <class T, class S = std::deque<T> >
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)
68 seq_.pop_front ();
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)
103 : stack_ (stack)
104 , range_ (range)
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 };
119 ]])
120
121 b4_defines_if(
122 [# We do want M4 expansion after # for CPP macros.
123 m4_changecom()
124 m4_divert_push(0)dnl
125 @output(b4_dir_prefix[]stack.hh@)@
126 b4_copyright([Stack handling for Bison parsers in C++])[
127
128 /**
129 ** \file ]b4_dir_prefix[stack.hh
130 ** Define the ]b4_namespace_ref[::stack class.
131 */
132
133 ]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
134
135 # include <deque>
136
137 ]b4_namespace_open[
138 ]b4_stack_define[
139 ]b4_namespace_close[
140
141 ]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
142 m4_divert_pop(0)
143 m4_popdef([b4_copyright_years])dnl
144 m4_changecom([#])
145 ])