aboutsummaryrefslogtreecommitdiff
path: root/inverse/employee.hxx
blob: 8c10e34c7b328a92dcf9551c26153b7aadbda2aa (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// file      : inverse/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>

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

using std::tr1::shared_ptr;

using odb::tr1::lazy_shared_ptr;
using odb::tr1::lazy_weak_ptr;

// The "pointer architecture" in this object model is as follows: All
// object pointers are lazy. The employee class holds shared pointers
// to employer, position, and projects. All other objects hold weak
// pointers back to the employee object. The weak sides are also the
// ones that are made inverse.
//
// The following bidirectional relationships are used:
//
// many-to-one  : employee <--> employer
// one-to-one   : employee <--> position
// many-to-many : employee <--> project
//

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

typedef std::vector<lazy_shared_ptr<project> > projects;
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 position
{
public:
  position (const std::string& title)
      : title_ (title)
  {
  }

  const std::string&
  title () const
  {
    return title_;
  }

  // Employee that fills this position. NULL if the position is vacant.
  //
  typedef ::employee employee_type;

  const lazy_weak_ptr<employee_type>&
  employee () const
  {
    return employee_;
  }

  void
  employee (lazy_weak_ptr<employee_type> employee)
  {
    employee_ = employee;
  }

private:
  friend class odb::access;

  position () {}

  #pragma db id auto
  unsigned long id_;

  std::string title_;

  #pragma db inverse(position_)
  lazy_weak_ptr<employee_type> employee_;
};

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

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

  // Employees working on this project.
  //
  typedef ::employees employees_type;

  const employees_type&
  employees () const
  {
    return employees_;
  }

  employees_type&
  employees ()
  {
    return employees_;
  }

private:
  friend class odb::access;

  project () {}

  #pragma db id
  std::string name_;

  #pragma db value_not_null inverse(projects_)
  employees_type employees_;
};

#pragma db object
class employee
{
public:
  typedef ::employer employer_type;
  typedef ::position position_type;

  employee (const std::string& first,
            const std::string& last,
            lazy_shared_ptr<employer_type> employer,
            lazy_shared_ptr<position_type> position)
      : first_ (first), last_ (last),
        employer_ (employer),
        position_ (position)
  {
  }

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

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

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

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

  // Position.
  //
  const lazy_shared_ptr<position_type>&
  position () const
  {
    return position_;
  }

  void
  position (lazy_shared_ptr<position_type> position)
  {
    position_ = position;
  }

  // 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
  lazy_shared_ptr<employer_type> employer_;

  #pragma db not_null
  lazy_shared_ptr<position_type> position_;

  #pragma db value_not_null unordered
  projects_type projects_;
};

#endif // EMPLOYEE_HXX