Skip to content

Commit 428780d

Browse files
committed
Added std::reference_wrapper packing and making object and object_with_zone support.
Fixed #370.
1 parent 6bf5160 commit 428780d

File tree

7 files changed

+140
-1
lines changed

7 files changed

+140
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ IF (MSGPACK_ENABLE_CXX)
176176
include/msgpack/adaptor/cpp11/array_char.hpp
177177
include/msgpack/adaptor/cpp11/array_unsigned_char.hpp
178178
include/msgpack/adaptor/cpp11/forward_list.hpp
179+
include/msgpack/adaptor/cpp11/reference_wrapper.hpp
179180
include/msgpack/adaptor/cpp11/shared_ptr.hpp
180181
include/msgpack/adaptor/cpp11/tuple.hpp
181182
include/msgpack/adaptor/cpp11/unique_ptr.hpp
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2015 KONDO Takatoshi
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
#ifndef MSGPACK_CPP11_REFERENCE_WRAPPER_HPP
20+
#define MSGPACK_CPP11_REFERENCE_WRAPPER_HPP
21+
22+
#include "msgpack/versioning.hpp"
23+
#include "msgpack/adaptor/adaptor_base.hpp"
24+
#include "msgpack/adaptor/check_container_size.hpp"
25+
26+
#include <memory>
27+
28+
namespace msgpack {
29+
30+
/// @cond
31+
MSGPACK_API_VERSION_NAMESPACE(v1) {
32+
/// @endcond
33+
34+
namespace adaptor {
35+
36+
template <typename T>
37+
struct pack<std::reference_wrapper<T>> {
38+
template <typename Stream>
39+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::reference_wrapper<T>& v) const {
40+
o.pack(v.get());
41+
return o;
42+
}
43+
};
44+
45+
template <typename T>
46+
struct object<std::reference_wrapper<T> > {
47+
void operator()(msgpack::object& o, const std::reference_wrapper<T>& v) const {
48+
msgpack::adaptor::object<T>()(o, v.get());
49+
}
50+
};
51+
52+
template <typename T>
53+
struct object_with_zone<std::reference_wrapper<T>> {
54+
void operator()(msgpack::object::with_zone& o, const std::reference_wrapper<T>& v) const {
55+
msgpack::adaptor::object_with_zone<T>()(o, v.get());
56+
}
57+
};
58+
59+
} // namespace adaptor
60+
61+
/// @cond
62+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
63+
/// @endcond
64+
65+
} // namespace msgpack
66+
67+
#endif // MSGPACK_CPP11_REFERENCE_WRAPPER_HPP

include/msgpack/type.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "adaptor/cpp11/array_char.hpp"
3434
#include "adaptor/cpp11/array_unsigned_char.hpp"
3535
#include "adaptor/cpp11/forward_list.hpp"
36+
#include "adaptor/cpp11/reference_wrapper.hpp"
3637
#include "adaptor/cpp11/shared_ptr.hpp"
3738
#include "adaptor/cpp11/tuple.hpp"
3839
#include "adaptor/cpp11/unique_ptr.hpp"

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ nobase_include_HEADERS += \
185185
../include/msgpack/adaptor/cpp11/array_char.hpp \
186186
../include/msgpack/adaptor/cpp11/array_unsigned_char.hpp \
187187
../include/msgpack/adaptor/cpp11/forward_list.hpp \
188+
../include/msgpack/adaptor/cpp11/reference_wrapper.hpp \
188189
../include/msgpack/adaptor/cpp11/shared_ptr.hpp \
189190
../include/msgpack/adaptor/cpp11/tuple.hpp \
190191
../include/msgpack/adaptor/cpp11/unique_ptr.hpp \

test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ IF (MSGPACK_CXX11)
4848
LIST (APPEND check_PROGRAMS
4949
iterator_cpp11.cpp
5050
msgpack_cpp11.cpp
51+
reference_cpp11.cpp
52+
reference_wrapper_cpp11.cpp
5153
shared_ptr_cpp11.cpp
5254
unique_ptr_cpp11.cpp
53-
reference_cpp11.cpp
5455
)
5556
ENDIF ()
5657

test/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ check_PROGRAMS += \
3939
iterator_cpp11 \
4040
msgpack_cpp11 \
4141
reference_cpp11 \
42+
reference_wrapper_cpp11 \
4243
shared_ptr_cpp11 \
4344
unique_ptr_cpp11
4445

@@ -76,6 +77,7 @@ zone_SOURCES = zone.cpp
7677
iterator_cpp11_SOURCES = iterator_cpp11.cpp
7778
msgpack_cpp11_SOURCES = msgpack_cpp11.cpp
7879
reference_cpp11_SOURCES = reference_cpp11.cpp
80+
reference_wrapper_cpp11_SOURCES = reference_wrapper_cpp11.cpp
7981
shared_ptr_cpp11_SOURCES = shared_ptr_cpp11.cpp
8082
unique_ptr_cpp11_SOURCES = unique_ptr_cpp11.cpp
8183

test/reference_wrapper_cpp11.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <msgpack.hpp>
2+
#include <sstream>
3+
#include <gtest/gtest.h>
4+
5+
#ifdef HAVE_CONFIG_H
6+
#include "config.h"
7+
#endif
8+
9+
#if !defined(MSGPACK_USE_CPP03)
10+
11+
TEST(MSGPACK_REFERENCE_WRAPPER, pack)
12+
{
13+
int i1 = 42;
14+
std::reference_wrapper<int> val1(i1);
15+
std::stringstream ss;
16+
msgpack::pack(ss, val1);
17+
msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
18+
int i2= oh.get().as<int>();
19+
EXPECT_EQ(val1, i2);
20+
EXPECT_EQ(i1, i2);
21+
}
22+
23+
TEST(MSGPACK_REFERENCE_WRAPPER, pack_vector)
24+
{
25+
int i1 = 42;
26+
std::vector<std::reference_wrapper<int>> val1{i1};
27+
std::stringstream ss;
28+
msgpack::pack(ss, val1);
29+
msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
30+
std::vector<int> val2 = oh.get().as<std::vector<int>>();
31+
EXPECT_EQ(val2.size(), 1);
32+
EXPECT_EQ(val1[0], val2[0]);
33+
}
34+
35+
TEST(MSGPACK_REFERENCE_WRAPPER, object)
36+
{
37+
int i1 = 42;
38+
std::reference_wrapper<int> val1(i1);
39+
msgpack::object o(val1);
40+
int i2= o.as<int>();
41+
EXPECT_EQ(val1, i2);
42+
EXPECT_EQ(i1, i2);
43+
}
44+
45+
TEST(MSGPACK_REFERENCE_WRAPPER, object_with_zone)
46+
{
47+
int i1 = 42;
48+
std::reference_wrapper<int> val1(i1);
49+
msgpack::zone z;
50+
msgpack::object o(val1, z);
51+
int i2= o.as<int>();
52+
EXPECT_EQ(val1, i2);
53+
EXPECT_EQ(i1, i2);
54+
}
55+
56+
TEST(MSGPACK_REFERENCE_WRAPPER, object_with_zone_string)
57+
{
58+
std::string s1 = "ABC";
59+
std::reference_wrapper<std::string> val1(s1);
60+
msgpack::zone z;
61+
msgpack::object o(val1, z);
62+
std::string s2= o.as<std::string>();
63+
EXPECT_EQ(s1, s2);
64+
}
65+
66+
#endif // !defined(MSGPACK_USE_CPP03)

0 commit comments

Comments
 (0)