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
32 pragma Annotate
(CodePeer
, Skip_Analysis
);
35 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
41 function "=" (Left
, Right
: Holder
) return Boolean is
43 if Left
.Element
= null and Right
.Element
= null then
45 elsif Left
.Element
/= null and Right
.Element
/= null then
46 return Left
.Element
.all = Right
.Element
.all;
56 overriding
procedure Adjust
(Container
: in out Holder
) is
58 if Container
.Element
/= null then
59 Container
.Element
:= new Element_Type
'(Container.Element.all);
65 overriding procedure Adjust (Control : in out Reference_Control_Type) is
67 if Control.Container /= null then
69 B : Natural renames Control.Container.Busy;
80 procedure Assign (Target : in out Holder; Source : Holder) is
82 if Target.Busy /= 0 then
83 raise Program_Error with "attempt to tamper with elements";
86 if Target.Element /= Source.Element then
87 Free (Target.Element);
89 if Source.Element /= null then
90 Target.Element := new Element_Type'(Source
.Element
.all);
99 procedure Clear
(Container
: in out Holder
) is
101 if Container
.Busy
/= 0 then
102 raise Program_Error
with "attempt to tamper with elements";
105 Free
(Container
.Element
);
108 ------------------------
109 -- Constant_Reference --
110 ------------------------
112 function Constant_Reference
113 (Container
: aliased Holder
) return Constant_Reference_Type
115 Ref
: constant Constant_Reference_Type
:=
116 (Element
=> Container
.Element
.all'Access,
117 Control
=> (Controlled
with Container
'Unrestricted_Access));
118 B
: Natural renames Ref
.Control
.Container
.Busy
;
122 end Constant_Reference
;
128 function Copy
(Source
: Holder
) return Holder
is
130 if Source
.Element
= null then
131 return (Controlled
with null, 0);
133 return (Controlled
with new Element_Type
'(Source.Element.all), 0);
141 function Element (Container : Holder) return Element_Type is
143 if Container.Element = null then
144 raise Constraint_Error with "container is empty";
146 return Container.Element.all;
154 overriding procedure Finalize (Container : in out Holder) is
156 if Container.Busy /= 0 then
157 raise Program_Error with "attempt to tamper with elements";
160 Free (Container.Element);
163 overriding procedure Finalize (Control : in out Reference_Control_Type) is
165 if Control.Container /= null then
167 B : Natural renames Control.Container.Busy;
173 Control.Container := null;
180 function Is_Empty (Container : Holder) return Boolean is
182 return Container.Element = null;
189 procedure Move (Target : in out Holder; Source : in out Holder) is
191 if Target.Busy /= 0 then
192 raise Program_Error with "attempt to tamper with elements";
195 if Source.Busy /= 0 then
196 raise Program_Error with "attempt to tamper with elements";
199 if Target.Element /= Source.Element then
200 Free (Target.Element);
201 Target.Element := Source.Element;
202 Source.Element := null;
210 procedure Query_Element
212 Process : not null access procedure (Element : Element_Type))
214 B : Natural renames Container'Unrestricted_Access.Busy;
217 if Container.Element = null then
218 raise Constraint_Error with "container is empty";
224 Process (Container.Element.all);
239 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
240 Container : out Holder)
245 if not Boolean'Input (Stream) then
246 Container.Element := new Element_Type'(Element_Type
'Input (Stream
));
251 (Stream
: not null access Root_Stream_Type
'Class;
252 Item
: out Constant_Reference_Type
)
255 raise Program_Error
with "attempt to stream reference";
259 (Stream
: not null access Root_Stream_Type
'Class;
260 Item
: out Reference_Type
)
263 raise Program_Error
with "attempt to stream reference";
271 (Container
: aliased in out Holder
) return Reference_Type
273 Ref
: constant Reference_Type
:=
274 (Element
=> Container
.Element
.all'Access,
275 Control
=> (Controlled
with Container
'Unrestricted_Access));
277 Container
.Busy
:= Container
.Busy
+ 1;
281 ---------------------
282 -- Replace_Element --
283 ---------------------
285 procedure Replace_Element
286 (Container
: in out Holder
;
287 New_Item
: Element_Type
)
290 if Container
.Busy
/= 0 then
291 raise Program_Error
with "attempt to tamper with elements";
295 X
: Element_Access
:= Container
.Element
;
297 -- Element allocator may need an accessibility check in case actual
298 -- type is class-wide or has access discriminants (RM 4.8(10.1) and
301 pragma Unsuppress
(Accessibility_Check
);
304 Container
.Element
:= new Element_Type
'(New_Item);
313 function To_Holder (New_Item : Element_Type) return Holder is
315 -- The element allocator may need an accessibility check in the case the
316 -- actual type is class-wide or has access discriminants (RM 4.8(10.1)
319 pragma Unsuppress (Accessibility_Check);
322 return (Controlled with new Element_Type'(New_Item
), 0);
329 procedure Update_Element
330 (Container
: in out Holder
;
331 Process
: not null access procedure (Element
: in out Element_Type
))
333 B
: Natural renames Container
.Busy
;
336 if Container
.Element
= null then
337 raise Constraint_Error
with "container is empty";
343 Process
(Container
.Element
.all);
358 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
362 Boolean'Output (Stream
, Container
.Element
= null);
364 if Container
.Element
/= null then
365 Element_Type
'Output (Stream
, Container
.Element
.all);
370 (Stream
: not null access Root_Stream_Type
'Class;
371 Item
: Reference_Type
)
374 raise Program_Error
with "attempt to stream reference";
378 (Stream
: not null access Root_Stream_Type
'Class;
379 Item
: Constant_Reference_Type
)
382 raise Program_Error
with "attempt to stream reference";
385 end Ada
.Containers
.Indefinite_Holders
;