From ac3cd9c34d260c6bbf615ed6e0413d0989f5a6e2 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Thu, 21 Apr 2011 16:50:50 +0200 Subject: Rewrite Qt example using Qt style --- boost/driver.cxx | 4 ++-- qt/database.hxx | 2 +- qt/driver.cxx | 62 ++++++++++++++++++++++++++++---------------------------- qt/employee.hxx | 59 +++++++++++++++++++++++------------------------------ 4 files changed, 59 insertions(+), 68 deletions(-) diff --git a/boost/driver.cxx b/boost/driver.cxx index 99ce69a..2ccaf87 100644 --- a/boost/driver.cxx +++ b/boost/driver.cxx @@ -43,8 +43,8 @@ main (int argc, char* argv[]) john->emails ().insert ("john_d@example.com"); john->emails ().insert ("john.doe@example.com"); - jane->emails ().insert ("jane_s@example.com"); - jane->emails ().insert ("jane.smith@example.com"); + jane->emails ().insert ("jane_d@example.com"); + jane->emails ().insert ("jane.doe@example.com"); // Set the inverse side of the employee-employer relationship. // diff --git a/qt/database.hxx b/qt/database.hxx index 5b96ae9..f4060d3 100644 --- a/qt/database.hxx +++ b/qt/database.hxx @@ -25,7 +25,7 @@ #endif inline std::auto_ptr -create_database (int& argc, char* argv[]) +createDatabase (int& argc, char* argv[]) { using namespace std; using namespace odb::core; diff --git a/qt/driver.cxx b/qt/driver.cxx index 79d4b4a..8889b96 100644 --- a/qt/driver.cxx +++ b/qt/driver.cxx @@ -8,7 +8,7 @@ #include #include -#include "database.hxx" // create_database +#include "database.hxx" // createDatabase #include "employee.hxx" #include "employee-odb.hxx" @@ -16,7 +16,8 @@ using namespace std; using namespace odb::core; -ostream& operator << (ostream& os, const QString& s) +ostream& +operator<< (ostream& os, const QString& s) { return os << s.toStdString (); } @@ -26,7 +27,7 @@ main (int argc, char* argv[]) { try { - auto_ptr db (create_database (argc, argv)); + auto_ptr db (createDatabase (argc, argv)); // Create a few persistent objects. // @@ -34,26 +35,26 @@ main (int argc, char* argv[]) // Simple Tech Ltd. // { - QSharedPointer er (new employer ("Simple Tech Ltd")); + QSharedPointer er (new Employer ("Simple Tech Ltd")); - QSharedPointer john ( - new employee ("John", + QSharedPointer john ( + new Employee ("John", "Doe", QDate (1974, 5, 23), QByteArray ("\0xF1\0x00\0x34\0x45\0x00\0xDE", 6), er)); - QSharedPointer jane ( - new employee ("Jane", - "Smith", + QSharedPointer jane ( + new Employee ("Jane", + "Doe", QDate (1983, 1, 18), QByteArray ("\0xD7\0x00\0x14", 3), er)); john->emails ().insert ("john_d@example.com"); john->emails ().insert ("john.doe@simple.com"); - jane->emails ().insert ("jane_s@example.com"); - jane->emails ().insert ("jane.smith@simple.com"); + jane->emails ().insert ("jane_d@example.com"); + jane->emails ().insert ("jane.doe@simple.com"); // Set the inverse side of the employee-employer relationship. // @@ -73,24 +74,24 @@ main (int argc, char* argv[]) // Complex Systems Inc. // { - QSharedPointer er (new employer ("Complex Systems Inc")); + QSharedPointer er (new Employer ("Complex Systems Inc")); - QSharedPointer john ( - new employee ("John", + QSharedPointer john ( + new Employee ("John", "Smith", QDate (1954, 8, 1), QByteArray ("\0x23\0xFD\0x8F\0x00", 4), er)); - QSharedPointer jane ( - new employee ("Jane", + QSharedPointer jane ( + new Employee ("Jane", "Smith", QDate (1976, 12, 31), QByteArray ("0x00\0x32\0x00\0x01\0x00", 5), er)); - john->emails ().insert ("john_d@example.com"); - john->emails ().insert ("john.doe@complex.com"); + john->emails ().insert ("john_s@example.com"); + john->emails ().insert ("john.smith@complex.com"); jane->emails ().insert ("jane_s@example.com"); jane->emails ().insert ("jane.smith@complex.com"); @@ -117,31 +118,30 @@ main (int argc, char* argv[]) session s; transaction t (db->begin ()); - QSharedPointer stl (db->load ("Simple Tech Ltd")); + QSharedPointer stl (db->load ("Simple Tech Ltd")); - employees& es (stl->employees ()); + Employees& es (stl->employees ()); - for (employees::iterator i (es.begin ()); i != es.end (); ++i) + for (Employees::iterator i (es.begin ()); i != es.end (); ++i) { - QLazyWeakPointer& lwp (*i); + QLazyWeakPointer& lwp (*i); // Load and lock the employee and his employer. // - QSharedPointer p (lwp.load ()); - QSharedPointer pe (p->employer ().load ()); + QSharedPointer p (lwp.load ()); + QSharedPointer pe (p->employer ().load ()); cout << p->first () << " " << p->last () << endl << " born: " << p->born ().toString () << endl; - for (emails::const_iterator j (p->emails ().begin ()), + for (Emails::const_iterator j (p->emails ().begin ()), e (p->emails ().end ()); j != e; ++j) { cout << " email: " << *j << endl; } - cout << " public key length: " << p->public_key ().size () << endl - << " employer: " - << pe->name () << endl + cout << " public key length: " << p->publicKey ().size () << endl + << " employer: " << pe->name () << endl << endl; } @@ -151,13 +151,13 @@ main (int argc, char* argv[]) // Search for Complex Systems Inc employees. // { - typedef odb::query query; - typedef odb::result result; + typedef odb::query query; + typedef odb::result result; session s; transaction t (db->begin ()); - result r (db->query ( + result r (db->query ( query::employer::name == "Complex Systems Inc")); for (result::iterator i (r.begin ()); i != r.end (); ++i) diff --git a/qt/employee.hxx b/qt/employee.hxx index 37ad0c9..3117596 100644 --- a/qt/employee.hxx +++ b/qt/employee.hxx @@ -18,17 +18,17 @@ // Forward declarations. // -class employer; -class employee; +class Employee; -typedef QSet emails; -typedef QList > employees; +typedef QSet Emails; +typedef QList > Employees; #pragma db object -class employer +class Employer { public: - employer (const QString& name) + + Employer (const QString& name) : name_ (name) { } @@ -39,17 +39,13 @@ public: return name_; } - // Employees of this employer. - // - typedef ::employees employees_type; - - const employees_type& + const Employees& employees () const { return employees_; } - employees_type& + Employees& employees () { return employees_; @@ -58,30 +54,29 @@ public: private: friend class odb::access; - employer () {} + Employer () {} #pragma db id QString name_; #pragma db not_null inverse(employer_) - employees_type employees_; + Employees employees_; }; #pragma db object -class employee +class Employee { public: - typedef ::employer employer_type; - employee (const QString& first, + Employee (const QString& first, const QString& last, const QDate& born, - const QByteArray& public_key, - QSharedPointer employer) + const QByteArray& publicKey, + QSharedPointer employer) : first_ (first), last_ (last), born_ (born), - public_key_ (public_key), + publicKey_ (publicKey), employer_ (employer) { } @@ -108,17 +103,13 @@ public: return born_; } - // Emails. - // - typedef ::emails emails_type; - - const emails_type& + const Emails& emails () const { return emails_; } - emails_type& + Emails& emails () { return emails_; @@ -127,21 +118,21 @@ public: // Public key. // const QByteArray& - public_key () const + publicKey () const { - return public_key_; + return publicKey_; } // Employer. // - QLazySharedPointer + QLazySharedPointer employer () const { return employer_; } void - employer (QSharedPointer employer) + employer (QSharedPointer employer) { employer_ = employer; } @@ -149,7 +140,7 @@ public: private: friend class odb::access; - employee () {} + Employee () {} #pragma db id auto unsigned long id_; @@ -157,11 +148,11 @@ private: QString first_; QString last_; QDate born_; - emails_type emails_; - QByteArray public_key_; + Emails emails_; + QByteArray publicKey_; #pragma db not_null - QLazySharedPointer employer_; + QLazySharedPointer employer_; }; #endif // EMPLOYEE_HXX -- cgit v1.1