diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-01-13 11:31:14 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-01-13 11:31:14 +0200 |
commit | ab51fa65f9e8cad4ef5a1db85029dfe6404e9a1f (patch) | |
tree | 36c78dd50d88e4797f2bbb886c41399006111fea /relationship/employee.hxx | |
parent | 5511613df7dce6142a84111488aaa25ff792d66b (diff) |
Add composite, relationship, and inverse examples
All add the TR1 <memory> test for the latter two examples.
Diffstat (limited to 'relationship/employee.hxx')
-rw-r--r-- | relationship/employee.hxx | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/relationship/employee.hxx b/relationship/employee.hxx new file mode 100644 index 0000000..e89e704 --- /dev/null +++ b/relationship/employee.hxx @@ -0,0 +1,161 @@ +// file : relationship/employee.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include <vector> +#include <string> + +#include <odb/core.hxx> + +// Include TR1 <memory> header in a compiler-specific fashion. Fall back +// on the Boost implementation if the compiler does not support TR1. +// +#include <odb/tr1/memory.hxx> + +using std::tr1::shared_ptr; + +// The "pointer architecture" in this object model is as follows: All +// object pointers are eager. The employee class holds shared pointers +// to employer and projects. +// +// The following unidirectional relationships are used: +// +// to-one : employee -> employer +// to-many : employee -> project +// + +// Forward declarations. +// +class employer; +class project; +class employee; + +typedef std::vector<shared_ptr<project> > projects; + +#pragma db object +class employer +{ +public: + employer (const std::string& name) + : name_ (name) + { + } + + const std::string& + name () const + { + return name_; + } + +private: + friend class odb::access; + + employer () {} + + #pragma db id + std::string name_; +}; + +#pragma db object +class project +{ +public: + project (const std::string& name) + : name_ (name) + { + } + + const std::string& + name () const + { + return name_; + } + +private: + friend class odb::access; + + project () {} + + #pragma db id + std::string name_; +}; + +#pragma db object +class employee +{ +public: + typedef ::employer employer_type; + + employee (const std::string& first, + const std::string& last, + shared_ptr<employer_type> employer) + : first_ (first), last_ (last), employer_ (employer) + { + } + + // Name. + // + const std::string& + first () const + { + return first_; + } + + const std::string& + last () const + { + return last_; + } + + // Employer. + // + shared_ptr<employer_type> + employer () const + { + return employer_; + } + + void + employer (shared_ptr<employer_type> employer) + { + employer_ = employer; + } + + // Projects. + // + typedef ::projects projects_type; + + const projects_type& + projects () const + { + return projects_; + } + + projects_type& + projects () + { + return projects_; + } + +private: + friend class odb::access; + + employee () {} + + #pragma db id auto + unsigned long id_; + + std::string first_; + std::string last_; + + #pragma db not_null + shared_ptr<employer_type> employer_; + + #pragma db not_null unordered + projects_type projects_; +}; + +#endif // EMPLOYEE_HXX |