diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am index 31096f0c..0979d235 100644 --- a/cpp/src/Makefile.am +++ b/cpp/src/Makefile.am @@ -58,10 +58,11 @@ nobase_include_HEADERS += \ msgpack/zone.hpp \ msgpack/type.hpp \ msgpack/type/bool.hpp \ + msgpack/type/deque.hpp \ msgpack/type/float.hpp \ + msgpack/type/fixint.hpp \ msgpack/type/int.hpp \ msgpack/type/list.hpp \ - msgpack/type/deque.hpp \ msgpack/type/map.hpp \ msgpack/type/nil.hpp \ msgpack/type/pair.hpp \ diff --git a/cpp/src/msgpack/pack.h b/cpp/src/msgpack/pack.h index 9c4ce595..c1564963 100644 --- a/cpp/src/msgpack/pack.h +++ b/cpp/src/msgpack/pack.h @@ -70,6 +70,15 @@ static int msgpack_pack_int16(msgpack_packer* pk, int16_t d); static int msgpack_pack_int32(msgpack_packer* pk, int32_t d); static int msgpack_pack_int64(msgpack_packer* pk, int64_t d); +static int msgpack_pack_fix_uint8(msgpack_packer* pk, uint8_t d); +static int msgpack_pack_fix_uint16(msgpack_packer* pk, uint16_t d); +static int msgpack_pack_fix_uint32(msgpack_packer* pk, uint32_t d); +static int msgpack_pack_fix_uint64(msgpack_packer* pk, uint64_t d); +static int msgpack_pack_fix_int8(msgpack_packer* pk, int8_t d); +static int msgpack_pack_fix_int16(msgpack_packer* pk, int16_t d); +static int msgpack_pack_fix_int32(msgpack_packer* pk, int32_t d); +static int msgpack_pack_fix_int64(msgpack_packer* pk, int64_t d); + static int msgpack_pack_float(msgpack_packer* pk, float d); static int msgpack_pack_double(msgpack_packer* pk, double d); @@ -99,6 +108,9 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d); #define msgpack_pack_inline_func_cint(name) \ inline int msgpack_pack ## name +#define msgpack_pack_inline_func_fixint(name) \ + inline int msgpack_pack_fix ## name + #define msgpack_pack_user msgpack_packer* #define msgpack_pack_append_buffer(user, buf, len) \ diff --git a/cpp/src/msgpack/pack.hpp b/cpp/src/msgpack/pack.hpp index 2f7dfa07..0090b961 100644 --- a/cpp/src/msgpack/pack.hpp +++ b/cpp/src/msgpack/pack.hpp @@ -45,6 +45,15 @@ public: packer& pack_int32(int32_t d); packer& pack_int64(int64_t d); + packer& pack_fix_uint8(uint8_t d); + packer& pack_fix_uint16(uint16_t d); + packer& pack_fix_uint32(uint32_t d); + packer& pack_fix_uint64(uint64_t d); + packer& pack_fix_int8(int8_t d); + packer& pack_fix_int16(int16_t d); + packer& pack_fix_int32(int32_t d); + packer& pack_fix_int64(int64_t d); + packer& pack_short(short d); packer& pack_int(int d); packer& pack_long(long d); @@ -78,6 +87,15 @@ private: static void _pack_int32(Stream& x, int32_t d); static void _pack_int64(Stream& x, int64_t d); + static void _pack_fix_uint8(Stream& x, uint8_t d); + static void _pack_fix_uint16(Stream& x, uint16_t d); + static void _pack_fix_uint32(Stream& x, uint32_t d); + static void _pack_fix_uint64(Stream& x, uint64_t d); + static void _pack_fix_int8(Stream& x, int8_t d); + static void _pack_fix_int16(Stream& x, int16_t d); + static void _pack_fix_int32(Stream& x, int32_t d); + static void _pack_fix_int64(Stream& x, int64_t 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); @@ -133,6 +151,10 @@ inline void pack(Stream& s, const T& v) template \ inline void packer::_pack ## name +#define msgpack_pack_inline_func_fixint(name) \ + template \ + inline void packer::_pack_fix ## name + #define msgpack_pack_user Stream& #define msgpack_pack_append_buffer append_buffer @@ -149,6 +171,7 @@ packer::packer(Stream& s) : m_stream(s) { } template packer::~packer() { } + template inline packer& packer::pack_uint8(uint8_t d) { _pack_uint8(m_stream, d); return *this; } @@ -182,6 +205,39 @@ inline packer& packer::pack_int64(int64_t d) { _pack_int64(m_stream, d); return *this;} +template +inline packer& packer::pack_fix_uint8(uint8_t d) +{ _pack_fix_uint8(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint16(uint16_t d) +{ _pack_fix_uint16(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint32(uint32_t d) +{ _pack_fix_uint32(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint64(uint64_t d) +{ _pack_fix_uint64(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int8(int8_t d) +{ _pack_fix_int8(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int16(int16_t d) +{ _pack_fix_int16(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int32(int32_t d) +{ _pack_fix_int32(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int64(int64_t d) +{ _pack_fix_int64(m_stream, d); return *this;} + + template inline packer& packer::pack_short(short d) { _pack_short(m_stream, d); return *this; } diff --git a/cpp/src/msgpack/type.hpp b/cpp/src/msgpack/type.hpp index a55c68e7..bca69bfd 100644 --- a/cpp/src/msgpack/type.hpp +++ b/cpp/src/msgpack/type.hpp @@ -1,8 +1,9 @@ #include "type/bool.hpp" +#include "type/deque.hpp" +#include "type/fixint.hpp" #include "type/float.hpp" #include "type/int.hpp" #include "type/list.hpp" -#include "type/deque.hpp" #include "type/map.hpp" #include "type/nil.hpp" #include "type/pair.hpp" diff --git a/cpp/src/msgpack/type/fixint.hpp b/cpp/src/msgpack/type/fixint.hpp new file mode 100644 index 00000000..da584159 --- /dev/null +++ b/cpp/src/msgpack/type/fixint.hpp @@ -0,0 +1,89 @@ +// +// MessagePack for C++ static resolution routine +// +// Copyright (C) 2020 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#ifndef MSGPACK_TYPE_FIXINT_HPP__ +#define MSGPACK_TYPE_FIXINT_HPP__ + +#include "msgpack/object.hpp" + +namespace msgpack { + +namespace type { + + +template +struct fix_int { + fix_int() : value(0) { } + fix_int(T value) : value(value) { } + operator T() const { return value; } + T get() const { return value; } +private: + const T value; +}; + + +typedef fix_int fix_uint8; +typedef fix_int fix_uint16; +typedef fix_int fix_uint32; +typedef fix_int fix_uint64; + +typedef fix_int fix_int8; +typedef fix_int fix_int16; +typedef fix_int fix_int32; +typedef fix_int fix_int64; + + +} // namespace type + + +template +inline packer& operator<< (packer& o, const type::fix_int8& v) + { o.pack_fix_int8(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int16& v) + { o.pack_fix_int16(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int32& v) + { o.pack_fix_int32(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int64& v) + { o.pack_fix_int64(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint8& v) + { o.pack_fix_uint8(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint16& v) + { o.pack_fix_uint16(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint32& v) + { o.pack_fix_uint32(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint64& v) + { o.pack_fix_uint64(v); return o; } + + +} // namespace msgpack + +#endif /* msgpack/type/fixint.hpp */ + diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am index bb9439e3..5225f28b 100644 --- a/cpp/test/Makefile.am +++ b/cpp/test/Makefile.am @@ -13,6 +13,8 @@ check_PROGRAMS = \ convert \ buffer \ cases \ + fixint \ + fixint_c \ version \ msgpackc_test \ msgpack_test @@ -38,6 +40,10 @@ buffer_LDADD = -lz cases_SOURCES = cases.cc +fixint_SOURCES = fixint.cc + +fixint_c_SOURCES = fixint_c.cc + version_SOURCES = version.cc msgpackc_test_SOURCES = msgpackc_test.cpp diff --git a/cpp/test/fixint.cc b/cpp/test/fixint.cc new file mode 100644 index 00000000..64a39acd --- /dev/null +++ b/cpp/test/fixint.cc @@ -0,0 +1,24 @@ +#include +#include + +template +void check_size(size_t size) { + T v(0); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, v); + EXPECT_EQ(size, sbuf.size()); +} + +TEST(fixint, size) +{ + check_size(2); + check_size(3); + check_size(5); + check_size(9); + + check_size(2); + check_size(3); + check_size(5); + check_size(9); +} + diff --git a/cpp/test/fixint_c.cc b/cpp/test/fixint_c.cc new file mode 100644 index 00000000..caa4d262 --- /dev/null +++ b/cpp/test/fixint_c.cc @@ -0,0 +1,32 @@ +#include +#include + +TEST(fixint, size) +{ + msgpack_sbuffer* sbuf = msgpack_sbuffer_new(); + msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write); + + size_t sum = 0; + + EXPECT_EQ(0, msgpack_pack_fix_int8(pk, 0)); + EXPECT_EQ(sum+=2, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_int16(pk, 0)); + EXPECT_EQ(sum+=3, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_int32(pk, 0)); + EXPECT_EQ(sum+=5, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_int64(pk, 0)); + EXPECT_EQ(sum+=9, sbuf->size); + + EXPECT_EQ(0, msgpack_pack_fix_uint8(pk, 0)); + EXPECT_EQ(sum+=2, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_uint16(pk, 0)); + EXPECT_EQ(sum+=3, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_uint32(pk, 0)); + EXPECT_EQ(sum+=5, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_uint64(pk, 0)); + EXPECT_EQ(sum+=9, sbuf->size); + + msgpack_sbuffer_free(sbuf); + msgpack_packer_free(pk); +} +