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-2020, 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
32--  This is an optimized version of Indefinite_Holders using copy-on-write.
33--  It is used on platforms that support atomic built-ins.
34
35private with Ada.Finalization;
36private with Ada.Streams;
37
38private with System.Atomic_Counters;
39private with Ada.Strings.Text_Output;
40
41generic
42   type Element_Type (<>) is private;
43   with function "=" (Left, Right : Element_Type) return Boolean is <>;
44
45package Ada.Containers.Indefinite_Holders is
46   pragma Annotate (CodePeer, Skip_Analysis);
47   pragma Preelaborate (Indefinite_Holders);
48   pragma Remote_Types (Indefinite_Holders);
49
50   type Holder is tagged private;
51   pragma Preelaborable_Initialization (Holder);
52
53   Empty_Holder : constant Holder;
54
55   function "=" (Left, Right : Holder) return Boolean;
56
57   function To_Holder (New_Item : Element_Type) return Holder;
58
59   function Is_Empty (Container : Holder) return Boolean;
60
61   procedure Clear (Container : in out Holder);
62
63   function Element (Container : Holder) return Element_Type;
64
65   procedure Replace_Element
66     (Container : in out Holder;
67      New_Item  : Element_Type);
68
69   procedure Query_Element
70     (Container : Holder;
71      Process   : not null access procedure (Element : Element_Type));
72   procedure Update_Element
73     (Container : in out Holder;
74      Process   : not null access procedure (Element : in out Element_Type));
75
76   type Constant_Reference_Type
77      (Element : not null access constant Element_Type) is private
78   with
79      Implicit_Dereference => Element;
80
81   type Reference_Type
82     (Element : not null access Element_Type) is private
83   with
84      Implicit_Dereference => Element;
85
86   function Constant_Reference
87     (Container : aliased Holder) return Constant_Reference_Type;
88   pragma Inline (Constant_Reference);
89
90   function Reference
91     (Container : aliased in out Holder) return Reference_Type;
92   pragma Inline (Reference);
93
94   procedure Assign (Target : in out Holder; Source : Holder);
95
96   function Copy (Source : Holder) return Holder;
97
98   procedure Move (Target : in out Holder; Source : in out Holder);
99
100   procedure Swap (Left, Right : in out Holder);
101
102private
103
104   use Ada.Finalization;
105   use Ada.Streams;
106
107   type Element_Access is access all Element_Type;
108   type Holder_Access is access all Holder;
109
110   type Shared_Holder is record
111      Counter : System.Atomic_Counters.Atomic_Counter;
112      Element : Element_Access;
113   end record;
114
115   type Shared_Holder_Access is access all Shared_Holder;
116
117   procedure Reference (Item : not null Shared_Holder_Access);
118   --  Increment reference counter
119
120   procedure Unreference (Item : not null Shared_Holder_Access);
121   --  Decrement reference counter, deallocate Item when counter goes to zero
122
123   procedure Read
124     (Stream    : not null access Ada.Streams.Root_Stream_Type'Class;
125      Container : out Holder);
126
127   procedure Write
128     (Stream    : not null access Ada.Streams.Root_Stream_Type'Class;
129      Container : Holder);
130
131   type Holder is new Ada.Finalization.Controlled with record
132      Reference : Shared_Holder_Access;
133      Busy      : Natural := 0;
134   end record with Put_Image => Put_Image;
135
136   procedure Put_Image
137     (S : in out Ada.Strings.Text_Output.Sink'Class; V : Holder);
138
139   for Holder'Read use Read;
140   for Holder'Write use Write;
141
142   overriding procedure Adjust (Container : in out Holder);
143   overriding procedure Finalize (Container : in out Holder);
144
145   type Reference_Control_Type is new Controlled with record
146      Container : Holder_Access;
147   end record;
148
149   overriding procedure Adjust (Control : in out Reference_Control_Type);
150   pragma Inline (Adjust);
151
152   overriding procedure Finalize (Control : in out Reference_Control_Type);
153   pragma Inline (Finalize);
154
155   type Constant_Reference_Type
156     (Element : not null access constant Element_Type) is
157      record
158         Control : Reference_Control_Type :=
159           raise Program_Error with "uninitialized reference";
160         --  The RM says, "The default initialization of an object of
161         --  type Constant_Reference_Type or Reference_Type propagates
162         --  Program_Error."
163      end record;
164
165   procedure Write
166     (Stream : not null access Root_Stream_Type'Class;
167      Item   : Constant_Reference_Type);
168
169   for Constant_Reference_Type'Write use Write;
170
171   procedure Read
172     (Stream : not null access Root_Stream_Type'Class;
173      Item   : out Constant_Reference_Type);
174
175   for Constant_Reference_Type'Read use Read;
176
177   type Reference_Type (Element : not null access Element_Type) is record
178      Control : Reference_Control_Type :=
179        raise Program_Error with "uninitialized reference";
180      --  The RM says, "The default initialization of an object of
181      --  type Constant_Reference_Type or Reference_Type propagates
182      --  Program_Error."
183   end record;
184
185   procedure Write
186     (Stream : not null access Root_Stream_Type'Class;
187      Item   : Reference_Type);
188
189   for Reference_Type'Write use Write;
190
191   procedure Read
192     (Stream : not null access Root_Stream_Type'Class;
193      Item   : out Reference_Type);
194
195   for Reference_Type'Read use Read;
196
197   Empty_Holder : constant Holder := (Controlled with null, 0);
198
199end Ada.Containers.Indefinite_Holders;
200