Skip to content

Commit

Permalink
Fixed msgpack#243.
Browse files Browse the repository at this point in the history
std::vector<unsigned char> and std::array<unsigned char> are mapped to BIN.
std::vector<uint8_t> and std::array<uint8_t> are mapped to BIN if uint8_t is the same type of unsigned char, otherwise mapped to ARRAY.

Added array_ref. When client wraps BIN mapped types above with array_ref as msgpack::type::array_ref<std::vector<char> >, the type is mapped to ARRAY.
  • Loading branch information
redboltz committed Aug 31, 2015
1 parent 9ee1168 commit a4c5382
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ IF (MSGPACK_ENABLE_CXX)
LIST (APPEND msgpack_HEADERS
include/msgpack.hpp
include/msgpack/adaptor/adaptor_base.hpp
include/msgpack/adaptor/array_ref.hpp
include/msgpack/adaptor/bool.hpp
include/msgpack/adaptor/boost/fusion.hpp
include/msgpack/adaptor/boost/optional.hpp
Expand Down
17 changes: 17 additions & 0 deletions include/msgpack/cpp_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ template <typename T>
struct enable_if<false, T> {
};

template<typename T, T val>
struct integral_constant {
static T const value = val;
typedef T value_type;
typedef integral_constant<T, val> type;
};

typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;

template<class T, class U>
struct is_same : false_type {};

template<class T>
struct is_same<T, T> : true_type {};

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
Expand All @@ -104,6 +120,7 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
using std::move;
using std::swap;
using std::enable_if;
using std::is_same;

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
Expand Down
3 changes: 3 additions & 0 deletions include/msgpack/type.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cpp_config.hpp"
#include "adaptor/array_ref.hpp"
#include "adaptor/bool.hpp"
#include "adaptor/char_ptr.hpp"
#include "adaptor/deque.hpp"
Expand All @@ -17,6 +18,7 @@
#include "adaptor/vector.hpp"
#include "adaptor/vector_bool.hpp"
#include "adaptor/vector_char.hpp"
#include "adaptor/vector_unsigned_char.hpp"
#include "adaptor/msgpack_tuple.hpp"
#include "adaptor/define.hpp"

Expand All @@ -29,6 +31,7 @@

#include "adaptor/cpp11/array.hpp"
#include "adaptor/cpp11/array_char.hpp"
#include "adaptor/cpp11/array_unsigned_char.hpp"
#include "adaptor/cpp11/forward_list.hpp"
#include "adaptor/cpp11/shared_ptr.hpp"
#include "adaptor/cpp11/tuple.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ if ENABLE_CXX
nobase_include_HEADERS += \
../include/msgpack.hpp \
../include/msgpack/adaptor/adaptor_base.hpp \
../include/msgpack/adaptor/array_ref.hpp \
../include/msgpack/adaptor/bool.hpp \
../include/msgpack/adaptor/boost/fusion.hpp \
../include/msgpack/adaptor/boost/optional.hpp \
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ INCLUDE_DIRECTORIES (
)

LIST (APPEND check_PROGRAMS
array_ref.cpp
buffer.cpp
cases.cpp
convert.cpp
Expand Down
2 changes: 2 additions & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ AM_C_CPPFLAGS = -I$(top_srcdir)/include -pthread
AM_LDFLAGS = $(top_builddir)/src/libmsgpack.la -lgtest_main -lgtest -lpthread

check_PROGRAMS = \
array_ref \
buffer \
cases \
convert \
Expand Down Expand Up @@ -45,6 +46,7 @@ check_PROGRAMS += \

TESTS = $(check_PROGRAMS)

array_ref_SOURCES = array_ref.cpp
buffer_SOURCES = buffer.cpp
cases_SOURCES = cases.cpp
convert_SOURCES = convert.cpp
Expand Down
37 changes: 37 additions & 0 deletions test/msgpack_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,43 @@ TEST(MSGPACK_STL, simple_buffer_vector_char)
}
}

TEST(MSGPACK_STL, simple_buffer_vector_unsigned_char)
{
typedef vector<unsigned char, test::allocator<unsigned char> > type;
for (unsigned int k = 0; k < kLoop; k++) {
type val1;
for (unsigned int i = 0; i < kElements; i++)
val1.push_back(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);
type const& val2 = ret.get().as<type>();
EXPECT_EQ(val1.size(), val2.size());
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
}
}

TEST(MSGPACK_STL, simple_buffer_vector_uint8_t)
{
if (!msgpack::is_same<uint8_t, unsigned char>::value) return;
typedef vector<uint8_t, test::allocator<uint8_t> > type;
for (unsigned int k = 0; k < kLoop; k++) {
type val1;
for (unsigned int i = 0; i < kElements; i++)
val1.push_back(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);
type const& val2 = ret.get().as<type>();
EXPECT_EQ(val1.size(), val2.size());
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
}
}

TEST(MSGPACK_STL, simple_buffer_vector_bool)
{
typedef vector<bool, test::allocator<bool> > type;
Expand Down
18 changes: 18 additions & 0 deletions test/msgpack_cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ TEST(MSGPACK_CPP11, simple_buffer_array_char)
}
}

TEST(MSGPACK_CPP11, simple_buffer_array_unsigned_char)
{
if (!msgpack::is_same<uint8_t, unsigned char>::value) return;
for (unsigned int k = 0; k < kLoop; k++) {
array<unsigned 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<unsigned char, kElements> val2 = ret.get().as<array<unsigned char, kElements> >();
EXPECT_EQ(val1.size(), val2.size());
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
}
}

// strong typedefs
namespace test {

Expand Down
67 changes: 67 additions & 0 deletions test/object_with_zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,39 @@ TEST(object_without_zone, vector_char)
}
}

// vector_unsgined_char
TEST(object_with_zone, vector_unsigned_char)
{
if (!msgpack::is_same<uint8_t, unsigned char>::value) return;
for (unsigned int k = 0; k < kLoop; k++) {
vector<unsigned char> v1;
v1.push_back(1);
for (unsigned int i = 1; i < kElements; i++)
v1.push_back(static_cast<unsigned char>(i));
msgpack::zone z;
msgpack::object obj(v1, z);
EXPECT_EQ(obj.as<vector<unsigned char> >(), v1);
v1.front() = 42;
EXPECT_EQ(obj.as<vector<unsigned char> >().front(), 1);
}
}

TEST(object_without_zone, vector_unsigned_char)
{
if (!msgpack::is_same<uint8_t, unsigned char>::value) return;
for (unsigned int k = 0; k < kLoop; k++) {
vector<unsigned char> v1;
v1.push_back(1);
for (unsigned int i = 1; i < kElements; i++)
v1.push_back(static_cast<unsigned char>(i));
msgpack::object obj(v1);
EXPECT_EQ(obj.as<vector<unsigned char> >(), v1);
v1.front() = 42;
// obj refer to v1
EXPECT_EQ(obj.as<vector<unsigned char> >().front(), 42);
}
}

// list
TEST(object_with_zone, list)
{
Expand Down Expand Up @@ -830,6 +863,40 @@ TEST(object_without_zone, array_char)
}
}

TEST(object_with_zone, array_unsigned_char)
{
if (!msgpack::is_same<uint8_t, unsigned char>::value) return;
typedef array<unsigned char, kElements> test_t;
for (unsigned int k = 0; k < kLoop; k++) {
test_t v1;
v1[0] = 1;
for (unsigned int i = 1; i < kElements; i++)
v1[i] = rand();
msgpack::zone z;
msgpack::object obj(v1, z);
EXPECT_EQ(obj.as<test_t>(), v1);
v1.front() = 42;
EXPECT_EQ(obj.as<test_t>().front(), 1);
}
}

TEST(object_without_zone, array_unsigned_char)
{
if (!msgpack::is_same<uint8_t, unsigned char>::value) return;
typedef array<unsigned char, kElements> test_t;
for (unsigned int k = 0; k < kLoop; k++) {
test_t v1;
v1[0] = 1;
for (unsigned int i = 1; i < kElements; i++)
v1[i] = rand();
msgpack::object obj(v1);
EXPECT_EQ(obj.as<test_t>(), v1);
v1.front() = 42;
// obj refer to v1
EXPECT_EQ(obj.as<test_t>().front(), 42);
}
}


TEST(object_with_zone, forward_list)
{
Expand Down

0 comments on commit a4c5382

Please sign in to comment.