aboutsummaryrefslogtreecommitdiff
path: root/odb/lazy-ptr-impl.hxx
blob: f4eeb2364dde3e2576cdb18fc081b5cb2ab96d37 (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
// file      : odb/lazy-ptr-impl.hxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_LAZY_PTR_IMPL_HXX
#define ODB_LAZY_PTR_IMPL_HXX

#include <odb/pre.hxx>

#include <utility> // std::move

#include <odb/forward.hxx> // odb::database
#include <odb/traits.hxx>

#include <odb/details/config.hxx> // ODB_CXX11

namespace odb
{
  struct lazy_ptr_impl_ref
  {
    void* id_;
    database* db_;
    void* loader_;
    void (*free_) (void*);
    void* (*copy_) (const void*);
  };

  class lazy_ptr_base
  {
  public:
    typedef odb::database database_type;

    ~lazy_ptr_base ();
    lazy_ptr_base ();
    lazy_ptr_base (const lazy_ptr_base&);
    lazy_ptr_base (const lazy_ptr_impl_ref&);

    lazy_ptr_base&
    operator= (const lazy_ptr_base&);

    lazy_ptr_base&
    operator= (const lazy_ptr_impl_ref&);

    // C++11 support.
    //
  public:
#ifdef ODB_CXX11
    lazy_ptr_base (lazy_ptr_base&&);

    lazy_ptr_base&
    operator= (lazy_ptr_base&&);
#endif

  public:
    // Reset both the id and database.
    //
    void
    reset ();

    // Reset the id.
    //
    void
    reset_id ();

    void
    swap (lazy_ptr_base&);

    database_type*
    database () const;

    typedef void* lazy_ptr_base::*unspecified_bool_type;
    operator unspecified_bool_type () const
    {
      return db_ != 0 ? &lazy_ptr_base::id_ : 0;
    }

    operator lazy_ptr_impl_ref ();

  protected:
    typedef void (*free_func) (void*);
    typedef void* (*copy_func) (const void*);

    // Makes a copy of id.
    //
    void
    reset_ (database_type*,
            void* loader,
            const void* id,
            free_func, copy_func);

    template <typename T>
    static void
    free (void*);

    template <typename T>
    static void*
    copy (const void*);

    template <typename T, typename DB>
    static typename object_traits<T>::pointer_type
    loader (database_type&, const typename object_traits<T>::id_type&);

  protected:
    void* id_;
    database_type* db_;
    void* loader_;

  private:
    free_func free_;
    copy_func copy_;
  };

  template <typename T>
  class lazy_ptr_impl: public lazy_ptr_base
  {
  public:
    lazy_ptr_impl ();

    template <typename DB, typename ID>
    lazy_ptr_impl (DB&, const ID&);

    lazy_ptr_impl (const lazy_ptr_impl&);

    template <typename Y>
    lazy_ptr_impl (const lazy_ptr_impl<Y>&);

    lazy_ptr_impl (const lazy_ptr_impl_ref&);

    lazy_ptr_impl&
    operator= (const lazy_ptr_impl&);

    template <typename Y>
    lazy_ptr_impl&
    operator= (const lazy_ptr_impl<Y>&);

    lazy_ptr_impl&
    operator= (const lazy_ptr_impl_ref&);

    // C++11 support.
    //
  public:
#ifdef ODB_CXX11
    lazy_ptr_impl (lazy_ptr_impl&&);

    template <typename Y>
    lazy_ptr_impl (lazy_ptr_impl<Y>&&);

    lazy_ptr_impl&
    operator= (lazy_ptr_impl&&);

    template <typename Y>
    lazy_ptr_impl&
    operator= (lazy_ptr_impl<Y>&&);
#endif

  public:
    using lazy_ptr_base::reset;
    using lazy_ptr_base::reset_id;

    template <typename DB, typename ID>
    void
    reset (DB&, const ID&);

    // Reset the id and set the database to the new value.
    //
    template <typename DB>
    void
    reset_db (DB&);

    template <typename ID>
    void
    reset_id (const ID&);

    template <typename O /* = T */>
    typename object_traits<O>::pointer_type
    load (bool reset_id);

    template <typename O /* = T */>
    typename object_traits<O>::id_type
    object_id () const;
  };
}

#include <odb/lazy-ptr-impl.ixx>
#include <odb/lazy-ptr-impl.txx>

#include <odb/post.hxx>

#endif // ODB_LAZY_PTR_IMPL_HXX