Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
simple-object-result.hxx
Go to the documentation of this file.
1 // file : odb/simple-object-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_SIMPLE_OBJECT_RESULT_HXX
6 #define ODB_SIMPLE_OBJECT_RESULT_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <cstddef> // std::size_t
11 #include <utility> // std::move
12 
13 #include <odb/forward.hxx>
14 #include <odb/traits.hxx>
15 #include <odb/result.hxx>
16 #include <odb/object-result.hxx>
17 #include <odb/pointer-traits.hxx>
18 
19 #include <odb/details/config.hxx> // ODB_CXX11
20 
21 namespace odb
22 {
23  // Implementation for non-polymorphic objects with object id.
24  //
25  template <typename T>
26  class object_result_impl: public result_impl
27  {
28  protected:
29  // In result_impl, T is always non-const and the same as object_type.
30  //
31  typedef T object_type;
33  typedef typename object_traits::id_type id_type;
34 
37 
38  friend class result<T>;
39  friend class result<const T>;
40  friend class result_iterator<T, class_object>;
41  friend class result_iterator<const T, class_object>;
42  friend class object_result_iterator<T, id_type, false>;
43  friend class object_result_iterator<const T, id_type, false>;
44 
45  protected:
47  : result_impl (conn), begin_ (true), end_ (false), current_ ()
48  {
49  }
50 
51  // To make this work with all kinds of pointers (raw, std::auto_ptr,
52  // shared), we need to make sure we don't make any copies of the
53  // pointer on the return path.
54  //
57  {
58  if (pointer_traits::null_ptr (current_) && !end_)
59  load ();
60 
61  return current_;
62  }
63 
64  void
66  {
67  current_ = pointer_type ();
68  guard_.release ();
69  }
70 
71  void
72  begin ()
73  {
74  if (begin_)
75  {
76  next ();
77  begin_ = false;
78  }
79  }
80 
81  bool
82  end () const
83  {
84  return end_;
85  }
86 
87  protected:
88  // The fetch argument is a hint to the implementation. If it is
89  // false then it means load_id() was already called (and presumably
90  // fetched the data into the object image) and the object image
91  // is still valid (so the implementation doesn't need to fetch
92  // the data again).
93  //
94  virtual void
95  load (object_type&, bool fetch = true) = 0;
96 
97  virtual id_type
98  load_id () = 0;
99 
100  virtual void
101  next () = 0;
102 
103  virtual void
104  cache () = 0;
105 
106  virtual std::size_t
107  size () = 0;
108 
109  protected:
110 #ifdef ODB_CXX11
111  void
112  current (pointer_type& p, bool guard = true)
113  {
114  current_ = std::move (p);
115 
116  if (guard)
117  guard_.reset (current_);
118  else
119  guard_.reset ();
120  }
121 
122  void
123  current (pointer_type&& p, bool guard = true)
124  {
125  current (p, guard);
126  }
127 #else
128  void
129  current (pointer_type p, bool guard = true)
130  {
131  current_ = p;
132 
133  if (guard)
134  guard_.reset (current_);
135  else
136  guard_.reset ();
137  }
138 #endif
139 
140  bool begin_;
141  bool end_;
142 
143  private:
144  void
145  load ();
146 
147  private:
148  pointer_type current_;
149  typename pointer_traits::guard guard_;
150  };
151 
152  template <typename T, typename ID>
153  class object_result_iterator<T, ID, false>
154  {
155  public:
156  // T can be const T while object_type is always non-const.
157  //
160 
162 
163  public:
165  : res_ (res)
166  {
167  }
168 
169  public:
171  load ()
172  {
173  typename object_traits<T>::pointer_type r (res_->current ());
174  res_->release ();
175  return r;
176  }
177 
178  void
179  load (object_type&);
180 
181  id_type
182  id ()
183  {
184  return res_->load_id ();
185  }
186 
187  protected:
189  };
190 }
191 
192 #include <odb/simple-object-result.txx>
193 
194 #include <odb/post.hxx>
195 
196 #endif // ODB_SIMPLE_OBJECT_RESULT_HXX
odb::pointer_traits< pointer_type > pointer_traits
Definition: simple-object-result.hxx:36
bool end() const
Definition: simple-object-result.hxx:82
pointer_type & current()
Definition: simple-object-result.hxx:56
object_result_impl< object_type > result_impl_type
Definition: simple-object-result.hxx:161
object_traits::id_type id_type
Definition: simple-object-result.hxx:33
odb::object_traits< object_type > object_traits
Definition: simple-object-result.hxx:32
result_impl_type * res_
Definition: simple-object-result.hxx:188
bool end_
Definition: simple-object-result.hxx:141
void release()
Definition: simple-object-result.hxx:65
virtual void next()=0
virtual void cache()=0
object_result_impl(odb::connection &conn)
Definition: simple-object-result.hxx:46
id_type id()
Definition: simple-object-result.hxx:182
virtual void load(object_type &, bool fetch=true)=0
Definition: object-result.hxx:24
Definition: pointer-traits.hxx:28
virtual std::size_t size()=0
object_traits< T >::object_type object_type
Definition: simple-object-result.hxx:158
T object_type
Definition: simple-object-result.hxx:31
Definition: object-result.hxx:63
Definition: forward.hxx:123
object_traits< T >::id_type id_type
Definition: simple-object-result.hxx:159
object_traits< T >::pointer_type load()
Definition: simple-object-result.hxx:171
void current(pointer_type p, bool guard=true)
Definition: simple-object-result.hxx:129
Definition: result.hxx:20
access::object_traits< T >::object_type object_type
Definition: traits.hxx:115
object_traits::pointer_type pointer_type
Definition: simple-object-result.hxx:35
Definition: traits.hxx:79
object_result_iterator(result_impl_type *res)
Definition: simple-object-result.hxx:164
void begin()
Definition: simple-object-result.hxx:72
Definition: connection.hxx:33
Definition: result.hxx:75
bool begin_
Definition: simple-object-result.hxx:140
virtual id_type load_id()=0
access::object_traits< T >::pointer_type pointer_type
Definition: traits.hxx:116
Definition: result.hxx:56