aboutsummaryrefslogtreecommitdiff
path: root/odb/cache-traits.hxx
blob: 51c41b88a803220bbfcd8ff2118248b1aac94ef5 (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
279
280
281
282
283
284
285
286
287
288
289
// file      : odb/cache-traits.hxx
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_CACHE_TRAITS_HXX
#define ODB_CACHE_TRAITS_HXX

#include <odb/pre.hxx>

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

namespace odb
{
  // Caching traits for objects passed by pointer.
  //
  template <typename P, typename ID, pointer_kind kind>
  struct pointer_cache_traits_impl;

  template <typename P>
  struct pointer_cache_traits: pointer_cache_traits_impl<
    P,
    typename object_traits<typename pointer_traits<P>::element_type>::id_type,
    pointer_traits<P>::kind>
  {
  };

  template <typename P, typename ID, pointer_kind kind>
  struct pointer_cache_traits_impl
  {
    typedef P pointer_type;
    typedef odb::pointer_traits<pointer_type> pointer_traits;
    typedef typename pointer_traits::element_type element_type;

    // element_type can be const while object_type is always non-const.
    //
    typedef typename object_traits<element_type>::object_type object_type;
    typedef typename object_traits<element_type>::id_type id_type;
    typedef session::object_position<object_type> position_type;

    struct insert_guard
    {
      insert_guard (const position_type& pos): pos_ (pos) {}
      ~insert_guard () {erase (pos_);}

      position_type
      position () const {return pos_;}

      void
      release () {pos_.map_ = 0;}

    private:
      position_type pos_;
    };

    // We need the insert() overload with explicit id to handle self-
    // references. In such cases the object is not yet loaded and the
    // id member does not contain the correct id.
    //
    // Qualify the database type to resolve a phony ambiguity in VC 10.
    //
    static position_type
    insert (odb::database& db, const id_type& id, const pointer_type& p)
    {
      if (session::has_current ())
      {
        // Cast away constness if any.
        //
        return session::current ().insert<object_type> (
          db, id, pointer_traits::cast (p));
      }
      else
        return position_type ();
    }

    static position_type
    insert (odb::database& db, const pointer_type& p)
    {
      const id_type& id (
        object_traits<object_type>::id (
          pointer_traits::get_ref (p)));

      return insert (db, id, p);
    }

    static pointer_type
    find (odb::database& db, const id_type& id)
    {
      if (session::has_current ())
        return session::current ().find<object_type> (db, id);
      else
        return pointer_type ();
    }

    static void
    erase (odb::database& db, const id_type& id)
    {
      if (session::has_current ())
        session::current ().erase<object_type> (db, id);
    }

    static void
    erase (const position_type& p)
    {
      if (p.map_ != 0)
        session::current ().erase<object_type> (p);
    }
  };

  template <typename P, pointer_kind kind>
  struct pointer_cache_traits_impl<P, void, kind>
  {
    typedef P pointer_type;
    struct position_type {};

    static position_type
    insert (odb::database&, const pointer_type&)
    {
      return position_type ();
    }
  };

  // Unique pointers don't work with the object cache.
  //
  template <typename P, typename ID>
  struct pointer_cache_traits_impl<P, ID, pk_unique>
  {
    typedef P pointer_type;
    typedef typename pointer_traits<pointer_type>::element_type element_type;
    typedef typename object_traits<element_type>::id_type id_type;
    struct position_type {};

    struct insert_guard
    {
      insert_guard (const position_type&) {}

      position_type
      position () const {return position_type ();}

      void
      release () {}
    };

    static position_type
    insert (odb::database&, const id_type&, const pointer_type&)
    {
      return position_type ();
    }

    static position_type
    insert (odb::database&, const pointer_type&)
    {
      return position_type ();
    }

    static pointer_type
    find (odb::database&, const id_type&) { return pointer_type (); }

    static void
    erase (odb::database&, const id_type&) {}

    static void
    erase (const position_type&) {}
  };

  template <typename P>
  struct pointer_cache_traits_impl<P, void, pk_unique>
  {
    typedef P pointer_type;
    struct position_type {};

    static position_type
    insert (odb::database&, const pointer_type&)
    {
      return position_type ();
    }
  };

  // Caching traits for objects passed by reference. Only if the object
  // pointer kind is raw do we add the object to the session.
  //
  template <typename T, typename ID, pointer_kind kind>
  struct reference_cache_traits_impl;

  template <typename T>
  struct reference_cache_traits: reference_cache_traits_impl<
    T,
    typename object_traits<T>::id_type,
    pointer_traits<typename object_traits<T>::pointer_type>::kind>
  {
  };

  template <typename T, typename ID, pointer_kind kind>
  struct reference_cache_traits_impl
  {
    typedef T element_type;
    typedef typename object_traits<element_type>::pointer_type pointer_type;
    typedef typename object_traits<element_type>::id_type id_type;

    typedef
    typename pointer_cache_traits<pointer_type>::position_type
    position_type;

    struct insert_guard
    {
      insert_guard (const position_type&) {}

      position_type
      position () const {return position_type ();}

      void
      release () {}
    };

    static position_type
    insert (odb::database&, const id_type&, element_type&)
    {
      return position_type ();
    }

    static position_type
    insert (odb::database&, element_type&)
    {
      return position_type ();
    }
  };

  template <typename T, pointer_kind kind>
  struct reference_cache_traits_impl<T, void, kind>
  {
    typedef T element_type;
    struct position_type {};

    static position_type
    insert (odb::database&, element_type&)
    {
      return position_type ();
    }
  };

  template <typename T, typename ID>
  struct reference_cache_traits_impl<T, ID, pk_raw>
  {
    typedef T element_type;
    typedef typename object_traits<element_type>::pointer_type pointer_type;
    typedef typename object_traits<element_type>::id_type id_type;

    typedef
    typename pointer_cache_traits<pointer_type>::position_type
    position_type;

    typedef
    typename pointer_cache_traits<pointer_type>::insert_guard
    insert_guard;

    static position_type
    insert (odb::database& db, const id_type& id, element_type& obj)
    {
      pointer_type p (&obj);
      return pointer_cache_traits<pointer_type>::insert (db, id, p);
    }

    static position_type
    insert (odb::database& db, element_type& obj)
    {
      pointer_type p (&obj);
      return pointer_cache_traits<pointer_type>::insert (db, p);
    }
  };

  template <typename T>
  struct reference_cache_traits_impl<T, void, pk_raw>
  {
    typedef T element_type;
    struct position_type {};

    static position_type
    insert (odb::database&, element_type&)
    {
      return position_type ();
    }
  };
}

#include <odb/post.hxx>

#endif // ODB_CACHE_TRAITS_HXX