aboutsummaryrefslogtreecommitdiff
path: root/odb/view-result.hxx
blob: 25fd0f5d8fb030fd685fe4d8c3860368119668e1 (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
// file      : odb/view-result.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_VIEW_RESULT_HXX
#define ODB_VIEW_RESULT_HXX

#include <odb/pre.hxx>

#include <cstddef>  // std::ptrdiff_t, std::size_t
#include <iterator> // iterator categories

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

#include <odb/details/shared-ptr.hxx>

namespace odb
{
  template <typename T>
  class result_impl<T, class_view>: public details::shared_base
  {
  public:
    virtual
    ~result_impl ();

  protected:
    friend class result<T, class_view>;
    friend class result<const T, class_view>;
    friend class result_iterator<T, class_view>;
    friend class result_iterator<const T, class_view>;

    typedef odb::database database_type;

    // In result_impl, T is always non-const and the same as view_type.
    //
    typedef T view_type;
    typedef odb::view_traits<view_type> view_traits;

    typedef typename view_traits::pointer_type pointer_type;
    typedef odb::pointer_traits<pointer_type> pointer_traits;

    result_impl (database_type& db)
        : begin_ (true), end_ (false), db_ (db), current_ ()
    {
    }

    database_type&
    database () const
    {
      return db_;
    }

    // To make this work with all kinds of pointers (raw, std::auto_ptr,
    // shared), we need to make sure we don't make any copies of the
    // pointer on the return path.
    //
    pointer_type&
    current ();

    void
    release ()
    {
      current_ = pointer_type ();
      guard_.release ();
    }

    void
    begin ()
    {
      if (begin_)
      {
        next ();
        begin_ = false;
      }
    }

    bool
    end () const
    {
      return end_;
    }

  protected:
    virtual void
    load (view_type&) = 0;

    virtual void
    next () = 0;

    virtual void
    cache () = 0;

    virtual std::size_t
    size () = 0;

  protected:
    void
    current (pointer_type p)
    {
      current_ = p;
      guard_.reset (current_);
    }

    bool begin_;
    bool end_;

  private:
    database_type& db_;
    pointer_type current_;
    typename pointer_traits::guard guard_;
  };

  template <typename T>
  class result_iterator<T, class_view>
  {
  public:
    typedef T value_type;
    typedef value_type& reference;
    typedef value_type* pointer;
    typedef std::ptrdiff_t difference_type;
    typedef std::input_iterator_tag iterator_category;

    // T can be const T while view_type is always non-const.
    //
    typedef typename view_traits<T>::view_type view_type;

    typedef result_impl<view_type, class_view> result_impl_type;

  public:
    explicit
    result_iterator (result_impl_type* res = 0)
        : res_ (res)
    {
    }

    // Input iterator requirements.
    //
  public:
    reference
    operator* () const
    {
      return pointer_traits::get_ref (res_->current ());
    }

    // Our value_type is already a pointer so return it instead of
    // a pointer to it (operator-> will just have to go one deeper
    // in the latter case).
    //
    pointer
    operator-> () const
    {
      return pointer_traits::get_ptr (res_->current ());
    }

    result_iterator&
    operator++ ()
    {
      res_->next ();
      return *this;
    }

    result_iterator
    operator++ (int)
    {
      // All non-end iterators for a result object move together.
      //
      res_->next ();
      return *this;
    }

  public:
    typename view_traits<T>::pointer_type
    load ()
    {
      typename view_traits<T>::pointer_type r (res_->current ());
      res_->release ();
      return r;
    }

    void
    load (view_type&);

  public:
    bool
    equal (result_iterator j) const
    {
      return  (res_ ? res_->end () : true) == (j.res_ ? j.res_->end () : true);
    }

  private:
    // Use unrestricted pointer traits since that's what is returned by
    // result_impl.
    //
    typedef
    odb::pointer_traits<typename view_traits<view_type>::pointer_type>
    pointer_traits;

    result_impl_type* res_;
  };

  // Input iterator requirements.
  //
  template <typename T>
  inline bool
  operator== (result_iterator<T, class_view> i,
              result_iterator<T, class_view> j)
  {
    return i.equal (j);
  }

  template <typename T>
  inline bool
  operator!= (result_iterator<T, class_view> i,
              result_iterator<T, class_view> j)
  {
    return !i.equal (j);
  }

  //
  //
  template <typename T>
  class result<T, class_view>
  {
  public:
    typedef typename view_traits<T>::pointer_type value_type;
    typedef value_type*       pointer;
    typedef const value_type* const_pointer;
    typedef value_type&       reference;
    typedef const value_type& const_reference;

    typedef result_iterator<T, class_view> iterator;

    typedef std::size_t    size_type;
    typedef std::ptrdiff_t difference_type;

    // T can be const T while view_type is always non-const.
    //
    typedef typename view_traits<T>::view_type view_type;
    typedef result_impl<view_type, class_view> result_impl_type;

  public:
    result ()
    {
    }

    explicit
    result (details::shared_ptr<result_impl_type> impl)
        : impl_ (impl)
    {
    }

    // Copying or assignment of a result object leads to one instance
    // being an alias for another. Think of copying a result as copying
    // a file handle -- the file you access through either of them is
    // still the same.
    //
  public:
    result (const result& r)
        : impl_ (r.impl_)
    {
    }

    result&
    operator= (const result& r)
    {
      if (impl_ != r.impl_)
        impl_ = r.impl_;

      return *this;
    }

    // Conversion from result<T> to result<const T>.
    //
    template <typename UT>
    result (const result<UT, class_view>& r)
        //
        // If you get a compiler error pointing to the line below saying
        // that the impl_ member is inaccessible, then you are most likely
        // trying to perform an illegal result conversion, for example,
        // from result<const obj> to result<obj>.
        //
        : impl_ (r.impl_)
    {
    }

    template <typename UT>
    result&
    operator= (const result<UT, class_view>& r)
    {
      // If you get a compiler error pointing to the line below saying
      // that the impl_ member is inaccessible, then you are most likely
      // trying to perform an illegal result conversion, for example,
      // from result<const obj> to result<obj>.
      //
      if (impl_ != r.impl_)
        impl_ = r.impl_;

      return *this;
    }

    void
    swap (result& r)
    {
      // @@ add swap() to shared_ptr.
      //
      details::shared_ptr<result_impl_type> p (impl_);
      impl_ = r.impl_;
      r.impl_ = p;
    }

  public:
    iterator
    begin ()
    {
      if (impl_)
        impl_->begin ();

      return iterator (impl_.get ());
    }

    iterator
    end ()
    {
      return iterator ();
    }

    // Cache the result instead of fetching the data from the database
    // one view at a time. This is necessary if you plan on performing
    // database operations while iterating over the result.
    //
  public:
    void
    cache ()
    {
      if (impl_)
        impl_->cache ();
    }

  public:
    bool
    empty () const
    {
      if (impl_ == 0)
        return true;

      impl_->begin ();
      return impl_->end ();
    }

    // Size is only known in cached results.
    //
    size_type
    size () const
    {
      return impl_ ? impl_->size () : 0;
    }

  private:
    friend class result<const T>;

    details::shared_ptr<result_impl_type> impl_;
  };
}

#include <odb/view-result.txx>

#include <odb/post.hxx>

#endif // ODB_VIEW_RESULT_HXX