aboutsummaryrefslogtreecommitdiff
path: root/odb/traits.hxx
blob: 0fd11e408a20e8129e823950afe677704113e950 (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
276
277
278
// file      : odb/traits.hxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_TRAITS_HXX
#define ODB_TRAITS_HXX

#include <odb/pre.hxx>

#include <odb/forward.hxx>
#include <odb/pointer-traits.hxx>

namespace odb
{
  template <typename T, typename P>
  class access::object_factory
  {
  public:
    typedef T object_type;
    typedef P pointer_type;

    static P
    create ()
    {
      // By default use pointer-specific construction.
      //
      return pointer_factory<T, P>::create ();
    }
  };

  template <typename T, typename P>
  class access::view_factory
  {
  public:
    typedef T view_type;
    typedef P pointer_type;

    static P
    create ()
    {
      // By default use pointer-specific construction.
      //
      return pointer_factory<T, P>::create ();
    }
  };

  template <typename T, typename P>
  class access::pointer_factory
  {
  public:
    typedef T value_type;
    typedef P pointer_type;

    static P
    create ()
    {
      void* v (pointer_traits<P>::allocate (sizeof (T)));
      mem_guard g (v);
      P p (new (v) T);
      g.release ();
      return p;
    }

  private:
    struct mem_guard
    {
      mem_guard (void* p): p_ (p) {}
      ~mem_guard () {if (p_) pointer_traits<P>::free (p_);}
      void release () {p_ = 0;}
      void* p_;
    };
  };

  //
  // class_traits
  //
  enum class_kind
  {
    class_object,
    class_view,
    class_other
  };

  template <typename T>
  struct class_traits
  {
    static const class_kind kind = class_other;
  };

  template <typename T>
  struct class_traits<const T>
  {
    static const class_kind kind = class_traits<T>::kind;
  };

  //
  // object_traits
  //

  template <typename T>
  //
  // If a C++ compiler issues an error pointing to this struct and
  // saying that it is incomplete, then you are most likely trying to
  // perform a database operation on a C++ type that is not a persistent
  // object. Or you forgot to include the corresponding -odb.hxx file.
  //
  struct object_traits:
    access::object_traits<T>,
    access::object_factory<T, typename access::object_traits<T>::pointer_type>
  {
    typedef
    odb::pointer_traits<typename access::object_traits<T>::pointer_type>
    pointer_traits;

    typedef typename access::object_traits<T>::object_type object_type;
    typedef typename access::object_traits<T>::pointer_type pointer_type;
    typedef typename pointer_traits::const_pointer_type const_pointer_type;
  };

  // Specialization for const objects. It only defines the id, object,
  // pointer, and const_pointer types with pointer and const_pointer
  // being the same. The idea is to only use this specialization in the
  // interfaces, with the implementations detecting this situation and
  // using the non-const object_traits version.
  //
  template <typename T>
  struct object_traits<const T>
  {
  private:
    typedef
    odb::pointer_traits<typename access::object_traits<T>::pointer_type>
    pointer_traits;

  public:
    typedef typename access::object_traits<T>::id_type id_type;
    typedef typename access::object_traits<T>::object_type object_type;
    typedef typename pointer_traits::const_pointer_type const_pointer_type;
    typedef const_pointer_type pointer_type;

    static const bool polymorphic = access::object_traits<T>::polymorphic;
  };

  // Specializations for pointer types to allow the C++ compiler to
  // instantiate persist(), etc., signatures in class database. The
  // overloads that use these specializations would never actually
  // be selected by the compiler.
  //
  template <typename T>
  struct object_traits<T*>
  {
    struct id_type {};
  };

  template <typename T>
  struct object_traits<T* const>
  {
    struct id_type {};
  };

  template <typename T, template <typename> class P>
  struct object_traits<P<T> >
  {
    struct id_type {};
  };

  template <typename T, typename A1, template <typename, typename> class P>
  struct object_traits<P<T, A1> >
  {
    struct id_type {};
  };

  template <typename T, template <typename> class P>
  struct object_traits<const P<T> >
  {
    struct id_type {};
  };

  template <typename T, typename A1, template <typename, typename> class P>
  struct object_traits<const P<T, A1> >
  {
    struct id_type {};
  };

  template <typename T, database_id DB>
  //
  // If a C++ compiler issues an error pointing to this struct and
  // saying that it is incomplete, then you are most likely trying to
  // perform a database operation on a C++ type that is not a persistent
  // object. Or you forgot to include the corresponding -odb.hxx file.
  //
  struct object_traits_impl:
    access::object_traits_impl<T, DB>,
    access::object_factory<T, typename access::object_traits<T>::pointer_type>
  {
    typedef
    odb::pointer_traits<typename access::object_traits<T>::pointer_type>
    pointer_traits;

    typedef typename access::object_traits<T>::object_type object_type;
    typedef typename access::object_traits<T>::pointer_type pointer_type;
    typedef typename pointer_traits::const_pointer_type const_pointer_type;
  };

  //
  // view_traits
  //

  template <typename T>
  //
  // If a C++ compiler issues an error pointing to this struct and
  // saying that it is incomplete, then you are most likely trying to
  // perform a database operation on a C++ type that is not a view
  // Or you forgot to include the corresponding -odb.hxx file.
  //
  struct view_traits:
    access::view_traits<T>,
    access::view_factory<T, typename access::view_traits<T>::pointer_type>
  {
    typedef
    odb::pointer_traits<typename access::view_traits<T>::pointer_type>
    pointer_traits;

    typedef typename access::view_traits<T>::view_type view_type;
    typedef typename access::view_traits<T>::pointer_type pointer_type;
  };

  // Specialization for const views. It only defines the view, pointer,
  // and const_pointer types with pointer and const_pointer being the
  // same. Similar to objects, the idea is to only use this specialization
  // in the interfaces, with the implementations detecting this situation
  // and using the non-const view_traits version.
  //
  template <typename T>
  struct view_traits<const T>
  {
  private:
    typedef
    odb::pointer_traits<typename access::view_traits<T>::pointer_type>
    pointer_traits;

  public:
    typedef typename access::view_traits<T>::view_type view_type;
    typedef typename pointer_traits::const_pointer_type const_pointer_type;
    typedef const_pointer_type pointer_type;
  };

  template <typename T, database_id DB>
  //
  // If a C++ compiler issues an error pointing to this struct and
  // saying that it is incomplete, then you are most likely trying to
  // perform a database operation on a C++ type that is not a view
  // Or you forgot to include the corresponding -odb.hxx file.
  //
  struct view_traits_impl:
    access::view_traits_impl<T, DB>,
    access::view_factory<T, typename access::view_traits<T>::pointer_type>
  {
    typedef
    odb::pointer_traits<typename access::view_traits<T>::pointer_type>
    pointer_traits;

    typedef typename access::view_traits<T>::view_type view_type;
    typedef typename access::view_traits<T>::pointer_type pointer_type;
  };

  //
  // composite_value_traits
  //

  template <typename T, database_id DB>
  struct composite_value_traits: access::composite_value_traits<T, DB>
  {
  };
}

#include <odb/post.hxx>

#endif // ODB_TRAITS_HXX