aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-08-04 13:32:25 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-08-04 13:32:25 +0200
commitb2fcd4dcdeb0f4962f8c8a1ce1a6fd64c2014062 (patch)
tree2cb2e003bc5865e0f36df9a3c1ddbc0991b1f154
parentf2955b7052e571eb4af0ad8194970f29e4b4ff9b (diff)
Add support for boost::optional and boost::shared_ptr as value wrappers
New test: boost/common/optional.
-rw-r--r--boost/README12
-rw-r--r--boost/driver.cxx35
-rw-r--r--boost/employee.hxx23
3 files changed, 57 insertions, 13 deletions
diff --git a/boost/README b/boost/README
index 021d601..5a95ac8 100644
--- a/boost/README
+++ b/boost/README
@@ -9,9 +9,11 @@ employee.hxx
We use shared_ptr/weak_ptr smart pointers provided by Boost (as well
as their lazy versions provided by the Boost profile library) to
establish a bidirectional employee-employer relationship. We also use
- the boost::gregorian::date type to store the employee's date of birth.
- Finally, we use the boost::unordered_set container to keep track of
- the employee's email addresses.
+ the boost::gregorian::date type to store the employee's date of birth
+ and the boost::unordered_set container to keep track of the employee's
+ email addresses Finally, we use boost::optional for the optional middle
+ name. If the middle name is not present, it will be represented in the
+ database as a NULL value.
employee-odb.hxx
employee-odb.ixx
@@ -45,8 +47,8 @@ driver.cxx
instance. It then creates a number of 'employee' and 'employer' objects and
persists them in the database. The next transaction loads all the employees
of a particular employer using the employee-employer relationship. Finally,
- the driver performs a database query which uses a data member of the Boost
- gregorian::date type in its criterion.
+ the driver performs a few database queries which use data members of the
+ various Boost value types in their criterion.
To run the example we may first need to create the database schema (for some
database systems, such as SQLite, the schema is embedded into the generated
diff --git a/boost/driver.cxx b/boost/driver.cxx
index 2ccaf87..bb1f134 100644
--- a/boost/driver.cxx
+++ b/boost/driver.cxx
@@ -39,7 +39,7 @@ main (int argc, char* argv[])
new employee ("John", "Doe", date (1975, Jan, 1), er));
shared_ptr<employee> jane (
- new employee ("Jane", "Doe", date (1976, Feb, 2), er));
+ new employee ("Jane", "Q", "Doe", date (1976, Feb, 2), er));
john->emails ().insert ("john_d@example.com");
john->emails ().insert ("john.doe@example.com");
@@ -67,7 +67,7 @@ main (int argc, char* argv[])
shared_ptr<employer> er (new employer ("Complex Systems Inc"));
shared_ptr<employee> john (
- new employee ("John", "Smith", date (1977, Mar, 3), er));
+ new employee ("John", "Z", "Smith", date (1977, Mar, 3), er));
shared_ptr<employee> jane (
new employee ("Jane", "Smith", date (1978, Apr, 4), er));
@@ -109,8 +109,14 @@ main (int argc, char* argv[])
lazy_weak_ptr<employee>& lwp (*i);
shared_ptr<employee> p (lwp.load ()); // Load and lock.
- cout << p->first () << " " << p->last () << endl
- << " born: " << p->born () << endl
+ cout << p->first () << " ";
+
+ if (p->middle ())
+ cout << *p->middle () << " ";
+
+ cout << p->last () << endl;
+
+ cout << " born: " << p->born () << endl
<< " employer: " << p->employer ()->name () << endl;
for (emails::const_iterator j (p->emails ().begin ());
@@ -125,13 +131,13 @@ main (int argc, char* argv[])
t.commit ();
}
+ typedef odb::query<employee> query;
+ typedef odb::result<employee> result;
+
// Search for Complex Systems Inc employees that were born before
// April 1978.
//
{
- typedef odb::query<employee> query;
- typedef odb::result<employee> result;
-
session s;
transaction t (db->begin ());
@@ -142,6 +148,21 @@ main (int argc, char* argv[])
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << i->first () << " " << i->last () << " " << i->born () << endl;
+ cout << endl;
+ t.commit ();
+ }
+
+ // Search for all the employees that don't have a middle name.
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+
+ result r (db->query<employee> (query::middle.is_null ()));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ cout << i->first () << " " << i->last () << endl;
+
t.commit ();
}
}
diff --git a/boost/employee.hxx b/boost/employee.hxx
index ffa017e..952f065 100644
--- a/boost/employee.hxx
+++ b/boost/employee.hxx
@@ -10,6 +10,7 @@
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
+#include <boost/optional.hpp>
#include <boost/unordered_set.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
@@ -86,7 +87,20 @@ public:
const std::string& last,
const date& born,
shared_ptr<employer_type> employer)
- : first_ (first), last_ (last), born_ (born), employer_ (employer)
+ : first_ (first), last_ (last),
+ born_ (born),
+ employer_ (employer)
+ {
+ }
+
+ employee (const std::string& first,
+ const std::string& middle,
+ const std::string& last,
+ const date& born,
+ shared_ptr<employer_type> employer)
+ : first_ (first), middle_ (middle), last_ (last),
+ born_ (born),
+ employer_ (employer)
{
}
@@ -98,6 +112,12 @@ public:
return first_;
}
+ const boost::optional<std::string>&
+ middle () const
+ {
+ return middle_;
+ }
+
const std::string&
last () const
{
@@ -151,6 +171,7 @@ private:
unsigned long id_;
std::string first_;
+ boost::optional<std::string> middle_;
std::string last_;
date born_;