rowset.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006-2008 Mateusz Loskot
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #ifndef SOCI_ROWSET_H_INCLUDED
8 #define SOCI_ROWSET_H_INCLUDED
9 
10 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
11 
12 #include "statement.h"
13 // std
14 #include <iterator>
15 #include <memory>
16 
17 namespace soci
18 {
19 
20 //
21 // rowset iterator of input category.
22 //
23 template <typename T>
25 {
26 public:
27 
28  // Standard iterator traits
29 
30  typedef std::input_iterator_tag iterator_category;
31  typedef T value_type;
32  typedef T * pointer;
33  typedef T & reference;
34  typedef ptrdiff_t difference_type;
35 
36  // Constructors
37 
39  : st_(0), define_(0)
40  {}
41 
42  rowset_iterator(statement & st, T & define)
43  : st_(&st), define_(&define)
44  {
45  assert(0 != st_);
46  assert(0 != define_);
47  assert(0 != st_->get_backend());
48 
49  // Fetch first row to properly initialize iterator
50  ++(*this);
51  }
52 
53  // Access operators
54 
55  reference operator*() const
56  {
57  return (*define_);
58  }
59 
60  pointer operator->() const
61  {
62  return &(operator*());
63  }
64 
65  // Iteration operators
66 
68  {
69  // Fetch next row from dataset
70 
71  if (st_->fetch() == false)
72  {
73  // Set iterator to non-derefencable state (pass-the-end)
74  st_ = 0;
75  define_ = 0;
76  }
77 
78  return (*this);
79  }
80 
82  {
83  rowset_iterator tmp(*this);
84  ++(*this);
85  return tmp;
86  }
87 
88  // Comparison operators
89 
90  bool operator==(rowset_iterator const & rhs) const
91  {
92  return (st_== rhs.st_ && define_ == rhs.define_);
93  }
94 
95  bool operator!=(rowset_iterator const & rhs) const
96  {
97  return ((*this == rhs) == false);
98  }
99 
100 private:
101 
103  T * define_;
104 
105 }; // class rowset_iterator
106 
107 namespace details
108 {
109 
110 //
111 // Implementation of rowset
112 //
113 template <typename T>
115 {
116 public:
117 
119 
121  : refs_(1), st_(new statement(prep)), define_(new T())
122  {
123  assert(0 != st_.get());
124  assert(0 != define_.get());
125 
127  st_->execute();
128  }
129 
130  void incRef()
131  {
132  ++refs_;
133  }
134 
135  void decRef()
136  {
137  if (--refs_ == 0)
138  {
139  delete this;
140  }
141  }
142 
143  iterator begin() const
144  {
145  // No ownership transfer occurs here
146  return iterator(*st_, *define_);
147  }
148 
149  iterator end() const
150  {
151  return iterator();
152  }
153 
154 private:
155 
156  unsigned int refs_;
157 
158  const std::auto_ptr<statement> st_;
159  const std::auto_ptr<T> define_;
160 
161  // Non-copyable
162  rowset_impl(rowset_impl const &);
163  rowset_impl & operator=(rowset_impl const &);
164 
165 }; // class rowset_impl
166 
167 } // namespace details
168 
169 
170 //
171 // rowset is a thin wrapper on statement and provides access to STL-like input iterator.
172 // The rowset_iterator can be used to easily loop through statement results and
173 // use STL algorithms accepting input iterators.
174 //
175 template <typename T = soci::row>
176 class rowset
177 {
178 public:
179 
180  typedef T value_type;
183 
184  // this is a conversion constructor
186  : pimpl_(new details::rowset_impl<T>(prep))
187  {
188  assert(0 != pimpl_);
189  }
190 
191  rowset(rowset const & other)
192  : pimpl_(other.pimpl_)
193  {
194  assert(0 != pimpl_);
195 
196  pimpl_->incRef();
197  }
198 
200  {
201  assert(0 != pimpl_);
202 
203  pimpl_->decRef();
204  }
205 
206  rowset& operator=(rowset const& rhs)
207  {
208  assert(0 != pimpl_);
209  assert(0 != rhs.pimpl_);
210 
211  if (&rhs != this)
212  {
213  rhs.pimpl_->incRef();
214  pimpl_->decRef();
215  pimpl_ = rhs.pimpl_;
216  }
217  return *this;
218  }
219 
220  const_iterator begin() const
221  {
222  assert(0 != pimpl_);
223 
224  return pimpl_->begin();
225  }
226 
227  const_iterator end() const
228  {
229  assert(0 != pimpl_);
230 
231  return pimpl_->end();
232  }
233 
234 private:
235 
236  // Pointer to implementation - the body
238 
239 }; // class rowset
240 
241 } // namespace soci
242 
243 #endif // SOCI_ROWSET_H_INCLUDED
bool operator==(rowset_iterator const &rhs) const
Definition: rowset.h:90
const_iterator begin() const
Definition: rowset.h:220
details::into_container< T, details::no_indicator > into(T &t)
Definition: into.h:51
T value_type
Definition: rowset.h:180
ptrdiff_t difference_type
Definition: rowset.h:34
rowset(rowset const &other)
Definition: rowset.h:191
rowset_iterator(statement &st, T &define)
Definition: rowset.h:42
details::statement_backend * get_backend()
Definition: statement.h:243
iterator begin() const
Definition: rowset.h:143
statement * st_
Definition: rowset.h:102
rowset(details::prepare_temp_type const &prep)
Definition: rowset.h:185
rowset_impl(details::prepare_temp_type const &prep)
Definition: rowset.h:120
const_iterator end() const
Definition: rowset.h:227
void exchange_for_rowset(details::into_container< T, Indicator > const &ic)
Definition: statement.h:231
rowset_iterator operator++(int)
Definition: rowset.h:81
unsigned int refs_
Definition: rowset.h:156
details::rowset_impl< T > * pimpl_
Definition: rowset.h:237
bool operator!=(rowset_iterator const &rhs) const
Definition: rowset.h:95
rowset_iterator & operator++()
Definition: rowset.h:67
rowset_iterator< T > iterator
Definition: rowset.h:118
const std::auto_ptr< T > define_
Definition: rowset.h:159
pointer operator->() const
Definition: rowset.h:60
reference operator*() const
Definition: rowset.h:55
std::input_iterator_tag iterator_category
Definition: rowset.h:30
rowset_iterator< T > iterator
Definition: rowset.h:181
rowset_iterator< T > const_iterator
Definition: rowset.h:182
iterator end() const
Definition: rowset.h:149
rowset & operator=(rowset const &rhs)
Definition: rowset.h:206
const std::auto_ptr< statement > st_
Definition: rowset.h:158
bool execute(bool withDataExchange=false)
Definition: statement.h:208


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41