aboutsummaryrefslogtreecommitdiff
path: root/odb/pointer-traits.hxx
blob: a24bf021e92fc7b26da6f061e74cfda60abddb5b (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// file      : odb/pointer-traits.hxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_POINTER_TRAITS_HXX
#define ODB_POINTER_TRAITS_HXX

#include <odb/pre.hxx>

#include <new>     // operators new/delete
#include <memory>  // std::auto_ptr, std::unique_ptr, std::shared_ptr/weak_ptr
#include <cstddef> // std::size_t

#include <odb/details/config.hxx>            // ODB_CXX11
#include <odb/details/meta/remove-const.hxx>

namespace odb
{
  enum pointer_kind
  {
    pk_raw,    // Raw pointer or equivalent (i.e., unmanaged).
    pk_unique, // Smart pointer that doesn't support sharing.
    pk_shared, // Smart pointer that supports sharing.
    pk_weak    // Weak counterpart for shared pointer.
  };

  template <typename P>
  class pointer_traits;

  //
  // Standard pointer guards.
  //

  // Raw pointer guard.
  //
  template <typename P>
  class raw_ptr_guard
  {
  public:
    ~raw_ptr_guard () {delete p_;}
    raw_ptr_guard (): p_ (0) {}

    explicit
    raw_ptr_guard (P p): p_ (p) {}

    void
    release () {p_ = 0;}

    void
    reset (P p = 0) {delete p_; p_ = p;}

  private:
    P p_;
  };

  // No-op pointer guard for smart pointers.
  //
  template <typename P>
  class smart_ptr_guard
  {
  public:
    smart_ptr_guard () {}

    explicit
    smart_ptr_guard (const P&) {}

    void
    release () {}

    void
    reset () {}

    void
    reset (const P&) {}
  };

  // Specialization for raw pointers.
  //
  template <typename T>
  class pointer_traits<T*>
  {
  public:
    static const pointer_kind kind = pk_raw;
    static const bool lazy = false;

    typedef T element_type;
    typedef T* pointer_type;
    typedef const T* const_pointer_type;
    typedef typename odb::details::meta::remove_const<T>::result*
    unrestricted_pointer_type;
    typedef raw_ptr_guard<pointer_type> guard;

    // Return raw pointer to the pointed-to element, including NULL.
    //
    static element_type*
    get_ptr (pointer_type p)
    {
      return p;
    }

    // Return reference to the pointed-to element.
    //
    static element_type&
    get_ref (pointer_type p)
    {
      return *p;
    }

    // Return true if the pointer is NULL.
    //
    static bool
    null_ptr (pointer_type p)
    {
      return p == 0;
    }

    // Casts.
    //
    static unrestricted_pointer_type
    const_pointer_cast (pointer_type p)
    {
      return const_cast<unrestricted_pointer_type> (p);
    }

    template <typename T1>
    static T1*
    static_pointer_cast (pointer_type p)
    {
      return static_cast<T1*> (p);
    }

    template <typename T1>
    static T1*
    dynamic_pointer_cast (pointer_type p)
    {
      return dynamic_cast<T1*> (p);
    }

  public:
    // Allocate memory for an element that will be managed by this
    // pointer.
    //
    static void*
    allocate (std::size_t n)
    {
      return operator new (n);
    }

    // Free memory allocated for an element. This functions is only
    // called if the constructor of the element being created fails.
    // Otherwise, the pointer (or guard) is used to delete the object
    // and free the memory. This behavior is identical to the one
    // used by operator delete overloading.
    //
    static void
    free (void* p)
    {
      operator delete (p);
    }
  };

  // Specialization for std::auto_ptr.
  //
  template <typename T>
  class pointer_traits< std::auto_ptr<T> >
  {
  public:
    static const pointer_kind kind = pk_unique;
    static const bool lazy = false;

    typedef T element_type;
    typedef std::auto_ptr<element_type> pointer_type;
    typedef std::auto_ptr<const element_type> const_pointer_type;
    typedef smart_ptr_guard<pointer_type> guard;

    static element_type*
    get_ptr (const pointer_type& p)
    {
      return p.get ();
    }

    static element_type&
    get_ref (const pointer_type& p)
    {
      return *p;
    }

    static bool
    null_ptr (const pointer_type& p)
    {
      return p.get () == 0;
    }

    // const_pointer_cast() is not provided.
    //

    // Note: transfers ownership.
    //
    template <typename T1>
    static std::auto_ptr<T1>
    static_pointer_cast (pointer_type& p)
    {
      return std::auto_ptr<T1> (static_cast<T1*> (p.release ()));
    }

    // Note: transfers ownership if successful.
    //
    template <typename T1>
    static std::auto_ptr<T1>
    dynamic_pointer_cast (pointer_type& p)
    {
      T1* p1 (dynamic_cast<T1*> (p.get ()));

      if (p1 != 0)
        p.release ();

      return std::auto_ptr<T1> (p1);
    }

  public:
    static void*
    allocate (std::size_t n)
    {
      return operator new (n);
    }

    static void
    free (void* p)
    {
      operator delete (p);
    }
  };

#ifdef ODB_CXX11

  // Specialization for C++11 std::unique_ptr.
  //
  template <typename T, typename D>
  class pointer_traits<std::unique_ptr<T, D>>
  {
  public:
    static const pointer_kind kind = pk_unique;
    static const bool lazy = false;

    typedef T element_type;
    typedef std::unique_ptr<element_type, D> pointer_type;
    typedef std::unique_ptr<const element_type, D> const_pointer_type;
    typedef smart_ptr_guard<pointer_type> guard;

    static element_type*
    get_ptr (const pointer_type& p)
    {
      return p.get ();
    }

    static element_type&
    get_ref (const pointer_type& p)
    {
      return *p;
    }

    static bool
    null_ptr (const pointer_type& p)
    {
      return !p;
    }

    // const_pointer_cast() is not provided.
    //

    // Note: transfers ownership.
    //
    template <typename T1>
    static std::unique_ptr<T1>
    static_pointer_cast (pointer_type& p)
    {
      return std::unique_ptr<T1> (static_cast<T1*> (p.release ()));
    }

    // Note: transfers ownership if successful.
    //
    template <typename T1>
    static std::unique_ptr<T1>
    dynamic_pointer_cast (pointer_type& p)
    {
      T1* p1 (dynamic_cast<T1*> (p.get ()));

      if (p1 != 0)
        p.release ();

      return std::unique_ptr<T1> (p1);
    }

  public:
    static void*
    allocate (std::size_t n)
    {
      return operator new (n);
    }

    static void
    free (void* p)
    {
      operator delete (p);
    }
  };

  // Specialization for C++11 std::shared_ptr.
  //
  template <typename T>
  class pointer_traits<std::shared_ptr<T>>
  {
  public:
    static const pointer_kind kind = pk_shared;
    static const bool lazy = false;

    typedef T element_type;
    typedef std::shared_ptr<element_type> pointer_type;
    typedef std::shared_ptr<const element_type> const_pointer_type;
    typedef typename odb::details::meta::remove_const<element_type>::result
    unrestricted_element_type;
    typedef std::shared_ptr<unrestricted_element_type>
    unrestricted_pointer_type;
    typedef smart_ptr_guard<pointer_type> guard;

    static element_type*
    get_ptr (const pointer_type& p)
    {
      return p.get ();
    }

    static element_type&
    get_ref (const pointer_type& p)
    {
      return *p;
    }

    static bool
    null_ptr (const pointer_type& p)
    {
      return !p;
    }

    static unrestricted_pointer_type
    const_pointer_cast (const pointer_type& p)
    {
      return std::const_pointer_cast<unrestricted_element_type> (p);
    }

    template <typename T1>
    static std::shared_ptr<T1>
    static_pointer_cast (const pointer_type& p)
    {
      return std::static_pointer_cast<T1> (p);
    }

    template <typename T1>
    static std::shared_ptr<T1>
    dynamic_pointer_cast (const pointer_type& p)
    {
      return std::dynamic_pointer_cast<T1> (p);
    }

  public:
    static void*
    allocate (std::size_t n)
    {
      return operator new (n);
    }

    static void
    free (void* p)
    {
      operator delete (p);
    }
  };

  // Specialization for C++11 std::weak_ptr.
  //
  template <typename T>
  class pointer_traits<std::weak_ptr<T>>
  {
  public:
    static const pointer_kind kind = pk_weak;
    static const bool lazy = false;

    typedef T element_type;
    typedef std::weak_ptr<element_type> pointer_type;
    typedef std::shared_ptr<element_type> strong_pointer_type;

    static strong_pointer_type
    lock (const pointer_type& p)
    {
      return p.lock ();
    }
  };

#endif // ODB_CXX11

}

#include <odb/post.hxx>

#endif // ODB_POINTER_TRAITS_HXX