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