From e02e3d57dc090c8fd333812b84e91805921e398c Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 21 Jan 2017 18:51:41 -0500 Subject: Moved some generator classes into the main namespace --- generator/CMakeLists.txt | 2 +- generator/generator.cpp | 13 +- generator/generator.h | 7 +- generator/group.cpp | 5 +- generator/group.h | 4 +- generator/lemma.h | 2 +- generator/notion.h | 2 +- generator/part.cpp | 1 - generator/part.h | 2 +- generator/role.h | 35 ------ generator/selrestr.cpp | 288 ------------------------------------------- generator/selrestr.h | 88 -------------- generator/word.h | 2 +- lib/role.h | 60 +++++++++ lib/selrestr.cpp | 309 +++++++++++++++++++++++++++++++++++++++++++++++ lib/selrestr.h | 90 ++++++++++++++ 16 files changed, 479 insertions(+), 431 deletions(-) delete mode 100644 generator/role.h delete mode 100644 generator/selrestr.cpp delete mode 100644 generator/selrestr.h create mode 100644 lib/role.h create mode 100644 lib/selrestr.cpp create mode 100644 lib/selrestr.h diff --git a/generator/CMakeLists.txt b/generator/CMakeLists.txt index 4f78eb8..5bbd82d 100644 --- a/generator/CMakeLists.txt +++ b/generator/CMakeLists.txt @@ -6,7 +6,7 @@ pkg_check_modules(sqlite3 sqlite3 REQUIRED) find_package(libxml2 REQUIRED) include_directories(${sqlite3_INCLUDE_DIR} ${LIBXML2_INCLUDE_DIR} ../vendor/json) -add_executable(generator notion.cpp word.cpp lemma.cpp form.cpp pronunciation.cpp group.cpp frame.cpp part.cpp selrestr.cpp database.cpp field.cpp generator.cpp main.cpp) +add_executable(generator notion.cpp word.cpp lemma.cpp form.cpp pronunciation.cpp group.cpp frame.cpp part.cpp ../lib/selrestr.cpp database.cpp field.cpp generator.cpp main.cpp) set_property(TARGET generator PROPERTY CXX_STANDARD 11) set_property(TARGET generator PROPERTY CXX_STANDARD_REQUIRED ON) target_link_libraries(generator ${sqlite3_LIBRARIES} ${LIBXML2_LIBRARIES}) diff --git a/generator/generator.cpp b/generator/generator.cpp index d88cb31..16183a2 100644 --- a/generator/generator.cpp +++ b/generator/generator.cpp @@ -5,10 +5,10 @@ #include #include #include -#include "enums.h" +#include "../lib/enums.h" #include "progress.h" -#include "selrestr.h" -#include "role.h" +#include "../lib/selrestr.h" +#include "../lib/role.h" #include "part.h" #include "field.h" #include "../lib/util.h" @@ -1290,21 +1290,20 @@ namespace verbly { { if (!xmlStrcmp(roletopnode->name, reinterpret_cast("THEMROLE"))) { - role r; - key = xmlGetProp(roletopnode, reinterpret_cast("type")); std::string roleName = reinterpret_cast(key); xmlFree(key); + selrestr roleSelrestrs; for (xmlNodePtr rolenode = roletopnode->xmlChildrenNode; rolenode != nullptr; rolenode = rolenode->next) { if (!xmlStrcmp(rolenode->name, reinterpret_cast("SELRESTRS"))) { - r.setSelrestrs(parseSelrestr(rolenode)); + roleSelrestrs = parseSelrestr(rolenode); } } - grp.addRole(roleName, std::move(r)); + grp.addRole({roleName, std::move(roleSelrestrs)}); } } } else if (!xmlStrcmp(node->name, reinterpret_cast("FRAMES"))) diff --git a/generator/generator.h b/generator/generator.h index e2a7404..c829c21 100644 --- a/generator/generator.h +++ b/generator/generator.h @@ -16,10 +16,11 @@ #include "frame.h" namespace verbly { + + enum class part_of_speech; + class selrestr; + namespace generator { - - enum class part_of_speech; - class selrestr; class generator { public: diff --git a/generator/group.cpp b/generator/group.cpp index 7cbd4c8..334c2aa 100644 --- a/generator/group.cpp +++ b/generator/group.cpp @@ -23,10 +23,11 @@ namespace verbly { parent_ = &parent; } - void group::addRole(std::string name, role r) + void group::addRole(role r) { - roleNames_.insert(name); + std::string name = r.getName(); roles_[name] = std::move(r); + roleNames_.insert(std::move(name)); } void group::addFrame(const frame& f) diff --git a/generator/group.h b/generator/group.h index efb8c5d..5084ea4 100644 --- a/generator/group.h +++ b/generator/group.h @@ -5,7 +5,7 @@ #include #include #include -#include "role.h" +#include "../lib/role.h" namespace verbly { namespace generator { @@ -24,7 +24,7 @@ namespace verbly { void setParent(const group& parent); - void addRole(std::string name, role r); + void addRole(role r); void addFrame(const frame& f); diff --git a/generator/lemma.h b/generator/lemma.h index 6452e08..f68667f 100644 --- a/generator/lemma.h +++ b/generator/lemma.h @@ -4,7 +4,7 @@ #include #include #include -#include "enums.h" +#include "../lib/enums.h" namespace verbly { namespace generator { diff --git a/generator/notion.h b/generator/notion.h index 76210de..cc64c48 100644 --- a/generator/notion.h +++ b/generator/notion.h @@ -4,7 +4,7 @@ #include #include #include -#include "enums.h" +#include "../lib/enums.h" namespace verbly { namespace generator { diff --git a/generator/part.cpp b/generator/part.cpp index dbd4e11..b69ec65 100644 --- a/generator/part.cpp +++ b/generator/part.cpp @@ -1,6 +1,5 @@ #include "part.h" #include -#include "selrestr.h" namespace verbly { namespace generator { diff --git a/generator/part.h b/generator/part.h index d044630..86d5d57 100644 --- a/generator/part.h +++ b/generator/part.h @@ -3,7 +3,7 @@ #include #include -#include "selrestr.h" +#include "../lib/selrestr.h" namespace verbly { namespace generator { diff --git a/generator/role.h b/generator/role.h deleted file mode 100644 index 5fa68b8..0000000 --- a/generator/role.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef ROLE_H_249F9A9C -#define ROLE_H_249F9A9C - -#include "selrestr.h" - -namespace verbly { - namespace generator { - - class role { - public: - - // Mutators - - void setSelrestrs(selrestr selrestrs) - { - selrestrs_ = selrestrs; - } - - // Accessors - - const selrestr& getSelrestrs() const - { - return selrestrs_; - } - - private: - - selrestr selrestrs_; - - }; - - }; -}; - -#endif /* end of include guard: ROLE_H_249F9A9C */ diff --git a/generator/selrestr.cpp b/generator/selrestr.cpp deleted file mode 100644 index 8bdd3f6..0000000 --- a/generator/selrestr.cpp +++ /dev/null @@ -1,288 +0,0 @@ -#include "selrestr.h" - -namespace verbly { - namespace generator { - - selrestr::selrestr(const selrestr& other) - { - type_ = other.type_; - - switch (type_) - { - case type::singleton: - { - singleton_.pos = other.singleton_.pos; - new(&singleton_.restriction) std::string(other.singleton_.restriction); - - break; - } - - case type::group: - { - new(&group_.children) std::list(other.group_.children); - group_.orlogic = other.group_.orlogic; - - break; - } - - case type::empty: - { - break; - } - } - } - - selrestr::selrestr(selrestr&& other) : selrestr() - { - swap(*this, other); - } - - selrestr& selrestr::operator=(selrestr other) - { - swap(*this, other); - - return *this; - } - - void swap(selrestr& first, selrestr& second) - { - using type = selrestr::type; - - type tempType = first.type_; - int tempPos; - std::string tempRestriction; - std::list tempChildren; - bool tempOrlogic; - - switch (tempType) - { - case type::singleton: - { - tempPos = first.singleton_.pos; - tempRestriction = std::move(first.singleton_.restriction); - - break; - } - - case type::group: - { - tempChildren = std::move(first.group_.children); - tempOrlogic = first.group_.orlogic; - - break; - } - - case type::empty: - { - break; - } - } - - first.~selrestr(); - - first.type_ = second.type_; - - switch (first.type_) - { - case type::singleton: - { - first.singleton_.pos = second.singleton_.pos; - new(&first.singleton_.restriction) std::string(std::move(second.singleton_.restriction)); - - break; - } - - case type::group: - { - new(&first.group_.children) std::list(std::move(second.group_.children)); - first.group_.orlogic = second.group_.orlogic; - - break; - } - - case type::empty: - { - break; - } - } - - second.~selrestr(); - - second.type_ = tempType; - - switch (second.type_) - { - case type::singleton: - { - second.singleton_.pos = tempPos; - new(&second.singleton_.restriction) std::string(std::move(tempRestriction)); - - break; - } - - case type::group: - { - new(&second.group_.children) std::list(std::move(tempChildren)); - second.group_.orlogic = tempOrlogic; - - break; - } - - case type::empty: - { - break; - } - } - } - - selrestr::~selrestr() - { - switch (type_) - { - case type::singleton: - { - using string_type = std::string; - singleton_.restriction.~string_type(); - - break; - } - - case type::group: - { - using list_type = std::list; - group_.children.~list_type(); - - break; - } - - case type::empty: - { - break; - } - } - } - - selrestr::selrestr() : type_(type::empty) - { - } - - selrestr::selrestr( - std::string restriction, - bool pos) : - type_(type::singleton) - { - new(&singleton_.restriction) std::string(std::move(restriction)); - singleton_.pos = pos; - } - - std::string selrestr::getRestriction() const - { - if (type_ == type::singleton) - { - return singleton_.restriction; - } else { - throw std::domain_error("Only singleton selrestrs have restrictions"); - } - } - - bool selrestr::getPos() const - { - if (type_ == type::singleton) - { - return singleton_.pos; - } else { - throw std::domain_error("Only singleton selrestrs have positivity flags"); - } - } - - selrestr::selrestr( - std::list children, - bool orlogic) : - type_(type::group) - { - new(&group_.children) std::list(std::move(children)); - group_.orlogic = orlogic; - } - - std::list selrestr::getChildren() const - { - if (type_ == type::group) - { - return group_.children; - } else { - throw std::domain_error("Only group selrestrs have children"); - } - } - - std::list::const_iterator selrestr::begin() const - { - if (type_ == type::group) - { - return std::begin(group_.children); - } else { - throw std::domain_error("Only group selrestrs have children"); - } - } - - std::list::const_iterator selrestr::end() const - { - if (type_ == type::group) - { - return std::end(group_.children); - } else { - throw std::domain_error("Only group selrestrs have children"); - } - } - - bool selrestr::getOrlogic() const - { - if (type_ == type::group) - { - return group_.orlogic; - } else { - throw std::domain_error("Only group selrestrs have logic"); - } - } - - nlohmann::json selrestr::toJson() const - { - switch (type_) - { - case type::empty: - { - return {}; - } - - case type::singleton: - { - return { - {"type", singleton_.restriction}, - {"pos", singleton_.pos} - }; - } - - case type::group: - { - std::string logic; - if (group_.orlogic) - { - logic = "or"; - } else { - logic = "and"; - } - - std::list children; - std::transform(std::begin(group_.children), std::end(group_.children), std::back_inserter(children), [] (const selrestr& child) { - return child.toJson(); - }); - - return { - {"logic", logic}, - {"children", children} - }; - } - } - } - - }; -}; diff --git a/generator/selrestr.h b/generator/selrestr.h deleted file mode 100644 index 5000970..0000000 --- a/generator/selrestr.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef SELRESTR_H_50652FB7 -#define SELRESTR_H_50652FB7 - -#include -#include -#include - -namespace verbly { - namespace generator { - - class selrestr { - public: - enum class type { - empty, - singleton, - group - }; - - // Copy and move constructors - - selrestr(const selrestr& other); - selrestr(selrestr&& other); - - // Assignment - - selrestr& operator=(selrestr other); - - // Swap - - friend void swap(selrestr& first, selrestr& second); - - // Destructor - - ~selrestr(); - - // Generic accessors - - type getType() const - { - return type_; - } - - // Empty - - selrestr(); - - // Singleton - - selrestr(std::string restriction, bool pos); - - std::string getRestriction() const; - - bool getPos() const; - - // Group - - selrestr(std::list children, bool orlogic); - - std::list getChildren() const; - - std::list::const_iterator begin() const; - - std::list::const_iterator end() const; - - bool getOrlogic() const; - - // Helpers - - nlohmann::json toJson() const; - - private: - union { - struct { - bool pos; - std::string restriction; - } singleton_; - struct { - std::list children; - bool orlogic; - } group_; - }; - type type_; - }; - - }; -}; - -#endif /* end of include guard: SELRESTR_H_50652FB7 */ diff --git a/generator/word.h b/generator/word.h index bfed586..1d77ed3 100644 --- a/generator/word.h +++ b/generator/word.h @@ -2,7 +2,7 @@ #define WORD_H_91F99D46 #include -#include "enums.h" +#include "../lib/enums.h" namespace verbly { namespace generator { diff --git a/lib/role.h b/lib/role.h new file mode 100644 index 0000000..8653710 --- /dev/null +++ b/lib/role.h @@ -0,0 +1,60 @@ +#ifndef ROLE_H_249F9A9C +#define ROLE_H_249F9A9C + +#include +#include +#include "../lib/selrestr.h" + +namespace verbly { + + class role { + public: + + // Default constructor + + role() = default; + + // Constructor + + role( + std::string name, + selrestr selrestrs = {}) : + valid_(true), + name_(name), + selrestrs_(selrestrs) + { + } + + // Accessors + + const std::string& getName() const + { + if (!valid_) + { + throw std::domain_error("Bad access to invalid role"); + } + + return name_; + } + + const selrestr& getSelrestrs() const + { + if (!valid_) + { + throw std::domain_error("Bad access to invalid role"); + } + + return selrestrs_; + } + + private: + + bool valid_ = false; + std::string name_; + selrestr selrestrs_; + + }; + +}; + +#endif /* end of include guard: ROLE_H_249F9A9C */ diff --git a/lib/selrestr.cpp b/lib/selrestr.cpp new file mode 100644 index 0000000..74ea726 --- /dev/null +++ b/lib/selrestr.cpp @@ -0,0 +1,309 @@ +#include "selrestr.h" + +namespace verbly { + + selrestr::selrestr(nlohmann::json data) + { + if (data.find("children") != data.end()) + { + type_ = type::group; + new(&group_.children) std::list(); + + for (const nlohmann::json& child : data["children"]) + { + group_.children.emplace_back(child); + } + + group_.orlogic = (data["logic"] == "or"); + } else if (data.find("type") != data.end()) + { + type_ = type::singleton; + singleton_.pos = data["pos"].get(); + new(&singleton_.restriction) std::string(data["type"].get()); + } else { + type_ = type::empty; + } + } + + selrestr::selrestr(const selrestr& other) + { + type_ = other.type_; + + switch (type_) + { + case type::singleton: + { + singleton_.pos = other.singleton_.pos; + new(&singleton_.restriction) std::string(other.singleton_.restriction); + + break; + } + + case type::group: + { + new(&group_.children) std::list(other.group_.children); + group_.orlogic = other.group_.orlogic; + + break; + } + + case type::empty: + { + break; + } + } + } + + selrestr::selrestr(selrestr&& other) : selrestr() + { + swap(*this, other); + } + + selrestr& selrestr::operator=(selrestr other) + { + swap(*this, other); + + return *this; + } + + void swap(selrestr& first, selrestr& second) + { + using type = selrestr::type; + + type tempType = first.type_; + int tempPos; + std::string tempRestriction; + std::list tempChildren; + bool tempOrlogic; + + switch (tempType) + { + case type::singleton: + { + tempPos = first.singleton_.pos; + tempRestriction = std::move(first.singleton_.restriction); + + break; + } + + case type::group: + { + tempChildren = std::move(first.group_.children); + tempOrlogic = first.group_.orlogic; + + break; + } + + case type::empty: + { + break; + } + } + + first.~selrestr(); + + first.type_ = second.type_; + + switch (first.type_) + { + case type::singleton: + { + first.singleton_.pos = second.singleton_.pos; + new(&first.singleton_.restriction) std::string(std::move(second.singleton_.restriction)); + + break; + } + + case type::group: + { + new(&first.group_.children) std::list(std::move(second.group_.children)); + first.group_.orlogic = second.group_.orlogic; + + break; + } + + case type::empty: + { + break; + } + } + + second.~selrestr(); + + second.type_ = tempType; + + switch (second.type_) + { + case type::singleton: + { + second.singleton_.pos = tempPos; + new(&second.singleton_.restriction) std::string(std::move(tempRestriction)); + + break; + } + + case type::group: + { + new(&second.group_.children) std::list(std::move(tempChildren)); + second.group_.orlogic = tempOrlogic; + + break; + } + + case type::empty: + { + break; + } + } + } + + selrestr::~selrestr() + { + switch (type_) + { + case type::singleton: + { + using string_type = std::string; + singleton_.restriction.~string_type(); + + break; + } + + case type::group: + { + using list_type = std::list; + group_.children.~list_type(); + + break; + } + + case type::empty: + { + break; + } + } + } + + selrestr::selrestr() : type_(type::empty) + { + } + + selrestr::selrestr( + std::string restriction, + bool pos) : + type_(type::singleton) + { + new(&singleton_.restriction) std::string(std::move(restriction)); + singleton_.pos = pos; + } + + std::string selrestr::getRestriction() const + { + if (type_ == type::singleton) + { + return singleton_.restriction; + } else { + throw std::domain_error("Only singleton selrestrs have restrictions"); + } + } + + bool selrestr::getPos() const + { + if (type_ == type::singleton) + { + return singleton_.pos; + } else { + throw std::domain_error("Only singleton selrestrs have positivity flags"); + } + } + + selrestr::selrestr( + std::list children, + bool orlogic) : + type_(type::group) + { + new(&group_.children) std::list(std::move(children)); + group_.orlogic = orlogic; + } + + std::list selrestr::getChildren() const + { + if (type_ == type::group) + { + return group_.children; + } else { + throw std::domain_error("Only group selrestrs have children"); + } + } + + std::list::const_iterator selrestr::begin() const + { + if (type_ == type::group) + { + return std::begin(group_.children); + } else { + throw std::domain_error("Only group selrestrs have children"); + } + } + + std::list::const_iterator selrestr::end() const + { + if (type_ == type::group) + { + return std::end(group_.children); + } else { + throw std::domain_error("Only group selrestrs have children"); + } + } + + bool selrestr::getOrlogic() const + { + if (type_ == type::group) + { + return group_.orlogic; + } else { + throw std::domain_error("Only group selrestrs have logic"); + } + } + + nlohmann::json selrestr::toJson() const + { + switch (type_) + { + case type::empty: + { + return {}; + } + + case type::singleton: + { + return { + {"type", singleton_.restriction}, + {"pos", singleton_.pos} + }; + } + + case type::group: + { + std::string logic; + if (group_.orlogic) + { + logic = "or"; + } else { + logic = "and"; + } + + std::list children; + std::transform(std::begin(group_.children), std::end(group_.children), std::back_inserter(children), [] (const selrestr& child) { + return child.toJson(); + }); + + return { + {"logic", logic}, + {"children", children} + }; + } + } + } + +}; diff --git a/lib/selrestr.h b/lib/selrestr.h new file mode 100644 index 0000000..9f82e3e --- /dev/null +++ b/lib/selrestr.h @@ -0,0 +1,90 @@ +#ifndef SELRESTR_H_50652FB7 +#define SELRESTR_H_50652FB7 + +#include +#include +#include "../vendor/json/json.hpp" + +namespace verbly { + + class selrestr { + public: + enum class type { + empty, + singleton, + group + }; + + // Construct from json + + explicit selrestr(nlohmann::json json); + + // Copy and move constructors + + selrestr(const selrestr& other); + selrestr(selrestr&& other); + + // Assignment + + selrestr& operator=(selrestr other); + + // Swap + + friend void swap(selrestr& first, selrestr& second); + + // Destructor + + ~selrestr(); + + // Generic accessors + + type getType() const + { + return type_; + } + + // Empty + + selrestr(); + + // Singleton + + selrestr(std::string restriction, bool pos); + + std::string getRestriction() const; + + bool getPos() const; + + // Group + + selrestr(std::list children, bool orlogic); + + std::list getChildren() const; + + std::list::const_iterator begin() const; + + std::list::const_iterator end() const; + + bool getOrlogic() const; + + // Helpers + + nlohmann::json toJson() const; + + private: + union { + struct { + bool pos; + std::string restriction; + } singleton_; + struct { + std::list children; + bool orlogic; + } group_; + }; + type type_; + }; + +}; + +#endif /* end of include guard: SELRESTR_H_50652FB7 */ -- cgit 1.4.1