type-conversion.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
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 
8 #ifndef SOCI_TYPE_CONVERSION_H_INCLUDED
9 #define SOCI_TYPE_CONVERSION_H_INCLUDED
10 
11 #include "type-conversion-traits.h"
12 #include "into-type.h"
13 #include "use-type.h"
14 // std
15 #include <cassert>
16 #include <cstddef>
17 #include <string>
18 #include <vector>
19 
20 namespace soci
21 {
22 
23 namespace details
24 {
25 
26 // this class is used to ensure correct order of construction
27 // of into_type and use_type elements that use type_conversion
28 
29 template <typename T>
31 {
33 };
34 
35 // Automatically create into_type from a type_conversion
36 
37 template <typename T>
39  : private base_value_holder<T>,
40  public into_type<typename type_conversion<T>::base_type>
41 {
42 public:
44 
46  : into_type<base_type>(details::base_value_holder<T>::val_, ownInd_)
47  , value_(value)
48  , ownInd_()
49  , ind_(ownInd_)
50  {
51  assert(ownInd_ == ind_);
52  }
53 
54  conversion_into_type(T & value, indicator & ind)
55  : into_type<base_type>(details::base_value_holder<T>::val_, ind)
56  , value_(value)
57  , ownInd_(ind) // unused, just keep the pair of indicator(s) consistent
58  , ind_(ind)
59  {
60  assert(ownInd_ == ind_);
61  }
62 
63 
64 private:
66  {
69  }
70 
71  T & value_;
72 
74 
75  // ind_ refers to either ownInd_, or the one provided by the user
76  // in any case, ind_ refers to some valid indicator
77  // and can be used by conversion routines
79 };
80 
81 // Automatically create use_type from a type_conversion
82 
83 template <typename T>
85  : private details::base_value_holder<T>,
86  public use_type<typename type_conversion<T>::base_type>
87 {
88 public:
90 
91  conversion_use_type(T & value, std::string const & name = std::string())
92  : use_type<base_type>(details::base_value_holder<T>::val_, ownInd_, name)
93  , value_(value)
94  , ownInd_()
95  , ind_(ownInd_)
96  , readOnly_(false)
97  {
98  assert(ownInd_ == ind_);
99 
100  // TODO: likely to be removed (SHA: c166625a28f7c907318134f625ff5acea7d9a1f8)
101  //convert_to_base();
102  }
103 
104  conversion_use_type(T const & value, std::string const & name = std::string())
105  : use_type<base_type>(details::base_value_holder<T>::val_, ownInd_, name)
106  , value_(const_cast<T &>(value))
107  , ownInd_()
108  , ind_(ownInd_)
109  , readOnly_(true)
110  {
111  assert(ownInd_ == ind_);
112 
113  // TODO: likely to be removed (SHA: c166625a28f7c907318134f625ff5acea7d9a1f8)
114  //convert_to_base();
115  }
116 
117  conversion_use_type(T & value, indicator & ind,
118  std::string const & name = std::string())
119  : use_type<base_type>(details::base_value_holder<T>::val_, ind, name)
120  , value_(value)
121  , ind_(ind)
122  , readOnly_(false)
123  {
124  // TODO: likely to be removed (SHA: c166625a28f7c907318134f625ff5acea7d9a1f8)
125  //convert_to_base();
126  }
127 
128  conversion_use_type(T const & value, indicator & ind,
129  std::string const & name = std::string())
130  : use_type<base_type>(details::base_value_holder<T>::val_, ind, name)
131  , value_(const_cast<T &>(value))
132  , ind_(ind)
133  , readOnly_(true)
134  {
135  // TODO: likely to be removed (SHA: c166625a28f7c907318134f625ff5acea7d9a1f8)
136  //convert_to_base();
137  }
138 
140  {
141  // NOTE:
142  // readOnly_ flag indicates that use_type object has been generated
143  // based on non-const object passed by user as input argument.
144  // For const objects, this is effectively no-op conversion.
145  // See standard_use_type::post_use() for more details.
146 
147  if (readOnly_ == false)
148  {
151  }
152  }
153 
155  {
158  }
159 
160 private:
161  T & value_;
162 
164 
165  // ind_ refers to either ownInd_, or the one provided by the user
166  // in any case, ind_ refers to some valid indicator
167  // and can be used by conversion routines
169 
170  bool readOnly_;
171 };
172 
173 // this class is used to ensure correct order of construction
174 // of vector based into_type and use_type elements that use type_conversion
175 
176 template <typename T>
178 {
179  base_vector_holder(std::size_t sz = 0) : vec_(sz) {}
180  mutable std::vector<typename type_conversion<T>::base_type> vec_;
181 };
182 
183 // Automatically create a std::vector based into_type from a type_conversion
184 
185 template <typename T>
186 class conversion_into_type<std::vector<T> >
187  : private details::base_vector_holder<T>,
188  public into_type<std::vector<typename type_conversion<T>::base_type> >
189 {
190 public:
191  typedef typename std::vector
192  <
195 
196  conversion_into_type(std::vector<T> & value)
197  : details::base_vector_holder<T>(value.size())
198  , into_type<base_type>(details::base_vector_holder<T>::vec_, ownInd_)
199  , value_(value)
200  , ownInd_()
201  , ind_(ownInd_)
202  {
203  assert(ownInd_ == ind_);
204  }
205 
206  conversion_into_type(std::vector<T> & value, std::vector<indicator> & ind)
207  : details::base_vector_holder<T>(value.size())
208  , into_type<base_type>(details::base_vector_holder<T>::vec_, ind)
209  , value_(value)
210  , ind_(ind)
211  {}
212 
213  virtual std::size_t size() const
214  {
215  // the user might have resized his vector in the meantime
216  // -> synchronize the base-value mirror to have the same size
217 
218  std::size_t const userSize = value_.size();
219  details::base_vector_holder<T>::vec_.resize(userSize);
220  return userSize;
221  }
222 
223  virtual void resize(std::size_t sz)
224  {
225  value_.resize(sz);
226  ind_.resize(sz);
228  }
229 
230 private:
232  {
233  std::size_t const sz = details::base_vector_holder<T>::vec_.size();
234 
235  for (std::size_t i = 0; i != sz; ++i)
236  {
238  details::base_vector_holder<T>::vec_[i], ind_[i], value_[i]);
239  }
240  }
241 
242  std::vector<T> & value_;
243 
244  std::vector<indicator> ownInd_;
245 
246  // ind_ refers to either ownInd_, or the one provided by the user
247  // in any case, ind_ refers to some valid vector of indicators
248  // and can be used by conversion routines
249  std::vector<indicator> & ind_;
250 };
251 
252 
253 // Automatically create a std::vector based use_type from a type_conversion
254 
255 template <typename T>
256 class conversion_use_type<std::vector<T> >
257  : private details::base_vector_holder<T>,
258  public use_type<std::vector<typename type_conversion<T>::base_type> >
259 {
260 public:
261  typedef typename std::vector
262  <
265 
266  conversion_use_type(std::vector<T> & value,
267  std::string const & name=std::string())
268  : details::base_vector_holder<T>(value.size())
269  , use_type<base_type>(
270  details::base_vector_holder<T>::vec_, ownInd_, name)
271  , value_(value)
272  , ownInd_()
273  , ind_(ownInd_)
274  {
275  assert(ownInd_ == ind_);
276  }
277 
278  conversion_use_type(std::vector<T> & value,
279  std::vector<indicator> & ind,
280  std::string const & name = std::string())
281  : details::base_vector_holder<T>(value.size())
282  , use_type<base_type>(
283  details::base_vector_holder<T>::vec_, ind, name)
284  , value_(value)
285  , ind_(ind)
286  {}
287 
288 private:
290  {
291  std::size_t const sz = details::base_vector_holder<T>::vec_.size();
292  value_.resize(sz);
293  ind_.resize(sz);
294  for (std::size_t i = 0; i != sz; ++i)
295  {
297  details::base_vector_holder<T>::vec_[i], value_[i], ind_[i]);
298  }
299  }
300 
302  {
303  std::size_t const sz = value_.size();
305  ind_.resize(sz);
306  for (std::size_t i = 0; i != sz; ++i)
307  {
308  type_conversion<T>::to_base(value_[i],
310  }
311  }
312 
313  std::vector<T> & value_;
314 
315  std::vector<indicator> ownInd_;
316 
317  // ind_ refers to either ownInd_, or the one provided by the user
318  // in any case, ind_ refers to some valid vector of indicators
319  // and can be used by conversion routines
320  std::vector<indicator> & ind_;
321 };
322 
323 template <typename T>
325 {
326  return into_type_ptr(new conversion_into_type<T>(t));
327 }
328 
329 template <typename T>
331 {
332  return into_type_ptr(new conversion_into_type<T>(t, ind));
333 }
334 
335 template <typename T>
336 use_type_ptr do_use(T & t, std::string const & name, user_type_tag)
337 {
338  return use_type_ptr(new conversion_use_type<T>(t, name));
339 }
340 
341 template <typename T>
342 use_type_ptr do_use(T const & t, std::string const & name, user_type_tag)
343 {
344  return use_type_ptr(new conversion_use_type<T>(t, name));
345 }
346 
347 template <typename T>
349  std::string const & name, user_type_tag)
350 {
351  return use_type_ptr(new conversion_use_type<T>(t, ind, name));
352 }
353 
354 template <typename T>
355 use_type_ptr do_use(T const & t, indicator & ind,
356  std::string const & name, user_type_tag)
357 {
358  return use_type_ptr(new conversion_use_type<T>(t, ind, name));
359 }
360 
361 } // namespace details
362 
363 } // namespace soci
364 
365 #endif // SOCI_TYPE_CONVERSION_H_INCLUDED
into_type_ptr do_into(T &t, basic_type_tag)
Definition: into-type.h:143
conversion_into_type(std::vector< T > &value, std::vector< indicator > &ind)
std::vector< typename type_conversion< T >::base_type > base_type
std::vector< typename type_conversion< T >::base_type > base_type
static void to_base(T const &in, base_type &out, indicator &ind)
std::vector< typename type_conversion< T >::base_type > vec_
conversion_into_type(T &value, indicator &ind)
conversion_use_type(std::vector< T > &value, std::vector< indicator > &ind, std::string const &name=std::string())
type_conversion< T >::base_type base_type
type_ptr< into_type_base > into_type_ptr
Definition: into-type.h:47
type_ptr< use_type_base > use_type_ptr
Definition: use-type.h:38
conversion_use_type(T const &value, indicator &ind, std::string const &name=std::string())
type_conversion< T >::base_type val_
static void from_base(base_type const &in, indicator ind, T &out)
conversion_use_type(T &value, std::string const &name=std::string())
use_type_ptr do_use(T &t, std::string const &name, user_type_tag)
type_conversion< T >::base_type base_type
conversion_use_type(T &value, indicator &ind, std::string const &name=std::string())
conversion_use_type(T const &value, std::string const &name=std::string())
conversion_use_type(std::vector< T > &value, std::string const &name=std::string())


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