diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-09-01 11:16:00 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-09-01 11:16:00 +0200 |
commit | c97a759dcc080705c956c87fce076709ca66a0c8 (patch) | |
tree | 0f11125e3137cef9fc11d669f8addc2955df0460 /access/person.hxx | |
parent | 55268128a30a56bfd42a4ffb382ad3a5179a739f (diff) |
Add 'access' and 'pimpl' examples
These illustrate the use of accessor/modifier functions and expressions
as well as virtual data members.
Diffstat (limited to 'access/person.hxx')
-rw-r--r-- | access/person.hxx | 125 |
1 files changed, 125 insertions, 0 deletions
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 |