25#ifndef JOIN_DATA_PACK_HPP
26#define JOIN_DATA_PACK_HPP
29#include <dconv/probe.hpp>
58 :
_out (document.rdbuf ())
130 if (value < -(1 << 15))
133 pack (
static_cast<uint32_t
> (value));
135 else if (value < -(1 << 7))
138 pack (
static_cast<uint16_t
> (value));
140 else if (value < -(1 << 5))
143 pack (
static_cast<uint8_t
> (value));
145 else if (value < (1 << 7))
147 append (
static_cast<uint8_t
> (value));
149 else if (value < (1 << 8))
152 pack (
static_cast<uint8_t
> (value));
154 else if (value < (1 << 16))
157 pack (
static_cast<uint16_t
> (value));
162 pack (
static_cast<uint32_t
> (value));
175 if (value < (1 << 7))
177 append (
static_cast<uint8_t
> (value));
179 else if (value < (1 << 8))
182 pack (
static_cast<uint8_t
> (value));
184 else if (value < (1 << 16))
187 pack (
static_cast<uint16_t
> (value));
205 if (value < -(1LL << 31))
208 pack (
static_cast<uint64_t
> (value));
210 else if (value < -(1LL << 15))
213 pack (
static_cast<uint32_t
> (value));
215 else if (value < -(1LL << 7))
218 pack (
static_cast<uint16_t
> (value));
220 else if (value < -(1LL << 5))
223 pack (
static_cast<uint8_t
> (value));
225 else if (value < (1LL << 7))
227 append (
static_cast<uint8_t
> (value));
229 else if (value < (1LL << 8))
232 pack (
static_cast<uint8_t
> (value));
234 else if (value < (1LL << 16))
237 pack (
static_cast<uint16_t
> (value));
239 else if (value < (1LL << 32))
242 pack (
static_cast<uint32_t
> (value));
247 pack (
static_cast<uint64_t
> (value));
260 if (value < (1ULL << 7))
262 append (
static_cast<uint8_t
> (value));
264 else if (value < (1ULL << 8))
267 pack (
static_cast<uint8_t
> (value));
269 else if (value < (1ULL << 16))
272 pack (
static_cast<uint16_t
> (value));
274 else if (value < (1ULL << 32))
277 pack (
static_cast<uint32_t
> (value));
295 float f =
static_cast<float> (value);
296 if (std::isfinite (value) &&
static_cast<double> (f) == value)
317 if (value.size () < 32)
319 append (
static_cast<char> (0xa0 | value.size ()));
321 else if (value.size () < 256)
324 pack (
static_cast<uint8_t
> (value.size ()));
326 else if (value.size () < 65536)
329 pack (
static_cast<uint16_t
> (value.size ()));
334 pack (
static_cast<uint32_t
> (value.size ()));
336 append (value.data (),
static_cast<uint32_t
> (value.size ()));
350 append (
static_cast<char> (0x90 | size));
352 else if (size < 65536)
355 pack (
static_cast<uint16_t
> (size));
392 append (
static_cast<char> (0x80 | size));
394 else if (size < 65536)
397 pack (
static_cast<uint16_t
> (size));
613 inline void append (
const char* data, uint32_t size)
620 _out->sputn (data, size);
632 template <
typename Type>
635 append (
reinterpret_cast<const char*
> (&
swap (value)),
sizeof (value));
711 template <
typename Handler>
712 int deserialize (
const char* document,
size_t length, Handler& handler)
714 dconv::StringProbe in (document, length);
715 return read (in, handler);
725 template <
typename Handler>
726 int deserialize (
const char* first,
const char* last, Handler& handler)
728 dconv::StringProbe in (first, last);
729 return read (in, handler);
738 template <
typename Handler>
741 dconv::StringProbe in (document.data (), document.size ());
742 return read (in, handler);
751 template <
typename Handler>
754 dconv::StreamProbe in (document);
755 return read (in, handler);
764 template <
typename Handler>
767 dconv::StreamProbe in (document);
768 return read (in, handler);
777 template <
typename Handler>
780 dconv::StreamProbe in (document);
781 return read (in, handler);
790 template <
typename Handler>
793 dconv::StreamProbe in (document);
794 return read (in, handler);
803 template <
typename Handler>
806 dconv::StreamProbe in (document);
807 return read (in, handler);
816 template <
typename Handler>
819 dconv::StreamProbe in (document);
820 return read (in, handler);
830 template <
typename ProbeType>
842 template <
typename ProbeType,
typename Handler>
843 int read (ProbeType& document, Handler&& handler)
849 if (
JOIN_LIKELY (document.peek () == std::char_traits<char>::eof ()))
873 template <
typename ProbeType,
typename Handler>
874 int readValue (ProbeType& document, Handler& handler,
size_t depth)
876 int ch = document.peek ();
883 uint8_t head =
static_cast<uint8_t
> (ch);
887 return readArray (document, handler, depth);
895 return readNull (document, handler);
903 return readTrue (document, handler);
909 else if (
isBin (head))
911 return readBin (document, handler);
931 template <
typename ProbeType,
typename Handler>
932 int readNull (ProbeType& document, Handler& handler,
bool isKey =
false)
935 return (isKey) ? handler.setKey (
nullptr) : handler.setNull ();
945 template <
typename ProbeType,
typename Handler>
946 int readFalse (ProbeType& document, Handler& handler,
bool isKey =
false)
949 return (isKey) ? handler.setKey (
false) : handler.setBool (
false);
959 template <
typename ProbeType,
typename Handler>
960 int readTrue (ProbeType& document, Handler& handler,
bool isKey =
false)
963 return (isKey) ? handler.setKey (
true) : handler.setBool (
true);
973 template <
typename ProbeType,
typename Handler>
974 int readArray (ProbeType& document, Handler& handler,
size_t depth)
984 if (document.getIf (0xdd))
991 else if (document.getIf (0xdc))
1000 len =
static_cast<uint8_t
> (document.get ()) & ~0x90u;
1018 return handler.stopArray ();
1028 template <
typename ProbeType,
typename Handler>
1029 int readObject (ProbeType& document, Handler& handler,
size_t depth)
1039 if (document.getIf (0xdf))
1046 else if (document.getIf (0xde))
1055 len =
static_cast<uint8_t
> (document.get ()) & ~0x80u;
1078 return handler.stopObject ();
1087 template <
typename ProbeType,
typename Handler>
1088 int readKey (ProbeType& document, Handler& handler)
1090 int ch = document.peek ();
1097 uint8_t head =
static_cast<uint8_t
> (ch);
1101 return readNull (document, handler,
true);
1105 return readFalse (document, handler,
true);
1109 return readTrue (document, handler,
true);
1133 template <
typename ProbeType,
typename Handler>
1134 int readString (ProbeType& document, Handler& handler,
bool isKey =
false)
1138 if (document.getIf (0xdb))
1145 else if (document.getIf (0xda))
1152 else if (document.getIf (0xd9))
1161 len =
static_cast<uint8_t
> (document.get ()) & ~0xa0u;
1164 thread_local std::string cache;
1165 std::string output = std::move (cache);
1172 int result = isKey ? handler.setKey (output) : handler.setString (output);
1173 cache = std::move (output);
1183 template <
typename ProbeType,
typename Handler>
1184 int readBin (ProbeType& document, Handler& handler)
1188 if (document.getIf (0xc6))
1195 else if (document.getIf (0xc5))
1202 else if (document.getIf (0xc4))
1210 thread_local std::string cache;
1211 std::string output = std::move (cache);
1218 int result = handler.setString (output);
1219 cache = std::move (output);
1230 template <
typename ProbeType,
typename Handler>
1231 int readNumber (ProbeType& document, Handler& handler,
bool isKey =
false)
1233 uint8_t head = document.get ();
1237 auto number = uint32_t (head);
1238 return isKey ? handler.setKey (number) : handler.setUint (number);
1240 else if (head >= 0xe0)
1242 auto number = int32_t (
static_cast<int8_t
> (head));
1243 return isKey ? handler.setKey (number) : handler.setInt (number);
1245 else if (head == 0xd3)
1252 return isKey ? handler.setKey (number) : handler.setInt64 (number);
1254 else if (head == 0xcf)
1261 return isKey ? handler.setKey (number) : handler.setUint64 (number);
1263 else if (head == 0xcb)
1270 return isKey ? handler.setKey (number) : handler.setDouble (number);
1272 else if (head == 0xd2)
1279 return isKey ? handler.setKey (number) : handler.setInt (number);
1281 else if (head == 0xce)
1288 return isKey ? handler.setKey (number) : handler.setUint (number);
1290 else if (head == 0xca)
1297 return isKey ? handler.setKey (number) : handler.setDouble (number);
1299 else if (head == 0xd1)
1306 return isKey ? handler.setKey (number) : handler.setInt (number);
1308 else if (head == 0xcd)
1315 return isKey ? handler.setKey (number) : handler.setUint (number);
1317 else if (head == 0xd0)
1324 return isKey ? handler.setKey (number) : handler.setInt (number);
1326 else if (head == 0xcc)
1333 return isKey ? handler.setKey (number) : handler.setUint (number);
1351 template <
typename ProbeType>
1352 static int readBytes (ProbeType& document, uint32_t len, std::string& output)
1356 size_t pos = output.size ();
1357 size_t chunk = std::min<size_t> (len, 65536);
1358 output.resize (pos + chunk);
1359 if (
JOIN_UNLIKELY (document.read (&output[pos], chunk) != chunk))
1375 template <
typename Type,
typename ProbeType,
typename Output>
1376 static int unpack (ProbeType& document, Output& output)
1377 requires std::is_arithmetic_v<Type>
1380 if (
JOIN_UNLIKELY (document.read (
reinterpret_cast<char*
> (&value), sizeof (value)) !=
sizeof (value)))
1385 output =
static_cast<Output
> (
swap (value));
1426 return ((c <= 0x7f) || (c >= 0xe0)) || (c == 0xd0) || (c == 0xd1) || (c == 0xd2);
1436 return (c == 0xcc) || (c == 0xcd) || (c == 0xce);
1466 return (c == 0xca) || (c == 0xcb);
1486 return ((c >= 0xa0) && (c <= 0xbf)) || (c == 0xd9) || (c == 0xda) || (c == 0xdb);
1496 return (c == 0xc4) || (c == 0xc5) || (c == 0xc6);
1506 return ((c >= 0x90) && (c <= 0x9f)) || (c == 0xdc) || (c == 0xdd);
1516 return ((c >= 0x80) && (c <= 0x8f)) || (c == 0xde) || (c == 0xdf);
message pack reader class.
Definition pack.hpp:666
int readTrue(ProbeType &document, Handler &handler, bool isKey=false)
parse a true value.
Definition pack.hpp:960
constexpr bool isString(uint8_t c)
check if string.
Definition pack.hpp:1484
PackReader & operator=(const PackReader &other)=delete
copy assignment.
constexpr bool isInt64(uint8_t c)
check if 64 bits integer.
Definition pack.hpp:1444
int deserialize(std::stringstream &document, Handler &handler)
deserialize a document.
Definition pack.hpp:752
constexpr bool isInt(uint8_t c)
check if 32 bits integer.
Definition pack.hpp:1424
constexpr bool isNull(uint8_t c)
check if null.
Definition pack.hpp:1394
int deserialize(const char *first, const char *last, Handler &handler)
deserialize a document.
Definition pack.hpp:726
int deserialize(std::istream &document, Handler &handler)
deserialize a document.
Definition pack.hpp:817
int deserialize(std::iostream &document, Handler &handler)
deserialize a document.
Definition pack.hpp:804
PackReader(const PackReader &other)=delete
copy constructor.
int deserialize(std::string_view document, Handler &handler)
deserialize a document.
Definition pack.hpp:739
static int unpack(ProbeType &document, Output &output)
unpack value from stream.
Definition pack.hpp:1376
int readValue(ProbeType &document, Handler &handler, size_t depth)
parse value.
Definition pack.hpp:874
constexpr bool isTrue(uint8_t c)
check if true.
Definition pack.hpp:1414
int read(ProbeType &document, Handler &&handler)
parse a document, resetting the handler on failure.
Definition pack.hpp:843
constexpr bool isObject(uint8_t c)
check if object.
Definition pack.hpp:1514
int readObject(ProbeType &document, Handler &handler, size_t depth)
parse an object value.
Definition pack.hpp:1029
~PackReader()=default
destroy instance.
int readFalse(ProbeType &document, Handler &handler, bool isKey=false)
parse a false value.
Definition pack.hpp:946
int deserialize(std::fstream &document, Handler &handler)
deserialize a document.
Definition pack.hpp:778
int readBin(ProbeType &document, Handler &handler)
parse binary data.
Definition pack.hpp:1184
int readNumber(ProbeType &document, Handler &handler, bool isKey=false)
parse a number value.
Definition pack.hpp:1231
int deserialize(std::ifstream &document, Handler &handler)
deserialize a document.
Definition pack.hpp:791
constexpr bool isUint(uint8_t c)
check if unsigned 32 bits integer.
Definition pack.hpp:1434
int readString(ProbeType &document, Handler &handler, bool isKey=false)
parse a string value.
Definition pack.hpp:1134
static int readBytes(ProbeType &document, uint32_t len, std::string &output)
read bytes by chunks.
Definition pack.hpp:1352
constexpr bool isUint64(uint8_t c)
check if unsigned 64 bits integer.
Definition pack.hpp:1454
int deserialize(std::istringstream &document, Handler &handler)
deserialize a document.
Definition pack.hpp:765
int read(ProbeType &document, Value &value)
parse a document into a value.
Definition pack.hpp:831
constexpr bool isFalse(uint8_t c)
check if false.
Definition pack.hpp:1404
constexpr bool isReal(uint8_t c)
check if real.
Definition pack.hpp:1464
PackReader()=default
default constructor.
PackReader(PackReader &&other)=delete
move constructor.
int readKey(ProbeType &document, Handler &handler)
parse a key of any type.
Definition pack.hpp:1088
int deserialize(const char *document, size_t length, Handler &handler)
deserialize a document.
Definition pack.hpp:712
constexpr bool isNumber(uint8_t c)
check if number.
Definition pack.hpp:1474
constexpr bool isBin(uint8_t c)
check if binary data.
Definition pack.hpp:1494
int readNull(ProbeType &document, Handler &handler, bool isKey=false)
parse a null value.
Definition pack.hpp:932
constexpr bool isArray(uint8_t c)
check if array.
Definition pack.hpp:1504
int readArray(ProbeType &document, Handler &handler, size_t depth)
parse an array value.
Definition pack.hpp:974
message pack writer class.
Definition pack.hpp:51
size_t _size
output buffer size.
Definition pack.hpp:656
int setKey(int32_t key)
set integer key.
Definition pack.hpp:455
int setKey(uint32_t key)
set unsigned integer key.
Definition pack.hpp:471
PackWriter(std::ostream &document)
create instance.
Definition pack.hpp:57
int setKey(const char *key)
set string key.
Definition pack.hpp:551
int setInt(int32_t value)
set integer value.
Definition pack.hpp:128
int setNull()
set null value.
Definition pack.hpp:97
int stopArray()
stop array.
Definition pack.hpp:370
~PackWriter()=default
destroy instance.
int setKey(std::string_view key)
set string key.
Definition pack.hpp:535
int setUint(uint32_t value)
set unsigned integer value.
Definition pack.hpp:173
int startObject(uint32_t size)
start object.
Definition pack.hpp:388
std::streambuf * _out
underlying output stream.
Definition pack.hpp:650
int startArray(uint32_t size)
start array.
Definition pack.hpp:346
int setKey(bool key)
set boolean key.
Definition pack.hpp:439
int setKey(double key)
set real key.
Definition pack.hpp:519
int setInt64(int64_t value)
set 64 bits integer value.
Definition pack.hpp:203
int setKey(std::nullptr_t key)
set null key.
Definition pack.hpp:423
void append(char data)
append character to output stream in batch.
Definition pack.hpp:599
void pack(Type value)
pack value into stream.
Definition pack.hpp:633
PackWriter & operator=(const PackWriter &other)=delete
copy assignment.
void append(const char *data, uint32_t size)
append data to output stream in batch.
Definition pack.hpp:613
void endValue()
mark the end of a value, flushing the document once complete.
Definition pack.hpp:641
int setBool(bool value)
set boolean value.
Definition pack.hpp:109
int setString(std::string_view value)
set string value.
Definition pack.hpp:315
PackWriter(PackWriter &&other)=delete
move constructor.
int setDouble(double value)
set real value.
Definition pack.hpp:293
int setKey(uint64_t key)
set unsigned 64 bits integer key.
Definition pack.hpp:503
char _buffer[1024]
output buffer.
Definition pack.hpp:653
int setKey(int64_t key)
set 64 bits integer key.
Definition pack.hpp:487
PackWriter(const PackWriter &other)=delete
copy constructor.
void reset() noexcept
reset the writer state, discarding buffered data.
Definition pack.hpp:588
size_t _depth
number of open containers.
Definition pack.hpp:659
void flush()
flush buffered data to output stream.
Definition pack.hpp:576
int stopObject()
stop object.
Definition pack.hpp:560
int setUint64(uint64_t value)
set unsigned 64 bits integer value.
Definition pack.hpp:258
const Object & members(const Object &object) const noexcept
get object members in writing order.
Definition pack.hpp:413
value builder class, filled by balanced SAX calls.
Definition value_builder.hpp:49
value class.
Definition value.hpp:92
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
Type & swap(Type &val)
swaps byte orders.
Definition utils.hpp:86
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195
constexpr size_t saxMaxDepth
max number of nested containers.
Definition sax.hpp:42
thread_local std::error_code lastError
last error.
Definition error.cpp:32
std::vector< Member > Object
object.
Definition value.hpp:86
#define JOIN_LIKELY(x)
Definition utils.hpp:50
#define JOIN_UNLIKELY(x)
Definition utils.hpp:51