From 24f87489a4d315cc01ca9d49a3f0209522fe6729 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 8 Jan 2012 17:27:40 +0200 Subject: Add support for defining composite value type as class template instantiations --- composite/README | 6 ++++-- composite/driver.cxx | 7 +++++-- composite/person.hxx | 20 ++++++++++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) (limited to 'composite') diff --git a/composite/README b/composite/README index bf9e745..e33e7a9 100644 --- a/composite/README +++ b/composite/README @@ -7,8 +7,10 @@ The example consists of the following files: person.hxx Header file defining the 'basic_name', 'name_extras', and 'name' composite - value types. It also defines the 'person' persistent class which use the - 'name' value type in one of its data members. + value types. It also defines the 'phone_numbers' composite value type as + an instantiation of the 'std::pair' class template. Finally it defines + the 'person' persistent class which uses the 'name' and 'phone_numbers' + value types in its data members. person-odb.hxx person-odb.ixx diff --git a/composite/driver.cxx b/composite/driver.cxx index 56be15d..346e768 100644 --- a/composite/driver.cxx +++ b/composite/driver.cxx @@ -27,7 +27,7 @@ main (int argc, char* argv[]) // unsigned int id; { - person p ("Joe", "Dirt", "Mr"); + person p ("Joe", "Dirt", "Mr", phone_numbers ("555 5555", "666 6666")); transaction t (db->begin ()); id = db->persist (p); @@ -49,7 +49,7 @@ main (int argc, char* argv[]) t.commit (); } - // Print the name information. + // Print the name and phone numbers. // { transaction t (db->begin ()); @@ -71,6 +71,9 @@ main (int argc, char* argv[]) { cout << " alias: " << i->first () << " " << i->last () << endl; } + + cout << " phone 1: " << joe->phone ().first << endl; + cout << " phone 2: " << joe->phone ().second << endl; } // Query the database for a person object. diff --git a/composite/person.hxx b/composite/person.hxx index dfc44e6..92da60c 100644 --- a/composite/person.hxx +++ b/composite/person.hxx @@ -123,6 +123,12 @@ private: name_extras extras_; }; +// We can also define a composite value type as a class template +// instantiation. Here we use std::pair to store person's phone +// numbers, in the order of preference. +// +typedef std::pair phone_numbers; +#pragma db value(phone_numbers) #pragma db object class person @@ -130,8 +136,9 @@ class person public: person (const std::string& first, const std::string& last, - const std::string& title) - : name_ (first, last, title) + const std::string& title, + const phone_numbers& phone) + : name_ (first, last, title), phone_ (phone) { } @@ -151,6 +158,14 @@ public: return name_; } + // Phone. + // + const phone_numbers& + phone () const + { + return phone_; + } + private: friend class odb::access; @@ -160,6 +175,7 @@ private: unsigned long id_; name_type name_; + phone_numbers phone_; }; #endif // PERSON_HXX -- cgit v1.1