]> git.saurik.com Git - bison.git/blob - data/stack.hh
skeletons: avoid empty switch constructs
[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_output_begin([b4_dir_prefix[]stack.hh])
22 b4_copyright([Stack handling for Bison parsers in C++],
23 [2002-2012])[
24
25 /**
26 ** \file ]b4_dir_prefix[stack.hh
27 ** Define the ]b4_namespace_ref[::stack class.
28 */
29
30 ]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
31
32 # include <deque>
33
34 ]b4_namespace_open[
35 template <class T, class S = std::deque<T> >
36 class stack
37 {
38 public:
39 // Hide our reversed order.
40 typedef typename S::reverse_iterator iterator;
41 typedef typename S::const_reverse_iterator const_iterator;
42
43 stack () : seq_ ()
44 {
45 }
46
47 stack (unsigned int n) : seq_ (n)
48 {
49 }
50
51 inline
52 T&
53 operator [] (unsigned int i)
54 {
55 return seq_[i];
56 }
57
58 inline
59 const T&
60 operator [] (unsigned int i) const
61 {
62 return seq_[i];
63 }
64
65 inline
66 void
67 push (const T& t)
68 {
69 seq_.push_front (t);
70 }
71
72 inline
73 void
74 pop (unsigned int n = 1)
75 {
76 for (; n; --n)
77 seq_.pop_front ();
78 }
79
80 void
81 clear ()
82 {
83 seq_.clear ();
84 }
85
86 inline
87 unsigned int
88 height () const
89 {
90 return seq_.size ();
91 }
92
93 inline const_iterator begin () const { return seq_.rbegin (); }
94 inline const_iterator end () const { return seq_.rend (); }
95
96 private:
97 stack (const stack&);
98 stack& operator= (const stack&);
99 S seq_;
100 };
101
102 /// Present a slice of the top of a stack.
103 template <class T, class S = stack<T> >
104 class slice
105 {
106 public:
107 slice (const S& stack, unsigned int range)
108 : stack_ (stack)
109 , range_ (range)
110 {
111 }
112
113 inline
114 const T&
115 operator [] (unsigned int i) const
116 {
117 return stack_[range_ - i];
118 }
119
120 private:
121 const S& stack_;
122 unsigned int range_;
123 };
124 ]b4_namespace_close[
125
126 ]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
127 b4_output_end()
128
129 m4_popdef([b4_copyright_years])