Skip to content

Commit 09d90e1

Browse files
committed
Supported 'plain' char.
Note: char, signed char, and unsigned char are three different types.
1 parent 6f56345 commit 09d90e1

File tree

6 files changed

+192
-13
lines changed

6 files changed

+192
-13
lines changed

pack_template.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,28 @@ msgpack_pack_inline_func(_int64)(msgpack_pack_user x, int64_t d)
372372
msgpack_pack_real_int64(x, d);
373373
}
374374

375+
msgpack_pack_inline_func(_char)(msgpack_pack_user x, char d)
376+
{
377+
#if defined(CHAR_MIN)
378+
#if CHAR_MIN < 0
379+
msgpack_pack_real_int8(x, d);
380+
#else
381+
msgpack_pack_real_uint8(x, d);
382+
#endif
383+
#else
384+
#error CHAR_MIN is not defined
385+
#endif
386+
}
387+
388+
msgpack_pack_inline_func(_signed_char)(msgpack_pack_user x, signed char d)
389+
{
390+
msgpack_pack_real_int8(x, d);
391+
}
392+
393+
msgpack_pack_inline_func(_unsigned_char)(msgpack_pack_user x, unsigned char d)
394+
{
395+
msgpack_pack_real_uint8(x, d);
396+
}
375397

376398
#ifdef msgpack_pack_inline_func_cint
377399

src/msgpack/pack.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ static void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_w
5252
static msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback);
5353
static void msgpack_packer_free(msgpack_packer* pk);
5454

55+
static int msgpack_pack_char(msgpack_packer* pk, char d);
56+
57+
static int msgpack_pack_signed_char(msgpack_packer* pk, signed char d);
5558
static int msgpack_pack_short(msgpack_packer* pk, short d);
5659
static int msgpack_pack_int(msgpack_packer* pk, int d);
5760
static int msgpack_pack_long(msgpack_packer* pk, long d);
5861
static int msgpack_pack_long_long(msgpack_packer* pk, long long d);
62+
static int msgpack_pack_unsigned_char(msgpack_packer* pk, unsigned char d);
5963
static int msgpack_pack_unsigned_short(msgpack_packer* pk, unsigned short d);
6064
static int msgpack_pack_unsigned_int(msgpack_packer* pk, unsigned int d);
6165
static int msgpack_pack_unsigned_long(msgpack_packer* pk, unsigned long d);

src/msgpack/pack.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ class packer {
5454
packer<Stream>& pack_fix_int32(int32_t d);
5555
packer<Stream>& pack_fix_int64(int64_t d);
5656

57+
packer<Stream>& pack_char(char d);
58+
packer<Stream>& pack_signed_char(signed char d);
5759
packer<Stream>& pack_short(short d);
5860
packer<Stream>& pack_int(int d);
5961
packer<Stream>& pack_long(long d);
6062
packer<Stream>& pack_long_long(long long d);
63+
packer<Stream>& pack_unsigned_char(unsigned char d);
6164
packer<Stream>& pack_unsigned_short(unsigned short d);
6265
packer<Stream>& pack_unsigned_int(unsigned int d);
6366
packer<Stream>& pack_unsigned_long(unsigned long d);
@@ -96,10 +99,14 @@ class packer {
9699
static void _pack_fix_int32(Stream& x, int32_t d);
97100
static void _pack_fix_int64(Stream& x, int64_t d);
98101

102+
static void _pack_char(Stream& x, char d);
103+
104+
static void _pack_signed_char(Stream& x, signed char d);
99105
static void _pack_short(Stream& x, short d);
100106
static void _pack_int(Stream& x, int d);
101107
static void _pack_long(Stream& x, long d);
102108
static void _pack_long_long(Stream& x, long long d);
109+
static void _pack_unsigned_char(Stream& x, unsigned char d);
103110
static void _pack_unsigned_short(Stream& x, unsigned short d);
104111
static void _pack_unsigned_int(Stream& x, unsigned int d);
105112
static void _pack_unsigned_long(Stream& x, unsigned long d);
@@ -238,6 +245,14 @@ inline packer<Stream>& packer<Stream>::pack_fix_int64(int64_t d)
238245
{ _pack_fix_int64(m_stream, d); return *this;}
239246

240247

248+
template <typename Stream>
249+
inline packer<Stream>& packer<Stream>::pack_char(char d)
250+
{ _pack_char(m_stream, d); return *this; }
251+
252+
template <typename Stream>
253+
inline packer<Stream>& packer<Stream>::pack_signed_char(signed char d)
254+
{ _pack_signed_char(m_stream, d); return *this; }
255+
241256
template <typename Stream>
242257
inline packer<Stream>& packer<Stream>::pack_short(short d)
243258
{ _pack_short(m_stream, d); return *this; }
@@ -254,6 +269,10 @@ template <typename Stream>
254269
inline packer<Stream>& packer<Stream>::pack_long_long(long long d)
255270
{ _pack_long_long(m_stream, d); return *this; }
256271

272+
template <typename Stream>
273+
inline packer<Stream>& packer<Stream>::pack_unsigned_char(unsigned char d)
274+
{ _pack_unsigned_char(m_stream, d); return *this; }
275+
257276
template <typename Stream>
258277
inline packer<Stream>& packer<Stream>::pack_unsigned_short(unsigned short d)
259278
{ _pack_unsigned_short(m_stream, d); return *this; }

src/msgpack/type/int.hpp

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,69 @@ namespace detail {
5757
}
5858
};
5959

60+
template <typename T>
61+
struct is_signed {
62+
static const bool value = std::numeric_limits<T>::is_signed;
63+
};
64+
6065
template <typename T>
6166
static inline T convert_integer(object o)
6267
{
63-
return detail::convert_integer_sign<T, std::numeric_limits<T>::is_signed>::convert(o);
68+
return detail::convert_integer_sign<T, is_signed<T>::value>::convert(o);
69+
}
70+
71+
template <bool Signed>
72+
struct pack_char_sign;
73+
74+
template <>
75+
struct pack_char_sign<true> {
76+
template <typename Stream>
77+
static inline packer<Stream>& pack(packer<Stream>& o, char v) {
78+
o.pack_int8(v); return o;
79+
}
80+
};
81+
82+
template <>
83+
struct pack_char_sign<false> {
84+
template <typename Stream>
85+
static inline packer<Stream>& pack(packer<Stream>& o, char v) {
86+
o.pack_uint8(v); return o;
87+
}
88+
};
89+
90+
template <typename Stream>
91+
static inline packer<Stream>& pack_char(packer<Stream>& o, char v) {
92+
return pack_char_sign<is_signed<char>::value>::pack(o, v);
93+
}
94+
95+
template <bool Signed>
96+
struct object_char_sign;
97+
98+
template <>
99+
struct object_char_sign<true> {
100+
static inline void make(object& o, char v) {
101+
v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v
102+
: o.type = type::POSITIVE_INTEGER, o.via.u64 = v;
103+
}
104+
};
105+
106+
template <>
107+
struct object_char_sign<false> {
108+
static inline void make(object& o, char v) {
109+
o.type = type::POSITIVE_INTEGER, o.via.u64 = v;
110+
}
111+
};
112+
113+
static inline void object_char(object& o, char v) {
114+
return object_char_sign<is_signed<char>::value>::make(o, v);
64115
}
65116

66117
} // namespace detail
67118
} // namespace type
68119

120+
inline char& operator>> (object const& o, char& v)
121+
{ v = type::detail::convert_integer<char>(o); return v; }
122+
69123

70124
inline signed char& operator>> (object o, signed char& v)
71125
{ v = type::detail::convert_integer<signed char>(o); return v; }
@@ -98,21 +152,24 @@ inline unsigned long& operator>> (object o, unsigned long& v)
98152
inline unsigned long long& operator>> (object o, unsigned long long& v)
99153
{ v = type::detail::convert_integer<unsigned long long>(o); return v; }
100154

155+
template <typename Stream>
156+
inline packer<Stream>& operator<< (packer<Stream>& o, char v)
157+
{ return type::detail::pack_char(o, v); }
101158

102159
template <typename Stream>
103-
inline packer<Stream>& operator<< (packer<Stream>& o, const signed char& v)
160+
inline packer<Stream>& operator<< (packer<Stream>& o, signed char v)
104161
{ o.pack_int8(v); return o; }
105162

106163
template <typename Stream>
107-
inline packer<Stream>& operator<< (packer<Stream>& o, const signed short& v)
164+
inline packer<Stream>& operator<< (packer<Stream>& o, signed short v)
108165
{ o.pack_short(v); return o; }
109166

110167
template <typename Stream>
111-
inline packer<Stream>& operator<< (packer<Stream>& o, const signed int& v)
168+
inline packer<Stream>& operator<< (packer<Stream>& o, signed int v)
112169
{ o.pack_int(v); return o; }
113170

114171
template <typename Stream>
115-
inline packer<Stream>& operator<< (packer<Stream>& o, const signed long& v)
172+
inline packer<Stream>& operator<< (packer<Stream>& o, signed long v)
116173
{ o.pack_long(v); return o; }
117174

118175
template <typename Stream>
@@ -121,26 +178,30 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const signed long long& v)
121178

122179

123180
template <typename Stream>
124-
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned char& v)
181+
inline packer<Stream>& operator<< (packer<Stream>& o, unsigned char v)
125182
{ o.pack_uint8(v); return o; }
126183

127184
template <typename Stream>
128-
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned short& v)
185+
inline packer<Stream>& operator<< (packer<Stream>& o, unsigned short v)
129186
{ o.pack_unsigned_short(v); return o; }
130187

131188
template <typename Stream>
132-
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned int& v)
189+
inline packer<Stream>& operator<< (packer<Stream>& o, unsigned int v)
133190
{ o.pack_unsigned_int(v); return o; }
134191

135192
template <typename Stream>
136-
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned long& v)
193+
inline packer<Stream>& operator<< (packer<Stream>& o, unsigned long v)
137194
{ o.pack_unsigned_long(v); return o; }
138195

139196
template <typename Stream>
140197
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned long long& v)
141198
{ o.pack_unsigned_long_long(v); return o; }
142199

143200

201+
inline void operator<< (object& o, char v)
202+
{ type::detail::object_char(o, v); }
203+
204+
144205
inline void operator<< (object& o, signed char v)
145206
{ v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
146207

@@ -153,7 +214,7 @@ inline void operator<< (object& o, signed int v)
153214
inline void operator<< (object& o, signed long v)
154215
{ v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
155216

156-
inline void operator<< (object& o, signed long long v)
217+
inline void operator<< (object& o, const signed long long& v)
157218
{ v < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v : o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
158219

159220

@@ -169,10 +230,14 @@ inline void operator<< (object& o, unsigned int v)
169230
inline void operator<< (object& o, unsigned long v)
170231
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
171232

172-
inline void operator<< (object& o, unsigned long long v)
233+
inline void operator<< (object& o, const unsigned long long& v)
173234
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v; }
174235

175236

237+
inline void operator<< (object::with_zone& o, char v)
238+
{ static_cast<object&>(o) << v; }
239+
240+
176241
inline void operator<< (object::with_zone& o, signed char v)
177242
{ static_cast<object&>(o) << v; }
178243

@@ -185,7 +250,7 @@ inline void operator<< (object::with_zone& o, signed int v)
185250
inline void operator<< (object::with_zone& o, signed long v)
186251
{ static_cast<object&>(o) << v; }
187252

188-
inline void operator<< (object::with_zone& o, signed long long v)
253+
inline void operator<< (object::with_zone& o, const signed long long& v)
189254
{ static_cast<object&>(o) << v; }
190255

191256

@@ -201,7 +266,7 @@ inline void operator<< (object::with_zone& o, unsigned int v)
201266
inline void operator<< (object::with_zone& o, unsigned long v)
202267
{ static_cast<object&>(o) << v; }
203268

204-
inline void operator<< (object::with_zone& o, unsigned long long v)
269+
inline void operator<< (object::with_zone& o, const unsigned long long& v)
205270
{ static_cast<object&>(o) << v; }
206271

207272

test/msgpack_test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ const double kEPS = 1e-10;
4545
} \
4646
} while(0)
4747

48+
TEST(MSGPACK, simple_buffer_char)
49+
{
50+
GEN_TEST(char);
51+
}
52+
53+
TEST(MSGPACK, simple_buffer_signed_char)
54+
{
55+
GEN_TEST(signed char);
56+
}
57+
58+
TEST(MSGPACK, simple_buffer_unsigned_char)
59+
{
60+
GEN_TEST(unsigned char);
61+
}
62+
63+
4864
TEST(MSGPACK, simple_buffer_short)
4965
{
5066
GEN_TEST(short);
@@ -781,6 +797,21 @@ TEST(MSGPACK_USER_DEFINED, simple_buffer_union_member)
781797
} \
782798
} while(0);
783799

800+
TEST(MSGPACK, vrefbuffer_char)
801+
{
802+
GEN_TEST_VREF(char);
803+
}
804+
805+
TEST(MSGPACK, vrefbuffer_signed_char)
806+
{
807+
GEN_TEST_VREF(signed char);
808+
}
809+
810+
TEST(MSGPACK, vrefbuffer_unsigned_char)
811+
{
812+
GEN_TEST_VREF(unsigned char);
813+
}
814+
784815
TEST(MSGPACK, vrefbuffer_short)
785816
{
786817
GEN_TEST_VREF(short);
@@ -901,6 +932,21 @@ TEST(MSGPACK, vrefbuffer_int64)
901932
; \
902933
}
903934

935+
TEST(MSGPACK, stream_char)
936+
{
937+
GEN_TEST_STREAM(char);
938+
}
939+
940+
TEST(MSGPACK, stream_signed_char)
941+
{
942+
GEN_TEST_STREAM(signed char);
943+
}
944+
945+
TEST(MSGPACK, stream_unsigned_char)
946+
{
947+
GEN_TEST_STREAM(unsigned char);
948+
}
949+
904950
TEST(MSGPACK, stream_short)
905951
{
906952
GEN_TEST_STREAM(short);

test/msgpackc_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ const double kEPS = 1e-10;
7676
} \
7777
} while(0)
7878

79+
TEST(MSGPACKC, simple_buffer_char)
80+
{
81+
#if defined(CHAR_MIN)
82+
#if CHAR_MIN < 0
83+
GEN_TEST_SIGNED(char, char);
84+
#else
85+
GEN_TEST_UNSIGNED(char, char);
86+
#endif
87+
#else
88+
#error CHAR_MIN is not defined
89+
#endif
90+
}
91+
92+
TEST(MSGPACKC, simple_buffer_singed_char)
93+
{
94+
GEN_TEST_SIGNED(signed char, signed_char);
95+
}
96+
7997
TEST(MSGPACKC, simple_buffer_short)
8098
{
8199
GEN_TEST_SIGNED(short, short);
@@ -96,6 +114,11 @@ TEST(MSGPACKC, simple_buffer_long_long)
96114
GEN_TEST_SIGNED(long long, long_long);
97115
}
98116

117+
TEST(MSGPACKC, simple_buffer_unsigned_char)
118+
{
119+
GEN_TEST_UNSIGNED(unsigned char, unsigned_char);
120+
}
121+
99122
TEST(MSGPACKC, simple_buffer_unsigned_short)
100123
{
101124
GEN_TEST_UNSIGNED(unsigned short, unsigned_short);

0 commit comments

Comments
 (0)