From 03596c07406ea33f76a0fbb3e004df50b8b832e6 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 14 Dec 2022 10:33:34 -0500 Subject: Converted verbly::generator::part to std::variant fixes #5 --- generator/part.h | 88 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 32 deletions(-) (limited to 'generator/part.h') 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 @@ #include #include +#include #include "../lib/enums.h" namespace verbly { @@ -32,24 +33,6 @@ namespace verbly { static part duplicate(const part& other); - // Copy and move constructors - - part(const part& other); - - part(part&& other); - - // Assignment - - part& operator=(part other); - - // Swap - - friend void swap(part& first, part& second); - - // Destructor - - ~part(); - // General accessors int getId() const @@ -82,6 +65,26 @@ namespace verbly { private: + struct noun_phrase_type { + std::string role; + std::set selrestrs; + std::set synrestrs; + }; + + struct preposition_type { + std::set choices; + bool literal; + }; + + using literal_type = std::string; + + using variant_type = + std::variant< + std::monostate, + noun_phrase_type, + preposition_type, + literal_type>; + static int nextId_; int id_; @@ -92,26 +95,47 @@ namespace verbly { { } - part(type t) : + part(type t, variant_type variant = {}) : id_(nextId_++), - type_(t) + type_(t), + variant_(std::move(variant)) { + bool valid_type = false; + switch (type_) + { + case part_type::noun_phrase: + { + valid_type = std::holds_alternative(variant_); + break; + } + case part_type::preposition: + { + valid_type = std::holds_alternative(variant_); + break; + } + case part_type::literal: + { + valid_type = std::holds_alternative(variant_); + break; + } + case part_type::invalid: + case part_type::verb: + case part_type::adjective: + case part_type::adverb: + { + valid_type = std::holds_alternative(variant_); + break; + } + } + if (!valid_type) + { + throw std::invalid_argument("Invalid variant provided for part"); + } } // Data - union { - struct { - std::string role; - std::set selrestrs; - std::set synrestrs; - } noun_phrase_; - struct { - std::set choices; - bool literal; - } preposition_; - std::string literal_; - }; + variant_type variant_; type type_ = type::invalid; -- cgit 1.4.1