diff options
Diffstat (limited to 'generator/part.cpp')
-rw-r--r-- | generator/part.cpp | 336 |
1 files changed, 336 insertions, 0 deletions
diff --git a/generator/part.cpp b/generator/part.cpp new file mode 100644 index 0000000..dbd4e11 --- /dev/null +++ b/generator/part.cpp | |||
@@ -0,0 +1,336 @@ | |||
1 | #include "part.h" | ||
2 | #include <stdexcept> | ||
3 | #include "selrestr.h" | ||
4 | |||
5 | namespace verbly { | ||
6 | namespace generator { | ||
7 | |||
8 | part part::createNounPhrase(std::string role, selrestr selrestrs, std::set<std::string> synrestrs) | ||
9 | { | ||
10 | part p(type::noun_phrase); | ||
11 | |||
12 | new(&p.noun_phrase_.role) std::string(std::move(role)); | ||
13 | new(&p.noun_phrase_.selrestrs) selrestr(std::move(selrestrs)); | ||
14 | new(&p.noun_phrase_.synrestrs) std::set<std::string>(std::move(synrestrs)); | ||
15 | |||
16 | return p; | ||
17 | } | ||
18 | |||
19 | part part::createVerb() | ||
20 | { | ||
21 | return part(type::verb); | ||
22 | } | ||
23 | |||
24 | part part::createPreposition(std::set<std::string> choices, bool literal) | ||
25 | { | ||
26 | part p(type::preposition); | ||
27 | |||
28 | new(&p.preposition_.choices) std::set<std::string>(std::move(choices)); | ||
29 | p.preposition_.literal = literal; | ||
30 | |||
31 | return p; | ||
32 | } | ||
33 | |||
34 | part part::createAdjective() | ||
35 | { | ||
36 | return part(type::adjective); | ||
37 | } | ||
38 | |||
39 | part part::createAdverb() | ||
40 | { | ||
41 | return part(type::adverb); | ||
42 | } | ||
43 | |||
44 | part part::createLiteral(std::string value) | ||
45 | { | ||
46 | part p(type::literal); | ||
47 | |||
48 | new(&p.literal_) std::string(std::move(value)); | ||
49 | |||
50 | return p; | ||
51 | } | ||
52 | |||
53 | part::part(const part& other) | ||
54 | { | ||
55 | type_ = other.type_; | ||
56 | |||
57 | switch (type_) | ||
58 | { | ||
59 | case type::noun_phrase: | ||
60 | { | ||
61 | new(&noun_phrase_.role) std::string(other.noun_phrase_.role); | ||
62 | new(&noun_phrase_.selrestrs) selrestr(other.noun_phrase_.selrestrs); | ||
63 | new(&noun_phrase_.synrestrs) std::set<std::string>(other.noun_phrase_.synrestrs); | ||
64 | |||
65 | break; | ||
66 | } | ||
67 | |||
68 | case type::preposition: | ||
69 | { | ||
70 | new(&preposition_.choices) std::set<std::string>(other.preposition_.choices); | ||
71 | preposition_.literal = other.preposition_.literal; | ||
72 | |||
73 | break; | ||
74 | } | ||
75 | |||
76 | case type::literal: | ||
77 | { | ||
78 | new(&literal_) std::string(other.literal_); | ||
79 | |||
80 | break; | ||
81 | } | ||
82 | |||
83 | case type::verb: | ||
84 | case type::adjective: | ||
85 | case type::adverb: | ||
86 | case type::invalid: | ||
87 | { | ||
88 | break; | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | |||
93 | part::part(part&& other) : part() | ||
94 | { | ||
95 | swap(*this, other); | ||
96 | } | ||
97 | |||
98 | part& part::operator=(part other) | ||
99 | { | ||
100 | swap(*this, other); | ||
101 | |||
102 | return *this; | ||
103 | } | ||
104 | |||
105 | void swap(part& first, part& second) | ||
106 | { | ||
107 | using type = part::type; | ||
108 | |||
109 | type tempType = first.type_; | ||
110 | std::string tempRole; | ||
111 | selrestr tempSelrestrs; | ||
112 | std::set<std::string> tempSynrestrs; | ||
113 | std::set<std::string> tempChoices; | ||
114 | bool tempPrepLiteral; | ||
115 | std::string tempLiteralValue; | ||
116 | |||
117 | switch (tempType) | ||
118 | { | ||
119 | case type::noun_phrase: | ||
120 | { | ||
121 | tempRole = std::move(first.noun_phrase_.role); | ||
122 | tempSelrestrs = std::move(first.noun_phrase_.selrestrs); | ||
123 | tempSynrestrs = std::move(first.noun_phrase_.synrestrs); | ||
124 | |||
125 | break; | ||
126 | } | ||
127 | |||
128 | case type::preposition: | ||
129 | { | ||
130 | tempChoices = std::move(first.preposition_.choices); | ||
131 | tempPrepLiteral = first.preposition_.literal; | ||
132 | |||
133 | break; | ||
134 | } | ||
135 | |||
136 | case type::literal: | ||
137 | { | ||
138 | tempLiteralValue = std::move(first.literal_); | ||
139 | |||
140 | break; | ||
141 | } | ||
142 | |||
143 | case type::verb: | ||
144 | case type::adjective: | ||
145 | case type::adverb: | ||
146 | case type::invalid: | ||
147 | { | ||
148 | break; | ||
149 | } | ||
150 | } | ||
151 | |||
152 | first.~part(); | ||
153 | |||
154 | first.type_ = second.type_; | ||
155 | |||
156 | switch (first.type_) | ||
157 | { | ||
158 | case type::noun_phrase: | ||
159 | { | ||
160 | new(&first.noun_phrase_.role) std::string(std::move(second.noun_phrase_.role)); | ||
161 | new(&first.noun_phrase_.selrestrs) selrestr(std::move(second.noun_phrase_.selrestrs)); | ||
162 | new(&first.noun_phrase_.synrestrs) std::set<std::string>(std::move(second.noun_phrase_.synrestrs)); | ||
163 | |||
164 | break; | ||
165 | } | ||
166 | |||
167 | case type::preposition: | ||
168 | { | ||
169 | new(&first.preposition_.choices) std::set<std::string>(std::move(second.preposition_.choices)); | ||
170 | first.preposition_.literal = second.preposition_.literal; | ||
171 | |||
172 | break; | ||
173 | } | ||
174 | |||
175 | case type::literal: | ||
176 | { | ||
177 | new(&first.literal_) std::string(std::move(second.literal_)); | ||
178 | |||
179 | break; | ||
180 | } | ||
181 | |||
182 | case type::verb: | ||
183 | case type::adjective: | ||
184 | case type::adverb: | ||
185 | case type::invalid: | ||
186 | { | ||
187 | break; | ||
188 | } | ||
189 | } | ||
190 | |||
191 | second.~part(); | ||
192 | |||
193 | second.type_ = tempType; | ||
194 | |||
195 | switch (second.type_) | ||
196 | { | ||
197 | case type::noun_phrase: | ||
198 | { | ||
199 | new(&second.noun_phrase_.role) std::string(std::move(tempRole)); | ||
200 | new(&second.noun_phrase_.selrestrs) selrestr(std::move(tempSelrestrs)); | ||
201 | new(&second.noun_phrase_.synrestrs) std::set<std::string>(std::move(tempSynrestrs)); | ||
202 | |||
203 | break; | ||
204 | } | ||
205 | |||
206 | case type::preposition: | ||
207 | { | ||
208 | new(&second.preposition_.choices) std::set<std::string>(std::move(tempChoices)); | ||
209 | second.preposition_.literal = tempPrepLiteral; | ||
210 | |||
211 | break; | ||
212 | } | ||
213 | |||
214 | case type::literal: | ||
215 | { | ||
216 | new(&second.literal_) std::string(std::move(tempLiteralValue)); | ||
217 | |||
218 | break; | ||
219 | } | ||
220 | |||
221 | case type::verb: | ||
222 | case type::adjective: | ||
223 | case type::adverb: | ||
224 | case type::invalid: | ||
225 | { | ||
226 | break; | ||
227 | } | ||
228 | } | ||
229 | } | ||
230 | |||
231 | part::~part() | ||
232 | { | ||
233 | switch (type_) | ||
234 | { | ||
235 | case type::noun_phrase: | ||
236 | { | ||
237 | using string_type = std::string; | ||
238 | using set_type = std::set<std::string>; | ||
239 | |||
240 | noun_phrase_.role.~string_type(); | ||
241 | noun_phrase_.selrestrs.~selrestr(); | ||
242 | noun_phrase_.synrestrs.~set_type(); | ||
243 | |||
244 | break; | ||
245 | } | ||
246 | |||
247 | case type::preposition: | ||
248 | { | ||
249 | using set_type = std::set<std::string>; | ||
250 | |||
251 | preposition_.choices.~set_type(); | ||
252 | |||
253 | break; | ||
254 | } | ||
255 | |||
256 | case type::literal: | ||
257 | { | ||
258 | using string_type = std::string; | ||
259 | |||
260 | literal_.~string_type(); | ||
261 | |||
262 | break; | ||
263 | } | ||
264 | |||
265 | case type::verb: | ||
266 | case type::adjective: | ||
267 | case type::adverb: | ||
268 | case type::invalid: | ||
269 | { | ||
270 | break; | ||
271 | } | ||
272 | } | ||
273 | } | ||
274 | |||
275 | std::string part::getNounRole() const | ||
276 | { | ||
277 | if (type_ == type::noun_phrase) | ||
278 | { | ||
279 | return noun_phrase_.role; | ||
280 | } else { | ||
281 | throw std::domain_error("part::getNounRole is only valid for noun phrase parts"); | ||
282 | } | ||
283 | } | ||
284 | |||
285 | selrestr part::getNounSelrestrs() const | ||
286 | { | ||
287 | if (type_ == type::noun_phrase) | ||
288 | { | ||
289 | return noun_phrase_.selrestrs; | ||
290 | } else { | ||
291 | throw std::domain_error("part::getNounSelrestrs is only valid for noun phrase parts"); | ||
292 | } | ||
293 | } | ||
294 | |||
295 | std::set<std::string> part::getNounSynrestrs() const | ||
296 | { | ||
297 | if (type_ == type::noun_phrase) | ||
298 | { | ||
299 | return noun_phrase_.synrestrs; | ||
300 | } else { | ||
301 | throw std::domain_error("part::getNounSynrestrs is only valid for noun phrase parts"); | ||
302 | } | ||
303 | } | ||
304 | |||
305 | std::set<std::string> part::getPrepositionChoices() const | ||
306 | { | ||
307 | if (type_ == type::preposition) | ||
308 | { | ||
309 | return preposition_.choices; | ||
310 | } else { | ||
311 | throw std::domain_error("part::getPrepositionChoices is only valid for preposition parts"); | ||
312 | } | ||
313 | } | ||
314 | |||
315 | bool part::isPrepositionLiteral() const | ||
316 | { | ||
317 | if (type_ == type::preposition) | ||
318 | { | ||
319 | return preposition_.literal; | ||
320 | } else { | ||
321 | throw std::domain_error("part::isPrepositionLiteral is only valid for preposition parts"); | ||
322 | } | ||
323 | } | ||
324 | |||
325 | std::string part::getLiteralValue() const | ||
326 | { | ||
327 | if (type_ == type::literal) | ||
328 | { | ||
329 | return literal_; | ||
330 | } else { | ||
331 | throw std::domain_error("part::getLiteralValue is only valid for literal parts"); | ||
332 | } | ||
333 | } | ||
334 | |||
335 | }; | ||
336 | }; | ||