Added member fucntion version of msgpack::type::tuple::get().

Added support functions for msgpack::type::tuple as same as std::tuple.
This commit is contained in:
Takatoshi Kondo
2013-09-02 18:29:24 +09:00
parent 36a87b6968
commit da601e4589
4 changed files with 190 additions and 13 deletions

View File

@@ -18,11 +18,92 @@
#ifndef MSGPACK_TYPE_TUPLE_HPP #ifndef MSGPACK_TYPE_TUPLE_HPP
#define MSGPACK_TYPE_TUPLE_HPP #define MSGPACK_TYPE_TUPLE_HPP
#include <tuple>
#include "msgpack/object.hpp" #include "msgpack/object.hpp"
#include "msgpack/cpp_config.hpp" #include "msgpack/cpp_config.hpp"
namespace msgpack { namespace msgpack {
namespace type {
// tuple
using std::get;
using std::tuple_size;
using std::tuple_element;
using std::uses_allocator;
using std::ignore;
template< class... Types >
class tuple : public std::tuple<Types...> {
public:
using std::tuple<Types...>::tuple;
template< std::size_t I>
typename tuple_element<I, std::tuple<Types...> >::type&
get() { return std::get<I>(*this); }
template< std::size_t I>
typename tuple_element<I, std::tuple<Types...> >::type const&
get() const { return std::get<I>(*this); }
template< std::size_t I>
typename tuple_element<I, std::tuple<Types...> >::type&&
get() && { return std::get<I>(*this); }
};
template< class... Types >
tuple<Types...> make_tuple( Types&&... args ) {
return tuple<Types...>(std::forward<Types&&>(args)...);
}
template< class... Types >
tuple<Types...> make_tuple( std::tuple<Types...>&& arg ) {
return tuple<Types...>(std::forward<std::tuple<Types...>&&>(arg));
}
template< class... Types >
tuple<Types&...> tie( Types&... args ) {
return std::tie(args...);
}
template< class... Types >
tuple<Types&&...> forward_as_tuple( Types&&... args ) {
return std::forward_as_tuple(std::forward<Types&&>(args)...);
}
namespace detail {
template < typename... Types >
std::tuple<Types...>&& get_std_tuple(tuple<Types...>&& t) {
return std::forward<std::tuple<Types...>&&>(t);
}
template < typename... Types >
std::tuple<Types...>& get_std_tuple(tuple<Types...>& t) {
return t;
}
template < typename... Types >
std::tuple<Types...> const& get_std_tuple(tuple<Types...> const& t) {
return t;
}
template < typename T >
T&& get_std_tuple(T&& t) {
return t;
}
}
template< class... Tuples >
auto tuple_cat(Tuples&&... args) ->
decltype(
msgpack::type::make_tuple(std::tuple_cat(detail::get_std_tuple(std::forward<Tuples&&>(args))...))
) {
return std::tuple_cat(detail::get_std_tuple(std::forward<Tuples&&>(args))...);
}
template< class... Types >
void swap( tuple<Types...>& lhs, tuple<Types...>& rhs ) {
lhs.swap(rhs);
}
} // namespace type
// --- Pack ( from tuple to packer stream --- // --- Pack ( from tuple to packer stream ---
template <typename Stream, typename Tuple, std::size_t N> template <typename Stream, typename Tuple, std::size_t N>
struct Packer { struct Packer {

View File

@@ -63,19 +63,6 @@ namespace msgpack {
using std::move; using std::move;
using std::swap; using std::swap;
namespace type {
// tuple
using std::tuple;
using std::get;
using std::make_tuple;
using std::tie;
using std::forward_as_tuple;
using std::tuple_cat;
using std::tuple_size;
using std::tuple_element;
using std::uses_allocator;
using std::ignore;
}
} // msgpack } // msgpack

View File

@@ -16,6 +16,7 @@ check_PROGRAMS = \
fixint \ fixint \
fixint_c \ fixint_c \
version \ version \
msgpack_tuple \
msgpackc_test \ msgpackc_test \
msgpack_test msgpack_test
@@ -46,6 +47,8 @@ fixint_c_SOURCES = fixint_c.cc
version_SOURCES = version.cc version_SOURCES = version.cc
msgpack_tuple_SOURCES = msgpack_tuple.cc
msgpackc_test_SOURCES = msgpackc_test.cpp msgpackc_test_SOURCES = msgpackc_test.cpp
msgpack_test_SOURCES = msgpack_test.cpp msgpack_test_SOURCES = msgpack_test.cpp

106
test/msgpack_tuple.cc Normal file
View File

@@ -0,0 +1,106 @@
#include <msgpack.hpp>
#include <gtest/gtest.h>
TEST(msgpack_tuple, member_get)
{
msgpack::type::tuple<int, bool, std::string> t1(42, true, "ABC");
EXPECT_EQ(42, t1.get<0>());
EXPECT_EQ(true, t1.get<1>());
EXPECT_EQ("ABC", t1.get<2>());
t1.get<0>() = 40;
t1.get<1>() = false;
t1.get<2>() = "DEFG";
EXPECT_EQ(40, t1.get<0>());
EXPECT_EQ(false, t1.get<1>());
EXPECT_EQ("DEFG", t1.get<2>());
}
TEST(msgpack_tuple, non_member_get)
{
msgpack::type::tuple<int, bool, std::string> t1(42, true, "ABC");
EXPECT_EQ(42, msgpack::type::get<0>(t1));
EXPECT_EQ(true, msgpack::type::get<1>(t1));
EXPECT_EQ("ABC", msgpack::type::get<2>(t1));
msgpack::type::get<0>(t1) = 40;
msgpack::type::get<1>(t1) = false;
msgpack::type::get<2>(t1) = "DEFG";
EXPECT_EQ(40, msgpack::type::get<0>(t1));
EXPECT_EQ(false, msgpack::type::get<1>(t1));
EXPECT_EQ("DEFG", msgpack::type::get<2>(t1));
}
TEST(msgpack_tuple, std_non_member_get)
{
msgpack::type::tuple<int, bool, std::string> t1(42, true, "ABC");
EXPECT_EQ(42, std::get<0>(t1));
EXPECT_EQ(true, std::get<1>(t1));
EXPECT_EQ("ABC", std::get<2>(t1));
std::get<0>(t1) = 40;
std::get<1>(t1) = false;
std::get<2>(t1) = "DEFG";
EXPECT_EQ(40, std::get<0>(t1));
EXPECT_EQ(false, std::get<1>(t1));
EXPECT_EQ("DEFG", std::get<2>(t1));
}
TEST(msgpack_tuple, make_tuple)
{
msgpack::type::tuple<int, bool, std::string> t1 = msgpack::type::make_tuple(42, true, "ABC");
EXPECT_EQ(42, t1.get<0>());
EXPECT_EQ(true, t1.get<1>());
EXPECT_EQ("ABC", t1.get<2>());
t1.get<0>() = 40;
t1.get<1>() = false;
t1.get<2>() = "DEFG";
EXPECT_EQ(40, t1.get<0>());
EXPECT_EQ(false, t1.get<1>());
EXPECT_EQ("DEFG", t1.get<2>());
msgpack::type::tuple<int, bool, std::string> t2 =
msgpack::type::make_tuple(std::make_tuple(42, true, "ABC"));
EXPECT_EQ(42, t2.get<0>());
EXPECT_EQ(true, t2.get<1>());
EXPECT_EQ("ABC", t2.get<2>());
}
TEST(msgpack_tuple, tie)
{
int i(42);
bool b(true);
std::string s("ABC");
msgpack::type::tuple<int, bool, std::string> t1 = msgpack::type::tie(i, b, s);
EXPECT_EQ(42, t1.get<0>());
EXPECT_EQ(true, t1.get<1>());
EXPECT_EQ("ABC", t1.get<2>());
t1.get<0>() = 40;
t1.get<1>() = false;
t1.get<2>() = "DEFG";
EXPECT_EQ(40, t1.get<0>());
EXPECT_EQ(false, t1.get<1>());
EXPECT_EQ("DEFG", t1.get<2>());
}
TEST(msgpack_tuple, tuple_cat)
{
msgpack::type::tuple<int> t1 = msgpack::type::make_tuple(42);
msgpack::type::tuple<bool, std::string> t2 = msgpack::type::make_tuple(true, "ABC");
msgpack::type::tuple<int, bool, std::string> t3 = msgpack::type::tuple_cat(t1, std::move(t2));
EXPECT_EQ(42, t3.get<0>());
EXPECT_EQ(true, t3.get<1>());
EXPECT_EQ("ABC", t3.get<2>());
}
TEST(msgpack_tuple, swap)
{
msgpack::type::tuple<int, bool, std::string> t1 = msgpack::type::make_tuple(42, true, "ABC");
msgpack::type::tuple<int, bool, std::string> t2 = msgpack::type::make_tuple(40, false, "DEFG");
msgpack::type::swap(t1, t2);
EXPECT_EQ(42, t2.get<0>());
EXPECT_EQ(true, t2.get<1>());
EXPECT_EQ("ABC", t2.get<2>());
EXPECT_EQ(40, t1.get<0>());
EXPECT_EQ(false, t1.get<1>());
EXPECT_EQ("DEFG", t1.get<2>());
}