diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2022-12-14 10:33:34 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2022-12-14 10:42:27 -0500 |
commit | e5a049d36221c6d07cb174a9a6a4a373ded656f4 (patch) | |
tree | fbf01ddc60df0896a68a782effbf3b3dda202d4b /generator/part.h | |
parent | e4fe5974f123cfba6d8eff908e7f1772b0b9635f (diff) | |
download | verbly-e5a049d36221c6d07cb174a9a6a4a373ded656f4.tar.gz verbly-e5a049d36221c6d07cb174a9a6a4a373ded656f4.tar.bz2 verbly-e5a049d36221c6d07cb174a9a6a4a373ded656f4.zip |
Converted verbly::generator::part to std::variant
fixes #5
Diffstat (limited to 'generator/part.h')
-rw-r--r-- | generator/part.h | 88 |
1 files changed, 56 insertions, 32 deletions
diff --git a/generator/part.h b/generator/part.h index 01791f5..550f95f 100644 --- a/generator/part.h +++ b/generator/part.h | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | #include <string> | 4 | #include <string> |
5 | #include <set> | 5 | #include <set> |
6 | #include <variant> | ||
6 | #include "../lib/enums.h" | 7 | #include "../lib/enums.h" |
7 | 8 | ||
8 | namespace verbly { | 9 | namespace verbly { |
@@ -32,24 +33,6 @@ namespace verbly { | |||
32 | 33 | ||
33 | static part duplicate(const part& other); | 34 | static part duplicate(const part& other); |
34 | 35 | ||
35 | // Copy and move constructors | ||
36 | |||
37 | part(const part& other); | ||
38 | |||
39 | part(part&& other); | ||
40 | |||
41 | // Assignment | ||
42 | |||
43 | part& operator=(part other); | ||
44 | |||
45 | // Swap | ||
46 | |||
47 | friend void swap(part& first, part& second); | ||
48 | |||
49 | // Destructor | ||
50 | |||
51 | ~part(); | ||
52 | |||
53 | // General accessors | 36 | // General accessors |
54 | 37 | ||
55 | int getId() const | 38 | int getId() const |
@@ -82,6 +65,26 @@ namespace verbly { | |||
82 | 65 | ||
83 | private: | 66 | private: |
84 | 67 | ||
68 | struct noun_phrase_type { | ||
69 | std::string role; | ||
70 | std::set<std::string> selrestrs; | ||
71 | std::set<std::string> synrestrs; | ||
72 | }; | ||
73 | |||
74 | struct preposition_type { | ||
75 | std::set<std::string> choices; | ||
76 | bool literal; | ||
77 | }; | ||
78 | |||
79 | using literal_type = std::string; | ||
80 | |||
81 | using variant_type = | ||
82 | std::variant< | ||
83 | std::monostate, | ||
84 | noun_phrase_type, | ||
85 | preposition_type, | ||
86 | literal_type>; | ||
87 | |||
85 | static int nextId_; | 88 | static int nextId_; |
86 | 89 | ||
87 | int id_; | 90 | int id_; |
@@ -92,26 +95,47 @@ namespace verbly { | |||
92 | { | 95 | { |
93 | } | 96 | } |
94 | 97 | ||
95 | part(type t) : | 98 | part(type t, variant_type variant = {}) : |
96 | id_(nextId_++), | 99 | id_(nextId_++), |
97 | type_(t) | 100 | type_(t), |
101 | variant_(std::move(variant)) | ||
98 | { | 102 | { |
103 | bool valid_type = false; | ||
104 | switch (type_) | ||
105 | { | ||
106 | case part_type::noun_phrase: | ||
107 | { | ||
108 | valid_type = std::holds_alternative<noun_phrase_type>(variant_); | ||
109 | break; | ||
110 | } | ||
111 | case part_type::preposition: | ||
112 | { | ||
113 | valid_type = std::holds_alternative<preposition_type>(variant_); | ||
114 | break; | ||
115 | } | ||
116 | case part_type::literal: | ||
117 | { | ||
118 | valid_type = std::holds_alternative<literal_type>(variant_); | ||
119 | break; | ||
120 | } | ||
121 | case part_type::invalid: | ||
122 | case part_type::verb: | ||
123 | case part_type::adjective: | ||
124 | case part_type::adverb: | ||
125 | { | ||
126 | valid_type = std::holds_alternative<std::monostate>(variant_); | ||
127 | break; | ||
128 | } | ||
129 | } | ||
130 | if (!valid_type) | ||
131 | { | ||
132 | throw std::invalid_argument("Invalid variant provided for part"); | ||
133 | } | ||
99 | } | 134 | } |
100 | 135 | ||
101 | // Data | 136 | // Data |
102 | 137 | ||
103 | union { | 138 | variant_type variant_; |
104 | struct { | ||
105 | std::string role; | ||
106 | std::set<std::string> selrestrs; | ||
107 | std::set<std::string> synrestrs; | ||
108 | } noun_phrase_; | ||
109 | struct { | ||
110 | std::set<std::string> choices; | ||
111 | bool literal; | ||
112 | } preposition_; | ||
113 | std::string literal_; | ||
114 | }; | ||
115 | 139 | ||
116 | type type_ = type::invalid; | 140 | type type_ = type::invalid; |
117 | 141 | ||