1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
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 --
9 -- Copyright (C) 2013-2023, Free Software Foundation, Inc. --
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. --
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. --
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. --
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 ------------------------------------------------------------------------------
32 -- This is an optimized version of Indefinite_Holders using copy-on-write.
33 -- It is used on platforms that support atomic built-ins.
35 private with Ada
.Finalization
;
36 private with Ada
.Streams
;
38 private with System
.Atomic_Counters
;
39 private with Ada
.Strings
.Text_Buffers
;
42 type Element_Type
(<>) is private;
43 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
45 package Ada
.Containers
.Indefinite_Holders
is
46 pragma Annotate
(CodePeer
, Skip_Analysis
);
47 pragma Preelaborate
(Indefinite_Holders
);
48 pragma Remote_Types
(Indefinite_Holders
);
50 type Holder
is tagged private;
51 pragma Preelaborable_Initialization
(Holder
);
53 Empty_Holder
: constant Holder
;
55 function "=" (Left
, Right
: Holder
) return Boolean;
57 function To_Holder
(New_Item
: Element_Type
) return Holder
;
59 function Is_Empty
(Container
: Holder
) return Boolean;
61 procedure Clear
(Container
: in out Holder
);
63 function Element
(Container
: Holder
) return Element_Type
;
65 procedure Replace_Element
66 (Container
: in out Holder
;
67 New_Item
: Element_Type
);
69 procedure Query_Element
71 Process
: not null access procedure (Element
: Element_Type
));
73 procedure Update_Element
74 (Container
: in out Holder
;
75 Process
: not null access procedure (Element
: in out Element_Type
));
77 type Constant_Reference_Type
78 (Element
: not null access constant Element_Type
) is private
80 Implicit_Dereference
=> Element
;
83 (Element
: not null access Element_Type
) is private
85 Implicit_Dereference
=> Element
;
87 function Constant_Reference
88 (Container
: aliased Holder
) return Constant_Reference_Type
;
89 pragma Inline
(Constant_Reference
);
92 (Container
: aliased in out Holder
) return Reference_Type
;
93 pragma Inline
(Reference
);
95 procedure Assign
(Target
: in out Holder
; Source
: Holder
);
97 function Copy
(Source
: Holder
) return Holder
;
99 procedure Move
(Target
: in out Holder
; Source
: in out Holder
);
101 procedure Swap
(Left
, Right
: in out Holder
);
105 use Ada
.Finalization
;
108 type Element_Access
is access all Element_Type
;
110 type Holder_Access
is access all Holder
;
112 type Shared_Holder
is record
113 Counter
: System
.Atomic_Counters
.Atomic_Counter
;
114 Element
: Element_Access
;
117 type Shared_Holder_Access
is access all Shared_Holder
;
119 procedure Reference
(Item
: not null Shared_Holder_Access
);
120 -- Increment reference counter
122 procedure Unreference
(Item
: not null Shared_Holder_Access
);
123 -- Decrement reference counter, deallocate Item when counter goes to zero
126 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
127 Container
: out Holder
);
130 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
133 type Holder
is new Ada
.Finalization
.Controlled
with record
134 Reference
: Shared_Holder_Access
;
136 end record with Put_Image
=> Put_Image
;
139 (S
: in out Ada
.Strings
.Text_Buffers
.Root_Buffer_Type
'Class; V
: Holder
);
141 for Holder
'Read use Read
;
142 for Holder
'Write use Write
;
144 overriding
procedure Adjust
(Container
: in out Holder
);
145 overriding
procedure Finalize
(Container
: in out Holder
);
147 type Reference_Control_Type
is new Controlled
with record
148 Container
: Holder_Access
;
151 overriding
procedure Adjust
(Control
: in out Reference_Control_Type
);
152 pragma Inline
(Adjust
);
154 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
);
155 pragma Inline
(Finalize
);
157 type Constant_Reference_Type
158 (Element
: not null access constant Element_Type
) is
160 Control
: Reference_Control_Type
:=
161 raise Program_Error
with "uninitialized reference";
162 -- The RM says, "The default initialization of an object of
163 -- type Constant_Reference_Type or Reference_Type propagates
168 (Stream
: not null access Root_Stream_Type
'Class;
169 Item
: Constant_Reference_Type
);
171 for Constant_Reference_Type
'Write use Write
;
174 (Stream
: not null access Root_Stream_Type
'Class;
175 Item
: out Constant_Reference_Type
);
177 for Constant_Reference_Type
'Read use Read
;
179 type Reference_Type
(Element
: not null access Element_Type
) is record
180 Control
: Reference_Control_Type
:=
181 raise Program_Error
with "uninitialized reference";
182 -- The RM says, "The default initialization of an object of
183 -- type Constant_Reference_Type or Reference_Type propagates
188 (Stream
: not null access Root_Stream_Type
'Class;
189 Item
: Reference_Type
);
191 for Reference_Type
'Write use Write
;
194 (Stream
: not null access Root_Stream_Type
'Class;
195 Item
: out Reference_Type
);
197 for Reference_Type
'Read use Read
;
199 Empty_Holder
: constant Holder
:= (Controlled
with null, 0);
201 end Ada
.Containers
.Indefinite_Holders
;