From: cfairles Date: Wed, 1 Oct 2008 22:04:02 +0000 (+0000) Subject: 2008-09-30 Chris Fairles X-Git-Url: http://git.sourceforge.jp/view?p=pf3gnuchains%2Fgcc-fork.git;a=commitdiff_plain;h=ff515a54ec0229947a28801160e09e215e96c299;ds=sidebyside 2008-09-30 Chris Fairles * include/std/tuple (tuple<>::swap): Implement swap for tuple as per DR 522 [Ready]. * testsuite/20_util/tuple/swap.cc: New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@140822 138bc75d-0d04-0410-961f-82ee72b054a4 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 359215df6f1..2ca3b9e0908 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,9 @@ +2008-09-30 Chris Fairles + + * include/std/tuple (tuple<>::swap): Implement swap for tuple as per + DR 522 [Ready]. + * testsuite/20_util/tuple/swap.cc: New. + 2008-09-30 Paolo Carlini PR libstdc++/30085 (again) diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index a295e4ef937..33e8731d3aa 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -81,6 +81,8 @@ namespace std _Head& _M_head() { return *this; } const _Head& _M_head() const { return *this; } + + void __swap_impl(_Head&&) { /* no-op */ } }; template @@ -99,6 +101,13 @@ namespace std _Head& _M_head() { return _M_head_impl; } const _Head& _M_head() const { return _M_head_impl; } + void + __swap_impl(_Head&& __h) + { + using std::swap; + swap(__h, _M_head_impl); + } + _Head _M_head_impl; }; @@ -118,7 +127,10 @@ namespace std * inheritance recursion. */ template - struct _Tuple_impl<_Idx> { }; + struct _Tuple_impl<_Idx> + { + void __swap_impl(_Tuple_impl&&) { /* no-op */ } + }; /** * Recursive tuple implementation. Here we store the @c Head element @@ -203,6 +215,13 @@ namespace std _M_tail() = std::move(__in._M_tail()); return *this; } + + void + __swap_impl(_Tuple_impl&& __in) + { + _Base::__swap_impl(__in._M_head()); + _Inherited::__swap_impl(__in._M_tail()); + } }; /// tuple @@ -274,11 +293,19 @@ namespace std static_cast<_Inherited&>(*this) = std::move(__in); return *this; } + + void + swap(tuple&& __in) + { _Inherited::__swap_impl(__in); } }; template<> - class tuple<> { }; + class tuple<> + { + public: + void swap(tuple&&) { /* no-op */ } + }; /// tuple (2-element), with construction and assignment from a pair. template @@ -368,6 +395,14 @@ namespace std this->_M_tail()._M_head() = std::move(__in.second); return *this; } + + void + swap(tuple&& __in) + { + using std::swap; + swap(this->_M_head(), __in._M_head()); + swap(this->_M_tail()._M_head(), __in._M_tail()._M_head()); + } }; @@ -628,6 +663,21 @@ namespace std tie(_Elements&... __args) { return tuple<_Elements&...>(__args...); } + template + inline void + swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y) + { __x.swap(__y); } + + template + inline void + swap(tuple<_Elements...>&& __x, tuple<_Elements...>& __y) + { __x.swap(__y); } + + template + inline void + swap(tuple<_Elements...>& __x, tuple<_Elements...>&& __y) + { __x.swap(__y); } + // A class (and instance) which can be used in 'tie' when an element // of a tuple is not required struct _Swallow_assign diff --git a/libstdc++-v3/testsuite/20_util/tuple/swap.cc b/libstdc++-v3/testsuite/20_util/tuple/swap.cc new file mode 100644 index 00000000000..2bebbe0674b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/tuple/swap.cc @@ -0,0 +1,118 @@ +// { dg-options "-std=gnu++0x" } + +// Copyright (C) 2007 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +// NOTE: This makes use of the fact that we know how moveable +// is implemented on tuple. If the implementation changed +// this test may begin to fail. + +#include +#include +#include + +struct MoveOnly +{ + explicit MoveOnly (int j) : i(j) { } + + MoveOnly (MoveOnly&& m) : i(m.i) { } + + MoveOnly& operator=(MoveOnly&& m) + { i = m.i; return *this; } + + MoveOnly(MoveOnly const&) = delete; + MoveOnly& operator=(MoveOnly const&) = delete; + + bool operator==(MoveOnly const& m) + { return i == m.i; } + + void swap(MoveOnly&& m) + { std::swap(m.i, i); } + + int i; +}; + +void swap(MoveOnly& m1, MoveOnly& m2) +{ m1.swap(m2); } + +MoveOnly +make_move_only (int i) +{ return MoveOnly(i); } + +void test01() +{ + std::tuple<> t1, t2; + std::swap(t1, t2); + + VERIFY( t1 == t2 ); +} + +void test02() +{ + bool test __attribute__((unused)) = true; + + std::tuple t1(1), t2(2); + std::swap(t1, t2); + + VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 ); +} + +void test03() +{ + bool test __attribute__((unused)) = true; + + std::tuple t1(1, 1.0f), t2(2, 2.0f); + std::swap(t1, t2); + + VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 ); + VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f ); +} + +void test04() +{ + bool test __attribute__((unused)) = true; + + std::tuple + t1(1, 1.0f, make_move_only(1)), + t2(2, 2.0f, make_move_only(2)); + + std::swap(t1, t2); + + VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 ); + VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f ); + VERIFY( std::get<2>(t1) == make_move_only(2) + && std::get<2>(t2) == make_move_only(1) ); +} + +int main() +{ + test01(); + test02(); + test03(); + test04(); + return 0; +}