diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-03-02 12:31:38 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-03-02 12:31:38 +0200 |
commit | d43a0047151d490dcd061e3f105bfc4eb9fac854 (patch) | |
tree | 93546657349fc5d8529376b7235ecc01bdaa73f2 /c++11/employee.hxx | |
parent | c55878df67c400d9ac940ee2d8dbd6e922af5ac3 (diff) |
New example that shows C++11 support, build infrastructure for C++11
Diffstat (limited to 'c++11/employee.hxx')
-rw-r--r-- | c++11/employee.hxx | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/c++11/employee.hxx b/c++11/employee.hxx new file mode 100644 index 0000000..3750ca0 --- /dev/null +++ b/c++11/employee.hxx @@ -0,0 +1,182 @@ +// file : boost/employee.hxx +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include <string> +#include <memory> // std::unique_ptr, std::shared_ptr +#include <vector> +#include <unordered_set> + +#include <odb/core.hxx> +#include <odb/lazy-ptr.hxx> // odb::lazy_shared_ptr, odb::lazy_weak_ptr + +// Forward declarations. +// +class employer; +class employee; + +#pragma db object pointer(std::shared_ptr) +class employer +{ +public: + employer (const std::string& name) + : name_ (name) + { + } + + const std::string& + name () const + { + return name_; + } + + // Employees of this employer. + // + typedef std::vector<odb::lazy_weak_ptr<employee>> employees_type; + + const employees_type& + employees () const + { + return employees_; + } + + employees_type& + employees () + { + return employees_; + } + +private: + friend class odb::access; + + employer () {} + + #pragma db id + std::string name_; + + #pragma db value_not_null inverse(employer_) + employees_type employees_; +}; + +#pragma db object pointer(std::shared_ptr) +class employee +{ +public: + typedef ::employer employer_type; + + employee (const std::string& first, + const std::string& last, + std::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_; + } + + // Emails. + // + typedef std::unordered_set<std::string> emails_type; + + const emails_type& + emails () const + { + return emails_; + } + + emails_type& + emails () + { + return emails_; + } + + // Employer. + // + std::shared_ptr<employer_type> + employer () const + { + return employer_; + } + + void + employer (std::shared_ptr<employer_type> employer) + { + employer_ = employer; + } + +private: + friend class odb::access; + + employee () {} + + #pragma db id auto + unsigned long id_; + + std::string first_; + std::string last_; + emails_type emails_; + + #pragma db not_null + std::shared_ptr<employer_type> employer_; +}; + +// std::unique_ptr is a good choice for an object pointer if we are +// not planning to do any sharing. +// +#pragma db object pointer(std::unique_ptr) +class pension_fund +{ +public: + pension_fund (const std::string& name) + : name_ (name) + { + } + + const std::string& + name () const + { + return name_; + } + + // Members of this fund. + // + typedef std::vector<odb::lazy_shared_ptr<employee>> members_type; + + const members_type& + members () const + { + return members_; + } + + members_type& + members () + { + return members_; + } + +private: + friend class odb::access; + + pension_fund () {} + + #pragma db id + std::string name_; + + #pragma db value_not_null + members_type members_; +}; + +#endif // EMPLOYEE_HXX |