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-2016, 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 -- Note: special attention must be paid to the case of simultaneous access
29 -- to internal shared objects and elements by different tasks. The Reference
30 -- counter of internal shared object is the only component protected using
31 -- atomic operations; other components and elements can be modified only when
32 -- reference counter is equal to one (so there are no other references to this
33 -- internal shared object and element).
35 with Ada
.Unchecked_Deallocation
;
37 package body Ada
.Containers
.Indefinite_Holders
is
40 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
42 procedure Detach
(Container
: Holder
);
43 -- Detach data from shared copy if necessary. This is necessary to prepare
44 -- container to be modified.
50 function "=" (Left
, Right
: Holder
) return Boolean is
52 if Left
.Reference
= Right
.Reference
then
54 -- Covers both null and not null but the same shared object cases
58 elsif Left
.Reference
/= null and Right
.Reference
/= null then
59 return Left
.Reference
.Element
.all = Right
.Reference
.Element
.all;
70 overriding
procedure Adjust
(Container
: in out Holder
) is
72 if Container
.Reference
/= null then
73 if Container
.Busy
= 0 then
75 -- Container is not locked, reuse existing internal shared object
77 Reference
(Container
.Reference
);
79 -- Otherwise, create copy of both internal shared object and
82 Container
.Reference
:=
86 new Element_Type'(Container
.Reference
.Element
.all));
93 overriding
procedure Adjust
(Control
: in out Reference_Control_Type
) is
95 if Control
.Container
/= null then
96 Reference
(Control
.Container
.Reference
);
97 Control
.Container
.Busy
:= Control
.Container
.Busy
+ 1;
105 procedure Assign
(Target
: in out Holder
; Source
: Holder
) is
107 if Target
.Busy
/= 0 then
108 raise Program_Error
with "attempt to tamper with elements";
111 if Target
.Reference
/= Source
.Reference
then
112 if Target
.Reference
/= null then
113 Unreference
(Target
.Reference
);
116 Target
.Reference
:= Source
.Reference
;
118 if Source
.Reference
/= null then
119 Reference
(Target
.Reference
);
128 procedure Clear
(Container
: in out Holder
) is
130 if Container
.Busy
/= 0 then
131 raise Program_Error
with "attempt to tamper with elements";
134 if Container
.Reference
/= null then
135 Unreference
(Container
.Reference
);
136 Container
.Reference
:= null;
140 ------------------------
141 -- Constant_Reference --
142 ------------------------
144 function Constant_Reference
145 (Container
: aliased Holder
) return Constant_Reference_Type
is
147 if Container
.Reference
= null then
148 raise Constraint_Error
with "container is empty";
154 Ref
: constant Constant_Reference_Type
:=
155 (Element
=> Container
.Reference
.Element
.all'Access,
156 Control
=> (Controlled
with Container
'Unrestricted_Access));
158 Reference
(Ref
.Control
.Container
.Reference
);
159 Ref
.Control
.Container
.Busy
:= Ref
.Control
.Container
.Busy
+ 1;
162 end Constant_Reference
;
168 function Copy
(Source
: Holder
) return Holder
is
170 if Source
.Reference
= null then
171 return (Controlled
with null, 0);
173 elsif Source
.Busy
= 0 then
175 -- Container is not locked, reuse internal shared object
177 Reference
(Source
.Reference
);
179 return (Controlled
with Source
.Reference
, 0);
182 -- Otherwise, create copy of both internal shared object and element
188 Element => new Element_Type'(Source
.Reference
.Element
.all)),
197 procedure Detach
(Container
: Holder
) is
199 if Container
.Busy
= 0
200 and then not System
.Atomic_Counters
.Is_One
201 (Container
.Reference
.Counter
)
203 -- Container is not locked and internal shared object is used by
204 -- other container, create copy of both internal shared object and
208 Old
: constant Shared_Holder_Access
:= Container
.Reference
;
211 Container
'Unrestricted_Access.Reference
:=
215 new Element_Type'(Container
.Reference
.Element
.all));
225 function Element
(Container
: Holder
) return Element_Type
is
227 if Container
.Reference
= null then
228 raise Constraint_Error
with "container is empty";
230 return Container
.Reference
.Element
.all;
238 overriding
procedure Finalize
(Container
: in out Holder
) is
240 if Container
.Busy
/= 0 then
241 raise Program_Error
with "attempt to tamper with elements";
244 if Container
.Reference
/= null then
245 Unreference
(Container
.Reference
);
246 Container
.Reference
:= null;
250 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
) is
252 if Control
.Container
/= null then
253 Unreference
(Control
.Container
.Reference
);
254 Control
.Container
.Busy
:= Control
.Container
.Busy
- 1;
255 Control
.Container
:= null;
263 function Is_Empty
(Container
: Holder
) return Boolean is
265 return Container
.Reference
= null;
272 procedure Move
(Target
: in out Holder
; Source
: in out Holder
) is
274 if Target
.Busy
/= 0 then
275 raise Program_Error
with "attempt to tamper with elements";
278 if Source
.Busy
/= 0 then
279 raise Program_Error
with "attempt to tamper with elements";
282 if Target
.Reference
/= Source
.Reference
then
283 if Target
.Reference
/= null then
284 Unreference
(Target
.Reference
);
287 Target
.Reference
:= Source
.Reference
;
288 Source
.Reference
:= null;
296 procedure Query_Element
298 Process
: not null access procedure (Element
: Element_Type
))
300 B
: Natural renames Container
'Unrestricted_Access.Busy
;
303 if Container
.Reference
= null then
304 raise Constraint_Error
with "container is empty";
312 Process
(Container
.Reference
.Element
.all);
327 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
328 Container
: out Holder
)
333 if not Boolean'Input (Stream
) then
334 Container
.Reference
:=
337 Element => new Element_Type'(Element_Type
'Input (Stream
)));
342 (Stream
: not null access Root_Stream_Type
'Class;
343 Item
: out Constant_Reference_Type
)
346 raise Program_Error
with "attempt to stream reference";
350 (Stream
: not null access Root_Stream_Type
'Class;
351 Item
: out Reference_Type
)
354 raise Program_Error
with "attempt to stream reference";
361 procedure Reference
(Item
: not null Shared_Holder_Access
) is
363 System
.Atomic_Counters
.Increment
(Item
.Counter
);
367 (Container
: aliased in out Holder
) return Reference_Type
370 if Container
.Reference
= null then
371 raise Constraint_Error
with "container is empty";
377 Ref
: constant Reference_Type
:=
378 (Element
=> Container
.Reference
.Element
.all'Access,
379 Control
=> (Controlled
with Container
'Unrestricted_Access));
381 Reference
(Ref
.Control
.Container
.Reference
);
382 Ref
.Control
.Container
.Busy
:= Ref
.Control
.Container
.Busy
+ 1;
387 ---------------------
388 -- Replace_Element --
389 ---------------------
391 procedure Replace_Element
392 (Container
: in out Holder
;
393 New_Item
: Element_Type
)
395 -- Element allocator may need an accessibility check in case actual type
396 -- is class-wide or has access discriminants (RM 4.8(10.1) and
399 pragma Unsuppress
(Accessibility_Check
);
402 if Container
.Busy
/= 0 then
403 raise Program_Error
with "attempt to tamper with elements";
406 if Container
.Reference
= null then
407 -- Holder is empty, allocate new Shared_Holder.
409 Container
.Reference
:=
412 Element => new Element_Type'(New_Item
));
414 elsif System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
) then
415 -- Shared_Holder can be reused.
417 Free
(Container
.Reference
.Element
);
418 Container
.Reference
.Element
:= new Element_Type
'(New_Item);
421 Unreference (Container.Reference);
422 Container.Reference :=
425 Element
=> new Element_Type
'(New_Item));
433 function To_Holder (New_Item : Element_Type) return Holder is
434 -- The element allocator may need an accessibility check in the case the
435 -- actual type is class-wide or has access discriminants (RM 4.8(10.1)
438 pragma Unsuppress (Accessibility_Check);
445 Element
=> new Element_Type
'(New_Item)), 0);
452 procedure Unreference (Item : not null Shared_Holder_Access) is
455 new Ada.Unchecked_Deallocation (Shared_Holder, Shared_Holder_Access);
457 Aux : Shared_Holder_Access := Item;
460 if System.Atomic_Counters.Decrement (Aux.Counter) then
470 procedure Update_Element
471 (Container : in out Holder;
472 Process : not null access procedure (Element : in out Element_Type))
474 B : Natural renames Container.Busy;
477 if Container.Reference = null then
478 raise Constraint_Error with "container is empty";
486 Process (Container.Reference.Element.all);
501 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
505 Boolean'Output (Stream, Container.Reference = null);
507 if Container.Reference /= null then
508 Element_Type'Output (Stream, Container.Reference.Element.all);
513 (Stream : not null access Root_Stream_Type'Class;
514 Item : Reference_Type)
517 raise Program_Error with "attempt to stream reference";
521 (Stream : not null access Root_Stream_Type'Class;
522 Item : Constant_Reference_Type)
525 raise Program_Error with "attempt to stream reference";
528 end Ada.Containers.Indefinite_Holders;