diff options
-rw-r--r-- | README | 8 | ||||
-rw-r--r-- | access/README | 55 | ||||
-rw-r--r-- | access/database.hxx | 92 | ||||
-rw-r--r-- | access/driver.cxx | 56 | ||||
-rw-r--r-- | access/makefile | 118 | ||||
-rw-r--r-- | access/person.hxx | 125 | ||||
-rw-r--r-- | makefile | 2 | ||||
-rw-r--r-- | pimpl/README | 53 | ||||
-rw-r--r-- | pimpl/database.hxx | 92 | ||||
-rw-r--r-- | pimpl/driver.cxx | 52 | ||||
-rw-r--r-- | pimpl/makefile | 118 | ||||
-rw-r--r-- | pimpl/person.cxx | 71 | ||||
-rw-r--r-- | pimpl/person.hxx | 56 |
13 files changed, 898 insertions, 0 deletions
@@ -50,9 +50,17 @@ view optimistic Shows how to use optimistic concurrency in ODB. +pimpl + Shows how to use virtual data members to implement a persistent class that + employes the pimpl C++ idiom. + c++11 Shows how to use ODB with C++11. +access + Shows various approaches used by ODB to access data members that cannot be + accessed directly. + boost Shows how to persist objects that use Boost smart pointers, containers, and value types with the help of the Boost profile library (libodb-boost). diff --git a/access/README b/access/README new file mode 100644 index 0000000..ebf27db --- /dev/null +++ b/access/README @@ -0,0 +1,55 @@ +This example shows various approaches used by ODB to access data members +that cannot be accessed directly. Approaches that are illustrated by this +example include the automatic discovery of suitable accessor/modifier +functions, explicit specification of the accessor/modifier functions, +explicit specification of more complex accessor/modifier expressions, and +use of virtual data members. + +The example consists of the following files: + +person.hxx + Header file implementing the 'person' persistent class. + +person-odb.hxx +person-odb.ixx +person-odb.cxx +person.sql + The first three files contain the database support code and the last file + contains the database schema for the person.hxx header. + + These files are generated by the ODB compiler from person.hxx using the + following command line: + + odb -d <database> --generate-query --generate-schema person.hxx + + Where <database> stands for the database system we are using, for example, + 'mysql'. + +database.hxx + Contains the create_database() function which instantiates the concrete + database class corresponding to the database system we are using. + +driver.cxx + Driver for the example. It includes the person.hxx and person-odb.hxx + headers to gain access to the persistent class and its database support + code. It also includes database.hxx for the create_database() function + declaration. + + In main() the driver first calls create_database() to obtain the database + instance. Then it executes a number of database transactions on the 'person' + objects. + +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 +code which makes this step unnecessary). Using MySQL as an example, this +can be achieved with the following command: + +mysql --user=odb_test --database=odb_test < person.sql + +Here we use 'odb_test' as the database login and also 'odb_test' as the +database name. + +Once the database schema is ready, we can run the example (using MySQL as +the database): + +./driver --user odb_test --database odb_test diff --git a/access/database.hxx b/access/database.hxx new file mode 100644 index 0000000..0be5482 --- /dev/null +++ b/access/database.hxx @@ -0,0 +1,92 @@ +// file : access/database.hxx +// copyright : not copyrighted - public domain + +// +// Create concrete database instance based on the DATABASE_* macros. +// + +#ifndef DATABASE_HXX +#define DATABASE_HXX + +#include <string> +#include <memory> // std::auto_ptr +#include <cstdlib> // std::exit +#include <iostream> + +#include <odb/database.hxx> + +#if defined(DATABASE_MYSQL) +# include <odb/mysql/database.hxx> +#elif defined(DATABASE_SQLITE) +# include <odb/connection.hxx> +# include <odb/transaction.hxx> +# include <odb/schema-catalog.hxx> +# include <odb/sqlite/database.hxx> +#elif defined(DATABASE_PGSQL) +# include <odb/pgsql/database.hxx> +#elif defined(DATABASE_ORACLE) +# include <odb/oracle/database.hxx> +#elif defined(DATABASE_MSSQL) +# include <odb/mssql/database.hxx> +#endif + +inline std::auto_ptr<odb::database> +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cout << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cout); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cout); +#elif defined(DATABASE_PGSQL) + odb::pgsql::database::print_usage (cout); +#elif defined(DATABASE_ORACLE) + odb::oracle::database::print_usage (cout); +#elif defined(DATABASE_MSSQL) + odb::mssql::database::print_usage (cout); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + auto_ptr<database> db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr<database> db ( + new odb::sqlite::database ( + argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + + // Create the database schema. Due to bugs in SQLite foreign key + // support for DDL statements, we need to temporarily disable + // foreign keys. + // + { + connection_ptr c (db->connection ()); + + c->execute ("PRAGMA foreign_keys=OFF"); + + transaction t (c->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + + c->execute ("PRAGMA foreign_keys=ON"); + } +#elif defined(DATABASE_PGSQL) + auto_ptr<database> db (new odb::pgsql::database (argc, argv)); +#elif defined(DATABASE_ORACLE) + auto_ptr<database> db (new odb::oracle::database (argc, argv)); +#elif defined(DATABASE_MSSQL) + auto_ptr<database> db (new odb::mssql::database (argc, argv)); +#endif + + return db; +} + +#endif // DATABASE_HXX diff --git a/access/driver.cxx b/access/driver.cxx new file mode 100644 index 0000000..f5d34c8 --- /dev/null +++ b/access/driver.cxx @@ -0,0 +1,56 @@ +// file : access/driver.cxx +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include "database.hxx" // create_database + +#include "person.hxx" +#include "person-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + { + person john ("john@doe.com", "John", "X", "Doe", 31); + person jane ("jane@doe.com", "Jane", "Y", "Doe", 29); + + transaction t (db->begin ()); + db->persist (john); + db->persist (jane); + t.commit (); + } + + { + typedef odb::result<person> result; + + transaction t (db->begin ()); + result r (db->query<person> ()); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + cout << i->getFirst () << ' ' + << i->g_middle () << ' ' + << i->last () << ' ' + << i->email () << ' ' + << i->age () << endl; + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/access/makefile b/access/makefile new file mode 100644 index 0000000..6dbe181 --- /dev/null +++ b/access/makefile @@ -0,0 +1,118 @@ +# file : access/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := person.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +$(call import,\ + $(scf_root)/import/libodb/stub.make,\ + l: odb.l,cpp-options: odb.l.cpp-options) + +ifdef db_id +$(call import,\ + $(scf_root)/import/libodb-$(db_id)/stub.make,\ + l: odb_db.l,cpp-options: odb_db.l.cpp-options) +endif + +ifeq ($(odb_db.l.cpp-options),) +odb_db.l.cpp-options := $(out_base)/.unbuildable +endif + +# Build. +# +$(driver): $(cxx_obj) $(odb_db.l) $(odb.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) -D$(db_macro) +$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--generate-query --table-prefix access_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(odb.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export name := $(name) +$(dist): export odb_header_stem := $(basename $(odb_hdr)) +$(dist): export extra_dist := README $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) README database.hxx) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): header := $(odb_hdr) +$(test): $(driver) + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + diff --git a/access/person.hxx b/access/person.hxx new file mode 100644 index 0000000..a0e37ea --- /dev/null +++ b/access/person.hxx @@ -0,0 +1,125 @@ +// file : access/person.hxx +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object +class person +{ +public: + person () {} + person (const std::string& email, + const std::string& first, + const std::string& middle, + const std::string& last, + unsigned short age) + : email_ (email), first_ (first), middle_ (middle), last_ (last) + { + data_.age = age; + } + + // Standard accessor/modifier names. Auto-discovered by ODB. + // + const std::string& + email () const + { + return email_; + } + + void + email (const std::string& email) + { + email_ = email; + } + + // Get/set-style accessor/modifier names. Also auto-discovered + // by ODB. + // + const std::string& + getFirst () const + { + return first_; + } + + std::string& + setFirst () + { + return first_; + } + + // Unconventional accessor/modifier names which ODB is unable to + // auto-discover (but see also the --{accessor,modifier}-regex + // options). We have to specify these names explicitly (see below). + // + const std::string& + g_middle () const + { + return middle_; + } + + void + s_middle (const std::string& middle) + { + middle_ = middle; + } + + // Accessor/modifier types do not match data member type. Again, + // we have to specify accessor/modifier expressions that perform + // the necessary conversions (see below). + // + const char* + last () const + { + return last_.c_str (); + } + + void + last (const char* last) + { + last_ = last; + } + + // Accessor/modifier for a data member that is wrapped in an + // anonymous struct. We use a virtual data member to handle + // this case. + // + unsigned short + age () const + { + return data_.age; + } + + void + age (unsigned short age) + { + data_.age = age; + } + +private: + #pragma db id + std::string email_; // Accessor and modifier are auto-discovered. + + std::string first_; // Accessor and modifier are auto-discovered. + + #pragma db get(g_middle) set(s_middle) + std::string middle_; + + #pragma db get(std::string (this.last ())) set(last ((?).c_str ())) + std::string last_; + + #pragma db transient + struct + { + unsigned short age; + } data_; + + #pragma db member(age) virtual(unsigned short) // Accessor and modifier + // are auto-discovered. +}; + +#endif // PERSON_HXX @@ -5,6 +5,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))build/bootstrap.make dirs := \ +access \ composite \ container \ hello \ @@ -13,6 +14,7 @@ inheritance/reuse \ query \ mapping \ optimistic \ +pimpl \ schema/embedded tr1_dirs := relationship inverse schema/custom view diff --git a/pimpl/README b/pimpl/README new file mode 100644 index 0000000..6c9f4b0 --- /dev/null +++ b/pimpl/README @@ -0,0 +1,53 @@ +This example shows how to use virtual data members to implement a persistent +class that employes the pimpl C++ idiom. + +The example consists of the following files: + +person.hxx +person.cxx + Header and source file implementing the 'person' persistent class that + uses the pimpl idiom. + +person-odb.hxx +person-odb.ixx +person-odb.cxx +person.sql + The first three files contain the database support code and the last file + contains the database schema for the person.hxx header. + + These files are generated by the ODB compiler from person.hxx using the + following command line: + + odb -d <database> --generate-query --generate-schema person.hxx + + Where <database> stands for the database system we are using, for example, + 'mysql'. + +database.hxx + Contains the create_database() function which instantiates the concrete + database class corresponding to the database system we are using. + +driver.cxx + Driver for the example. It includes the person.hxx and person-odb.hxx + headers to gain access to the persistent class and its database support + code. It also includes database.hxx for the create_database() function + declaration. + + In main() the driver first calls create_database() to obtain the database + instance. Then it executes a number of database transactions on the 'person' + objects. + +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 +code which makes this step unnecessary). Using MySQL as an example, this +can be achieved with the following command: + +mysql --user=odb_test --database=odb_test < person.sql + +Here we use 'odb_test' as the database login and also 'odb_test' as the +database name. + +Once the database schema is ready, we can run the example (using MySQL as +the database): + +./driver --user odb_test --database odb_test diff --git a/pimpl/database.hxx b/pimpl/database.hxx new file mode 100644 index 0000000..c159128 --- /dev/null +++ b/pimpl/database.hxx @@ -0,0 +1,92 @@ +// file : pimpl/database.hxx +// copyright : not copyrighted - public domain + +// +// Create concrete database instance based on the DATABASE_* macros. +// + +#ifndef DATABASE_HXX +#define DATABASE_HXX + +#include <string> +#include <memory> // std::auto_ptr +#include <cstdlib> // std::exit +#include <iostream> + +#include <odb/database.hxx> + +#if defined(DATABASE_MYSQL) +# include <odb/mysql/database.hxx> +#elif defined(DATABASE_SQLITE) +# include <odb/connection.hxx> +# include <odb/transaction.hxx> +# include <odb/schema-catalog.hxx> +# include <odb/sqlite/database.hxx> +#elif defined(DATABASE_PGSQL) +# include <odb/pgsql/database.hxx> +#elif defined(DATABASE_ORACLE) +# include <odb/oracle/database.hxx> +#elif defined(DATABASE_MSSQL) +# include <odb/mssql/database.hxx> +#endif + +inline std::auto_ptr<odb::database> +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cout << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cout); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cout); +#elif defined(DATABASE_PGSQL) + odb::pgsql::database::print_usage (cout); +#elif defined(DATABASE_ORACLE) + odb::oracle::database::print_usage (cout); +#elif defined(DATABASE_MSSQL) + odb::mssql::database::print_usage (cout); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + auto_ptr<database> db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr<database> db ( + new odb::sqlite::database ( + argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + + // Create the database schema. Due to bugs in SQLite foreign key + // support for DDL statements, we need to temporarily disable + // foreign keys. + // + { + connection_ptr c (db->connection ()); + + c->execute ("PRAGMA foreign_keys=OFF"); + + transaction t (c->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + + c->execute ("PRAGMA foreign_keys=ON"); + } +#elif defined(DATABASE_PGSQL) + auto_ptr<database> db (new odb::pgsql::database (argc, argv)); +#elif defined(DATABASE_ORACLE) + auto_ptr<database> db (new odb::oracle::database (argc, argv)); +#elif defined(DATABASE_MSSQL) + auto_ptr<database> db (new odb::mssql::database (argc, argv)); +#endif + + return db; +} + +#endif // DATABASE_HXX diff --git a/pimpl/driver.cxx b/pimpl/driver.cxx new file mode 100644 index 0000000..b97fd92 --- /dev/null +++ b/pimpl/driver.cxx @@ -0,0 +1,52 @@ +// file : pimpl/driver.cxx +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include "database.hxx" // create_database + +#include "person.hxx" +#include "person-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + { + person john ("john@doe.com", "John Doe", 31); + person jane ("jane@doe.com", "Jane Doe", 29); + + transaction t (db->begin ()); + db->persist (john); + db->persist (jane); + t.commit (); + } + + { + typedef odb::result<person> result; + + transaction t (db->begin ()); + result r (db->query<person> ()); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + cout << i->name () << ' ' << i->email () << ' ' << i->age () << endl; + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/pimpl/makefile b/pimpl/makefile new file mode 100644 index 0000000..5c826f3 --- /dev/null +++ b/pimpl/makefile @@ -0,0 +1,118 @@ +# file : pimpl/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make + +cxx_tun := driver.cxx person.cxx +odb_hdr := person.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +$(call import,\ + $(scf_root)/import/libodb/stub.make,\ + l: odb.l,cpp-options: odb.l.cpp-options) + +ifdef db_id +$(call import,\ + $(scf_root)/import/libodb-$(db_id)/stub.make,\ + l: odb_db.l,cpp-options: odb_db.l.cpp-options) +endif + +ifeq ($(odb_db.l.cpp-options),) +odb_db.l.cpp-options := $(out_base)/.unbuildable +endif + +# Build. +# +$(driver): $(cxx_obj) $(odb_db.l) $(odb.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) -D$(db_macro) +$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--generate-query --table-prefix pimpl_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(odb.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export name := $(name) +$(dist): export odb_header_stem := $(basename $(odb_hdr)) +$(dist): export extra_dist := README $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) README database.hxx) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): header := $(odb_hdr) +$(test): $(driver) + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + diff --git a/pimpl/person.cxx b/pimpl/person.cxx new file mode 100644 index 0000000..6922afc --- /dev/null +++ b/pimpl/person.cxx @@ -0,0 +1,71 @@ +// file : pimpl/person.cxx +// copyright : not copyrighted - public domain + +#include "person.hxx" + +using namespace std; + +struct person::impl +{ + impl () {} + impl (const string& e, const string& n, unsigned short a) + : email (e), name (n), age (a) {} + + string email; + string name; + unsigned short age; +}; + +person:: +~person () +{ + delete pimpl_; +} + +person:: +person () + : pimpl_ (new impl) +{ +} + +person:: +person (const string& e, const string& n, unsigned short a) + : pimpl_ (new impl (e, n, a)) +{ +} + +const string& person:: +email () const +{ + return pimpl_->email; +} + +void person:: +email (const string& e) +{ + pimpl_->email = e; +} + +const string& person:: +name () const +{ + return pimpl_->name; +} + +void person:: +name (const string& n) +{ + pimpl_->name = n; +} + +unsigned short person:: +age () const +{ + return pimpl_->age; +} + +void person:: +age (unsigned short a) const +{ + pimpl_->age = a; +} diff --git a/pimpl/person.hxx b/pimpl/person.hxx new file mode 100644 index 0000000..d296c28 --- /dev/null +++ b/pimpl/person.hxx @@ -0,0 +1,56 @@ +// file : pimpl/person.hxx +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object +class person +{ +public: + ~person (); + person (const std::string& email, + const std::string& name, + unsigned short age); + + const std::string& + email () const; + + void + email (const std::string&); + + const std::string& + name () const; + + void + name (const std::string&); + + unsigned short + age () const; + + void + age (unsigned short) const; + +private: + person (const person&); + person& operator= (const person&); + +private: + friend class odb::access; + person (); + + struct impl; + + #pragma db transient + impl* pimpl_; + + #pragma db member(email) virtual(std::string) id + #pragma db member(name) virtual(std::string) + #pragma db member(age) virtual(unsigned short) +}; + +#endif // PERSON_HXX |