summary refs log tree commit diff stats
path: root/generator/part.h
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2022-12-14 10:33:34 -0500
committerStar Rauchenberger <fefferburbia@gmail.com>2022-12-14 10:33:34 -0500
commit03596c07406ea33f76a0fbb3e004df50b8b832e6 (patch)
tree5d974cae10add856348f410986f67728734946a6 /generator/part.h
parent6218b88920120dea23d605856b9d27f9deb4746c (diff)
downloadverbly-03596c07406ea33f76a0fbb3e004df50b8b832e6.tar.gz
verbly-03596c07406ea33f76a0fbb3e004df50b8b832e6.tar.bz2
verbly-03596c07406ea33f76a0fbb3e004df50b8b832e6.zip
Converted verbly::generator::part to std::variant
fixes #5
Diffstat (limited to 'generator/part.h')
-rw-r--r--generator/part.h88
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
8namespace verbly { 9namespace 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