Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
polymorphic-object-result.hxx
Go to the documentation of this file.
1 // file : odb/polymorphic-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_POLYMORPHIC_OBJECT_RESULT_HXX
6 #define ODB_POLYMORPHIC_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 polymorphic objects with object id.
24  //
25  template <typename T>
26  class polymorphic_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  typedef typename object_traits::root_type root_type;
40  typedef typename root_traits::discriminator_type discriminator_type;
41 
42  friend class result<T>;
43  friend class result<const T>;
44  friend class result_iterator<T, class_object>;
45  friend class result_iterator<const T, class_object>;
46  friend class object_result_iterator<T, id_type, true>;
47  friend class object_result_iterator<const T, id_type, true>;
48 
49  protected:
51  : result_impl (conn), begin_ (true), end_ (false), current_ ()
52  {
53  }
54 
55  // To make this work with all kinds of pointers (raw, std::auto_ptr,
56  // shared), we need to make sure we don't make any copies of the
57  // pointer on the return path.
58  //
61  {
62  if (pointer_traits::null_ptr (current_) && !end_)
63  load ();
64 
65  return current_;
66  }
67 
68  void
70  {
71  current_ = pointer_type ();
72  guard_.release ();
73  }
74 
75  void
76  begin ()
77  {
78  if (begin_)
79  {
80  next ();
81  begin_ = false;
82  }
83  }
84 
85  bool
86  end () const
87  {
88  return end_;
89  }
90 
91  protected:
92  // The fetch argument is a hint to the implementation. If it is
93  // false then it means load_id() was already called (and presumably
94  // fetched the data into the object image) and the object image
95  // is still valid (so the implementation doesn't need to fetch
96  // the data again).
97  //
98  // The load() signature differs from the non-polymorphic cases in
99  // that we pass a pointer to object instead of a reference. The
100  // object is only passed if the user requests loading into an
101  // existing instance. Otherwise, we pass NULL and load() is
102  // responsible for creating the object of a correct dynamic
103  // type and managing the object cache insertion.
104  //
105  virtual void
106  load (object_type*, bool fetch = true) = 0;
107 
108  virtual id_type
109  load_id () = 0;
110 
111  virtual discriminator_type
112  load_discriminator () = 0;
113 
114  virtual void
115  next () = 0;
116 
117  virtual void
118  cache () = 0;
119 
120  virtual std::size_t
121  size () = 0;
122 
123  protected:
124 #ifdef ODB_CXX11
125  void
126  current (pointer_type& p, bool guard = true)
127  {
128  current_ = std::move (p);
129 
130  if (guard)
131  guard_.reset (current_);
132  else
133  guard_.reset ();
134  }
135 
136  void
137  current (pointer_type&& p, bool guard = true)
138  {
139  current (p, guard);
140  }
141 #else
142  void
143  current (pointer_type p, bool guard = true)
144  {
145  current_ = p;
146 
147  if (guard)
148  guard_.reset (current_);
149  else
150  guard_.reset ();
151  }
152 #endif
153 
154  bool begin_;
155  bool end_;
156 
157  private:
158  void
159  load ();
160 
161  private:
162  pointer_type current_;
163  typename pointer_traits::guard guard_;
164  };
165 
166  template <typename T, typename ID>
167  class object_result_iterator<T, ID, true>
168  {
169  public:
170  // T can be const T while object_type is always non-const.
171  //
177 
179 
180  public:
182  : res_ (res)
183  {
184  }
185 
186  public:
188  load ()
189  {
190  typename object_traits<T>::pointer_type r (res_->current ());
191  res_->release ();
192  return r;
193  }
194 
195  void
196  load (object_type&);
197 
198  id_type
199  id ()
200  {
201  return res_->load_id ();
202  }
203 
204  discriminator_type
206  {
207  return res_->load_discriminator ();
208  }
209 
210  protected:
212  };
213 }
214 
215 #include <odb/polymorphic-object-result.txx>
216 
217 #include <odb/post.hxx>
218 
219 #endif // ODB_POLYMORPHIC_OBJECT_RESULT_HXX
odb::pointer_traits< pointer_type > pointer_traits
Definition: polymorphic-object-result.hxx:36
void begin()
Definition: polymorphic-object-result.hxx:76
object_traits::id_type id_type
Definition: polymorphic-object-result.hxx:33
bool end() const
Definition: polymorphic-object-result.hxx:86
object_result_iterator(result_impl_type *res)
Definition: polymorphic-object-result.hxx:181
void release()
Definition: polymorphic-object-result.hxx:69
object_traits< T >::root_type root_type
Definition: polymorphic-object-result.hxx:174
object_traits::pointer_type pointer_type
Definition: polymorphic-object-result.hxx:35
object_traits< T >::object_type object_type
Definition: polymorphic-object-result.hxx:172
Definition: pointer-traits.hxx:28
object_traits::root_type root_type
Definition: polymorphic-object-result.hxx:38
odb::object_traits< object_type > object_traits
Definition: polymorphic-object-result.hxx:32
virtual void load(object_type *, bool fetch=true)=0
Definition: object-result.hxx:63
Definition: forward.hxx:123
polymorphic_object_result_impl(odb::connection &conn)
Definition: polymorphic-object-result.hxx:50
bool begin_
Definition: polymorphic-object-result.hxx:154
Definition: result.hxx:20
access::object_traits< T >::object_type object_type
Definition: traits.hxx:115
discriminator_type discriminator()
Definition: polymorphic-object-result.hxx:205
odb::object_traits< root_type > root_traits
Definition: polymorphic-object-result.hxx:39
Definition: traits.hxx:79
result_impl_type * res_
Definition: polymorphic-object-result.hxx:211
Definition: connection.hxx:33
id_type id()
Definition: polymorphic-object-result.hxx:199
root_traits::discriminator_type discriminator_type
Definition: polymorphic-object-result.hxx:40
object_traits< T >::pointer_type load()
Definition: polymorphic-object-result.hxx:188
object_traits< root_type >::discriminator_type discriminator_type
Definition: polymorphic-object-result.hxx:176
virtual discriminator_type load_discriminator()=0
pointer_type & current()
Definition: polymorphic-object-result.hxx:60
polymorphic_object_result_impl< object_type > result_impl_type
Definition: polymorphic-object-result.hxx:178
object_traits< T >::id_type id_type
Definition: polymorphic-object-result.hxx:173
virtual std::size_t size()=0
Definition: result.hxx:75
void current(pointer_type p, bool guard=true)
Definition: polymorphic-object-result.hxx:143
Definition: object-result.hxx:27
access::object_traits< T >::pointer_type pointer_type
Definition: traits.hxx:116
T object_type
Definition: polymorphic-object-result.hxx:31
Definition: result.hxx:56
bool end_
Definition: polymorphic-object-result.hxx:155