25#ifndef __JOIN_PACK_HPP__
26#define __JOIN_PACK_HPP__
116 virtual int setInt (int32_t value)
override
118 if (value < -(1 << 15))
121 pack (
static_cast <uint32_t
> (value));
123 else if (value < -(1 << 7))
126 pack (
static_cast <uint16_t
> (value));
128 else if (value < -(1 << 5))
131 pack (
static_cast <uint8_t
> (value));
133 else if (value < (1 << 7))
135 append (
static_cast <uint8_t
> (value));
137 else if (value < (1 << 8))
140 pack (
static_cast <uint8_t
> (value));
142 else if (value < (1 << 16))
145 pack (
static_cast <uint16_t
> (value));
150 pack (
static_cast <uint32_t
> (value));
162 if (value < (1 << 7))
164 append (
static_cast <uint8_t
> (value));
166 else if (value < (1 << 8))
169 pack (
static_cast <uint8_t
> (value));
171 else if (value < (1 << 16))
174 pack (
static_cast <uint16_t
> (value));
191 if (value < -(1LL << 31))
194 pack (
static_cast <uint64_t
> (value));
196 else if (value < -(1LL << 15))
199 pack (
static_cast <uint32_t
> (value));
201 else if (value < -(1LL << 7))
204 pack (
static_cast <uint16_t
> (value));
206 else if (value < -(1LL << 5))
209 pack (
static_cast <uint8_t
> (value));
211 else if (value < (1LL << 7))
213 append (
static_cast <uint8_t
> (value));
215 else if (value < (1LL << 8))
218 pack (
static_cast <uint8_t
> (value));
220 else if (value < (1LL << 16))
223 pack (
static_cast <uint16_t
> (value));
225 else if (value < (1LL << 32))
228 pack (
static_cast <uint32_t
> (value));
233 pack (
static_cast <uint64_t
> (value));
245 if (value < (1ULL << 7))
247 append (
static_cast <uint8_t
> (value));
249 else if (value < (1ULL << 8))
252 pack (
static_cast <uint8_t
> (value));
254 else if (value < (1ULL << 16))
257 pack (
static_cast <uint16_t
> (value));
259 else if (value < (1ULL << 32))
262 pack (
static_cast <uint32_t
> (value));
289 virtual int setString (
const std::string& value)
override
291 if (value.size () < 32)
293 append (
static_cast <char> (0xa0 | value.size ()));
295 else if (value.size () < 256)
298 pack (
static_cast <uint8_t
> (value.size ()));
300 else if (value.size () < 65536)
303 pack (
static_cast <uint16_t
> (value.size ()));
308 pack (
static_cast <uint32_t
> (value.size ()));
310 append (value.c_str (), value.size ());
323 append (
static_cast <char> (0x90 | size));
325 else if (size < 65536)
328 pack (
static_cast <uint16_t
> (size));
347 append (
static_cast <char> (0x80 | size));
349 else if (size < 65536)
352 pack (
static_cast <uint16_t
> (size));
377 template <
typename Type>
380 append (
reinterpret_cast <const char *
> (&
swap (value)),
sizeof (value));
461 StringView in (document.c_str (), document.size ());
537 template <
typename ViewType>
542 if (
JOIN_LIKELY (document.peek () == std::char_traits <char>::eof ()))
558 template <
typename ViewType>
561 uint8_t head =
static_cast <uint8_t
> (document.peek ());
589 else if (
isBin (head))
617 template <
typename ViewType>
629 template <
typename ViewType>
641 template <
typename ViewType>
653 template <
typename ViewType>
658 if (document.getIf (0xdd))
662 else if (document.getIf (0xdc))
694 template <
typename ViewType>
699 if (document.getIf (0xdf))
703 else if (document.getIf (0xde))
741 template <
typename ViewType>
746 if (document.getIf (0xdb))
750 else if (document.getIf (0xda))
754 else if (document.getIf (0xd9))
780 template <
typename ViewType>
785 if (document.getIf (0xc6))
789 else if (document.getIf (0xc5))
793 else if (document.getIf (0xc4))
815 template <
typename ViewType>
818 if (document.getIf (0xd3))
822 else if (document.getIf (0xcf))
826 else if (document.getIf (0xcb))
830 else if (document.getIf (0xd2))
834 else if (document.getIf (0xce))
838 else if (document.getIf (0xca))
842 else if (document.getIf (0xd1))
846 else if (document.getIf (0xcd))
850 else if (document.getIf (0xd0))
854 else if (document.getIf (0xcc))
859 return setInt (document.get ());
867 template <
typename Type,
typename ViewType>
868 std::enable_if_t <std::is_arithmetic <Type>::value, Type>
872 if (
JOIN_UNLIKELY (document.read (
reinterpret_cast <char *
> (&value), sizeof (value)) !=
sizeof (value)))
874 throw std::range_error (
"not enough data to unpack");
916 return ((c <= 0x7f) || (c >= 0xe0)) || (c == 0xd0) || (c == 0xd1) || (c == 0xd2) || (c == 0xd3);
926 return (c == 0xcc) || (c == 0xcd) || (c == 0xce);
956 return (c == 0xca) || (c == 0xcb);
976 return ((c >= 0xa0) && (c <= 0xbf)) || (c == 0xd9) || (c == 0xda) || (c == 0xdb);
986 return (c == 0xc4) || (c == 0xc5) || (c == 0xc6);
996 return ((c >= 0x90) && (c <= 0x9f)) || (c == 0xdc) || (c == 0xdd);
1006 return ((c >= 0x80) && (c <= 0x8f)) || (c == 0xde) || (c == 0xdf);
basic stream view.
Definition view.hpp:368
message pack reader class.
Definition pack.hpp:388
constexpr bool isString(uint8_t c)
check if string.
Definition pack.hpp:974
int deserialize(std::ifstream &document) override
deserialize a document.
Definition pack.hpp:503
int readValue(ViewType &document)
parse value.
Definition pack.hpp:559
PackReader & operator=(const PackReader &other)=delete
copy assignment.
constexpr bool isInt64(uint8_t c)
check if 64 bits integer.
Definition pack.hpp:934
int deserialize(const char *document, size_t length) override
deserialize a document.
Definition pack.hpp:436
constexpr bool isInt(uint8_t c)
check if 32 bits integer.
Definition pack.hpp:914
constexpr bool isNull(uint8_t c)
check if null.
Definition pack.hpp:884
PackReader(const PackReader &other)=delete
copy constructor.
int deserialize(std::fstream &document) override
deserialize a document.
Definition pack.hpp:492
constexpr bool isTrue(uint8_t c)
check if true.
Definition pack.hpp:904
int read(ViewType &document)
parse a document.
Definition pack.hpp:538
int deserialize(const char *first, const char *last) override
deserialize a document.
Definition pack.hpp:448
int deserialize(std::iostream &document) override
deserialize a document.
Definition pack.hpp:514
int deserialize(std::stringstream &document) override
deserialize a document.
Definition pack.hpp:470
constexpr bool isObject(uint8_t c)
check if object.
Definition pack.hpp:1004
int readNull(ViewType &document)
parse a null value.
Definition pack.hpp:618
int readArray(ViewType &document)
parse an array value.
Definition pack.hpp:654
virtual ~PackReader()=default
destroy instance.
constexpr bool isUint(uint8_t c)
check if unsigned 32 bits integer.
Definition pack.hpp:924
int readTrue(ViewType &document)
parse a true value.
Definition pack.hpp:642
int readNumber(ViewType &document)
parse a number value.
Definition pack.hpp:816
constexpr bool isUint64(uint8_t c)
check if unsigned 64 bits integer.
Definition pack.hpp:944
constexpr bool isFalse(uint8_t c)
check if false.
Definition pack.hpp:894
constexpr bool isReal(uint8_t c)
check if real.
Definition pack.hpp:954
int deserialize(std::istringstream &document) override
deserialize a document.
Definition pack.hpp:481
PackReader(PackReader &&other)=delete
move constructor.
int readFalse(ViewType &document)
parse a false value.
Definition pack.hpp:630
static std::enable_if_t< std::is_arithmetic< Type >::value, Type > unpack(ViewType &document)
unpack value from stream.
Definition pack.hpp:869
int readObject(ViewType &document)
parse an object value.
Definition pack.hpp:695
int deserialize(const std::string &document) override
deserialize a document.
Definition pack.hpp:459
constexpr bool isNumber(uint8_t c)
check if number.
Definition pack.hpp:964
constexpr bool isBin(uint8_t c)
check if binary data.
Definition pack.hpp:984
int readString(ViewType &document, bool isKey=false)
parse a string value.
Definition pack.hpp:742
int deserialize(std::istream &document) override
deserialize a document.
Definition pack.hpp:525
constexpr bool isArray(uint8_t c)
check if array.
Definition pack.hpp:994
PackReader(Value &root)
default constructor.
Definition pack.hpp:394
int readBin(ViewType &document)
parse binary data.
Definition pack.hpp:781
message pack writer class.
Definition pack.hpp:41
virtual int setNull() override
set null value.
Definition pack.hpp:87
PackWriter(std::ostream &document)
create instance.
Definition pack.hpp:47
virtual int setDouble(double value) override
set real value.
Definition pack.hpp:277
virtual int setString(const std::string &value) override
set string value.
Definition pack.hpp:289
virtual int setInt64(int64_t value) override
set 64 bits integer value.
Definition pack.hpp:189
virtual int setUint(uint32_t value) override
set unsigned integer value.
Definition pack.hpp:160
virtual int setKey(const std::string &key) override
set key.
Definition pack.hpp:367
virtual int setBool(bool value) override
set boolean value.
Definition pack.hpp:98
virtual int setUint64(uint64_t value) override
set unsigned 64 bits integer value.
Definition pack.hpp:243
virtual ~PackWriter()=default
destroy instance.
virtual int startArray(uint32_t size=0) override
start array.
Definition pack.hpp:319
void pack(Type value)
pack value into stream.
Definition pack.hpp:378
PackWriter & operator=(const PackWriter &other)=delete
copy assignment.
virtual int setInt(int32_t value) override
set integer value.
Definition pack.hpp:116
virtual int startObject(uint32_t size=0) override
start object.
Definition pack.hpp:343
PackWriter(PackWriter &&other)=delete
move constructor.
PackWriter(const PackWriter &other)=delete
copy constructor.
stream deserializer abstract class.
Definition sax.hpp:443
virtual int stopObject() override
stop object.
Definition sax.hpp:734
virtual int startArray(uint32_t size=0) override
start array.
Definition sax.hpp:636
virtual int setInt64(int64_t value) override
set 64 bits integer value.
Definition sax.hpp:596
virtual int setUint(uint32_t value) override
set unsigned integer value.
Definition sax.hpp:586
virtual int setNull() override
set null value.
Definition sax.hpp:556
virtual int setUint64(uint64_t value) override
set unsigned 64 bits integer value.
Definition sax.hpp:606
virtual int setKey(const std::string &key) override
set key.
Definition sax.hpp:724
virtual int setInt(int32_t value) override
set integer value.
Definition sax.hpp:576
virtual int startObject(uint32_t size=0) override
start object.
Definition sax.hpp:687
virtual int setBool(bool value) override
set boolean value.
Definition sax.hpp:566
virtual int stopArray() override
stop array.
Definition sax.hpp:672
virtual int setDouble(double value) override
set real value.
Definition sax.hpp:616
virtual int setString(const std::string &value) override
set string value.
Definition sax.hpp:626
stream serializer abstract class.
Definition sax.hpp:243
void append(char data) noexcept
append character to output stream in batch.
Definition sax.hpp:373
string view.
Definition view.hpp:78
value class.
Definition value.hpp:63
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code)
Create an std::error_code object.
Definition error.cpp:154
__inline__ Type & swap(Type &val)
swaps byte orders.
Definition utils.hpp:176
thread_local std::error_code lastError
last error.
Definition error.cpp:32
#define JOIN_LIKELY(x)
Definition utils.hpp:46
#define JOIN_UNLIKELY(x)
Definition utils.hpp:47