Merge pull request #373 from redboltz/add_ref_wrapper

Added std::reference_wrapper packing and making object and object_wit…
This commit is contained in:
Takatoshi Kondo 2015-10-15 11:02:47 +09:00
commit 0a261fca42
7 changed files with 141 additions and 1 deletions

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

View File

@ -0,0 +1,75 @@
//
// 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>
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<T>()(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<T>()(o, v.get());
}
};
} // namespace adaptor
/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
} // namespace msgpack
#endif // MSGPACK_CPP11_REFERENCE_WRAPPER_HPP

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"

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 \

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 ()

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

View File

@ -0,0 +1,59 @@
#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_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_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);
}
#endif // !defined(MSGPACK_USE_CPP03)