diff --git a/pack_template.h b/pack_template.h index 65c959dd..cbba461a 100644 --- a/pack_template.h +++ b/pack_template.h @@ -372,6 +372,28 @@ msgpack_pack_inline_func(_int64)(msgpack_pack_user x, int64_t d) msgpack_pack_real_int64(x, d); } +msgpack_pack_inline_func(_char)(msgpack_pack_user x, char d) +{ +#if defined(CHAR_MIN) +#if CHAR_MIN < 0 + msgpack_pack_real_int8(x, d); +#else + msgpack_pack_real_uint8(x, d); +#endif +#else +#error CHAR_MIN is not defined +#endif +} + +msgpack_pack_inline_func(_signed_char)(msgpack_pack_user x, signed char d) +{ + msgpack_pack_real_int8(x, d); +} + +msgpack_pack_inline_func(_unsigned_char)(msgpack_pack_user x, unsigned char d) +{ + msgpack_pack_real_uint8(x, d); +} #ifdef msgpack_pack_inline_func_cint diff --git a/src/msgpack/pack.h b/src/msgpack/pack.h index f86e9299..10b061a6 100644 --- a/src/msgpack/pack.h +++ b/src/msgpack/pack.h @@ -52,10 +52,14 @@ static void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_w static msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback); static void msgpack_packer_free(msgpack_packer* pk); +static int msgpack_pack_char(msgpack_packer* pk, char d); + +static int msgpack_pack_signed_char(msgpack_packer* pk, signed char d); static int msgpack_pack_short(msgpack_packer* pk, short d); static int msgpack_pack_int(msgpack_packer* pk, int d); static int msgpack_pack_long(msgpack_packer* pk, long d); static int msgpack_pack_long_long(msgpack_packer* pk, long long d); +static int msgpack_pack_unsigned_char(msgpack_packer* pk, unsigned char d); static int msgpack_pack_unsigned_short(msgpack_packer* pk, unsigned short d); static int msgpack_pack_unsigned_int(msgpack_packer* pk, unsigned int d); static int msgpack_pack_unsigned_long(msgpack_packer* pk, unsigned long d); diff --git a/src/msgpack/pack.hpp b/src/msgpack/pack.hpp index 0090b961..cb8352b0 100644 --- a/src/msgpack/pack.hpp +++ b/src/msgpack/pack.hpp @@ -54,10 +54,13 @@ public: packer& pack_fix_int32(int32_t d); packer& pack_fix_int64(int64_t d); + packer& pack_char(char d); + packer& pack_signed_char(signed char d); packer& pack_short(short d); packer& pack_int(int d); packer& pack_long(long d); packer& pack_long_long(long long d); + packer& pack_unsigned_char(unsigned char d); packer& pack_unsigned_short(unsigned short d); packer& pack_unsigned_int(unsigned int d); packer& pack_unsigned_long(unsigned long d); @@ -96,10 +99,14 @@ private: static void _pack_fix_int32(Stream& x, int32_t d); static void _pack_fix_int64(Stream& x, int64_t d); + static void _pack_char(Stream& x, char d); + + static void _pack_signed_char(Stream& x, signed char d); static void _pack_short(Stream& x, short d); static void _pack_int(Stream& x, int d); static void _pack_long(Stream& x, long d); static void _pack_long_long(Stream& x, long long d); + static void _pack_unsigned_char(Stream& x, unsigned char d); static void _pack_unsigned_short(Stream& x, unsigned short d); static void _pack_unsigned_int(Stream& x, unsigned int d); static void _pack_unsigned_long(Stream& x, unsigned long d); @@ -238,6 +245,14 @@ inline packer& packer::pack_fix_int64(int64_t d) { _pack_fix_int64(m_stream, d); return *this;} +template +inline packer& packer::pack_char(char d) +{ _pack_char(m_stream, d); return *this; } + +template +inline packer& packer::pack_signed_char(signed char d) +{ _pack_signed_char(m_stream, d); return *this; } + template inline packer& packer::pack_short(short d) { _pack_short(m_stream, d); return *this; } @@ -254,6 +269,10 @@ template inline packer& packer::pack_long_long(long long d) { _pack_long_long(m_stream, d); return *this; } +template +inline packer& packer::pack_unsigned_char(unsigned char d) +{ _pack_unsigned_char(m_stream, d); return *this; } + template inline packer& packer::pack_unsigned_short(unsigned short d) { _pack_unsigned_short(m_stream, d); return *this; } diff --git a/src/msgpack/type/int.hpp b/src/msgpack/type/int.hpp index e45121df..6a1477ee 100644 --- a/src/msgpack/type/int.hpp +++ b/src/msgpack/type/int.hpp @@ -57,15 +57,69 @@ namespace detail { } }; + template + struct is_signed { + static const bool value = std::numeric_limits::is_signed; + }; + template static inline T convert_integer(object o) { - return detail::convert_integer_sign::is_signed>::convert(o); + return detail::convert_integer_sign::value>::convert(o); + } + + template + struct pack_char_sign; + + template <> + struct pack_char_sign { + template + static inline packer& pack(packer& o, char v) { + o.pack_int8(v); return o; + } + }; + + template <> + struct pack_char_sign { + template + static inline packer& pack(packer& o, char v) { + o.pack_uint8(v); return o; + } + }; + + template + static inline packer& pack_char(packer& o, char v) { + return pack_char_sign::value>::pack(o, v); + } + + template + struct object_char_sign; + + template <> + struct object_char_sign { + static inline void make(object& o, char v) { + v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v + : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; + } + }; + + template <> + struct object_char_sign { + static inline void make(object& o, char v) { + o.type = type::POSITIVE_INTEGER, o.via.u64 = v; + } + }; + + static inline void object_char(object& o, char v) { + return object_char_sign::value>::make(o, v); } } // namespace detail } // namespace type +inline char& operator>> (object const& o, char& v) + { v = type::detail::convert_integer(o); return v; } + inline signed char& operator>> (object o, signed char& v) { v = type::detail::convert_integer(o); return v; } @@ -98,49 +152,56 @@ inline unsigned long& operator>> (object o, unsigned long& v) inline unsigned long long& operator>> (object o, unsigned long long& v) { v = type::detail::convert_integer(o); return v; } +template +inline packer& operator<< (packer& o, char v) + { return type::detail::pack_char(o, v); } template -inline packer& operator<< (packer& o, const signed char& v) +inline packer& operator<< (packer& o, signed char v) { o.pack_int8(v); return o; } template -inline packer& operator<< (packer& o, const signed short& v) +inline packer& operator<< (packer& o, signed short v) { o.pack_short(v); return o; } template -inline packer& operator<< (packer& o, const signed int& v) +inline packer& operator<< (packer& o, signed int v) { o.pack_int(v); return o; } template -inline packer& operator<< (packer& o, const signed long& v) +inline packer& operator<< (packer& o, signed long v) { o.pack_long(v); return o; } template -inline packer& operator<< (packer& o, const signed long long& v) +inline packer& operator<< (packer& o, signed long long v) { o.pack_long_long(v); return o; } template -inline packer& operator<< (packer& o, const unsigned char& v) +inline packer& operator<< (packer& o, unsigned char v) { o.pack_uint8(v); return o; } template -inline packer& operator<< (packer& o, const unsigned short& v) +inline packer& operator<< (packer& o, unsigned short v) { o.pack_unsigned_short(v); return o; } template -inline packer& operator<< (packer& o, const unsigned int& v) +inline packer& operator<< (packer& o, unsigned int v) { o.pack_unsigned_int(v); return o; } template -inline packer& operator<< (packer& o, const unsigned long& v) +inline packer& operator<< (packer& o, unsigned long v) { o.pack_unsigned_long(v); return o; } template -inline packer& operator<< (packer& o, const unsigned long long& v) +inline packer& operator<< (packer& o, unsigned long long v) { o.pack_unsigned_long_long(v); return o; } +inline void operator<< (object& o, char v) + { type::detail::object_char(o, v); } + + inline void operator<< (object& o, signed char v) { v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; } @@ -173,6 +234,10 @@ inline void operator<< (object& o, unsigned long long v) { o.type = type::POSITIVE_INTEGER, o.via.u64 = v; } +inline void operator<< (object::with_zone& o, char v) + { static_cast(o) << v; } + + inline void operator<< (object::with_zone& o, signed char v) { static_cast(o) << v; } @@ -185,7 +250,7 @@ inline void operator<< (object::with_zone& o, signed int v) inline void operator<< (object::with_zone& o, signed long v) { static_cast(o) << v; } -inline void operator<< (object::with_zone& o, signed long long v) +inline void operator<< (object::with_zone& o, const signed long long& v) { static_cast(o) << v; } @@ -201,7 +266,7 @@ inline void operator<< (object::with_zone& o, unsigned int v) inline void operator<< (object::with_zone& o, unsigned long v) { static_cast(o) << v; } -inline void operator<< (object::with_zone& o, unsigned long long v) +inline void operator<< (object::with_zone& o, const unsigned long long& v) { static_cast(o) << v; } diff --git a/test/msgpack_test.cpp b/test/msgpack_test.cpp index 5606b9fd..5b60dc5b 100644 --- a/test/msgpack_test.cpp +++ b/test/msgpack_test.cpp @@ -45,6 +45,22 @@ const double kEPS = 1e-10; } \ } while(0) +TEST(MSGPACK, simple_buffer_char) +{ + GEN_TEST(char); +} + +TEST(MSGPACK, simple_buffer_signed_char) +{ + GEN_TEST(signed char); +} + +TEST(MSGPACK, simple_buffer_unsigned_char) +{ + GEN_TEST(unsigned char); +} + + TEST(MSGPACK, simple_buffer_short) { GEN_TEST(short); @@ -781,6 +797,21 @@ TEST(MSGPACK_USER_DEFINED, simple_buffer_union_member) } \ } while(0); +TEST(MSGPACK, vrefbuffer_char) +{ + GEN_TEST_VREF(char); +} + +TEST(MSGPACK, vrefbuffer_signed_char) +{ + GEN_TEST_VREF(signed char); +} + +TEST(MSGPACK, vrefbuffer_unsigned_char) +{ + GEN_TEST_VREF(unsigned char); +} + TEST(MSGPACK, vrefbuffer_short) { GEN_TEST_VREF(short); @@ -901,6 +932,21 @@ TEST(MSGPACK, vrefbuffer_int64) ; \ } +TEST(MSGPACK, stream_char) +{ + GEN_TEST_STREAM(char); +} + +TEST(MSGPACK, stream_signed_char) +{ + GEN_TEST_STREAM(signed char); +} + +TEST(MSGPACK, stream_unsigned_char) +{ + GEN_TEST_STREAM(unsigned char); +} + TEST(MSGPACK, stream_short) { GEN_TEST_STREAM(short); diff --git a/test/msgpackc_test.cpp b/test/msgpackc_test.cpp index 08c21d70..589ac4eb 100644 --- a/test/msgpackc_test.cpp +++ b/test/msgpackc_test.cpp @@ -76,6 +76,24 @@ const double kEPS = 1e-10; } \ } while(0) +TEST(MSGPACKC, simple_buffer_char) +{ +#if defined(CHAR_MIN) +#if CHAR_MIN < 0 + GEN_TEST_SIGNED(char, char); +#else + GEN_TEST_UNSIGNED(char, char); +#endif +#else +#error CHAR_MIN is not defined +#endif +} + +TEST(MSGPACKC, simple_buffer_singed_char) +{ + GEN_TEST_SIGNED(signed char, signed_char); +} + TEST(MSGPACKC, simple_buffer_short) { GEN_TEST_SIGNED(short, short); @@ -96,6 +114,11 @@ TEST(MSGPACKC, simple_buffer_long_long) GEN_TEST_SIGNED(long long, long_long); } +TEST(MSGPACKC, simple_buffer_unsigned_char) +{ + GEN_TEST_UNSIGNED(unsigned char, unsigned_char); +} + TEST(MSGPACKC, simple_buffer_unsigned_short) { GEN_TEST_UNSIGNED(unsigned short, unsigned_short);