]> git.saurik.com Git - bison.git/blame - data/stack.hh
m4: allow the definition of side-effect only macros
[bison.git] / data / stack.hh
CommitLineData
51bacae6
AD
1# C++ skeleton for Bison
2
7d6bad19 3# Copyright (C) 2002-2013 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],
7d6bad19 19 [2002-2013])
51bacae6 20
5de5b987
AD
21# b4_stack_define
22# ---------------
23m4_define([b4_stack_define],
e7b26e94 24[[ template <class T, class S = std::vector<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&
5f87211c 44 operator[] (unsigned int i)
51bacae6 45 {
e7b26e94 46 return seq_[seq_.size () - 1 - i];
51bacae6
AD
47 }
48
49 inline
50 const T&
5f87211c 51 operator[] (unsigned int i) const
51bacae6 52 {
e7b26e94 53 return seq_[seq_.size () - 1 - i];
51bacae6
AD
54 }
55
97ae878e
AD
56 /// Steal the contents of \a t.
57 ///
58 /// Close to move-semantics.
51bacae6
AD
59 inline
60 void
97ae878e 61 push (T& t)
51bacae6 62 {
97ae878e
AD
63 seq_.push_back (T());
64 operator[](0).move (t);
51bacae6
AD
65 }
66
67 inline
68 void
69 pop (unsigned int n = 1)
70 {
71 for (; n; --n)
e7b26e94 72 seq_.pop_back ();
51bacae6
AD
73 }
74
75 inline
76 typename S::size_type
77 size () const
78 {
79 return seq_.size ();
80 }
81
82 inline
83 const_iterator
84 begin () const
85 {
86 return seq_.rbegin ();
87 }
88
89 inline
90 const_iterator
91 end () const
92 {
93 return seq_.rend ();
94 }
95
96 private:
97 /// The wrapped container.
98 S seq_;
99 };
100
101 /// Present a slice of the top of a stack.
102 template <class T, class S = stack<T> >
103 class slice
104 {
105 public:
106 slice (const S& stack, unsigned int range)
a9b67b5b
AD
107 : stack_ (stack)
108 , range_ (range)
51bacae6
AD
109 {
110 }
111
112 inline
113 const T&
114 operator [] (unsigned int i) const
115 {
116 return stack_[range_ - i];
117 }
118
119 private:
120 const S& stack_;
121 unsigned int range_;
122 };
5de5b987
AD
123]])
124
125b4_defines_if(
064e42b0 126[b4_output_begin([b4_dir_prefix[]stack.hh])
5de5b987
AD
127b4_copyright([Stack handling for Bison parsers in C++])[
128
129/**
130 ** \file ]b4_dir_prefix[stack.hh
131 ** Define the ]b4_namespace_ref[::stack class.
132 */
133
134]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
135
e7b26e94 136# include <vector>
5de5b987
AD
137
138]b4_namespace_open[
139]b4_stack_define[
51bacae6
AD
140]b4_namespace_close[
141
22172d47 142]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
1c7ec959 143b4_output_end()
5de5b987 144])
1c7ec959
AD
145
146m4_popdef([b4_copyright_years])