From 38c17f093615a16a4b4ec6dc2b5d3edb5c1d3895 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 27 Sep 2018 21:40:52 -0400 Subject: More hkutil refactoring All database access goes through hatkirby::database now. verbly::token, verbly::statement::condition, and verbly::part have been converted to use mpark::variant now. verbly::binding has been deleted, and replaced with a mpark::variant typedef in statement.h. This means that the only remaining tagged union class is verbly::generator::part. refs #5 --- lib/part.h | 97 ++++++++++++++++++++++++++------------------------------------ 1 file changed, 40 insertions(+), 57 deletions(-) (limited to 'lib/part.h') diff --git a/lib/part.h b/lib/part.h index 456bad0..7783a61 100644 --- a/lib/part.h +++ b/lib/part.h @@ -5,12 +5,12 @@ #include #include #include +#include +#include #include "field.h" #include "filter.h" #include "enums.h" -struct sqlite3_stmt; - namespace verbly { class database; @@ -20,11 +20,16 @@ namespace verbly { // Static factories - static part createNounPhrase(std::string role, std::set selrestrs, std::set synrestrs); + static part createNounPhrase( + std::string role, + std::set selrestrs, + std::set synrestrs); static part createVerb(); - static part createPreposition(std::vector choices, bool literal); + static part createPreposition( + std::vector choices, + bool literal); static part createAdjective(); @@ -32,41 +37,12 @@ namespace verbly { static part createLiteral(std::string value); - // Default constructor - - part() - { - } - // Construct from database - part(const database& db, sqlite3_stmt* row); - - // 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(); + part(const database& db, hatkirby::row row); // General accessors - bool isValid() const - { - return (type_ != part_type::invalid); - } - part_type getType() const { return type_; @@ -74,23 +50,23 @@ namespace verbly { // Noun phrase accessors - std::string getNounRole() const; + const std::string& getNounRole() const; - std::set getNounSelrestrs() const; + const std::set& getNounSelrestrs() const; - std::set getNounSynrestrs() const; + const std::set& getNounSynrestrs() const; bool nounHasSynrestr(std::string synrestr) const; // Preposition accessors - std::vector getPrepositionChoices() const; + const std::vector& getPrepositionChoices() const; bool isPrepositionLiteral() const; // Literal accessors - std::string getLiteralValue() const; + const std::string& getLiteralValue() const; // Type info @@ -123,7 +99,7 @@ namespace verbly { }; static const selrestr_field selrestrs; - + class synrestr_field { public: @@ -139,29 +115,36 @@ namespace verbly { private: - // Private constructors - - part(part_type t) : type_(t) - { - } - // Data - union { - struct { - std::string role; - std::set selrestrs; - std::set synrestrs; - } noun_phrase_; - struct { - std::vector choices; - bool literal; - } preposition_; - std::string literal_; + struct np_type { + std::string role; + std::set selrestrs; + std::set synrestrs; + }; + + struct prep_type { + std::vector choices; + bool literal; }; + using variant_type = + mpark::variant< + mpark::monostate, + np_type, + prep_type, + std::string>; + + variant_type variant_; + part_type type_ = part_type::invalid; + // Private constructors + + part(part_type t, variant_type v = {}) : type_(t), variant_(v) + { + } + }; }; -- cgit 1.4.1