mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-16 15:01:14 +02:00
c, c++ binding: catch up with ruby binding
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@89 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
parent
8f3444c081
commit
adba617f45
2
c/pack.h
2
c/pack.h
@ -39,6 +39,8 @@ void msgpack_pack_free(msgpack_pack_t* ctx);
|
||||
|
||||
void msgpack_pack_int(msgpack_pack_t* ctx, int d);
|
||||
void msgpack_pack_unsigned_int(msgpack_pack_t* ctx, unsigned int d);
|
||||
void msgpack_pack_long(msgpack_pack_t* ctx, long d);
|
||||
void msgpack_pack_unsigned_long(msgpack_pack_t* ctx, unsigned long d);
|
||||
void msgpack_pack_uint8(msgpack_pack_t* ctx, uint8_t d);
|
||||
void msgpack_pack_uint16(msgpack_pack_t* ctx, uint16_t d);
|
||||
void msgpack_pack_uint32(msgpack_pack_t* ctx, uint32_t d);
|
||||
|
42
cpp/pack.hpp
42
cpp/pack.hpp
@ -30,29 +30,33 @@ public:
|
||||
packer(Stream& s);
|
||||
|
||||
public:
|
||||
void pack_int(int d) { pack_int_impl(m_stream, d); }
|
||||
void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); }
|
||||
void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); }
|
||||
void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); }
|
||||
void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); }
|
||||
void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); }
|
||||
void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); }
|
||||
void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); }
|
||||
void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); }
|
||||
void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); }
|
||||
void pack_float(float d) { pack_float_impl(m_stream, d); }
|
||||
void pack_double(double d) { pack_double_impl(m_stream, d); }
|
||||
void pack_nil() { pack_nil_impl(m_stream); }
|
||||
void pack_true() { pack_true_impl(m_stream); }
|
||||
void pack_false() { pack_false_impl(m_stream); }
|
||||
void pack_array(unsigned int n) { pack_array_impl(m_stream, n); }
|
||||
void pack_map(unsigned int n) { pack_map_impl(m_stream, n); }
|
||||
void pack_raw(size_t l) { pack_raw_impl(m_stream, l); }
|
||||
void pack_raw_body(const char* b, size_t l) { pack_raw_body_impl(m_stream, b, l); }
|
||||
void pack_int(int d) { pack_int_impl(m_stream, d); }
|
||||
void pack_long(long d) { pack_long_impl(m_stream, d); }
|
||||
void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); }
|
||||
void pack_unsigned_long(unsigned long d) { pack_unsigned_long_impl(m_stream, d); }
|
||||
void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); }
|
||||
void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); }
|
||||
void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); }
|
||||
void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); }
|
||||
void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); }
|
||||
void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); }
|
||||
void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); }
|
||||
void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); }
|
||||
void pack_float(float d) { pack_float_impl(m_stream, d); }
|
||||
void pack_double(double d) { pack_double_impl(m_stream, d); }
|
||||
void pack_nil() { pack_nil_impl(m_stream); }
|
||||
void pack_true() { pack_true_impl(m_stream); }
|
||||
void pack_false() { pack_false_impl(m_stream); }
|
||||
void pack_array(unsigned int n) { pack_array_impl(m_stream, n); }
|
||||
void pack_map(unsigned int n) { pack_map_impl(m_stream, n); }
|
||||
void pack_raw(size_t l) { pack_raw_impl(m_stream, l); }
|
||||
void pack_raw_body(const char* b, size_t l) { pack_raw_body_impl(m_stream, b, l); }
|
||||
|
||||
private:
|
||||
static void pack_int_impl(Stream& x, int d);
|
||||
static void pack_long_impl(Stream& x, long d);
|
||||
static void pack_unsigned_int_impl(Stream& x, unsigned int d);
|
||||
static void pack_unsigned_long_impl(Stream& x, unsigned long d);
|
||||
static void pack_uint8_impl(Stream& x, uint8_t d);
|
||||
static void pack_uint16_impl(Stream& x, uint16_t d);
|
||||
static void pack_uint32_impl(Stream& x, uint32_t d);
|
||||
|
@ -68,145 +68,145 @@
|
||||
* Integer
|
||||
*/
|
||||
|
||||
static inline void msgpack_pack_compress_int32(msgpack_pack_user x, uint32_t d)
|
||||
{
|
||||
if(d < -(1<<5)) {
|
||||
if(d < -(1<<15)) {
|
||||
// signed 32
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
} else if(d < -(1<<7)) {
|
||||
// signed 16
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
// signed 8
|
||||
const unsigned char buf[2] = {0xd0, (uint8_t)d};
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
}
|
||||
} else if(d < (1<<7)) {
|
||||
// fixnum
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1);
|
||||
} else {
|
||||
if(d < (1<<8)) {
|
||||
// unsigned 8
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d};
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
} else if(d < (1<<16)) {
|
||||
// unsigned 16
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
// unsigned 32
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
#define msgpack_pack_compress_int32(x, d) \
|
||||
do { \
|
||||
if(d < -(1<<5)) { \
|
||||
if(d < -(1<<15)) { \
|
||||
/* signed 32 */ \
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} else if(d < -(1<<7)) { \
|
||||
/* signed 16 */ \
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* signed 8 */ \
|
||||
const unsigned char buf[2] = {0xd0, (uint8_t)d}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} else if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
} else { \
|
||||
if(d < (1<<8)) { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} else if(d < (1<<16)) { \
|
||||
/* unsigned 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* unsigned 32 */ \
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
static inline void msgpack_pack_compress_uint32(msgpack_pack_user x, uint32_t d)
|
||||
{
|
||||
if(d < (1<<8)) {
|
||||
if(d < (1<<7)) {
|
||||
// fixnum
|
||||
msgpack_pack_append_buffer(x, (unsigned char*)&d, 1);
|
||||
} else {
|
||||
// unsigned 8
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d};
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
}
|
||||
} else {
|
||||
if(d < (1<<16)) {
|
||||
// unsigned 16
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
// unsigned 32
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
#define msgpack_pack_compress_uint32(x, d) \
|
||||
do { \
|
||||
if(d < (1<<8)) { \
|
||||
if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); \
|
||||
} else { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} else { \
|
||||
if(d < (1<<16)) { \
|
||||
/* unsigned 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* unsigned 32 */ \
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
static inline void msgpack_pack_compress_int64(msgpack_pack_user x, int64_t d)
|
||||
{
|
||||
if(d < -(1LL<<5)) {
|
||||
if(d < -(1LL<<15)) {
|
||||
if(d < -(1LL<<31)) {
|
||||
// signed 64
|
||||
const unsigned char buf[9] = {0xd3, STORE_BE64(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
} else {
|
||||
// signed 32
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
}
|
||||
} else {
|
||||
if(d < -(1<<7)) {
|
||||
// signed 16
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else {
|
||||
// signed 8
|
||||
const unsigned char buf[2] = {0xd0, (uint8_t)d};
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
}
|
||||
}
|
||||
} else if(d < (1<<7)) {
|
||||
// fixnum
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1);
|
||||
} else {
|
||||
if(d < (1LL<<16)) {
|
||||
if(d < (1<<8)) {
|
||||
// unsigned 8
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d};
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
} else {
|
||||
// unsigned 16
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
}
|
||||
} else {
|
||||
if(d < (1LL<<32)) {
|
||||
// unsigned 32
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
} else {
|
||||
// unsigned 64
|
||||
const unsigned char buf[9] = {0xcf, STORE_BE64(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#define msgpack_pack_compress_int64(x, d) \
|
||||
do { \
|
||||
if(d < -(1LL<<5)) { \
|
||||
if(d < -(1LL<<15)) { \
|
||||
if(d < -(1LL<<31)) { \
|
||||
/* signed 64 */ \
|
||||
const unsigned char buf[9] = {0xd3, STORE_BE64(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 9); \
|
||||
} else { \
|
||||
/* signed 32 */ \
|
||||
const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} \
|
||||
} else { \
|
||||
if(d < -(1<<7)) { \
|
||||
/* signed 16 */ \
|
||||
const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else { \
|
||||
/* signed 8 */ \
|
||||
const unsigned char buf[2] = {0xd0, (uint8_t)d}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} \
|
||||
} else if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
|
||||
} else { \
|
||||
if(d < (1LL<<16)) { \
|
||||
if(d < (1<<8)) { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} else { \
|
||||
/* unsigned 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} \
|
||||
} else { \
|
||||
if(d < (1LL<<32)) { \
|
||||
/* unsigned 32 */ \
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} else { \
|
||||
/* unsigned 64 */ \
|
||||
const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 9); \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
static inline void msgpack_pack_compress_uint64(msgpack_pack_user x, uint64_t d)
|
||||
{
|
||||
if(d < (1ULL<<8)) {
|
||||
if(d < (1<<7)) {
|
||||
// fixnum
|
||||
msgpack_pack_append_buffer(x, (unsigned char*)&d, 1);
|
||||
} else {
|
||||
// unsigned 8
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d};
|
||||
msgpack_pack_append_buffer(x, buf, 2);
|
||||
}
|
||||
} else {
|
||||
if(d < (1ULL<<16)) {
|
||||
// signed 16
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 3);
|
||||
} else if(d < (1ULL<<32)) {
|
||||
// signed 32
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 5);
|
||||
} else {
|
||||
// signed 64
|
||||
const unsigned char buf[9] = {0xcf, STORE_BE64(d)};
|
||||
msgpack_pack_append_buffer(x, buf, 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
#define msgpack_pack_compress_uint64(x, d) \
|
||||
do { \
|
||||
if(d < (1ULL<<8)) { \
|
||||
if(d < (1<<7)) { \
|
||||
/* fixnum */ \
|
||||
msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); \
|
||||
} else { \
|
||||
/* unsigned 8 */ \
|
||||
const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
|
||||
msgpack_pack_append_buffer(x, buf, 2); \
|
||||
} \
|
||||
} else { \
|
||||
if(d < (1ULL<<16)) { \
|
||||
/* signed 16 */ \
|
||||
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 3); \
|
||||
} else if(d < (1ULL<<32)) { \
|
||||
/* signed 32 */ \
|
||||
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 5); \
|
||||
} else { \
|
||||
/* signed 64 */ \
|
||||
const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; \
|
||||
msgpack_pack_append_buffer(x, buf, 9); \
|
||||
} \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
msgpack_pack_inline_func(int)(msgpack_pack_user x, int d)
|
||||
{
|
||||
@ -244,6 +244,10 @@ msgpack_pack_inline_func(unsigned_long)(msgpack_pack_user x, unsigned long d)
|
||||
#endif
|
||||
}
|
||||
|
||||
#undef msgpack_pack_compress_int32
|
||||
#undef msgpack_pack_compress_uint32
|
||||
#undef msgpack_pack_compress_int64
|
||||
#undef msgpack_pack_compress_uint64
|
||||
|
||||
msgpack_pack_inline_func(uint8)(msgpack_pack_user x, uint8_t d)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user