diff options
Diffstat (limited to 'generator/form.cpp')
| -rw-r--r-- | generator/form.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
| diff --git a/generator/form.cpp b/generator/form.cpp new file mode 100644 index 0000000..6be9d47 --- /dev/null +++ b/generator/form.cpp | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #include "form.h" | ||
| 2 | #include <algorithm> | ||
| 3 | #include <list> | ||
| 4 | #include "database.h" | ||
| 5 | #include "field.h" | ||
| 6 | #include "pronunciation.h" | ||
| 7 | |||
| 8 | namespace verbly { | ||
| 9 | namespace generator { | ||
| 10 | |||
| 11 | int form::nextId_ = 0; | ||
| 12 | |||
| 13 | form::form(std::string text) : | ||
| 14 | id_(nextId_++), | ||
| 15 | text_(text), | ||
| 16 | complexity_(std::count(std::begin(text), std::end(text), ' ') + 1), | ||
| 17 | proper_(std::any_of(std::begin(text), std::end(text), std::isupper)) | ||
| 18 | { | ||
| 19 | } | ||
| 20 | |||
| 21 | void form::addPronunciation(const pronunciation& p) | ||
| 22 | { | ||
| 23 | pronunciations_.insert(&p); | ||
| 24 | } | ||
| 25 | |||
| 26 | database& operator<<(database& db, const form& arg) | ||
| 27 | { | ||
| 28 | // Serialize the form first. | ||
| 29 | { | ||
| 30 | std::list<field> fields; | ||
| 31 | fields.emplace_back("form_id", arg.getId()); | ||
| 32 | fields.emplace_back("form", arg.getText()); | ||
| 33 | fields.emplace_back("complexity", arg.getComplexity()); | ||
| 34 | fields.emplace_back("proper", arg.isProper()); | ||
| 35 | |||
| 36 | db.insertIntoTable("forms", std::move(fields)); | ||
| 37 | } | ||
| 38 | |||
| 39 | // Then, serialize the form/pronunciation relationship. | ||
| 40 | for (const pronunciation* p : arg.getPronunciations()) | ||
| 41 | { | ||
| 42 | std::list<field> fields; | ||
| 43 | fields.emplace_back("form_id", arg.getId()); | ||
| 44 | fields.emplace_back("pronunciation_id", p->getId()); | ||
| 45 | |||
| 46 | db.insertIntoTable("forms_pronunciations", std::move(fields)); | ||
| 47 | } | ||
| 48 | |||
| 49 | return db; | ||
| 50 | } | ||
| 51 | |||
| 52 | }; | ||
| 53 | }; | ||
