1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT LIBRARY COMPONENTS                          --
4--                                                                          --
5--    A D A . C O N T A I N E R S . I N D E F I N I T E _ H O L D E R S     --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--             Copyright (C) 2013, Free Software Foundation, Inc.           --
10--                                                                          --
11-- This specification is derived from the Ada Reference Manual for use with --
12-- GNAT. The copyright notice above, and the license provisions that follow --
13-- apply solely to the  contents of the part following the private keyword. --
14--                                                                          --
15-- GNAT is free software;  you can  redistribute it  and/or modify it under --
16-- terms of the  GNU General Public License as published  by the Free Soft- --
17-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
18-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
21--                                                                          --
22-- As a special exception under Section 7 of GPL version 3, you are granted --
23-- additional permissions described in the GCC Runtime Library Exception,   --
24-- version 3.1, as published by the Free Software Foundation.               --
25--                                                                          --
26-- You should have received a copy of the GNU General Public License and    --
27-- a copy of the GCC Runtime Library Exception along with this program;     --
28-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
29-- <http://www.gnu.org/licenses/>.                                          --
30------------------------------------------------------------------------------
31
32private with Ada.Finalization;
33private with Ada.Streams;
34private with System.Atomic_Counters;
35
36generic
37   type Element_Type (<>) is private;
38   with function "=" (Left, Right : Element_Type) return Boolean is <>;
39
40package Ada.Containers.Indefinite_Holders is
41   pragma Preelaborate (Indefinite_Holders);
42   pragma Remote_Types (Indefinite_Holders);
43
44   type Holder is tagged private;
45   pragma Preelaborable_Initialization (Holder);
46
47   Empty_Holder : constant Holder;
48
49   function "=" (Left, Right : Holder) return Boolean;
50
51   function To_Holder (New_Item : Element_Type) return Holder;
52
53   function Is_Empty (Container : Holder) return Boolean;
54
55   procedure Clear (Container : in out Holder);
56
57   function Element (Container : Holder) return Element_Type;
58
59   procedure Replace_Element
60     (Container : in out Holder;
61      New_Item  : Element_Type);
62
63   procedure Query_Element
64     (Container : Holder;
65      Process   : not null access procedure (Element : Element_Type));
66   procedure Update_Element
67     (Container : Holder;
68      Process   : not null access procedure (Element : in out Element_Type));
69
70   procedure Assign (Target : in out Holder; Source : Holder);
71
72   function Copy (Source : Holder) return Holder;
73
74   procedure Move (Target : in out Holder; Source : in out Holder);
75
76private
77
78   package AF renames Ada.Finalization;
79
80   type Element_Access is access all Element_Type;
81
82   type Shared_Holder is record
83      Counter : System.Atomic_Counters.Atomic_Counter;
84      Element : Element_Access;
85   end record;
86
87   type Shared_Holder_Access is access all Shared_Holder;
88
89   procedure Reference (Item : not null Shared_Holder_Access);
90   --  Increment reference counter
91
92   procedure Unreference (Item : not null Shared_Holder_Access);
93   --  Decrement reference counter, deallocate Item when counter goes to zero
94
95   procedure Read
96     (Stream    : not null access Ada.Streams.Root_Stream_Type'Class;
97      Container : out Holder);
98
99   procedure Write
100     (Stream    : not null access Ada.Streams.Root_Stream_Type'Class;
101      Container : Holder);
102
103   type Holder is new Ada.Finalization.Controlled with record
104      Reference : Shared_Holder_Access;
105      Busy      : Natural := 0;
106   end record;
107   for Holder'Read use Read;
108   for Holder'Write use Write;
109
110   overriding procedure Adjust (Container : in out Holder);
111   overriding procedure Finalize (Container : in out Holder);
112
113   Empty_Holder : constant Holder := (AF.Controlled with null, 0);
114
115end Ada.Containers.Indefinite_Holders;
116