aboutsummaryrefslogtreecommitdiff
path: root/boost/employee.hxx
blob: 8d6173aeffed204136b90960af93c9f849b77439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// file      : boost/employee.hxx
// copyright : not copyrighted - public domain

#ifndef EMPLOYEE_HXX
#define EMPLOYEE_HXX

#include <vector>
#include <string>

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/unordered_set.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>

#include <odb/core.hxx>

#include <odb/boost/lazy-ptr.hxx>

using boost::shared_ptr;
using boost::weak_ptr;

using odb::boost::lazy_shared_ptr;
using odb::boost::lazy_weak_ptr;

using boost::uuids::uuid;
using boost::gregorian::date;

// Forward declarations.
//
class employer;
class employee;

typedef boost::unordered_set<std::string> emails;
typedef std::vector<lazy_weak_ptr<employee> > employees;

#pragma db object
class employer
{
public:
  employer (const std::string& name)
      : name_ (name)
  {
  }

  const std::string&
  name () const
  {
    return name_;
  }

  // Employees of this employer.
  //
  typedef ::employees 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
class employee
{
public:
  typedef ::employer employer_type;

  employee (const std::string& first,
            const std::string& last,
            const date& born,
            shared_ptr<employer_type> employer)
      : id_ (boost::uuids::random_generator () ()),
        first_ (first), last_ (last),
        born_ (born),
        employer_ (employer)
  {
  }

  employee (const std::string& first,
            const std::string& middle,
            const std::string& last,
            const date& born,
            shared_ptr<employer_type> employer)
      : id_ (boost::uuids::random_generator () ()),
        first_ (first), middle_ (middle), last_ (last),
        born_ (born),
        employer_ (employer)
  {
  }

  // Id.
  //
  const uuid&
  id () const
  {
    return id_;
  }

  // Name.
  //
  const std::string&
  first () const
  {
    return first_;
  }

  const boost::optional<std::string>&
  middle () const
  {
    return middle_;
  }

  const std::string&
  last () const
  {
    return last_;
  }

  // Date of birth.
  //
  const date&
  born () const
  {
    return born_;
  }

  // Emails.
  //
  typedef ::emails emails_type;

  const emails_type&
  emails () const
  {
    return emails_;
  }

  emails_type&
  emails ()
  {
    return emails_;
  }

  // Employer.
  //
  shared_ptr<employer_type>
  employer () const
  {
    return employer_;
  }

  void
  employer (shared_ptr<employer_type> employer)
  {
    employer_ = employer;
  }

private:
  friend class odb::access;

  employee () {}

  #pragma db id
  uuid id_;

  std::string first_;
  boost::optional<std::string> middle_;
  std::string last_;

  date born_;
  emails_type emails_;

  #pragma db not_null
  shared_ptr<employer_type> employer_;
};

#endif // EMPLOYEE_HXX