Compare commits

...

22 Commits

Author SHA1 Message Date
Takatoshi Kondo b76c8aeb83 Merge pull request #384 from jpetso/jpetso_refwrap
Support to-object conversions for std::reference_wrapper<const T>.
2015-11-13 10:23:44 +09:00
Jakob Petsovits b5599ef3fc Support to-object conversions for std::reference_wrapper<const T>.
Previously the conversion would fail because struct object is not
generally provided for the const version of the type, but because
the wrapper would pass down the type unchanged, it would look for
exactly that missing template specialization unsuccessfully.

This is specifically an issue for std::reference_wrapper because
std::cref() returns an std::reference_wrapper<const T>.
2015-11-11 19:40:22 -05:00
Takatoshi Kondo 1a7aa5e5ec Merge pull request #383 from bpay/vs2015-cpp11
Fix VS2015 still using C++03 API
2015-11-10 22:33:46 +09:00
Ben Payne aad5b96083 Fix VS2015 still using C++03 API 2015-11-08 15:58:55 -05:00
Takatoshi Kondo dea3190d36 Merge pull request #381 from mogemimi/fix-minor-typo
Fix minor typos
2015-11-07 20:41:58 +09:00
mogemimi c001a52582 Fix minor typos 2015-11-07 15:47:11 +09:00
Takatoshi Kondo f58eb11fd5 Merge pull request #380 from whoshuu/expand-buffer-tests
Add tests for expanding an unpacker buffer
2015-11-01 12:02:01 +09:00
Takatoshi Kondo 134beee0cd Merge pull request #379 from whoshuu/increase-unpack-coverage
Add coverage for all free unpack codepaths
2015-10-28 11:10:13 +09:00
Huu Nguyen 434dae8424 Add tests for expanding an unpacker buffer 2015-10-26 23:08:43 -04:00
Huu Nguyen d8dd77884b Add coverage for all free unpack codepaths 2015-10-26 21:27:11 -04:00
Takatoshi Kondo ea991d5a01 Merge pull request #378 from whoshuu/add-nil-pack-test
Add test for packing/unpacking nil
2015-10-25 19:06:50 +09:00
Huu Nguyen 6a127eb24f Add test for packing/unpacking nil 2015-10-23 08:49:06 -07:00
Takatoshi Kondo 5e57dc5da7 Merge pull request #376 from tbeu/patch-1
Only set MSGPACK_DLLEXPORT if not already defined
2015-10-20 12:50:51 +09:00
Takatoshi Kondo d23a649427 Merge pull request #375 from whoshuu/test-parse-error
Write test case for parse error when unpacking unused 0xc1
2015-10-20 12:50:09 +09:00
tbeu 12ae60fd77 Only set MSGPACK_DLLEXPORT if not already defined
Allow applications that inlcude msgpack.h to predefine MSGPACK_DLLEXPORT, e.g., to not export any symbols in case of MSVC.
2015-10-18 22:55:32 +02:00
Huu Nguyen 282b0b5927 Write test case for parse error when unpacking unused 0xc1 2015-10-18 15:14:38 -04:00
Takatoshi Kondo 0a261fca42 Merge pull request #373 from redboltz/add_ref_wrapper
Added std::reference_wrapper packing and making object and object_wit…
2015-10-15 11:02:47 +09:00
Takatoshi Kondo 20104301f3 Added convert adaptor support. 2015-10-11 12:16:34 +09:00
Takatoshi Kondo 428780db53 Added std::reference_wrapper packing and making object and object_with_zone support.
Fixed #370.
2015-10-09 15:45:29 +09:00
Takatoshi Kondo 6bf5160bf2 Merge branch 'spatz-patch-4' 2015-09-10 22:12:11 +09:00
Takatoshi Kondo ef45359d81 Fix warning with -Wconversion at pack_v4raw(). 2015-09-10 22:11:52 +09:00
Dror Levin 3202bb6a2e Fix warning with -Wconversion 2015-09-06 13:07:40 +03:00
16 changed files with 315 additions and 18 deletions
+1
View File
@@ -176,6 +176,7 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/adaptor/cpp11/array_char.hpp
include/msgpack/adaptor/cpp11/array_unsigned_char.hpp
include/msgpack/adaptor/cpp11/forward_list.hpp
include/msgpack/adaptor/cpp11/reference_wrapper.hpp
include/msgpack/adaptor/cpp11/shared_ptr.hpp
include/msgpack/adaptor/cpp11/tuple.hpp
include/msgpack/adaptor/cpp11/unique_ptr.hpp
+1 -1
View File
@@ -22,7 +22,7 @@
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
// To supress warning on Boost.1.58.0
// To suppress warning on Boost.1.58.0
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
@@ -0,0 +1,76 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2015 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_REFERENCE_WRAPPER_HPP
#define MSGPACK_CPP11_REFERENCE_WRAPPER_HPP
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include <memory>
#include <type_traits>
namespace msgpack {
/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond
namespace adaptor {
template <typename T>
struct convert<std::reference_wrapper<T>> {
msgpack::object const& operator()(msgpack::object const& o, std::reference_wrapper<T>& v) const {
msgpack::adaptor::convert<T>()(o, v.get());
return o;
}
};
template <typename T>
struct pack<std::reference_wrapper<T>> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::reference_wrapper<T>& v) const {
o.pack(v.get());
return o;
}
};
template <typename T>
struct object<std::reference_wrapper<T> > {
void operator()(msgpack::object& o, const std::reference_wrapper<T>& v) const {
msgpack::adaptor::object<typename std::remove_const<T>::type>()(o, v.get());
}
};
template <typename T>
struct object_with_zone<std::reference_wrapper<T>> {
void operator()(msgpack::object::with_zone& o, const std::reference_wrapper<T>& v) const {
msgpack::adaptor::object_with_zone<typename std::remove_const<T>::type>()(o, v.get());
}
};
} // namespace adaptor
/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
} // namespace msgpack
#endif // MSGPACK_CPP11_REFERENCE_WRAPPER_HPP
+7 -7
View File
@@ -99,7 +99,7 @@ template <>
struct pack<msgpack::type::ext> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::ext& v) const {
// size limit has aleady been checked at ext's constructor
// size limit has already been checked at ext's constructor
uint32_t size = v.size();
o.pack_ext(size, v.type());
o.pack_ext_body(v.data(), size);
@@ -110,7 +110,7 @@ struct pack<msgpack::type::ext> {
template <>
struct object_with_zone<msgpack::type::ext> {
void operator()(msgpack::object::with_zone& o, const msgpack::type::ext& v) const {
// size limit has aleady been checked at ext's constructor
// size limit has already been checked at ext's constructor
uint32_t size = v.size();
o.type = msgpack::type::EXT;
char* ptr = static_cast<char*>(o.zone.allocate_align(size + 1));
@@ -131,7 +131,7 @@ public:
// A default constructed ext_ref object::m_ptr doesn't have the buffer to point to.
// In order to avoid nullptr checking branches, m_ptr points to m_size.
// So type() returns unspecified but valid value. It might be a zero because m_size
// is initialized as zero, but shoudn't assume that.
// is initialized as zero, but shouldn't assume that.
ext_ref() : m_ptr(static_cast<char*>(static_cast<void*>(&m_size))), m_size(0) {}
ext_ref(const char* p, uint32_t s) :
m_ptr(s == 0 ? static_cast<char*>(static_cast<void*>(&m_size)) : p),
@@ -139,7 +139,7 @@ public:
detail::check_container_size_for_ext<sizeof(std::size_t)>(s);
}
// size limit has aleady been checked at ext's constructor
// size limit has already been checked at ext's constructor
ext_ref(ext const& x) : m_ptr(&x.m_data[0]), m_size(x.size()) {}
const char* data() const {
@@ -184,7 +184,7 @@ private:
};
inline ext::ext(ext_ref const& x) {
// size limit has aleady been checked at ext_ref's constructor
// size limit has already been checked at ext_ref's constructor
m_data.reserve(x.size() + 1);
m_data.push_back(x.type());
@@ -208,7 +208,7 @@ template <>
struct pack<msgpack::type::ext_ref> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::ext_ref& v) const {
// size limit has aleady been checked at ext_ref's constructor
// size limit has already been checked at ext_ref's constructor
uint32_t size = v.size();
o.pack_ext(size, v.type());
o.pack_ext_body(v.data(), size);
@@ -219,7 +219,7 @@ struct pack<msgpack::type::ext_ref> {
template <>
struct object<msgpack::type::ext_ref> {
void operator()(msgpack::object& o, const msgpack::type::ext_ref& v) const {
// size limit has aleady been checked at ext_ref's constructor
// size limit has already been checked at ext_ref's constructor
uint32_t size = v.size();
o.type = msgpack::type::EXT;
o.via.ext.ptr = v.m_ptr;
+1 -1
View File
@@ -74,7 +74,7 @@ struct object_with_zone<type::nil> {
}
};
} // namespace adaptror
} // namespace adaptor
template <>
inline void msgpack::object::as<void>() const
+3 -6
View File
@@ -32,8 +32,7 @@
#if defined __cplusplus
#if __cplusplus < 201103L
#if defined(MSGPACK_USE_CPP03)
#if !defined(nullptr)
# if _MSC_VER < 1600
@@ -101,7 +100,7 @@ struct is_same<T, T> : true_type {};
} // namespace msgpack
#else // __cplusplus < 201103L
#else // MSGPACK_USE_CPP03
#include <memory>
#include <tuple>
@@ -128,8 +127,6 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
} // namespace msgpack
#endif // __cplusplus < 201103L
#endif // __cplusplus
#endif // MSGPACK_USE_CPP03
#endif /* msgpack/cpp_config.hpp */
+2 -2
View File
@@ -693,7 +693,7 @@ template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_str(uint32_t l)
{
if(l < 32) {
unsigned char d = 0xa0u | static_cast<uint8_t>(l);
unsigned char d = static_cast<uint8_t>(0xa0u | l);
char buf = take8_8(d);
append_buffer(&buf, 1);
} else if(l < 256) {
@@ -725,7 +725,7 @@ template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_v4raw(uint32_t l)
{
if(l < 32) {
unsigned char d = 0xa0u | static_cast<uint8_t>(l);
unsigned char d = static_cast<uint8_t>(0xa0u | l);
char buf = take8_8(d);
append_buffer(&buf, 1);
} else if(l < 65536) {
+2
View File
@@ -38,11 +38,13 @@
# include <stdbool.h>
#endif
#if !defined(MSGPACK_DLLEXPORT)
#if defined(_MSC_VER)
# define MSGPACK_DLLEXPORT __declspec(dllexport)
#else /* _MSC_VER */
# define MSGPACK_DLLEXPORT
#endif /* _MSC_VER */
#endif
#ifdef _WIN32
# define _msgpack_atomic_counter_header <windows.h>
+1
View File
@@ -33,6 +33,7 @@
#include "adaptor/cpp11/array_char.hpp"
#include "adaptor/cpp11/array_unsigned_char.hpp"
#include "adaptor/cpp11/forward_list.hpp"
#include "adaptor/cpp11/reference_wrapper.hpp"
#include "adaptor/cpp11/shared_ptr.hpp"
#include "adaptor/cpp11/tuple.hpp"
#include "adaptor/cpp11/unique_ptr.hpp"
+1
View File
@@ -185,6 +185,7 @@ nobase_include_HEADERS += \
../include/msgpack/adaptor/cpp11/array_char.hpp \
../include/msgpack/adaptor/cpp11/array_unsigned_char.hpp \
../include/msgpack/adaptor/cpp11/forward_list.hpp \
../include/msgpack/adaptor/cpp11/reference_wrapper.hpp \
../include/msgpack/adaptor/cpp11/shared_ptr.hpp \
../include/msgpack/adaptor/cpp11/tuple.hpp \
../include/msgpack/adaptor/cpp11/unique_ptr.hpp \
+2 -1
View File
@@ -48,9 +48,10 @@ IF (MSGPACK_CXX11)
LIST (APPEND check_PROGRAMS
iterator_cpp11.cpp
msgpack_cpp11.cpp
reference_cpp11.cpp
reference_wrapper_cpp11.cpp
shared_ptr_cpp11.cpp
unique_ptr_cpp11.cpp
reference_cpp11.cpp
)
ENDIF ()
+2
View File
@@ -39,6 +39,7 @@ check_PROGRAMS += \
iterator_cpp11 \
msgpack_cpp11 \
reference_cpp11 \
reference_wrapper_cpp11 \
shared_ptr_cpp11 \
unique_ptr_cpp11
@@ -76,6 +77,7 @@ zone_SOURCES = zone.cpp
iterator_cpp11_SOURCES = iterator_cpp11.cpp
msgpack_cpp11_SOURCES = msgpack_cpp11.cpp
reference_cpp11_SOURCES = reference_cpp11.cpp
reference_wrapper_cpp11_SOURCES = reference_wrapper_cpp11.cpp
shared_ptr_cpp11_SOURCES = shared_ptr_cpp11.cpp
unique_ptr_cpp11_SOURCES = unique_ptr_cpp11.cpp
+18
View File
@@ -553,3 +553,21 @@ TEST(limit, unpacker_array_over)
EXPECT_TRUE(false);
}
}
TEST(limit, unpacker_reserve)
{
msgpack::unpacker u(nullptr, nullptr, MSGPACK_UNPACKER_INIT_BUFFER_SIZE,
msgpack::unpack_limit());
std::size_t original_capacity = u.buffer_capacity();
u.reserve_buffer(original_capacity + 1u);
EXPECT_EQ((original_capacity + COUNTER_SIZE) * 2 - COUNTER_SIZE, u.buffer_capacity());
}
TEST(limit, unpacker_reserve_more_than_twice)
{
msgpack::unpacker u(nullptr, nullptr, MSGPACK_UNPACKER_INIT_BUFFER_SIZE,
msgpack::unpack_limit());
std::size_t original_capacity = u.buffer_capacity();
u.reserve_buffer(original_capacity * 3);
EXPECT_EQ((original_capacity + COUNTER_SIZE) * 4 - COUNTER_SIZE, u.buffer_capacity());
}
+10
View File
@@ -278,6 +278,16 @@ TEST(MSGPACK, simple_buffer_double)
}
}
TEST(MSGPACK, simple_buffer_nil)
{
msgpack::sbuffer sbuf;
msgpack::packer<msgpack::sbuffer> packer(sbuf);
packer.pack_nil();
msgpack::unpacked ret;
msgpack::unpack(ret, sbuf.data(), sbuf.size());
EXPECT_EQ(ret.get().type, msgpack::type::NIL);
}
TEST(MSGPACK, simple_buffer_true)
{
msgpack::sbuffer sbuf;
+93
View File
@@ -372,3 +372,96 @@ TEST(unpack, insufficient_bytes_zone)
EXPECT_EQ(off, 0u);
}
}
TEST(unpack, parse_error)
{
msgpack::sbuffer sbuf;
char c = '\xc1';
sbuf.write(&c, 1);
bool thrown = false;
msgpack::unpacked msg;
try {
msgpack::unpack(msg, sbuf.data(), sbuf.size());
EXPECT_TRUE(false);
}
catch (msgpack::parse_error const&) {
thrown = true;
}
EXPECT_TRUE(thrown);
}
TEST(unpack, returned_parse_error)
{
msgpack::sbuffer sbuf;
char c = '\xc1';
sbuf.write(&c, 1);
bool thrown = false;
try {
msgpack::unpack(sbuf.data(), sbuf.size());
EXPECT_TRUE(false);
}
catch (msgpack::parse_error const&) {
thrown = true;
}
EXPECT_TRUE(thrown);
}
TEST(unpack, zone_parse_error)
{
msgpack::sbuffer sbuf;
char c = '\xc1';
sbuf.write(&c, 1);
bool thrown = false;
msgpack::zone z;
try {
msgpack::unpack(z, sbuf.data(), sbuf.size());
EXPECT_TRUE(false);
}
catch (msgpack::parse_error const&) {
thrown = true;
}
EXPECT_TRUE(thrown);
}
TEST(unpack, extra_bytes)
{
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, 1);
msgpack::unpacked msg = msgpack::unpack(sbuf.data(), sbuf.size() + 1);
EXPECT_EQ(1, msg.get().as<int>());
}
TEST(unpack, zone_extra_bytes)
{
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, 1);
msgpack::zone z;
msgpack::object obj = msgpack::unpack(z, sbuf.data(), sbuf.size() + 1);
EXPECT_EQ(1, obj.as<int>());
}
TEST(unpack, int_off_larger_than_length)
{
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, 1);
std::size_t off = 2;
bool thrown = false;
try {
msgpack::unpacked msg = msgpack::unpack(sbuf.data(), sbuf.size(), off);
}
catch (msgpack::insufficient_bytes const&) {
thrown = true;
}
EXPECT_TRUE(thrown);
EXPECT_EQ(off, 2u);
}
+95
View File
@@ -0,0 +1,95 @@
#include <msgpack.hpp>
#include <sstream>
#include <gtest/gtest.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if !defined(MSGPACK_USE_CPP03)
TEST(MSGPACK_REFERENCE_WRAPPER, pack_convert)
{
int i1 = 42;
std::reference_wrapper<int> val1(i1);
std::stringstream ss;
msgpack::pack(ss, val1);
msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
int i2 = 0;
std::reference_wrapper<int> val2(i2);
oh.get().convert(val2);
EXPECT_EQ(i1, i2);
}
TEST(MSGPACK_REFERENCE_WRAPPER, pack_convert_const)
{
const int i1 = 42;
std::reference_wrapper<const int> val1(i1);
std::stringstream ss;
msgpack::pack(ss, val1);
msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
int i2 = 0;
std::reference_wrapper<int> val2(i2);
oh.get().convert(val2);
EXPECT_EQ(i1, i2);
}
TEST(MSGPACK_REFERENCE_WRAPPER, pack_vector)
{
int i1 = 42;
std::vector<std::reference_wrapper<int>> val1{i1};
std::stringstream ss;
msgpack::pack(ss, val1);
msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
std::vector<int> val2 = oh.get().as<std::vector<int>>();
EXPECT_EQ(val2.size(), 1);
EXPECT_EQ(val1[0], val2[0]);
}
TEST(MSGPACK_REFERENCE_WRAPPER, object)
{
int i1 = 42;
std::reference_wrapper<int> val1(i1);
msgpack::object o(val1);
int i2 = 0;
std::reference_wrapper<int> val2(i2);
o.convert(val2);
EXPECT_EQ(i1, i2);
}
TEST(MSGPACK_REFERENCE_WRAPPER, object_const)
{
const int i1 = 42;
std::reference_wrapper<const int> val1(i1);
msgpack::object o(val1);
int i2 = 0;
std::reference_wrapper<int> val2(i2);
o.convert(val2);
EXPECT_EQ(i1, i2);
}
TEST(MSGPACK_REFERENCE_WRAPPER, object_with_zone)
{
std::string s1 = "ABC";
std::reference_wrapper<std::string> val1(s1);
msgpack::zone z;
msgpack::object o(val1, z);
std::string s2 = "DE";
std::reference_wrapper<std::string> val2(s2);
o.convert(val2);
EXPECT_EQ(s1, s2);
}
TEST(MSGPACK_REFERENCE_WRAPPER, object_with_zone_const)
{
const std::string s1 = "ABC";
std::reference_wrapper<const std::string> val1(s1);
msgpack::zone z;
msgpack::object o(val1, z);
std::string s2 = "DE";
std::reference_wrapper<std::string> val2(s2);
o.convert(val2);
EXPECT_EQ(s1, s2);
}
#endif // !defined(MSGPACK_USE_CPP03)