]> git.saurik.com Git - bison.git/blame - data/variant.hh
Style changes.
[bison.git] / data / variant.hh
CommitLineData
507aa0e2
AD
1# C++ skeleton for Bison
2
3# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4# Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20## --------- ##
21## variant. ##
22## --------- ##
23
24# b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
25# ------------------------------------------------
26# Run some ACTION ("build", or "destroy") on YYVAL of symbol type
27# YYTYPE.
28m4_define([b4_symbol_variant],
29[m4_pushdef([b4_dollar_dollar],
30 [$2.$3< $][3 >(m4_shift3($@))])dnl
31 switch ($1)
32 {
33b4_type_foreach([b4_type_action_])[]dnl
34 default:
35 break;
36 }
37m4_popdef([b4_dollar_dollar])dnl
38])
39
40
41# _b4_char_sizeof_counter
42# -----------------------
43# A counter used by _b4_char_sizeof_dummy to create fresh symbols.
44m4_define([_b4_char_sizeof_counter],
45[0])
46
47# _b4_char_sizeof_dummy
48# ---------------------
49# At each call return a new C++ identifier.
50m4_define([_b4_char_sizeof_dummy],
51[m4_define([_b4_char_sizeof_counter], m4_incr(_b4_char_sizeof_counter))dnl
52dummy[]_b4_char_sizeof_counter])
53
54
55# b4_char_sizeof_(SYMBOL-NUM)
56# ---------------------------
57# A comment describing this symbol.
58m4_define([b4_char_sizeof_],
59[ // b4_symbol([$1], [tag])
60])
61
62# b4_char_sizeof(SYMBOL-NUMS)
63# ---------------------------
64# To be mapped on the list of type names to produce:
65#
66# char dummy1[sizeof(type_name_1)];
67# char dummy2[sizeof(type_name_2)];
68#
69# for defined type names.
70m4_define([b4_char_sizeof],
71[b4_symbol_if([$1], [has_type],
72[
73m4_map([b4_char_sizeof_], [$@])dnl
74 char _b4_char_sizeof_dummy@{sizeof([b4_symbol([$1], [type])])@};
75])])
76
77
78# b4_variant_definition
79# ---------------------
80# Define "variant".
81m4_define([b4_variant_definition],
82[[
83 /// A char[S] buffer to store and retrieve objects.
84 ///
85 /// Sort of a variant, but does not keep track of the nature
86 /// of the stored data, since that knowledge is available
87 /// via the current state.
88 template <size_t S>
89 struct variant
90 {]b4_assert_if([
91 /// Whether something is contained.
92 bool built;
93])[
94 /// Empty construction.
95 inline
96 variant ()]b4_assert_if([
97 : built (false)])[
98 {}
99
100 /// Instantiate a \a T in here.
101 template <typename T>
102 inline T&
103 build ()
104 {]b4_assert_if([
105 assert (!built);
106 built = true;])[
107 return *new (buffer.raw) T;
108 }
109
110 /// Instantiate a \a T in here from \a t.
111 template <typename T>
112 inline T&
113 build (const T& t)
114 {]b4_assert_if([
115 assert(!built);
116 built = true;])[
117 return *new (buffer.raw) T(t);
118 }
119
120 /// Construct and fill.
121 template <typename T>
122 inline
123 variant (const T& t)]b4_assert_if([
124 : built (true)])[
125 {
126 new (buffer.raw) T(t);
127 }
128
129 /// Accessor to a built \a T.
130 template <typename T>
131 inline T&
132 as ()
133 {]b4_assert_if([
134 assert (built);])[
135 return reinterpret_cast<T&>(buffer.raw);
136 }
137
138 /// Const accessor to a built \a T (for %printer).
139 template <typename T>
140 inline const T&
141 as () const
142 {]b4_assert_if([
143 assert(built);])[
144 return reinterpret_cast<const T&>(buffer.raw);
145 }
146
147 /// Swap the content with \a other.
148 template <typename T>
149 inline void
150 swap (variant<S>& other)
151 {
152 std::swap (as<T>(), other.as<T>());
153 }
154
155 /// Assign the content of \a other to this.
156 /// Destroys \a other.
157 template <typename T>
158 inline void
159 build (variant<S>& other)
160 {
161 build<T>();
162 swap<T>(other);
163 other.destroy<T>();
164 }
165
166 /// Destroy the stored \a T.
167 template <typename T>
168 inline void
169 destroy ()
170 {
171 as<T>().~T();]b4_assert_if([
172 built = false;])[
173 }
174
175 /// A buffer large enough to store any of the semantic values.
176 /// Long double is chosen as it has the strongest alignment
177 /// constraints.
178 union
179 {
180 long double align_me;
181 char raw[S];
182 } buffer;
183 };
184]])
185
186
187## -------------------------- ##
188## Adjustments for variants. ##
189## -------------------------- ##
190
191
192# How the semantic value is extracted when using variants.
193
194# b4_symbol_value(VAL, [TYPE])
195# ----------------------------
196m4_define([b4_symbol_value],
197[m4_ifval([$2],
198 [$1.as< $2 >()],
199 [$1])])
200
201# b4_symbol_value_template(VAL, [TYPE])
202# -------------------------------------
203# Same as b4_symbol_value, but used in a template method.
204m4_define([b4_symbol_value_template],
205[m4_ifval([$2],
206 [$1.template as< $2 >()],
207 [$1])])