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) 2012-2014, 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
.Element
= null and Right
.Element
= null then
43 elsif Left
.Element
/= null and Right
.Element
/= null then
44 return Left
.Element
.all = Right
.Element
.all;
54 overriding
procedure Adjust
(Container
: in out Holder
) is
56 if Container
.Element
/= null then
57 Container
.Element
:= new Element_Type
'(Container.Element.all);
63 overriding procedure Adjust (Control : in out Reference_Control_Type) is
65 if Control.Container /= null then
67 B : Natural renames Control.Container.Busy;
78 procedure Assign (Target : in out Holder; Source : Holder) is
80 if Target.Busy /= 0 then
81 raise Program_Error with "attempt to tamper with elements";
84 if Target.Element /= Source.Element then
85 Free (Target.Element);
87 if Source.Element /= null then
88 Target.Element := new Element_Type'(Source
.Element
.all);
97 procedure Clear
(Container
: in out Holder
) is
99 if Container
.Busy
/= 0 then
100 raise Program_Error
with "attempt to tamper with elements";
103 Free
(Container
.Element
);
106 ------------------------
107 -- Constant_Reference --
108 ------------------------
110 function Constant_Reference
111 (Container
: aliased Holder
) return Constant_Reference_Type
113 Ref
: constant Constant_Reference_Type
:=
114 (Element
=> Container
.Element
.all'Access,
115 Control
=> (Controlled
with Container
'Unrestricted_Access));
116 B
: Natural renames Ref
.Control
.Container
.Busy
;
120 end Constant_Reference
;
126 function Copy
(Source
: Holder
) return Holder
is
128 if Source
.Element
= null then
129 return (Controlled
with null, 0);
131 return (Controlled
with new Element_Type
'(Source.Element.all), 0);
139 function Element (Container : Holder) return Element_Type is
141 if Container.Element = null then
142 raise Constraint_Error with "container is empty";
144 return Container.Element.all;
152 overriding procedure Finalize (Container : in out Holder) is
154 if Container.Busy /= 0 then
155 raise Program_Error with "attempt to tamper with elements";
158 Free (Container.Element);
161 overriding procedure Finalize (Control : in out Reference_Control_Type) is
163 if Control.Container /= null then
165 B : Natural renames Control.Container.Busy;
171 Control.Container := null;
178 function Is_Empty (Container : Holder) return Boolean is
180 return Container.Element = null;
187 procedure Move (Target : in out Holder; Source : in out Holder) is
189 if Target.Busy /= 0 then
190 raise Program_Error with "attempt to tamper with elements";
193 if Source.Busy /= 0 then
194 raise Program_Error with "attempt to tamper with elements";
197 if Target.Element /= Source.Element then
198 Free (Target.Element);
199 Target.Element := Source.Element;
200 Source.Element := null;
208 procedure Query_Element
210 Process : not null access procedure (Element : Element_Type))
212 B : Natural renames Container'Unrestricted_Access.Busy;
215 if Container.Element = null then
216 raise Constraint_Error with "container is empty";
222 Process (Container.Element.all);
237 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
238 Container : out Holder)
243 if not Boolean'Input (Stream) then
244 Container.Element := new Element_Type'(Element_Type
'Input (Stream
));
249 (Stream
: not null access Root_Stream_Type
'Class;
250 Item
: out Constant_Reference_Type
)
253 raise Program_Error
with "attempt to stream reference";
257 (Stream
: not null access Root_Stream_Type
'Class;
258 Item
: out Reference_Type
)
261 raise Program_Error
with "attempt to stream reference";
269 (Container
: aliased in out Holder
) return Reference_Type
271 Ref
: constant Reference_Type
:=
272 (Element
=> Container
.Element
.all'Access,
273 Control
=> (Controlled
with Container
'Unrestricted_Access));
275 Container
.Busy
:= Container
.Busy
+ 1;
279 ---------------------
280 -- Replace_Element --
281 ---------------------
283 procedure Replace_Element
284 (Container
: in out Holder
;
285 New_Item
: Element_Type
)
288 if Container
.Busy
/= 0 then
289 raise Program_Error
with "attempt to tamper with elements";
293 X
: Element_Access
:= Container
.Element
;
295 -- Element allocator may need an accessibility check in case actual
296 -- type is class-wide or has access discriminants (RM 4.8(10.1) and
299 pragma Unsuppress
(Accessibility_Check
);
302 Container
.Element
:= new Element_Type
'(New_Item);
311 function To_Holder (New_Item : Element_Type) return Holder is
313 -- The element allocator may need an accessibility check in the case the
314 -- actual type is class-wide or has access discriminants (RM 4.8(10.1)
317 pragma Unsuppress (Accessibility_Check);
320 return (Controlled with new Element_Type'(New_Item
), 0);
327 procedure Update_Element
328 (Container
: in out Holder
;
329 Process
: not null access procedure (Element
: in out Element_Type
))
331 B
: Natural renames Container
.Busy
;
334 if Container
.Element
= null then
335 raise Constraint_Error
with "container is empty";
341 Process
(Container
.Element
.all);
356 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
360 Boolean'Output (Stream
, Container
.Element
= null);
362 if Container
.Element
/= null then
363 Element_Type
'Output (Stream
, Container
.Element
.all);
368 (Stream
: not null access Root_Stream_Type
'Class;
369 Item
: Reference_Type
)
372 raise Program_Error
with "attempt to stream reference";
376 (Stream
: not null access Root_Stream_Type
'Class;
377 Item
: Constant_Reference_Type
)
380 raise Program_Error
with "attempt to stream reference";
383 end Ada
.Containers
.Indefinite_Holders
;