Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
view-result.hxx
Go to the documentation of this file.
1 // file : odb/view-result.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_VIEW_RESULT_HXX
6 #define ODB_VIEW_RESULT_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <cstddef> // std::ptrdiff_t, std::size_t
11 #include <iterator> // iterator categories
12 #include <utility> // std::move
13 
14 #include <odb/forward.hxx>
15 #include <odb/traits.hxx>
16 #include <odb/result.hxx>
17 #include <odb/pointer-traits.hxx>
18 
19 #include <odb/details/config.hxx> // ODB_CXX11
20 
21 namespace odb
22 {
23  template <typename T>
25  {
26  protected:
27  friend class result<T>;
28  friend class result<const T>;
29  friend class result_iterator<T, class_view>;
30  friend class result_iterator<const T, class_view>;
31 
32  // In result_impl, T is always non-const and the same as view_type.
33  //
34  typedef T view_type;
36 
39 
41  : result_impl (conn), begin_ (true), end_ (false), current_ ()
42  {
43  }
44 
45  // To make this work with all kinds of pointers (raw, std::auto_ptr,
46  // shared), we need to make sure we don't make any copies of the
47  // pointer on the return path.
48  //
50  current ();
51 
52  void
54  {
55  current_ = pointer_type ();
56  guard_.release ();
57  }
58 
59  void
60  begin ()
61  {
62  if (begin_)
63  {
64  next ();
65  begin_ = false;
66  }
67  }
68 
69  bool
70  end () const
71  {
72  return end_;
73  }
74 
75  protected:
76  virtual void
77  load (view_type&) = 0;
78 
79  virtual void
80  next () = 0;
81 
82  virtual void
83  cache () = 0;
84 
85  virtual std::size_t
86  size () = 0;
87 
88  protected:
89 #ifdef ODB_CXX11
90  void
92  {
93  current_ = std::move (p);
94  guard_.reset (current_);
95  }
96 
97  void
98  current (pointer_type&& p)
99  {
100  current (p);
101  }
102 #else
103  void
105  {
106  current_ = p;
107  guard_.reset (current_);
108  }
109 #endif
110 
111  bool begin_;
112  bool end_;
113 
114  private:
115  pointer_type current_;
116  typename pointer_traits::guard guard_;
117  };
118 
119  template <typename T>
121  {
122  public:
123  typedef T value_type;
125  typedef value_type* pointer;
126  typedef std::ptrdiff_t difference_type;
127  typedef std::input_iterator_tag iterator_category;
128 
129  // T can be const T while view_type is always non-const.
130  //
132 
134 
135  public:
136  explicit
138  : res_ (res)
139  {
140  }
141 
142  // Input iterator requirements.
143  //
144  public:
145  reference
146  operator* () const
147  {
148  return pointer_traits::get_ref (res_->current ());
149  }
150 
151  // Our value_type is already a pointer so return it instead of
152  // a pointer to it (operator-> will just have to go one deeper
153  // in the latter case).
154  //
155  pointer
156  operator-> () const
157  {
158  return pointer_traits::get_ptr (res_->current ());
159  }
160 
162  operator++ ()
163  {
164  res_->next ();
165  return *this;
166  }
167 
169  operator++ (int)
170  {
171  // All non-end iterators for a result object move together.
172  //
173  res_->next ();
174  return *this;
175  }
176 
177  public:
179  load ()
180  {
181  typename view_traits<T>::pointer_type r (res_->current ());
182  res_->release ();
183  return r;
184  }
185 
186  void
187  load (view_type&);
188 
189  public:
190  bool
192  {
193  return (res_ ? res_->end () : true) == (j.res_ ? j.res_->end () : true);
194  }
195 
196  private:
197  // Use unrestricted pointer traits since that's what is returned by
198  // result_impl.
199  //
200  typedef
203 
204  result_impl_type* res_;
205  };
206 
207  //
208  //
209  template <typename T>
211  {
212  public:
214 
215  // T can be const T while view_type is always non-const.
216  //
219  };
220 }
221 
222 #include <odb/view-result.txx>
223 
224 #include <odb/post.hxx>
225 
226 #endif // ODB_VIEW_RESULT_HXX
view_traits< T >::pointer_type load()
Definition: view-result.hxx:179
view_result_impl< view_type > result_impl_type
Definition: view-result.hxx:218
std::input_iterator_tag iterator_category
Definition: view-result.hxx:127
T view_type
Definition: view-result.hxx:34
view_traits< T >::view_type view_type
Definition: view-result.hxx:131
odb::pointer_traits< pointer_type > pointer_traits
Definition: view-result.hxx:38
bool begin_
Definition: view-result.hxx:111
virtual void next()=0
Definition: traits.hxx:80
access::view_traits< T >::pointer_type pointer_type
Definition: traits.hxx:230
bool equal(result_iterator j) const
Definition: view-result.hxx:191
Definition: pointer-traits.hxx:28
value_type & reference
Definition: view-result.hxx:124
access::view_traits< T >::view_type view_type
Definition: traits.hxx:229
Definition: result.hxx:53
void release()
Definition: view-result.hxx:53
pointer_type & current()
Definition: result.hxx:20
std::ptrdiff_t difference_type
Definition: view-result.hxx:126
Definition: view-result.hxx:24
view_traits< T >::pointer_type value_type
Definition: view-result.hxx:213
virtual void cache()=0
result_iterator(result_impl_type *res=0)
Definition: view-result.hxx:137
Definition: connection.hxx:33
bool end_
Definition: view-result.hxx:112
bool end() const
Definition: view-result.hxx:70
Definition: forward.hxx:129
view_traits< T >::view_type view_type
Definition: view-result.hxx:217
view_result_impl(odb::connection &conn)
Definition: view-result.hxx:40
void current(pointer_type p)
Definition: view-result.hxx:104
value_type * pointer
Definition: view-result.hxx:125
Definition: result.hxx:75
view_traits::pointer_type pointer_type
Definition: view-result.hxx:37
T value_type
Definition: view-result.hxx:123
odb::view_traits< view_type > view_traits
Definition: view-result.hxx:35
virtual std::size_t size()=0
view_result_impl< view_type > result_impl_type
Definition: view-result.hxx:133
virtual void load(view_type &)=0
void begin()
Definition: view-result.hxx:60
Definition: result.hxx:56