public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] DR 522 (Ready) - tuple<>::swap
@ 2008-10-01  0:00 Chris Fairles
  2008-10-01  8:57 ` Paolo Carlini
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Fairles @ 2008-10-01  0:00 UTC (permalink / raw)
  To: Gcc Patch List, libstdc++

[-- Attachment #1: Type: text/plain, Size: 99 bytes --]

Implements DR 522 - tuple<>::swap. Am I using swap ADL correctly here?
If so, ok for trunk?

Chris

[-- Attachment #2: tuple_swap.patch.txt --]
[-- Type: text/plain, Size: 6130 bytes --]

Index: include/std/tuple
===================================================================
--- include/std/tuple	(revision 140798)
+++ include/std/tuple	(working copy)
@@ -81,6 +81,8 @@
 
       _Head&       _M_head()       { return *this; }
       const _Head& _M_head() const { return *this; }
+
+      void __swap_impl(_Head&&) { /* no-op */ }
     };
 
   template<std::size_t _Idx, typename _Head>
@@ -99,6 +101,13 @@
       _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 @@
    * inheritance recursion.
    */
   template<std::size_t _Idx>
-    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 @@
 	  _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 @@
 	  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<typename _T1, typename _T2>
@@ -368,6 +395,14 @@
 	  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 @@
     tie(_Elements&... __args)
     { return tuple<_Elements&...>(__args...); }
 
+  template<typename... _Elements>
+    inline void 
+    swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
+    { __x.swap(__y); }
+
+  template<typename... _Elements>
+    inline void
+    swap(tuple<_Elements...>&& __x, tuple<_Elements...>& __y)
+    { __x.swap(__y); }
+
+  template<typename... _Elements>
+    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
Index: testsuite/20_util/tuple/swap.cc
===================================================================
--- testsuite/20_util/tuple/swap.cc	(revision 0)
+++ testsuite/20_util/tuple/swap.cc	(revision 0)
@@ -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 <tuple>
+#include <utility>
+#include <testsuite_hooks.h>
+
+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<int> 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<int, float> 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<int, float, MoveOnly> 
+    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;
+}

[-- Attachment #3: Changelog_tuple_swap.txt --]
[-- Type: text/plain, Size: 435 bytes --]

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 140798)
+++ ChangeLog	(working copy)
@@ -1,3 +1,9 @@
+2008-09-30  Chris Fairles  <cfairles@gcc.gnu.org>
+
+	* 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  <paolo.carlini@oracle.com>
 
 	PR libstdc++/30085 (again)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] DR 522 (Ready) - tuple<>::swap
  2008-10-01  0:00 [PATCH] DR 522 (Ready) - tuple<>::swap Chris Fairles
@ 2008-10-01  8:57 ` Paolo Carlini
  2008-10-01 16:07   ` Paolo Carlini
  0 siblings, 1 reply; 4+ messages in thread
From: Paolo Carlini @ 2008-10-01  8:57 UTC (permalink / raw)
  To: Chris Fairles; +Cc: Gcc Patch List, libstdc++

Hi,
> Implements DR 522 - tuple<>::swap. Am I using swap ADL correctly here?
>   
I think you are. In fact, if I remember correctly, that particular idiom
is also suggested explicitly in Scott Meyers' Effective C++, it would be
nice if somebody could double check that...

Therefore, barring comments during the next 1-2 days, please go ahead
with the patch, and many thanks again!

Paolo.

PS: I would suggest marking patches for the library with [v3] or
something similar, it helps when the mailing list is followed via email
only...

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] DR 522 (Ready) - tuple<>::swap
  2008-10-01  8:57 ` Paolo Carlini
@ 2008-10-01 16:07   ` Paolo Carlini
  2008-10-01 22:11     ` Chris Fairles
  0 siblings, 1 reply; 4+ messages in thread
From: Paolo Carlini @ 2008-10-01 16:07 UTC (permalink / raw)
  To: Chris Fairles; +Cc: Gcc Patch List, libstdc++

Paolo Carlini wrote:
> I think you are. In fact, if I remember correctly, that particular idiom
> is also suggested explicitly in Scott Meyers' Effective C++, it would be
> nice if somebody could double check that...
>   
I can now confirm this: it's Item 25 of the Third Ed. Good.

Paolo.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] DR 522 (Ready) - tuple<>::swap
  2008-10-01 16:07   ` Paolo Carlini
@ 2008-10-01 22:11     ` Chris Fairles
  0 siblings, 0 replies; 4+ messages in thread
From: Chris Fairles @ 2008-10-01 22:11 UTC (permalink / raw)
  To: Paolo Carlini; +Cc: Gcc Patch List, libstdc++

On Wed, Oct 1, 2008 at 11:36 AM, Paolo Carlini <paolo.carlini@oracle.com> wrote:
> Paolo Carlini wrote:
>> I think you are. In fact, if I remember correctly, that particular idiom
>> is also suggested explicitly in Scott Meyers' Effective C++, it would be
>> nice if somebody could double check that...
>>
> I can now confirm this: it's Item 25 of the Third Ed. Good.
>
> Paolo.
>

swap ADL logic is sound, been a day, noone seems to strongly object,
committed to trunk.

Chris

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-10-01 22:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-01  0:00 [PATCH] DR 522 (Ready) - tuple<>::swap Chris Fairles
2008-10-01  8:57 ` Paolo Carlini
2008-10-01 16:07   ` Paolo Carlini
2008-10-01 22:11     ` Chris Fairles

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).