From adba617f45f89cacbd23667744d0cc17668ecdda Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 15 Feb 2009 09:10:00 +0000 Subject: [PATCH] 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 --- c/pack.h | 2 + cpp/pack.hpp | 42 +++--- msgpack/pack_template.h | 276 ++++++++++++++++++++-------------------- 3 files changed, 165 insertions(+), 155 deletions(-) diff --git a/c/pack.h b/c/pack.h index ec0683e6..fac642ff 100644 --- a/c/pack.h +++ b/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); diff --git a/cpp/pack.hpp b/cpp/pack.hpp index 4fda4ff0..aee4b873 100644 --- a/cpp/pack.hpp +++ b/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); diff --git a/msgpack/pack_template.h b/msgpack/pack_template.h index 04f8c983..94fbfd47 100644 --- a/msgpack/pack_template.h +++ b/msgpack/pack_template.h @@ -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) {