aboutsummaryrefslogtreecommitdiff
path: root/common/readonly/test.hxx
blob: 45797b6b574ca4888cb94076abb239290fe68412 (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
// file      : common/readonly/test.hxx
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef TEST_HXX
#define TEST_HXX

#include <vector>
#include <memory> // std::unique_ptr

#include <odb/core.hxx>

// Simple readonly object.
//
#pragma db object
struct simple
{
  simple (unsigned long i, unsigned long x)
      : id (i), ro (x), co (x), rw (x)
  {
  }

  simple (): co (0) {}

  #pragma db id
  unsigned long id;

  #pragma db readonly
  unsigned long ro;

  const unsigned long co;

  unsigned long rw;
};

// Readonly pointer.
//
#pragma db object
struct pointer
{
  pointer (unsigned long i, pointer* p = 0): id (i), ro (p), co (p), rw (p) {}
  pointer (): ro (0), co (0), rw (0) {}

  ~pointer ()
  {
    delete ro;

    if (co != ro)
      delete co;

    if (rw != ro && rw != co)
      delete rw;
  }

  #pragma db id
  unsigned long id;

  #pragma db readonly
  pointer* ro;

  pointer* const co;

  pointer* rw;
};

// Composite readonly value as well as simple readonly value inside
// a composite.
//
#pragma db value readonly
struct ro_value
{
  ro_value () {}
  ro_value (unsigned long x): v (x) {}

  unsigned long v;
};

#pragma db value
struct value: ro_value
{
  value (): co (0) {}
  value (unsigned long x): ro_value (x), ro (x), co (x), rw (x) {}

  #pragma db readonly
  unsigned long ro;

  const unsigned long co;

  unsigned long rw;
};

#pragma db object
struct composite
{
  composite (unsigned long i, unsigned long x)
      : id (i), ro (x), co (x), rw (x), v (x)
  {
  }

  composite () {}

  #pragma db id
  unsigned long id;

  #pragma db readonly
  value ro;

  const value co;

  value rw;
  ro_value v;
};

// Readonly container.
//
#pragma db object
struct container
{
  container (unsigned long i): id (i) {}
  container () {}

  #pragma db id
  unsigned long id;

  #pragma db readonly
  std::vector<unsigned long> ro;

  const std::vector<unsigned long> co;

  std::vector<unsigned long> rw;
};

// Readonly object.
//
#pragma db object readonly
struct simple_object
{
  simple_object (unsigned long i, unsigned long x): id (i), sv (x) {}
  simple_object () {}

  #pragma db id
  unsigned long id;

  unsigned long sv;
};

#pragma db object
struct object
{
  object (unsigned long i, unsigned long x): id (i), sv (x) {}
  object () {}

  #pragma db id
  unsigned long id;

  unsigned long sv;
};

#pragma db object readonly
struct ro_object: object
{
  ro_object (unsigned long i, unsigned long x)
      : object (i, x), cv (x)
  {
  }

  ro_object () {}

  value cv;
  std::vector<unsigned long> cr;
};

#pragma db object
struct rw_object: ro_object
{
  rw_object (unsigned long i, unsigned long x)
      : ro_object (i, x), rw_sv (x)
  {
  }

  rw_object () {}

  unsigned long rw_sv;
};

// Readonly wrappers. Here we make sure that only const wrappers with
// const wrapped types are automatically treated as readonly.
//
#pragma db object
struct wrapper
{
  wrapper (unsigned long i, unsigned long x)
      : id (i),
        pl (new unsigned long (x)),
        cpl (new unsigned long (x)),
        pcl (new unsigned long (x)),
        cpcl (new unsigned long (x))
  {
  }

  wrapper () {}

  #pragma db id
  unsigned long id;

  std::unique_ptr<unsigned long> pl;
  const std::unique_ptr<unsigned long> cpl;
  std::unique_ptr<const unsigned long> pcl;
  const std::unique_ptr<const unsigned long> cpcl;
};

// Readonly object with auto id.
//
#pragma db object readonly
struct ro_auto
{
  ro_auto (unsigned long n): num (n) {}
  ro_auto () {}

  #pragma db id auto
  unsigned long id;

  unsigned long num;
};

#endif // TEST_HXX