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, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 ------------------------------------------------------------------------------
28 with Ada
.Unchecked_Deallocation
;
30 package body Ada
.Containers
.Indefinite_Holders
is
33 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
39 function "=" (Left
, Right
: Holder
) return Boolean is
41 if Left
.Reference
= null and Right
.Reference
= null then
44 elsif Left
.Reference
/= null and Right
.Reference
/= null then
45 return Left
.Reference
.Element
.all = Right
.Reference
.Element
.all;
56 overriding
procedure Adjust
(Container
: in out Holder
) is
58 if Container
.Reference
/= null then
59 Reference
(Container
.Reference
);
69 procedure Assign
(Target
: in out Holder
; Source
: Holder
) is
71 if Target
.Busy
/= 0 then
72 raise Program_Error
with "attempt to tamper with elements";
75 if Target
.Reference
/= Source
.Reference
then
76 if Target
.Reference
/= null then
77 Unreference
(Target
.Reference
);
80 Target
.Reference
:= Source
.Reference
;
82 if Source
.Reference
/= null then
83 Reference
(Target
.Reference
);
92 procedure Clear
(Container
: in out Holder
) is
94 if Container
.Busy
/= 0 then
95 raise Program_Error
with "attempt to tamper with elements";
98 Unreference
(Container
.Reference
);
99 Container
.Reference
:= null;
106 function Copy
(Source
: Holder
) return Holder
is
108 if Source
.Reference
= null then
109 return (AF
.Controlled
with null, 0);
111 Reference
(Source
.Reference
);
113 return (AF
.Controlled
with Source
.Reference
, 0);
121 function Element
(Container
: Holder
) return Element_Type
is
123 if Container
.Reference
= null then
124 raise Constraint_Error
with "container is empty";
126 return Container
.Reference
.Element
.all;
134 overriding
procedure Finalize
(Container
: in out Holder
) is
136 if Container
.Busy
/= 0 then
137 raise Program_Error
with "attempt to tamper with elements";
140 if Container
.Reference
/= null then
141 Unreference
(Container
.Reference
);
142 Container
.Reference
:= null;
150 function Is_Empty
(Container
: Holder
) return Boolean is
152 return Container
.Reference
= null;
159 procedure Move
(Target
: in out Holder
; Source
: in out Holder
) is
161 if Target
.Busy
/= 0 then
162 raise Program_Error
with "attempt to tamper with elements";
165 if Source
.Busy
/= 0 then
166 raise Program_Error
with "attempt to tamper with elements";
169 if Target
.Reference
/= Source
.Reference
then
170 if Target
.Reference
/= null then
171 Unreference
(Target
.Reference
);
174 Target
.Reference
:= Source
.Reference
;
175 Source
.Reference
:= null;
183 procedure Query_Element
185 Process
: not null access procedure (Element
: Element_Type
))
187 B
: Natural renames Container
'Unrestricted_Access.Busy
;
190 if Container
.Reference
= null then
191 raise Constraint_Error
with "container is empty";
197 Process
(Container
.Reference
.Element
.all);
212 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
213 Container
: out Holder
)
218 if not Boolean'Input (Stream
) then
219 Container
.Reference
:=
222 Element => new Element_Type'(Element_Type
'Input (Stream
)));
230 procedure Reference
(Item
: not null Shared_Holder_Access
) is
232 System
.Atomic_Counters
.Increment
(Item
.Counter
);
235 ---------------------
236 -- Replace_Element --
237 ---------------------
239 procedure Replace_Element
240 (Container
: in out Holder
;
241 New_Item
: Element_Type
)
243 -- Element allocator may need an accessibility check in case actual type
244 -- is class-wide or has access discriminants (RM 4.8(10.1) and
247 pragma Unsuppress
(Accessibility_Check
);
250 if Container
.Busy
/= 0 then
251 raise Program_Error
with "attempt to tamper with elements";
254 if Container
.Reference
= null then
255 -- Holder is empty, allocate new Shared_Holder.
257 Container
.Reference
:=
260 Element => new Element_Type'(New_Item
));
262 elsif System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
) then
263 -- Shared_Holder can be reused.
265 Free
(Container
.Reference
.Element
);
266 Container
.Reference
.Element
:= new Element_Type
'(New_Item);
269 Unreference (Container.Reference);
270 Container.Reference :=
273 Element
=> new Element_Type
'(New_Item));
281 function To_Holder (New_Item : Element_Type) return Holder is
282 -- The element allocator may need an accessibility check in the case the
283 -- actual type is class-wide or has access discriminants (RM 4.8(10.1)
286 pragma Unsuppress (Accessibility_Check);
293 Element
=> new Element_Type
'(New_Item)), 0);
300 procedure Unreference (Item : not null Shared_Holder_Access) is
303 new Ada.Unchecked_Deallocation (Shared_Holder, Shared_Holder_Access);
305 Aux : Shared_Holder_Access := Item;
308 if System.Atomic_Counters.Decrement (Aux.Counter) then
318 procedure Update_Element
320 Process : not null access procedure (Element : in out Element_Type))
322 B : Natural renames Container'Unrestricted_Access.Busy;
325 if Container.Reference = null then
326 raise Constraint_Error with "container is empty";
332 Process (Container.Reference.Element.all);
347 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
351 Boolean'Output (Stream, Container.Reference = null);
353 if Container.Reference /= null then
354 Element_Type'Output (Stream, Container.Reference.Element.all);
358 end Ada.Containers.Indefinite_Holders;