pg_async
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
protocol_io_traits.inl
Go to the documentation of this file.
1 
8 #ifndef LIB_PG_ASYNC_INCLUDE_TIP_DB_PG_PROTOCOL_IO_TRAITS_INL_
9 #define LIB_PG_ASYNC_INCLUDE_TIP_DB_PG_PROTOCOL_IO_TRAITS_INL_
10 
13 
14 #include <tip/util/endian.hpp>
15 #include <algorithm>
16 #include <cassert>
17 #include <iterator>
18 
19 namespace tip {
20 namespace db {
21 namespace pg {
22 namespace io {
23 namespace detail {
24 
25 template <typename T>
26 template <typename InputIterator >
27 InputIterator
28 binary_data_parser<T, INTEGRAL>::operator()(InputIterator begin, InputIterator end)
29 {
30  typedef std::iterator_traits< InputIterator > iter_traits;
31  typedef typename iter_traits::value_type iter_value_type;
32  static_assert(std::is_same< iter_value_type, byte >::type::value,
33  "Input iterator must be over a char container");
34  assert( (end - begin) >= size() && "Buffer size is insufficient" );
35  value_type tmp(0);
36  char* p = reinterpret_cast<char*>(&tmp);
37  char* e = p + size();
38  while (p != e && begin != end) {
39  *p++ = *begin++;
40  }
41  tmp = util::endian::big_to_native(tmp);
42  std::swap( base_type::value, tmp );
43  return begin;
44 }
45 
46 template < typename T >
47 bool
49 {
50  return (*this)( std::back_inserter(buffer) );
51 }
52 
53 template < typename T >
54 template < typename OutputIterator >
55 bool
57 {
58 // typedef OutputIterator iterator_type;
59 // typedef std::iterator_traits< iterator_type > iter_traits;
60 // typedef typename iter_traits::value_type iter_value_type;
61 // static_assert(std::is_same< iter_value_type, byte >::value,
62 // "Output iterator must be over a char container");
63 
64  T tmp = util::endian::native_to_big(base_type::value);
65  char const* p = reinterpret_cast<char const*>(&tmp);
66  char const* e = p + size();
67  std::copy(p, e, out);
68  return true;
69 }
70 
71 
72 } // namespace detail
73 
74 template < typename T >
75 template < typename InputIterator >
76 InputIterator
78  (InputIterator begin, InputIterator end)
79 {
80  typedef std::iterator_traits< InputIterator > iter_traits;
81  typedef typename iter_traits::value_type iter_value_type;
82  static_assert(std::is_same< iter_value_type, byte >::type::value,
83  "Input iterator must be over a char container");
84 
85  std::istringstream is(std::string(begin, end));
86  is >> base_type::value;
87  begin += size();
88  return begin;
89 }
90 
91 template < typename InputIterator >
92 InputIterator
94  (InputIterator begin, InputIterator end)
95 {
96  typedef InputIterator iterator_type;
97  typedef std::iterator_traits< iterator_type > iter_traits;
98  typedef typename iter_traits::value_type iter_value_type;
99  static_assert(std::is_same< iter_value_type, byte >::type::value,
100  "Input iterator must be over a char container");
101 
102  integer sz = end - begin;
103 
104  std::string tmp;
105  tmp.reserve(sz);
106  for (; begin != end && *begin; ++begin) {
107  tmp.push_back(*begin);
108  }
109  if (!*begin)
110  ++begin;
111  base_type::value.swap(tmp);
112  return begin;
113 }
114 
115 template < typename InputIterator >
116 InputIterator
118  (InputIterator begin, InputIterator end)
119 {
120  typedef InputIterator iterator_type;
121  typedef std::iterator_traits< iterator_type > iter_traits;
122  typedef typename iter_traits::value_type iter_value_type;
123  static_assert(std::is_same< iter_value_type, byte >::type::value,
124  "Input iterator must be over a char container");
125 
126  std::string literal;
127  iterator_type tmp = protocol_read< TEXT_DATA_FORMAT >(begin, end, literal);
128  if (use_literal(literal)) {
129  return tmp;
130  }
131  return begin;
132 }
133 
134 template < typename InputIterator >
135 InputIterator
137  (InputIterator begin, InputIterator end)
138 {
139  typedef InputIterator iterator_type;
140  typedef std::iterator_traits< iterator_type > iter_traits;
141  typedef typename iter_traits::value_type iter_value_type;
142  static_assert(std::is_same< iter_value_type, byte >::type::value,
143  "Input iterator must be over a char container");
144 
145  assert( (end - begin) >= size() && "Buffer size is insufficient" );
146  base_type::value = *begin++;
147  return begin;
148 }
149 
150 template < typename InputIterator >
151 InputIterator
153  (InputIterator begin, InputIterator end)
154 {
155  typedef InputIterator iterator_type;
156  typedef std::iterator_traits< iterator_type > iter_traits;
157  typedef typename iter_traits::value_type iter_value_type;
158  static_assert(std::is_same< iter_value_type, byte >::type::value,
159  "Input iterator must be over a char container");
160  std::vector<byte> data;
161 
162  auto result = detail::bytea_parser().parse(begin, end, std::back_inserter(data));
163  if (result.first) {
164  base_type::value.data.swap(data);
165  return result.second;
166  }
167  return begin;
168 }
169 
170 template < typename InputIterator >
171 InputIterator
173  (InputIterator begin, InputIterator end)
174 {
175  typedef InputIterator iterator_type;
176  typedef std::iterator_traits< iterator_type > iter_traits;
177  typedef typename iter_traits::value_type iter_value_type;
178  static_assert(std::is_same< iter_value_type, byte >::type::value,
179  "Input iterator must be over a char container");
180 
181  bytea::container_type tmp(begin, end);
182  std::swap(base_type::value.data, tmp);
183 
184  return end;
185 }
186 
187 } // namespace io
188 } // namespace pg
189 } // namespace db
190 } // namespace tip
191 
192 #endif /* LIB_PG_ASYNC_INCLUDE_TIP_DB_PG_PROTOCOL_IO_TRAITS_INL_ */
std::vector< byte > container_type
Definition: common.hpp:197
Base structure for a binary data formatter. Has no definition.
std::pair< boost::tribool, InputIterator > parse(InputIterator begin, InputIterator end, OutputIterator data)
boost::int_t< 32 >::exact integer
4-byte integer, to match PostgreSQL integer and serial types
Definition: common.hpp:162
Base structure for a binary data parser. Has no definition.