xref: /aosp_15_r20/prebuilts/build-tools/common/bison/skeletons/stack.hh (revision cda5da8d549138a6648c5ee6d7a49cf8f4a657be)
1 # C++ skeleton for Bison
2 
3 # Copyright (C) 2002-2015, 2018-2021 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 <https://www.gnu.org/licenses/>.
17 
18 
19 # b4_stack_file
20 # -------------
21 # Name of the file containing the stack class, if we want this file.
22 b4_header_if([b4_required_version_if([30200], [],
23                                      [m4_define([b4_stack_file], [stack.hh])])])
24 
25 
26 # b4_stack_define
27 # ---------------
28 m4_define([b4_stack_define],
29 [[    /// A stack with random access from its top.
30     template <typename T, typename S = std::vector<T> >
31     class stack
32     {
33     public:
34       // Hide our reversed order.
35       typedef typename S::iterator iterator;
36       typedef typename S::const_iterator const_iterator;
37       typedef typename S::size_type size_type;
38       typedef typename std::ptrdiff_t index_type;
39 
40       stack (size_type n = 200) YY_NOEXCEPT
41         : seq_ (n)
42       {}
43 
44 #if 201103L <= YY_CPLUSPLUS
45       /// Non copyable.
46       stack (const stack&) = delete;
47       /// Non copyable.
48       stack& operator= (const stack&) = delete;
49 #endif
50 
51       /// Random access.
52       ///
53       /// Index 0 returns the topmost element.
54       const T&
55       operator[] (index_type i) const
56       {
57         return seq_[size_type (size () - 1 - i)];
58       }
59 
60       /// Random access.
61       ///
62       /// Index 0 returns the topmost element.
63       T&
64       operator[] (index_type i)
65       {
66         return seq_[size_type (size () - 1 - i)];
67       }
68 
69       /// Steal the contents of \a t.
70       ///
71       /// Close to move-semantics.
72       void
73       push (YY_MOVE_REF (T) t)
74       {
75         seq_.push_back (T ());
76         operator[] (0).move (t);
77       }
78 
79       /// Pop elements from the stack.
80       void
81       pop (std::ptrdiff_t n = 1) YY_NOEXCEPT
82       {
83         for (; 0 < n; --n)
84           seq_.pop_back ();
85       }
86 
87       /// Pop all elements from the stack.
88       void
89       clear () YY_NOEXCEPT
90       {
91         seq_.clear ();
92       }
93 
94       /// Number of elements on the stack.
95       index_type
96       size () const YY_NOEXCEPT
97       {
98         return index_type (seq_.size ());
99       }
100 
101       /// Iterator on top of the stack (going downwards).
102       const_iterator
103       begin () const YY_NOEXCEPT
104       {
105         return seq_.begin ();
106       }
107 
108       /// Bottom of the stack.
109       const_iterator
110       end () const YY_NOEXCEPT
111       {
112         return seq_.end ();
113       }
114 
115       /// Present a slice of the top of a stack.
116       class slice
117       {
118       public:
119         slice (const stack& stack, index_type range) YY_NOEXCEPT
120           : stack_ (stack)
121           , range_ (range)
122         {}
123 
124         const T&
125         operator[] (index_type i) const
126         {
127           return stack_[range_ - i];
128         }
129 
130       private:
131         const stack& stack_;
132         index_type range_;
133       };
134 
135     private:
136 #if YY_CPLUSPLUS < 201103L
137       /// Non copyable.
138       stack (const stack&);
139       /// Non copyable.
140       stack& operator= (const stack&);
141 #endif
142       /// The wrapped container.
143       S seq_;
144     };
145 ]])
146 
147 m4_ifdef([b4_stack_file],
148 [b4_output_begin([b4_dir_prefix], [b4_stack_file])[
149 ]b4_generated_by[
150 // Starting with Bison 3.2, this file is useless: the structure it
151 // used to define is now defined with the parser itself.
152 //
153 // To get rid of this file:
154 // 1. add '%require "3.2"' (or newer) to your grammar file
155 // 2. remove references to this file from your build system.
156 ]b4_output_end[
157 ]])
158