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.cpp | 350 ++++++++++++----------------------------------------------- 1 file changed, 71 insertions(+), 279 deletions(-) (limited to 'lib/part.cpp') diff --git a/lib/part.cpp b/lib/part.cpp index e7e467b..bd8501a 100644 --- a/lib/part.cpp +++ b/lib/part.cpp @@ -1,6 +1,5 @@ #include "part.h" #include -#include #include #include "database.h" @@ -26,15 +25,19 @@ namespace verbly { const part::selrestr_field part::selrestrs = {}; const part::synrestr_field part::synrestrs = {}; - part part::createNounPhrase(std::string role, std::set selrestrs, std::set synrestrs) + part part::createNounPhrase( + std::string role, + std::set selrestrs, + std::set synrestrs) { - part p(part_type::noun_phrase); - - new(&p.noun_phrase_.role) std::string(std::move(role)); - new(&p.noun_phrase_.selrestrs) std::set(std::move(selrestrs)); - new(&p.noun_phrase_.synrestrs) std::set(std::move(synrestrs)); - - return p; + return part { + part_type::noun_phrase, + np_type { + std::move(role), + std::move(selrestrs), + std::move(synrestrs) + } + }; } part part::createVerb() @@ -44,12 +47,13 @@ namespace verbly { part part::createPreposition(std::vector choices, bool literal) { - part p(part_type::preposition); - - new(&p.preposition_.choices) std::vector(std::move(choices)); - p.preposition_.literal = literal; - - return p; + return part { + part_type::preposition, + prep_type { + std::move(choices), + literal + } + }; } part part::createAdjective() @@ -64,83 +68,53 @@ namespace verbly { part part::createLiteral(std::string value) { - part p(part_type::literal); - - new(&p.literal_) std::string(std::move(value)); - - return p; + return part { + part_type::literal, + std::move(value) + }; } - part::part(const database& db, sqlite3_stmt* row) + part::part(const database& db, hatkirby::row row) { - int id = sqlite3_column_int(row, 0); + int id = mpark::get(row[0]); - type_ = static_cast(sqlite3_column_int(row, 3)); + type_ = static_cast(mpark::get(row[3])); switch (type_) { case part_type::noun_phrase: { - new(&noun_phrase_.role) std::string(reinterpret_cast(sqlite3_column_blob(row, 4))); - new(&noun_phrase_.selrestrs) std::set(db.selrestrs(id)); - new(&noun_phrase_.synrestrs) std::set(db.synrestrs(id)); + variant_ = np_type { + mpark::get(row[4]), + db.selrestrs(id), + db.synrestrs(id) + }; break; } case part_type::preposition: { - std::string serializedChoices(reinterpret_cast(sqlite3_column_blob(row, 5))); - new(&preposition_.choices) std::vector(hatkirby::split>(serializedChoices, ",")); - - preposition_.literal = (sqlite3_column_int(row, 6) == 1); - - break; - } - - case part_type::literal: - { - new(&literal_) std::string(reinterpret_cast(sqlite3_column_blob(row, 7))); + hatkirby::blob_type raw = + mpark::get(row[5]); - break; - } + std::string serializedChoices( + std::begin(raw), + std::end(raw)); - case part_type::verb: - case part_type::adjective: - case part_type::adverb: - case part_type::invalid: - { - break; - } - } - } - - part::part(const part& other) - { - type_ = other.type_; - - switch (type_) - { - case part_type::noun_phrase: - { - new(&noun_phrase_.role) std::string(other.noun_phrase_.role); - new(&noun_phrase_.selrestrs) std::set(other.noun_phrase_.selrestrs); - new(&noun_phrase_.synrestrs) std::set(other.noun_phrase_.synrestrs); - - break; - } - - case part_type::preposition: - { - new(&preposition_.choices) std::vector(other.preposition_.choices); - preposition_.literal = other.preposition_.literal; + variant_ = prep_type { + hatkirby::split>( + std::move(serializedChoices), + ","), + (mpark::get(row[6]) == 1) + }; break; } case part_type::literal: { - new(&literal_) std::string(other.literal_); + variant_ = mpark::get(row[7]); break; } @@ -155,256 +129,74 @@ namespace verbly { } } - part::part(part&& other) : part() + const std::string& part::getNounRole() const { - swap(*this, other); - } - - part& part::operator=(part other) - { - swap(*this, other); - - return *this; - } - - void swap(part& first, part& second) - { - using type = part_type; - - type tempType = first.type_; - std::string tempRole; - std::set tempSelrestrs; - std::set tempSynrestrs; - std::vector tempChoices; - bool tempPrepLiteral; - std::string tempLiteralValue; - - switch (tempType) - { - case type::noun_phrase: - { - tempRole = std::move(first.noun_phrase_.role); - tempSelrestrs = std::move(first.noun_phrase_.selrestrs); - tempSynrestrs = std::move(first.noun_phrase_.synrestrs); - - break; - } - - case type::preposition: - { - tempChoices = std::move(first.preposition_.choices); - tempPrepLiteral = first.preposition_.literal; - - break; - } - - case type::literal: - { - tempLiteralValue = std::move(first.literal_); - - break; - } - - case type::verb: - case type::adjective: - case type::adverb: - case type::invalid: - { - break; - } - } - - first.~part(); - - first.type_ = second.type_; - - switch (first.type_) + if (type_ != part_type::noun_phrase) { - case type::noun_phrase: - { - new(&first.noun_phrase_.role) std::string(std::move(second.noun_phrase_.role)); - new(&first.noun_phrase_.selrestrs) std::set(std::move(second.noun_phrase_.selrestrs)); - new(&first.noun_phrase_.synrestrs) std::set(std::move(second.noun_phrase_.synrestrs)); - - break; - } - - case type::preposition: - { - new(&first.preposition_.choices) std::vector(std::move(second.preposition_.choices)); - first.preposition_.literal = second.preposition_.literal; - - break; - } - - case type::literal: - { - new(&first.literal_) std::string(std::move(second.literal_)); - - break; - } - - case type::verb: - case type::adjective: - case type::adverb: - case type::invalid: - { - break; - } + throw std::domain_error("part is not a noun phrase"); } - second.~part(); - - second.type_ = tempType; - - switch (second.type_) - { - case type::noun_phrase: - { - new(&second.noun_phrase_.role) std::string(std::move(tempRole)); - new(&second.noun_phrase_.selrestrs) std::set(std::move(tempSelrestrs)); - new(&second.noun_phrase_.synrestrs) std::set(std::move(tempSynrestrs)); - - break; - } - - case type::preposition: - { - new(&second.preposition_.choices) std::vector(std::move(tempChoices)); - second.preposition_.literal = tempPrepLiteral; - - break; - } - - case type::literal: - { - new(&second.literal_) std::string(std::move(tempLiteralValue)); - - break; - } - - case type::verb: - case type::adjective: - case type::adverb: - case type::invalid: - { - break; - } - } + return mpark::get(variant_).role; } - part::~part() + const std::set& part::getNounSelrestrs() const { - switch (type_) + if (type_ != part_type::noun_phrase) { - case part_type::noun_phrase: - { - using string_type = std::string; - using set_type = std::set; - - noun_phrase_.role.~string_type(); - noun_phrase_.selrestrs.~set_type(); - noun_phrase_.synrestrs.~set_type(); - - break; - } - - case part_type::preposition: - { - using vector_type = std::vector; - - preposition_.choices.~vector_type(); - - break; - } - - case part_type::literal: - { - using string_type = std::string; - - literal_.~string_type(); - - break; - } - - case part_type::verb: - case part_type::adjective: - case part_type::adverb: - case part_type::invalid: - { - break; - } + throw std::domain_error("part is not a noun phrase"); } - } - std::string part::getNounRole() const - { - if (type_ == part_type::noun_phrase) - { - return noun_phrase_.role; - } else { - throw std::domain_error("part::getNounRole is only valid for noun phrase parts"); - } + return mpark::get(variant_).selrestrs; } - std::set part::getNounSelrestrs() const + const std::set& part::getNounSynrestrs() const { - if (type_ == part_type::noun_phrase) + if (type_ != part_type::noun_phrase) { - return noun_phrase_.selrestrs; - } else { - throw std::domain_error("part::getNounSelrestrs is only valid for noun phrase parts"); + throw std::domain_error("part is not a noun phrase"); } - } - std::set part::getNounSynrestrs() const - { - if (type_ == part_type::noun_phrase) - { - return noun_phrase_.synrestrs; - } else { - throw std::domain_error("part::getNounSynrestrs is only valid for noun phrase parts"); - } + return mpark::get(variant_).synrestrs; } bool part::nounHasSynrestr(std::string synrestr) const { if (type_ != part_type::noun_phrase) { - throw std::domain_error("part::nounHasSynrestr is only valid for noun phrase parts"); + throw std::domain_error("part is not a noun phrase"); } - return (noun_phrase_.synrestrs.count(synrestr) == 1); + return mpark::get(variant_).synrestrs.count(synrestr); } - std::vector part::getPrepositionChoices() const + const std::vector& part::getPrepositionChoices() const { - if (type_ == part_type::preposition) + if (type_ != part_type::preposition) { - return preposition_.choices; - } else { - throw std::domain_error("part::getPrepositionChoices is only valid for preposition parts"); + throw std::domain_error("part is not a preposition"); } + + return mpark::get(variant_).choices; } bool part::isPrepositionLiteral() const { - if (type_ == part_type::preposition) + if (type_ != part_type::preposition) { - return preposition_.literal; - } else { - throw std::domain_error("part::isPrepositionLiteral is only valid for preposition parts"); + throw std::domain_error("part is not a preposition"); } + + return mpark::get(variant_).literal; } - std::string part::getLiteralValue() const + const std::string& part::getLiteralValue() const { - if (type_ == part_type::literal) + if (type_ != part_type::literal) { - return literal_; - } else { - throw std::domain_error("part::getLiteralValue is only valid for literal parts"); + throw std::domain_error("part is not a literal"); } + + return mpark::get(variant_); } filter part::synrestr_field::operator%=(std::string synrestr) const -- cgit 1.4.1