mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-22 16:33:49 +01:00
cpp: adds fixed length serialization for integers
This commit is contained in:
parent
59ba8dec4e
commit
fe2a0f5089
@ -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 \
|
||||
|
@ -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) \
|
||||
|
@ -45,6 +45,15 @@ public:
|
||||
packer<Stream>& pack_int32(int32_t d);
|
||||
packer<Stream>& pack_int64(int64_t d);
|
||||
|
||||
packer<Stream>& pack_fix_uint8(uint8_t d);
|
||||
packer<Stream>& pack_fix_uint16(uint16_t d);
|
||||
packer<Stream>& pack_fix_uint32(uint32_t d);
|
||||
packer<Stream>& pack_fix_uint64(uint64_t d);
|
||||
packer<Stream>& pack_fix_int8(int8_t d);
|
||||
packer<Stream>& pack_fix_int16(int16_t d);
|
||||
packer<Stream>& pack_fix_int32(int32_t d);
|
||||
packer<Stream>& pack_fix_int64(int64_t d);
|
||||
|
||||
packer<Stream>& pack_short(short d);
|
||||
packer<Stream>& pack_int(int d);
|
||||
packer<Stream>& 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 <typename Stream> \
|
||||
inline void packer<Stream>::_pack ## name
|
||||
|
||||
#define msgpack_pack_inline_func_fixint(name) \
|
||||
template <typename Stream> \
|
||||
inline void packer<Stream>::_pack_fix ## name
|
||||
|
||||
#define msgpack_pack_user Stream&
|
||||
|
||||
#define msgpack_pack_append_buffer append_buffer
|
||||
@ -149,6 +171,7 @@ packer<Stream>::packer(Stream& s) : m_stream(s) { }
|
||||
template <typename Stream>
|
||||
packer<Stream>::~packer() { }
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_uint8(uint8_t d)
|
||||
{ _pack_uint8(m_stream, d); return *this; }
|
||||
@ -182,6 +205,39 @@ inline packer<Stream>& packer<Stream>::pack_int64(int64_t d)
|
||||
{ _pack_int64(m_stream, d); return *this;}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_fix_uint8(uint8_t d)
|
||||
{ _pack_fix_uint8(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_fix_uint16(uint16_t d)
|
||||
{ _pack_fix_uint16(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_fix_uint32(uint32_t d)
|
||||
{ _pack_fix_uint32(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_fix_uint64(uint64_t d)
|
||||
{ _pack_fix_uint64(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_fix_int8(int8_t d)
|
||||
{ _pack_fix_int8(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_fix_int16(int16_t d)
|
||||
{ _pack_fix_int16(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_fix_int32(int32_t d)
|
||||
{ _pack_fix_int32(m_stream, d); return *this; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_fix_int64(int64_t d)
|
||||
{ _pack_fix_int64(m_stream, d); return *this;}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& packer<Stream>::pack_short(short d)
|
||||
{ _pack_short(m_stream, d); return *this; }
|
||||
|
@ -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"
|
||||
|
89
cpp/src/msgpack/type/fixint.hpp
Normal file
89
cpp/src/msgpack/type/fixint.hpp
Normal file
@ -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 <typename T>
|
||||
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<uint8_t> fix_uint8;
|
||||
typedef fix_int<uint16_t> fix_uint16;
|
||||
typedef fix_int<uint32_t> fix_uint32;
|
||||
typedef fix_int<uint64_t> fix_uint64;
|
||||
|
||||
typedef fix_int<int8_t> fix_int8;
|
||||
typedef fix_int<int16_t> fix_int16;
|
||||
typedef fix_int<int32_t> fix_int32;
|
||||
typedef fix_int<int64_t> fix_int64;
|
||||
|
||||
|
||||
} // namespace type
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int8& v)
|
||||
{ o.pack_fix_int8(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int16& v)
|
||||
{ o.pack_fix_int16(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int32& v)
|
||||
{ o.pack_fix_int32(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int64& v)
|
||||
{ o.pack_fix_int64(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint8& v)
|
||||
{ o.pack_fix_uint8(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint16& v)
|
||||
{ o.pack_fix_uint16(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint32& v)
|
||||
{ o.pack_fix_uint32(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint64& v)
|
||||
{ o.pack_fix_uint64(v); return o; }
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/fixint.hpp */
|
||||
|
@ -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
|
||||
|
24
cpp/test/fixint.cc
Normal file
24
cpp/test/fixint.cc
Normal file
@ -0,0 +1,24 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
template <typename T>
|
||||
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<msgpack::type::fix_int8>(2);
|
||||
check_size<msgpack::type::fix_int16>(3);
|
||||
check_size<msgpack::type::fix_int32>(5);
|
||||
check_size<msgpack::type::fix_int64>(9);
|
||||
|
||||
check_size<msgpack::type::fix_uint8>(2);
|
||||
check_size<msgpack::type::fix_uint16>(3);
|
||||
check_size<msgpack::type::fix_uint32>(5);
|
||||
check_size<msgpack::type::fix_uint64>(9);
|
||||
}
|
||||
|
32
cpp/test/fixint_c.cc
Normal file
32
cpp/test/fixint_c.cc
Normal file
@ -0,0 +1,32 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user