From ed51d2333515a228424e1dc4a80d388760782aa9 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Tue, 4 Mar 2014 21:27:47 +0000 Subject: [PATCH] Refined tuple implementation. --- cpp11/tuple.hpp | 85 ++++++++++++++++++------------------------- test/msgpack_tuple.cc | 23 ++++-------- 2 files changed, 43 insertions(+), 65 deletions(-) diff --git a/cpp11/tuple.hpp b/cpp11/tuple.hpp index a747a759..1d068a37 100644 --- a/cpp11/tuple.hpp +++ b/cpp11/tuple.hpp @@ -32,76 +32,61 @@ namespace type { using std::tuple_element; using std::uses_allocator; using std::ignore; + using std::make_tuple; + using std::tie; + using std::forward_as_tuple; + using std::swap; template< class... Types > class tuple : public std::tuple { public: - using std::tuple::tuple; + using base = std::tuple; + + using base::tuple; + + tuple() = default; + tuple(tuple const&) = default; + tuple(tuple&&) = default; + + template + tuple(tuple const& other):base(static_cast const&>(other)) {} + template + tuple(tuple && other):base(static_cast &&>(other)) {} + + tuple& operator=(tuple const&) = default; + tuple& operator=(tuple&&) = default; + + template + tuple& operator=(tuple const& other) { + *static_cast(this) = static_cast const&>(other); + return *this; + } + template + tuple& operator=(tuple && other) { + *static_cast(this) = static_cast &&>(other); + return *this; + } template< std::size_t I> - typename tuple_element >::type& + typename tuple_element::type& get() { return std::get(*this); } template< std::size_t I> - typename tuple_element >::type const& + typename tuple_element::type const& get() const { return std::get(*this); } template< std::size_t I> - typename tuple_element >::type&& + typename tuple_element::type&& get() && { return std::get(*this); } }; - template< class... Types > - tuple make_tuple( Types&&... args ) { - return tuple(std::forward(args)...); - } - - template< class... Types > - tuple make_tuple( std::tuple&& arg ) { - return tuple(std::forward&&>(arg)); - } - - template< class... Types > - tuple tie( Types&... args ) { - return std::tie(args...); - } - - template< class... Types > - tuple forward_as_tuple( Types&&... args ) { - return std::forward_as_tuple(std::forward(args)...); - } - - namespace detail { - template < typename... Types > - std::tuple&& get_std_tuple(tuple&& t) { - return std::forward&&>(t); - } - template < typename... Types > - std::tuple& get_std_tuple(tuple& t) { - return t; - } - template < typename... Types > - std::tuple const& get_std_tuple(tuple 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(args))...)) + std::tuple_cat(std::forward::type::base>(args)...) ) { - return std::tuple_cat(detail::get_std_tuple(std::forward(args))...); + return std::tuple_cat(std::forward::type::base>(args)...); } - - template< class... Types > - void swap( tuple& lhs, tuple& rhs ) { - lhs.swap(rhs); - } - } // namespace type // --- Pack ( from tuple to packer stream --- diff --git a/test/msgpack_tuple.cc b/test/msgpack_tuple.cc index b5a7e8f3..2b11a83e 100644 --- a/test/msgpack_tuple.cc +++ b/test/msgpack_tuple.cc @@ -61,8 +61,7 @@ TEST(msgpack_tuple, make_tuple) TEST(msgpack_tuple, std_make_tuple) { - msgpack::type::tuple t1 = - msgpack::type::make_tuple(std::make_tuple(42, true, "ABC")); + msgpack::type::tuple t1 = std::make_tuple(42, true, "ABC"); EXPECT_EQ(42, t1.get<0>()); EXPECT_EQ(true, t1.get<1>()); EXPECT_EQ("ABC", t1.get<2>()); @@ -70,19 +69,13 @@ TEST(msgpack_tuple, std_make_tuple) TEST(msgpack_tuple, tie) { - int i(42); - bool b(true); - std::string s("ABC"); - msgpack::type::tuple 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>()); + int i(43); + bool b(false); + std::string s("DEFG"); + msgpack::type::tie(i, b, s) = msgpack::type::make_tuple(42, true, "ABC"); + EXPECT_EQ(42, i); + EXPECT_EQ(true, b); + EXPECT_EQ("ABC", s); } TEST(msgpack_tuple, tuple_cat)