aboutsummaryrefslogtreecommitdiff
path: root/access/person.hxx
blob: a0e37eaa5d3d1d8d70ede37f95b130387731fea6 (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
// 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