Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
no-id-object-result.hxx
Go to the documentation of this file.
1 // file : odb/no-id-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_NO_ID_OBJECT_RESULT_HXX
6 #define ODB_NO_ID_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 objects without object id (always non-polymorphic).
24  //
25  template <typename T>
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 
36 
37  friend class result<T>;
38  friend class result<const T>;
39  friend class result_iterator<T, class_object>;
40  friend class result_iterator<const T, class_object>;
41  friend class object_result_iterator<T, void, false>;
42  friend class object_result_iterator<const T, void, false>;
43 
44  protected:
46  : result_impl (conn), begin_ (true), end_ (false), current_ ()
47  {
48  }
49 
50  // To make this work with all kinds of pointers (raw, std::auto_ptr,
51  // shared), we need to make sure we don't make any copies of the
52  // pointer on the return path.
53  //
56  {
57  if (pointer_traits::null_ptr (current_) && !end_)
58  load ();
59 
60  return current_;
61  }
62 
63  void
65  {
66  current_ = pointer_type ();
67  guard_.release ();
68  }
69 
70  void
71  begin ()
72  {
73  if (begin_)
74  {
75  next ();
76  begin_ = false;
77  }
78  }
79 
80  bool
81  end () const
82  {
83  return end_;
84  }
85 
86  protected:
87  virtual void
88  load (object_type&) = 0;
89 
90  virtual void
91  next () = 0;
92 
93  virtual void
94  cache () = 0;
95 
96  virtual std::size_t
97  size () = 0;
98 
99  protected:
100 #ifdef ODB_CXX11
101  void
102  current (pointer_type& p)
103  {
104  current_ = std::move (p);
105  guard_.reset (current_);
106  }
107 
108  void
109  current (pointer_type&& p)
110  {
111  current (p);
112  }
113 #else
114  void
116  {
117  current_ = p;
118  guard_.reset (current_);
119  }
120 #endif
121 
122  bool begin_;
123  bool end_;
124 
125  private:
126  void
127  load ();
128 
129  private:
130  pointer_type current_;
131  typename pointer_traits::guard guard_;
132  };
133 
134  template <typename T>
135  class object_result_iterator<T, void, false>
136  {
137  public:
138  // T can be const T while object_type is always non-const.
139  //
141 
143 
144  public:
146  : res_ (res)
147  {
148  }
149 
150  public:
152  load ()
153  {
154  typename object_traits<T>::pointer_type r (res_->current ());
155  res_->release ();
156  return r;
157  }
158 
159  void
161  {
162  // Objects without ids are not stored in session cache.
163  //
164  if (!res_->end ())
165  res_->load (obj);
166  }
167 
168  protected:
170  };
171 }
172 
173 #include <odb/no-id-object-result.txx>
174 
175 #include <odb/post.hxx>
176 
177 #endif // ODB_NO_ID_OBJECT_RESULT_HXX
odb::pointer_traits< pointer_type > pointer_traits
Definition: no-id-object-result.hxx:35
T object_type
Definition: no-id-object-result.hxx:31
pointer_type & current()
Definition: no-id-object-result.hxx:55
object_traits< T >::object_type object_type
Definition: no-id-object-result.hxx:140
result_impl_type * res_
Definition: no-id-object-result.hxx:169
Definition: pointer-traits.hxx:28
bool begin_
Definition: no-id-object-result.hxx:122
Definition: object-result.hxx:63
Definition: forward.hxx:123
Definition: result.hxx:20
access::object_traits< T >::object_type object_type
Definition: traits.hxx:115
object_result_iterator(result_impl_type *res)
Definition: no-id-object-result.hxx:145
no_id_object_result_impl(odb::connection &conn)
Definition: no-id-object-result.hxx:45
virtual std::size_t size()=0
Definition: traits.hxx:79
object_traits::pointer_type pointer_type
Definition: no-id-object-result.hxx:34
Definition: connection.hxx:33
bool end() const
Definition: no-id-object-result.hxx:81
object_traits< T >::pointer_type load()
Definition: no-id-object-result.hxx:152
no_id_object_result_impl< object_type > result_impl_type
Definition: no-id-object-result.hxx:142
virtual void load(object_type &)=0
bool end_
Definition: no-id-object-result.hxx:123
void current(pointer_type p)
Definition: no-id-object-result.hxx:115
odb::object_traits< object_type > object_traits
Definition: no-id-object-result.hxx:32
Definition: result.hxx:75
Definition: no-id-object-result.hxx:26
void load(object_type &obj)
Definition: no-id-object-result.hxx:160
void begin()
Definition: no-id-object-result.hxx:71
access::object_traits< T >::pointer_type pointer_type
Definition: traits.hxx:116
void release()
Definition: no-id-object-result.hxx:64
Definition: result.hxx:56