mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-11-10 08:08:32 +01:00
Added the C++11 std::array support.
std::array<char, N> is mapped to BIN. That is the same mannar as std::vector.
This commit is contained in:
@@ -79,6 +79,8 @@ IF (MSGPACK_ENABLE_CXX)
|
||||
LIST (APPEND msgpack_HEADERS
|
||||
include/msgpack.hpp
|
||||
include/msgpack/adaptor/bool.hpp
|
||||
include/msgpack/adaptor/cpp11/array.hpp
|
||||
include/msgpack/adaptor/cpp11/array_char.hpp
|
||||
include/msgpack/adaptor/cpp11/tuple.hpp
|
||||
include/msgpack/adaptor/define.hpp
|
||||
include/msgpack/adaptor/deque.hpp
|
||||
|
||||
68
include/msgpack/adaptor/cpp11/array.hpp
Normal file
68
include/msgpack/adaptor/cpp11/array.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2014 KONDO Takatoshi
|
||||
//
|
||||
// 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_CPP11_ARRAY_HPP
|
||||
#define MSGPACK_CPP11_ARRAY_HPP
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include "msgpack/cpp_config.hpp"
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
inline std::array<T, N>& operator>> (object const& o, std::array<T, N>& v) {
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
if(o.via.array.size != N) { throw type_error(); }
|
||||
if(o.via.array.size > 0) {
|
||||
object* p = o.via.array.ptr;
|
||||
object* const pend = o.via.array.ptr + o.via.array.size;
|
||||
T* it = &v[0];
|
||||
do {
|
||||
p->convert(*it);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T, std::size_t N>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::array<T, N>& v) {
|
||||
o.pack_array(v.size());
|
||||
for(auto const& e : v) o.pack(e);
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
inline void operator<< (object::with_zone& o, const std::array<T, N>& v) {
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = nullptr;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = static_cast<object*>(o.zone->allocate_align(sizeof(object)*v.size()));
|
||||
for (auto const& e : v) *p++ = object(e, o.zone);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif // MSGPACK_CPP11_ARRAY_HPP
|
||||
74
include/msgpack/adaptor/cpp11/array_char.hpp
Normal file
74
include/msgpack/adaptor/cpp11/array_char.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2014 KONDO Takatoshi
|
||||
//
|
||||
// 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_ARRAY_CHAR_HPP
|
||||
#define MSGPACK_TYPE_ARRAY_CHAR_HPP
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <array>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
template <std::size_t N>
|
||||
inline std::array<char, N>& operator>> (object const& o, std::array<char, N>& v)
|
||||
{
|
||||
switch (o.type) {
|
||||
case type::BIN:
|
||||
if(o.via.bin.size != N) { throw type_error(); }
|
||||
std::memcpy(v.data(), o.via.bin.ptr, o.via.bin.size);
|
||||
break;
|
||||
case type::STR:
|
||||
if(o.via.str.size != N) { throw type_error(); }
|
||||
std::memcpy(v.data(), o.via.str.ptr, N);
|
||||
break;
|
||||
default:
|
||||
throw type_error();
|
||||
break;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, std::size_t N>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::array<char, N>& v)
|
||||
{
|
||||
o.pack_bin(v.size());
|
||||
o.pack_bin_body(v.data(), v.size());
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
inline void operator<< (object& o, const std::array<char, N>& v)
|
||||
{
|
||||
o.type = type::BIN;
|
||||
o.via.bin.ptr = v.data();
|
||||
o.via.bin.size = static_cast<uint32_t>(v.size());
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
inline void operator<< (object::with_zone& o, const std::array<char, N>& v)
|
||||
{
|
||||
o.type = type::BIN;
|
||||
char* ptr = static_cast<char*>(o.zone->allocate_align(v.size()));
|
||||
o.via.bin.ptr = ptr;
|
||||
o.via.bin.size = static_cast<uint32_t>(v.size());
|
||||
std::memcpy(ptr, v.data(), v.size());
|
||||
}
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif // MSGPACK_TYPE_ARRAY_CHAR_HPP
|
||||
@@ -20,7 +20,8 @@
|
||||
|
||||
#if !defined(MSGPACK_USE_CPP03)
|
||||
|
||||
#include "adaptor/cpp11/array.hpp"
|
||||
#include "adaptor/cpp11/array_char.hpp"
|
||||
#include "adaptor/cpp11/tuple.hpp"
|
||||
|
||||
#endif // !defined(MSGPACK_USE_CPP03)
|
||||
|
||||
|
||||
@@ -53,6 +53,8 @@ if ENABLE_CXX
|
||||
nobase_include_HEADERS += \
|
||||
../include/msgpack.hpp \
|
||||
../include/msgpack/adaptor/bool.hpp \
|
||||
../include/msgpack/adaptor/cpp11/array.hpp \
|
||||
../include/msgpack/adaptor/cpp11/array_char.hpp \
|
||||
../include/msgpack/adaptor/cpp11/tuple.hpp \
|
||||
../include/msgpack/adaptor/define.hpp \
|
||||
../include/msgpack/adaptor/deque.hpp \
|
||||
|
||||
@@ -23,6 +23,7 @@ SET (check_PROGRAMS
|
||||
msgpack_tuple.cc
|
||||
msgpack_test.cpp
|
||||
msgpackc_test.cpp
|
||||
msgpackc_test_cpp11.cpp
|
||||
reference.cc
|
||||
)
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ check_PROGRAMS = \
|
||||
msgpack_tuple \
|
||||
msgpackc_test \
|
||||
msgpack_test \
|
||||
msgpack_test_cpp11 \
|
||||
reference
|
||||
|
||||
TESTS = $(check_PROGRAMS)
|
||||
@@ -53,6 +54,8 @@ msgpackc_test_SOURCES = msgpackc_test.cpp
|
||||
|
||||
msgpack_test_SOURCES = msgpack_test.cpp
|
||||
|
||||
msgpack_test_cpp11_SOURCES = msgpack_test_cpp11.cpp
|
||||
|
||||
reference_SOURCES = reference.cc
|
||||
|
||||
EXTRA_DIST = cases.mpac cases_compact.mpac
|
||||
|
||||
@@ -862,22 +862,6 @@ TEST(MSGPACK_TR1, simple_buffer_unordered_multiset)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(MSGPACK_USE_CPP03)
|
||||
// C++11
|
||||
|
||||
TEST(MSGPACK_CPP11, simple_tuple)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
std::tuple<bool, std::string, double> val1(true, "kzk", 12.3);
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
std::tuple<bool, std::string, double> val2 = ret.get().as<std::tuple<bool, std::string, double> >();
|
||||
EXPECT_EQ(val1, val2);
|
||||
EXPECT_FALSE(ret.referenced());
|
||||
}
|
||||
|
||||
#endif // !defined(MSGPACK_USE_CPP03)
|
||||
|
||||
// User-Defined Structures
|
||||
|
||||
@@ -984,45 +968,6 @@ TEST(MSGPACK_USER_DEFINED, simple_buffer_enum_member)
|
||||
EXPECT_FALSE(ret.referenced());
|
||||
}
|
||||
|
||||
#if !defined(MSGPACK_USE_CPP03)
|
||||
|
||||
class TestEnumClassMemberClass
|
||||
{
|
||||
public:
|
||||
TestEnumClassMemberClass()
|
||||
: t1(TestEnumClassType::STATE_A), t2(TestEnumClassType::STATE_B), t3(TestEnumClassType::STATE_C) {}
|
||||
|
||||
enum class TestEnumClassType:long {
|
||||
STATE_INVALID = 0,
|
||||
STATE_A = 1,
|
||||
STATE_B = 2,
|
||||
STATE_C = 3
|
||||
};
|
||||
TestEnumClassType t1;
|
||||
TestEnumClassType t2;
|
||||
TestEnumClassType t3;
|
||||
|
||||
MSGPACK_DEFINE(t1, t2, t3);
|
||||
};
|
||||
|
||||
MSGPACK_ADD_ENUM(TestEnumClassMemberClass::TestEnumClassType);
|
||||
|
||||
TEST(MSGPACK_USER_DEFINED, simple_buffer_enum_class_member)
|
||||
{
|
||||
TestEnumClassMemberClass val1;
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
TestEnumClassMemberClass val2 = ret.get().as<TestEnumClassMemberClass>();
|
||||
EXPECT_EQ(val1.t1, val2.t1);
|
||||
EXPECT_EQ(val1.t2, val2.t2);
|
||||
EXPECT_EQ(val1.t3, val2.t3);
|
||||
EXPECT_FALSE(ret.referenced());
|
||||
}
|
||||
|
||||
#endif // !defined(MSGPACK_USE_CPP03)
|
||||
|
||||
class TestUnionMemberClass
|
||||
{
|
||||
public:
|
||||
|
||||
102
test/msgpack_test_cpp11.cpp
Normal file
102
test/msgpack_test_cpp11.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "msgpack.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
const unsigned int kLoop = 10000;
|
||||
const unsigned int kElements = 100;
|
||||
const double kEPS = 1e-10;
|
||||
|
||||
#if !defined(MSGPACK_USE_CPP03)
|
||||
|
||||
// C++11
|
||||
|
||||
TEST(MSGPACK_CPP11, simple_tuple)
|
||||
{
|
||||
msgpack::sbuffer sbuf;
|
||||
std::tuple<bool, std::string, double> val1(true, "kzk", 12.3);
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
std::tuple<bool, std::string, double> val2 = ret.get().as<std::tuple<bool, std::string, double> >();
|
||||
EXPECT_EQ(val1, val2);
|
||||
EXPECT_FALSE(ret.referenced());
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP11, simple_array)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
array<int, kElements> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1[i] = rand();
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
EXPECT_EQ(ret.get().type, msgpack::type::ARRAY);
|
||||
array<int, kElements> val2 = ret.get().as<array<int, kElements> >();
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
|
||||
EXPECT_FALSE(ret.referenced());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP11, simple_buffer_array_char)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
array<char, kElements> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1[i] = rand();
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
EXPECT_EQ(ret.get().type, msgpack::type::BIN);
|
||||
array<char, kElements> val2 = ret.get().as<array<char, kElements> >();
|
||||
EXPECT_EQ(val1.size(), val2.size());
|
||||
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TestEnumClassMemberClass
|
||||
{
|
||||
public:
|
||||
TestEnumClassMemberClass()
|
||||
: t1(TestEnumClassType::STATE_A), t2(TestEnumClassType::STATE_B), t3(TestEnumClassType::STATE_C) {}
|
||||
|
||||
enum class TestEnumClassType:long {
|
||||
STATE_INVALID = 0,
|
||||
STATE_A = 1,
|
||||
STATE_B = 2,
|
||||
STATE_C = 3
|
||||
};
|
||||
TestEnumClassType t1;
|
||||
TestEnumClassType t2;
|
||||
TestEnumClassType t3;
|
||||
|
||||
MSGPACK_DEFINE(t1, t2, t3);
|
||||
};
|
||||
|
||||
MSGPACK_ADD_ENUM(TestEnumClassMemberClass::TestEnumClassType);
|
||||
|
||||
TEST(MSGPACK_USER_DEFINED, simple_buffer_enum_class_member)
|
||||
{
|
||||
TestEnumClassMemberClass val1;
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
TestEnumClassMemberClass val2 = ret.get().as<TestEnumClassMemberClass>();
|
||||
EXPECT_EQ(val1.t1, val2.t1);
|
||||
EXPECT_EQ(val1.t2, val2.t2);
|
||||
EXPECT_EQ(val1.t3, val2.t3);
|
||||
EXPECT_FALSE(ret.referenced());
|
||||
}
|
||||
|
||||
#endif // !defined(MSGPACK_USE_CPP03)
|
||||
Reference in New Issue
Block a user