6#include <bond/core/config.h>
10#include <bond/core/bond_version.h>
11#include <bond/core/detail/checked.h>
13#include <boost/call_traits.hpp>
14#include <boost/noncopyable.hpp>
101template <
typename BufferT>
102class FastBinaryWriter;
105template <
typename BufferT>
109 typedef BufferT Buffer;
110 typedef DynamicParser<FastBinaryReader&> Parser;
113 BOND_STATIC_CONSTEXPR uint16_t magic = FAST_PROTOCOL;
114 BOND_STATIC_CONSTEXPR uint16_t version = v1;
127 : _input(that._input)
134 return _input == rhs._input;
139 typename boost::call_traits<Buffer>::const_reference
147 typename boost::call_traits<Buffer>::reference
156 uint16_t magic_value, version_value;
158 _input.Read(magic_value);
159 _input.Read(version_value);
161 return magic_value == FastBinaryReader::magic
162 && version_value <= FastBinaryReader::version;
167 template <
typename T>
168 typename boost::disable_if<is_string_type<T> >::type
176 template <
typename T>
177 typename boost::enable_if<is_string_type<T> >::type
182 ReadVariableUnsigned(_input, length);
183 detail::ReadStringData(_input, value, length);
188 void Read(blob& value, uint32_t size)
190 _input.Read(value, size);
193 void ReadStructBegin()
201 void ReadFieldBegin(BondDataType& type, uint16_t&
id)
205 if (type != BT_STOP && type != BT_STOP_BASE)
216 void ReadContainerBegin(uint32_t& size, BondDataType& type)
219 ReadVariableUnsigned(_input, size);
224 void ReadContainerBegin(uint32_t& size, std::pair<BondDataType, BondDataType>& type)
226 ReadType(type.first);
227 ReadType(type.second);
228 ReadVariableUnsigned(_input, size);
232 void ReadContainerEnd()
236 template <
typename T>
239 SkipType<get_type_id<T>::value>();
243 template <
typename T>
244 void Skip(
const bonded<T, FastBinaryReader&>&)
246 SkipType<BT_STRUCT>();
249 void Skip(BondDataType type)
255 void ReadType(BondDataType& type)
260 type =
static_cast<BondDataType
>(byte);
263 using BT = BondDataType;
266 typename boost::enable_if_c<(T == BT_BOOL || T == BT_UINT8 || T == BT_INT8)>::type
267 SkipType(uint32_t size = 1)
269 _input.Skip(detail::checked_multiply(size,
sizeof(uint8_t)));
273 typename boost::enable_if_c<(T == BT_UINT16 || T == BT_INT16)>::type
274 SkipType(uint32_t size = 1)
276 _input.Skip(detail::checked_multiply(size,
sizeof(uint16_t)));
280 typename boost::enable_if_c<(T == BT_UINT32 || T == BT_INT32)>::type
281 SkipType(uint32_t size = 1)
283 _input.Skip(detail::checked_multiply(size,
sizeof(uint32_t)));
287 typename boost::enable_if_c<(T == BT_UINT64 || T == BT_INT64)>::type
288 SkipType(uint32_t size = 1)
290 _input.Skip(detail::checked_multiply(size,
sizeof(uint64_t)));
294 typename boost::enable_if_c<(T == BT_FLOAT)>::type
295 SkipType(uint32_t size = 1)
297 _input.Skip(detail::checked_multiply(size,
sizeof(
float)));
301 typename boost::enable_if_c<(T == BT_DOUBLE)>::type
302 SkipType(uint32_t size = 1)
304 _input.Skip(detail::checked_multiply(size,
sizeof(
double)));
308 typename boost::enable_if_c<(T == BT_STRING)>::type
313 ReadVariableUnsigned(_input, size);
318 typename boost::enable_if_c<(T == BT_WSTRING)>::type
323 ReadVariableUnsigned(_input, size);
324 _input.Skip(detail::checked_multiply(size,
sizeof(uint16_t)));
328 typename boost::enable_if_c<(T == BT_STRUCT)>::type
336 BondDataType field_type;
338 for (ReadFieldBegin(field_type,
id);
339 field_type != BT_STOP && field_type != BT_STOP_BASE;
340 ReadFieldEnd(), ReadFieldBegin(field_type,
id))
342 SkipType(field_type);
347 if (field_type == BT_STOP)
353 typename boost::enable_if_c<(T == BT_SET || T == BT_LIST)>::type
356 BondDataType element_type;
359 ReadContainerBegin(size, element_type);
360 SkipType(element_type, size);
365 typename boost::enable_if_c<(T == BT_MAP)>::type
368 std::pair<BondDataType, BondDataType> element_type;
371 ReadContainerBegin(size, element_type);
372 for (int64_t i = 0; i < size; ++i)
374 SkipType(element_type.first);
375 SkipType(element_type.second);
381 typename boost::enable_if_c<(T == BT_STRING || T == BT_WSTRING || T == BT_STRUCT
382 || T == BT_SET || T == BT_LIST || T == BT_MAP)>::type
383 SkipType(uint32_t size)
385 for (int64_t i = 0; i < size; ++i)
391 template <
typename... Args>
392 void SkipType(BondDataType type, Args&&... args)
399 SkipType<BT_BOOL>(std::forward<Args>(args)...);
404 SkipType<BT_UINT16>(std::forward<Args>(args)...);
409 SkipType<BT_UINT32>(std::forward<Args>(args)...);
414 SkipType<BT_UINT64>(std::forward<Args>(args)...);
418 SkipType<BT_FLOAT>(std::forward<Args>(args)...);
422 SkipType<BT_DOUBLE>(std::forward<Args>(args)...);
426 SkipType<BT_STRING>(std::forward<Args>(args)...);
430 SkipType<BT_WSTRING>(std::forward<Args>(args)...);
435 SkipType<BT_SET>(std::forward<Args>(args)...);
439 SkipType<BT_MAP>(std::forward<Args>(args)...);
443 SkipType<BT_STRUCT>(std::forward<Args>(args)...);
454template <
typename Buffer>
455BOND_CONSTEXPR_OR_CONST uint16_t FastBinaryReader<Buffer>::magic;
457template <
typename Buffer>
458BOND_CONSTEXPR_OR_CONST uint16_t FastBinaryReader<Buffer>::version;
462template <
typename BufferT>
467 typedef BufferT Buffer;
477 typename boost::call_traits<Buffer>::reference
485 _output.Write(Reader::magic);
486 _output.Write(Reader::version);
492 void WriteStructBegin(
const Metadata& ,
bool )
495 void WriteStructEnd(
bool base =
false)
497 WriteType(base ? BT_STOP_BASE : BT_STOP);
500 template <
typename T>
501 void WriteField(uint16_t
id,
const bond::Metadata& ,
const T& value)
503 WriteFieldBegin(get_type_id<T>::value,
id);
508 void WriteFieldBegin(BondDataType type, uint16_t
id,
const bond::Metadata& )
510 WriteFieldBegin(type,
id);
513 void WriteFieldBegin(BondDataType type, uint16_t
id)
522 void WriteContainerBegin(uint32_t size, BondDataType type)
525 WriteVariableUnsigned(_output, size);
529 void WriteContainerBegin(uint32_t size, std::pair<BondDataType, BondDataType> type)
531 WriteType(type.first);
532 WriteType(type.second);
533 WriteVariableUnsigned(_output, size);
536 void WriteContainerEnd()
541 typename boost::disable_if<is_string_type<T> >::type
542 Write(
const T& value)
544 _output.Write(value);
548 template <
typename T>
549 typename boost::enable_if<is_string_type<T> >::type
550 Write(
const T& value)
552 uint32_t length = string_length(value);
554 WriteVariableUnsigned(_output, length);
555 detail::WriteStringData(_output, value, length);
559 void Write(
const blob& value)
561 _output.Write(value);
565 void WriteType(BondDataType type)
567 _output.Write(
static_cast<uint8_t
>(type));
Reader for Fast Binary protocol.
Definition: fast_binary.h:107
bool operator==(const FastBinaryReader &rhs) const
Comparison operator.
Definition: fast_binary.h:132
boost::call_traits< Buffer >::const_reference GetBuffer() const
Access to underlying buffer.
Definition: fast_binary.h:140
FastBinaryReader(const FastBinaryReader &that) BOND_NOEXCEPT
Copy constructor.
Definition: fast_binary.h:126
FastBinaryReader(typename boost::call_traits< Buffer >::param_type buffer)
Construct from input buffer/stream containing serialized data.
Definition: fast_binary.h:117
boost::call_traits< Buffer >::reference GetBuffer()
Access to underlying buffer.
Definition: fast_binary.h:148
Writer for Fast Binary protocol.
Definition: fast_binary.h:465
boost::call_traits< Buffer >::reference GetBuffer()
Access to underlying buffer.
Definition: fast_binary.h:478
FastBinaryWriter(Buffer &buffer)
Construct from output buffer/stream.
Definition: fast_binary.h:471
namespace bond
Definition: apply.h:17