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-2014, 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
;
41 type Element_Type
(<>) is private;
42 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
44 package Ada
.Containers
.Indefinite_Holders
is
45 pragma Preelaborate
(Indefinite_Holders
);
46 pragma Remote_Types
(Indefinite_Holders
);
48 type Holder
is tagged private;
49 pragma Preelaborable_Initialization
(Holder
);
51 Empty_Holder
: constant Holder
;
53 function "=" (Left
, Right
: Holder
) return Boolean;
55 function To_Holder
(New_Item
: Element_Type
) return Holder
;
57 function Is_Empty
(Container
: Holder
) return Boolean;
59 procedure Clear
(Container
: in out Holder
);
61 function Element
(Container
: Holder
) return Element_Type
;
63 procedure Replace_Element
64 (Container
: in out Holder
;
65 New_Item
: Element_Type
);
67 procedure Query_Element
69 Process
: not null access procedure (Element
: Element_Type
));
70 procedure Update_Element
71 (Container
: in out Holder
;
72 Process
: not null access procedure (Element
: in out Element_Type
));
74 type Constant_Reference_Type
75 (Element
: not null access constant Element_Type
) is private
77 Implicit_Dereference
=> Element
;
80 (Element
: not null access Element_Type
) is private
82 Implicit_Dereference
=> Element
;
84 function Constant_Reference
85 (Container
: aliased Holder
) return Constant_Reference_Type
;
86 pragma Inline
(Constant_Reference
);
89 (Container
: aliased in out Holder
) return Reference_Type
;
90 pragma Inline
(Reference
);
92 procedure Assign
(Target
: in out Holder
; Source
: Holder
);
94 function Copy
(Source
: Holder
) return Holder
;
96 procedure Move
(Target
: in out Holder
; Source
: in out Holder
);
100 use Ada
.Finalization
;
103 type Element_Access
is access all Element_Type
;
104 type Holder_Access
is access all Holder
;
106 type Shared_Holder
is record
107 Counter
: System
.Atomic_Counters
.Atomic_Counter
;
108 Element
: Element_Access
;
111 type Shared_Holder_Access
is access all Shared_Holder
;
113 procedure Reference
(Item
: not null Shared_Holder_Access
);
114 -- Increment reference counter
116 procedure Unreference
(Item
: not null Shared_Holder_Access
);
117 -- Decrement reference counter, deallocate Item when counter goes to zero
120 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
121 Container
: out Holder
);
124 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
127 type Holder
is new Ada
.Finalization
.Controlled
with record
128 Reference
: Shared_Holder_Access
;
131 for Holder
'Read use Read
;
132 for Holder
'Write use Write
;
134 overriding
procedure Adjust
(Container
: in out Holder
);
135 overriding
procedure Finalize
(Container
: in out Holder
);
137 type Reference_Control_Type
is new Controlled
with record
138 Container
: Holder_Access
;
141 overriding
procedure Adjust
(Control
: in out Reference_Control_Type
);
142 pragma Inline
(Adjust
);
144 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
);
145 pragma Inline
(Finalize
);
147 type Constant_Reference_Type
148 (Element
: not null access constant Element_Type
) is
150 Control
: Reference_Control_Type
;
154 (Stream
: not null access Root_Stream_Type
'Class;
155 Item
: Constant_Reference_Type
);
157 for Constant_Reference_Type
'Write use Write
;
160 (Stream
: not null access Root_Stream_Type
'Class;
161 Item
: out Constant_Reference_Type
);
163 for Constant_Reference_Type
'Read use Read
;
165 type Reference_Type
(Element
: not null access Element_Type
) is record
166 Control
: Reference_Control_Type
;
170 (Stream
: not null access Root_Stream_Type
'Class;
171 Item
: Reference_Type
);
173 for Reference_Type
'Write use Write
;
176 (Stream
: not null access Root_Stream_Type
'Class;
177 Item
: out Reference_Type
);
179 for Reference_Type
'Read use Read
;
181 Empty_Holder
: constant Holder
:= (Controlled
with null, 0);
183 end Ada
.Containers
.Indefinite_Holders
;