]> git.saurik.com Git - bison.git/blob - data/stack.hh
c++: fix position operator signatures
[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 ]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
34
35 # include <deque>
36
37 ]b4_namespace_open[
38 template <class T, class S = std::deque<T> >
39 class stack
40 {
41 public:
42 // Hide our reversed order.
43 typedef typename S::reverse_iterator iterator;
44 typedef typename S::const_reverse_iterator const_iterator;
45
46 stack () : seq_ ()
47 {
48 }
49
50 stack (unsigned int n) : seq_ (n)
51 {
52 }
53
54 inline
55 T&
56 operator [] (unsigned int i)
57 {
58 return seq_[i];
59 }
60
61 inline
62 const T&
63 operator [] (unsigned int i) const
64 {
65 return seq_[i];
66 }
67
68 inline
69 void
70 push (const T& t)
71 {
72 seq_.push_front (t);
73 }
74
75 inline
76 void
77 pop (unsigned int n = 1)
78 {
79 for (; n; --n)
80 seq_.pop_front ();
81 }
82
83 inline
84 unsigned int
85 height () const
86 {
87 return seq_.size ();
88 }
89
90 inline const_iterator begin () const { return seq_.rbegin (); }
91 inline const_iterator end () const { return seq_.rend (); }
92
93 private:
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 ]b4_namespace_close[
120
121 ]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
122 m4_divert_pop(0)
123 m4_popdef([b4_copyright_years])dnl
124 m4_changecom([#])