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:
frsyuki 2009-02-15 09:10:00 +00:00
parent 8f3444c081
commit adba617f45
3 changed files with 165 additions and 155 deletions

View File

@ -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_int(msgpack_pack_t* ctx, int d);
void msgpack_pack_unsigned_int(msgpack_pack_t* ctx, unsigned 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_uint8(msgpack_pack_t* ctx, uint8_t d);
void msgpack_pack_uint16(msgpack_pack_t* ctx, uint16_t d); void msgpack_pack_uint16(msgpack_pack_t* ctx, uint16_t d);
void msgpack_pack_uint32(msgpack_pack_t* ctx, uint32_t d); void msgpack_pack_uint32(msgpack_pack_t* ctx, uint32_t d);

View File

@ -30,29 +30,33 @@ public:
packer(Stream& s); packer(Stream& s);
public: public:
void pack_int(int d) { pack_int_impl(m_stream, d); } 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_long(long d) { pack_long_impl(m_stream, d); }
void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); } void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); }
void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); } void pack_unsigned_long(unsigned long d) { pack_unsigned_long_impl(m_stream, d); }
void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); } void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); }
void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); } void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); }
void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); } void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); }
void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); } void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); }
void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); } void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); }
void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); } void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); }
void pack_float(float d) { pack_float_impl(m_stream, d); } void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); }
void pack_double(double d) { pack_double_impl(m_stream, d); } void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); }
void pack_nil() { pack_nil_impl(m_stream); } void pack_float(float d) { pack_float_impl(m_stream, d); }
void pack_true() { pack_true_impl(m_stream); } void pack_double(double d) { pack_double_impl(m_stream, d); }
void pack_false() { pack_false_impl(m_stream); } void pack_nil() { pack_nil_impl(m_stream); }
void pack_array(unsigned int n) { pack_array_impl(m_stream, n); } void pack_true() { pack_true_impl(m_stream); }
void pack_map(unsigned int n) { pack_map_impl(m_stream, n); } void pack_false() { pack_false_impl(m_stream); }
void pack_raw(size_t l) { pack_raw_impl(m_stream, l); } void pack_array(unsigned int n) { pack_array_impl(m_stream, n); }
void pack_raw_body(const char* b, size_t l) { pack_raw_body_impl(m_stream, b, l); } 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: private:
static void pack_int_impl(Stream& x, int d); 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_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_uint8_impl(Stream& x, uint8_t d);
static void pack_uint16_impl(Stream& x, uint16_t d); static void pack_uint16_impl(Stream& x, uint16_t d);
static void pack_uint32_impl(Stream& x, uint32_t d); static void pack_uint32_impl(Stream& x, uint32_t d);

View File

@ -68,145 +68,145 @@
* Integer * Integer
*/ */
static inline void msgpack_pack_compress_int32(msgpack_pack_user x, uint32_t d) #define msgpack_pack_compress_int32(x, d) \
{ do { \
if(d < -(1<<5)) { if(d < -(1<<5)) { \
if(d < -(1<<15)) { if(d < -(1<<15)) { \
// signed 32 /* signed 32 */ \
const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; \
msgpack_pack_append_buffer(x, buf, 5); msgpack_pack_append_buffer(x, buf, 5); \
} else if(d < -(1<<7)) { } else if(d < -(1<<7)) { \
// signed 16 /* signed 16 */ \
const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; \
msgpack_pack_append_buffer(x, buf, 3); msgpack_pack_append_buffer(x, buf, 3); \
} else { } else { \
// signed 8 /* signed 8 */ \
const unsigned char buf[2] = {0xd0, (uint8_t)d}; const unsigned char buf[2] = {0xd0, (uint8_t)d}; \
msgpack_pack_append_buffer(x, buf, 2); msgpack_pack_append_buffer(x, buf, 2); \
} } \
} else if(d < (1<<7)) { } else if(d < (1<<7)) { \
// fixnum /* fixnum */ \
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
} else { } else { \
if(d < (1<<8)) { if(d < (1<<8)) { \
// unsigned 8 /* unsigned 8 */ \
const unsigned char buf[2] = {0xcc, (uint8_t)d}; const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
msgpack_pack_append_buffer(x, buf, 2); msgpack_pack_append_buffer(x, buf, 2); \
} else if(d < (1<<16)) { } else if(d < (1<<16)) { \
// unsigned 16 /* unsigned 16 */ \
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
msgpack_pack_append_buffer(x, buf, 3); msgpack_pack_append_buffer(x, buf, 3); \
} else { } else { \
// unsigned 32 /* unsigned 32 */ \
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
msgpack_pack_append_buffer(x, buf, 5); msgpack_pack_append_buffer(x, buf, 5); \
} } \
} } \
} } while(0)
static inline void msgpack_pack_compress_uint32(msgpack_pack_user x, uint32_t d) #define msgpack_pack_compress_uint32(x, d) \
{ do { \
if(d < (1<<8)) { if(d < (1<<8)) { \
if(d < (1<<7)) { if(d < (1<<7)) { \
// fixnum /* fixnum */ \
msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); \
} else { } else { \
// unsigned 8 /* unsigned 8 */ \
const unsigned char buf[2] = {0xcc, (uint8_t)d}; const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
msgpack_pack_append_buffer(x, buf, 2); msgpack_pack_append_buffer(x, buf, 2); \
} } \
} else { } else { \
if(d < (1<<16)) { if(d < (1<<16)) { \
// unsigned 16 /* unsigned 16 */ \
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
msgpack_pack_append_buffer(x, buf, 3); msgpack_pack_append_buffer(x, buf, 3); \
} else { } else { \
// unsigned 32 /* unsigned 32 */ \
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
msgpack_pack_append_buffer(x, buf, 5); msgpack_pack_append_buffer(x, buf, 5); \
} } \
} } \
} } while(0)
static inline void msgpack_pack_compress_int64(msgpack_pack_user x, int64_t d) #define msgpack_pack_compress_int64(x, d) \
{ do { \
if(d < -(1LL<<5)) { if(d < -(1LL<<5)) { \
if(d < -(1LL<<15)) { if(d < -(1LL<<15)) { \
if(d < -(1LL<<31)) { if(d < -(1LL<<31)) { \
// signed 64 /* signed 64 */ \
const unsigned char buf[9] = {0xd3, STORE_BE64(d)}; const unsigned char buf[9] = {0xd3, STORE_BE64(d)}; \
msgpack_pack_append_buffer(x, buf, 9); msgpack_pack_append_buffer(x, buf, 9); \
} else { } else { \
// signed 32 /* signed 32 */ \
const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; \
msgpack_pack_append_buffer(x, buf, 5); msgpack_pack_append_buffer(x, buf, 5); \
} } \
} else { } else { \
if(d < -(1<<7)) { if(d < -(1<<7)) { \
// signed 16 /* signed 16 */ \
const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; \
msgpack_pack_append_buffer(x, buf, 3); msgpack_pack_append_buffer(x, buf, 3); \
} else { } else { \
// signed 8 /* signed 8 */ \
const unsigned char buf[2] = {0xd0, (uint8_t)d}; const unsigned char buf[2] = {0xd0, (uint8_t)d}; \
msgpack_pack_append_buffer(x, buf, 2); msgpack_pack_append_buffer(x, buf, 2); \
} } \
} } \
} else if(d < (1<<7)) { } else if(d < (1<<7)) { \
// fixnum /* fixnum */ \
msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \
} else { } else { \
if(d < (1LL<<16)) { if(d < (1LL<<16)) { \
if(d < (1<<8)) { if(d < (1<<8)) { \
// unsigned 8 /* unsigned 8 */ \
const unsigned char buf[2] = {0xcc, (uint8_t)d}; const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
msgpack_pack_append_buffer(x, buf, 2); msgpack_pack_append_buffer(x, buf, 2); \
} else { } else { \
// unsigned 16 /* unsigned 16 */ \
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
msgpack_pack_append_buffer(x, buf, 3); msgpack_pack_append_buffer(x, buf, 3); \
} } \
} else { } else { \
if(d < (1LL<<32)) { if(d < (1LL<<32)) { \
// unsigned 32 /* unsigned 32 */ \
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
msgpack_pack_append_buffer(x, buf, 5); msgpack_pack_append_buffer(x, buf, 5); \
} else { } else { \
// unsigned 64 /* unsigned 64 */ \
const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; \
msgpack_pack_append_buffer(x, buf, 9); msgpack_pack_append_buffer(x, buf, 9); \
} } \
} } \
} } \
} } while(0)
static inline void msgpack_pack_compress_uint64(msgpack_pack_user x, uint64_t d) #define msgpack_pack_compress_uint64(x, d) \
{ do { \
if(d < (1ULL<<8)) { if(d < (1ULL<<8)) { \
if(d < (1<<7)) { if(d < (1<<7)) { \
// fixnum /* fixnum */ \
msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); \
} else { } else { \
// unsigned 8 /* unsigned 8 */ \
const unsigned char buf[2] = {0xcc, (uint8_t)d}; const unsigned char buf[2] = {0xcc, (uint8_t)d}; \
msgpack_pack_append_buffer(x, buf, 2); msgpack_pack_append_buffer(x, buf, 2); \
} } \
} else { } else { \
if(d < (1ULL<<16)) { if(d < (1ULL<<16)) { \
// signed 16 /* signed 16 */ \
const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \
msgpack_pack_append_buffer(x, buf, 3); msgpack_pack_append_buffer(x, buf, 3); \
} else if(d < (1ULL<<32)) { } else if(d < (1ULL<<32)) { \
// signed 32 /* signed 32 */ \
const unsigned char buf[5] = {0xce, STORE_BE32(d)}; const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \
msgpack_pack_append_buffer(x, buf, 5); msgpack_pack_append_buffer(x, buf, 5); \
} else { } else { \
// signed 64 /* signed 64 */ \
const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; \
msgpack_pack_append_buffer(x, buf, 9); msgpack_pack_append_buffer(x, buf, 9); \
} } \
} } \
} } while(0)
msgpack_pack_inline_func(int)(msgpack_pack_user x, int d) 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 #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) msgpack_pack_inline_func(uint8)(msgpack_pack_user x, uint8_t d)
{ {