[Ada] AI12-0350 Swap for Indefinite_Holders
authorArnaud Charlet <charlet@adacore.com>
Mon, 23 Mar 2020 08:49:13 +0000 (04:49 -0400)
committerPierre-Marie de Rodat <derodat@adacore.com>
Fri, 12 Jun 2020 08:29:24 +0000 (04:29 -0400)
2020-06-12  Arnaud Charlet  <charlet@adacore.com>

gcc/ada/

* libgnat/a-coinho.ads, libgnat/a-coinho.adb,
libgnat/a-coinho__shared.ads, libgnat/a-coinho__shared.adb
(Swap): New procedure.

gcc/ada/libgnat/a-coinho.adb
gcc/ada/libgnat/a-coinho.ads
gcc/ada/libgnat/a-coinho__shared.adb
gcc/ada/libgnat/a-coinho__shared.ads

index ef58e9e..c5da943 100644 (file)
@@ -304,6 +304,30 @@ package body Ada.Containers.Indefinite_Holders is
       end;
    end Replace_Element;
 
+   ----------
+   -- Swap --
+   ----------
+
+   procedure Swap (Left, Right : in out Holder) is
+   begin
+      if Left.Busy /= 0 then
+         raise Program_Error with "attempt to tamper with elements";
+      end if;
+
+      if Right.Busy /= 0 then
+         raise Program_Error with "attempt to tamper with elements";
+      end if;
+
+      if Left.Element /= Right.Element then
+         declare
+            Tmp : constant Element_Access := Left.Element;
+         begin
+            Left.Element := Right.Element;
+            Right.Element := Tmp;
+         end;
+      end if;
+   end Swap;
+
    ---------------
    -- To_Holder --
    ---------------
index af22cbf..bf6165e 100644 (file)
@@ -92,6 +92,8 @@ package Ada.Containers.Indefinite_Holders is
 
    procedure Move (Target : in out Holder; Source : in out Holder);
 
+   procedure Swap (Left, Right : in out Holder);
+
 private
 
    use Ada.Finalization;
index bfeb6ad..43f5d52 100644 (file)
@@ -426,6 +426,30 @@ package body Ada.Containers.Indefinite_Holders is
       end if;
    end Replace_Element;
 
+   ----------
+   -- Swap --
+   ----------
+
+   procedure Swap (Left, Right : in out Holder) is
+   begin
+      if Left.Busy /= 0 then
+         raise Program_Error with "attempt to tamper with elements";
+      end if;
+
+      if Right.Busy /= 0 then
+         raise Program_Error with "attempt to tamper with elements";
+      end if;
+
+      if Left.Reference /= Right.Reference then
+         declare
+            Tmp : constant Shared_Holder_Access := Left.Reference;
+         begin
+            Left.Reference := Right.Reference;
+            Right.Reference := Tmp;
+         end;
+      end if;
+   end Swap;
+
    ---------------
    -- To_Holder --
    ---------------
index af2e7e0..0345b5e 100644 (file)
@@ -96,6 +96,8 @@ package Ada.Containers.Indefinite_Holders is
 
    procedure Move (Target : in out Holder; Source : in out Holder);
 
+   procedure Swap (Left, Right : in out Holder);
+
 private
 
    use Ada.Finalization;