Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
result.hxx
Go to the documentation of this file.
1 // file : odb/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_RESULT_HXX
6 #define ODB_RESULT_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <cstddef> // std::ptrdiff_t, std::size_t
11 
12 #include <odb/forward.hxx> // odb::core
13 #include <odb/traits.hxx>
14 
15 #include <odb/details/export.hxx>
16 #include <odb/details/shared-ptr.hxx>
17 
18 namespace odb
19 {
20  class LIBODB_EXPORT result_impl: public details::shared_base
21  {
22  public:
23  virtual
24  ~result_impl ();
25 
26  virtual void
27  invalidate () = 0;
28 
29  protected:
31 
32  protected:
35 
36  // Doubly-linked list of results.
37  //
38  // prev_ == 0 means we are the first element.
39  // next_ == 0 means we are the last element.
40  // next_ == this means we are not on the list.
41  //
42  protected:
43  friend class connection;
44 
45  void
46  list_remove ();
47 
50  };
51 
52  template <typename T, class_kind kind>
53  class result_base;
54 
55  template <typename T, class_kind kind = class_traits<T>::kind>
57 
58  // Input iterator requirements.
59  //
60  template <typename T, class_kind kind>
61  inline bool
63  {
64  return i.equal (j);
65  }
66 
67  template <typename T, class_kind kind>
68  inline bool
70  {
71  return !i.equal (j);
72  }
73 
74  template <typename T>
75  class result: result_base<T, class_traits<T>::kind>
76  {
77  public:
79 
81 
82  typedef typename base::value_type value_type;
83  typedef value_type* pointer;
84  typedef const value_type* const_pointer;
86  typedef const value_type& const_reference;
87 
89 
90  typedef std::size_t size_type;
91  typedef std::ptrdiff_t difference_type;
92 
93  typedef typename base::result_impl_type result_impl_type;
94 
95  public:
96  result ()
97  {
98  }
99 
100  explicit
101  result (details::shared_ptr<result_impl_type> impl)
102  : impl_ (impl)
103  {
104  }
105 
106  // Copying or assignment of a result instance leads to one instance
107  // being an alias for another. Think of copying a result as copying
108  // a file handle -- the file you access through either of them is
109  // still the same.
110  //
111  public:
112  result (const result& r)
113  : impl_ (r.impl_)
114  {
115  }
116 
117  result&
118  operator= (const result& r)
119  {
120  if (impl_ != r.impl_)
121  impl_ = r.impl_;
122 
123  return *this;
124  }
125 
126  // Conversion from result<T> to result<const T>.
127  //
128  template <typename UT>
129  result (const result<UT>& r)
130  //
131  // If you get a compiler error pointing to the line below saying
132  // that the impl_ member is inaccessible, then you are most likely
133  // trying to perform an illegal result conversion, for example,
134  // from result<const obj> to result<obj>.
135  //
136  : impl_ (r.impl_)
137  {
138  }
139 
140  template <typename UT>
141  result&
143  {
144  // If you get a compiler error pointing to the line below saying
145  // that the impl_ member is inaccessible, then you are most likely
146  // trying to perform an illegal result conversion, for example,
147  // from result<const obj> to result<obj>.
148  //
149  if (impl_ != r.impl_)
150  impl_ = r.impl_;
151 
152  return *this;
153  }
154 
155  void
157  {
158  // @@ add swap() to shared_ptr.
159  //
160  details::shared_ptr<result_impl_type> p (impl_);
161  impl_ = r.impl_;
162  r.impl_ = p;
163  }
164 
165  public:
166  iterator
167  begin ()
168  {
169  if (impl_)
170  impl_->begin ();
171 
172  return iterator (impl_.get ());
173  }
174 
175  iterator
176  end ()
177  {
178  return iterator ();
179  }
180 
181  // Cache the result instead of fetching the data from the database
182  // one row at a time. This is necessary if you plan on performing
183  // database operations while iterating over the result.
184  //
185  public:
186  void
187  cache ()
188  {
189  if (impl_)
190  impl_->cache ();
191  }
192 
193  public:
194  bool
195  empty () const
196  {
197  if (impl_ == 0)
198  return true;
199 
200  impl_->begin ();
201  return impl_->end ();
202  }
203 
204  // Size is only known in cached results.
205  //
206  size_type
207  size () const
208  {
209  return impl_ ? impl_->size () : 0;
210  }
211 
212  private:
213  friend class result<const T>;
214 
215  details::shared_ptr<result_impl_type> impl_;
216  };
217 
218  namespace common
219  {
220  using odb::result;
221  using odb::result_iterator;
222  }
223 }
224 
225 #include <odb/post.hxx>
226 
227 #endif // ODB_RESULT_HXX
base::result_impl_type result_impl_type
Definition: result.hxx:93
void swap(result &r)
Definition: result.hxx:156
const value_type * const_pointer
Definition: result.hxx:84
void cache()
Definition: result.hxx:187
result()
Definition: result.hxx:96
result(const result &r)
Definition: result.hxx:112
connection & conn_
Definition: result.hxx:34
base::value_type value_type
Definition: result.hxx:82
value_type & reference
Definition: result.hxx:85
class_kind
Definition: traits.hxx:77
Definition: result.hxx:53
result(const result< UT > &r)
Definition: result.hxx:129
Definition: result.hxx:20
result(details::shared_ptr< result_impl_type > impl)
Definition: result.hxx:101
std::size_t size_type
Definition: result.hxx:90
database & db_
Definition: result.hxx:33
result & operator=(const result &r)
Definition: result.hxx:118
bool operator!=(const lazy_ptr< T > &, const lazy_ptr< Y > &)
result_iterator< T, kind > iterator
Definition: result.hxx:88
result_impl * prev_
Definition: result.hxx:48
Definition: database.hxx:38
Definition: connection.hxx:33
std::ptrdiff_t difference_type
Definition: result.hxx:91
iterator begin()
Definition: result.hxx:167
iterator end()
Definition: result.hxx:176
size_type size() const
Definition: result.hxx:207
const value_type & const_reference
Definition: result.hxx:86
result_base< T, kind > base
Definition: result.hxx:80
value_type * pointer
Definition: result.hxx:83
static const class_kind kind
Definition: result.hxx:78
Definition: result.hxx:75
bool empty() const
Definition: result.hxx:195
bool operator==(const lazy_ptr< T > &, const lazy_ptr< Y > &)
result_impl * next_
Definition: result.hxx:49
Definition: traits.hxx:85
Definition: result.hxx:56