6#include <bond/core/config.h>
9#include <bond/core/containers.h>
18template <
typename Buffer,
typename T,
typename Enable =
void>
struct
19implements_varint_write
23template <
typename Buffer,
typename T>
struct
24implements_varint_write<Buffer, T,
25#ifdef BOND_NO_SFINAE_EXPR
26 typename boost::enable_if<check_method<void (Buffer::*)(T), &Buffer::WriteVariableUnsigned> >::type>
28 detail::mpl::void_t<
decltype(std::declval<Buffer>().WriteVariableUnsigned(std::declval<T>()))>>
35template<
typename Buffer,
typename T>
36inline void GenericWriteVariableUnsigned(Buffer& output, T value);
39template<
typename Buffer,
typename T>
41typename boost::enable_if<implements_varint_write<Buffer, T> >::type
42WriteVariableUnsigned(Buffer& output, T value)
44 BOOST_STATIC_ASSERT(std::is_unsigned<T>::value);
47 output.WriteVariableUnsigned(value);
51template<
typename Buffer,
typename T>
53typename boost::disable_if<implements_varint_write<Buffer, T> >::type
54WriteVariableUnsigned(Buffer& output, T value)
56 BOOST_STATIC_ASSERT(std::is_unsigned<T>::value);
59 GenericWriteVariableUnsigned(output, value);
63template<
typename Buffer,
typename T>
65inline void GenericWriteVariableUnsigned(Buffer& output, T value)
71 output.Write(
static_cast<uint8_t
>(x | 0x80));
72 WriteVariableUnsigned(output, value);
76 output.Write(
static_cast<uint8_t
>(x));
81template <
typename Buffer,
typename T,
typename Enable =
void>
struct
86template <
typename Buffer,
typename T>
struct
87implements_varint_read<Buffer, T,
88#ifdef BOND_NO_SFINAE_EXPR
89 typename boost::enable_if<check_method<void (Buffer::*)(T&), &Buffer::ReadVariableUnsigned> >::type>
91 detail::mpl::void_t<
decltype(std::declval<Buffer>().ReadVariableUnsigned(std::declval<T&>()))>>
96template<
typename Buffer,
typename T>
98typename boost::enable_if<implements_varint_read<Buffer, T> >::type
99ReadVariableUnsigned(Buffer& input, T& value)
101 BOOST_STATIC_ASSERT(std::is_unsigned<T>::value);
104 input.ReadVariableUnsigned(value);
108template<
typename Buffer,
typename T>
110void GenericReadVariableUnsigned(Buffer& input, T& value)
120 T part =
byte & 0x7f;
121 value += part << shift;
128template<
typename Buffer,
typename T>
130typename boost::disable_if<implements_varint_read<Buffer, T> >::type
131ReadVariableUnsigned(Buffer& input, T& value)
133 BOOST_STATIC_ASSERT(std::is_unsigned<T>::value);
136 GenericReadVariableUnsigned(input, value);
143typename std::make_unsigned<T>::type EncodeZigZag(T value)
145 return (value << 1) ^ (value >> (
sizeof(T) * 8 - 1));
151typename std::make_signed<T>::type DecodeZigZag(T value)
153 return (value >> 1) ^ (-
static_cast<typename std::make_signed<T>::type
>((value & 1)));
161inline char HexDigit(
int n)
164 return d < 10 ? (
'0' + d) : (
'a' + d - 10);
167inline int HexDigit(
char c)
169 if (c >=
'a' && c <=
'f')
171 else if (c >=
'A' && c <=
'F')
177template <
typename T,
typename Enable =
void>
struct
180template <
typename T>
struct
181string_char_int_type<T, typename boost::enable_if<is_string<T> >::type>
183 typedef uint8_t type;
186template <
typename T>
struct
187string_char_int_type<T, typename boost::enable_if<is_wstring<T> >::type>
189 typedef uint16_t type;
192template <
typename Buffer,
typename T>
193typename boost::enable_if_c<(
sizeof(
typename element_type<T>::type) ==
sizeof(
typename string_char_int_type<T>::type))>::type
194inline ReadStringData(Buffer& input, T& value, uint32_t length)
196 resize_string(value, length);
197 input.Read(string_data(value), length *
sizeof(
typename element_type<T>::type));
200template <
typename Buffer,
typename T>
201typename boost::enable_if_c<(
sizeof(
typename element_type<T>::type) >
sizeof(
typename string_char_int_type<T>::type))>::type
202inline ReadStringData(Buffer& input, T& value, uint32_t length)
204 resize_string(value, length);
205 typename element_type<T>::type* data = string_data(value);
206 typename element_type<T>::type*
const data_end = data + length;
207 typename string_char_int_type<T>::type ch;
208 for (; data != data_end; ++data)
211 *data =
static_cast<typename element_type<T>::type
>(ch);
215template <
typename Buffer,
typename T>
216typename boost::enable_if_c<(
sizeof(
typename element_type<T>::type) ==
sizeof(
typename string_char_int_type<T>::type))>::type
217inline WriteStringData(Buffer& output,
const T& value, uint32_t length)
219 output.Write(string_data(value), length *
sizeof(
typename element_type<T>::type));
222template <
typename Buffer,
typename T>
223typename boost::enable_if_c<(
sizeof(
typename element_type<T>::type) >
sizeof(
typename string_char_int_type<T>::type))>::type
224inline WriteStringData(Buffer& output,
const T& value, uint32_t length)
226 const typename element_type<T>::type* data = string_data(value);
227 const typename element_type<T>::type*
const data_end = data + length;
228 typename string_char_int_type<T>::type ch;
229 for (; data != data_end; ++data)
231 ch =
static_cast<typename string_char_int_type<T>::type
>(*data);
namespace bond
Definition: apply.h:17