re PR bootstrap/51346 (LTO bootstrap failed with bootstrap-profiled)
[official-gcc.git] / gcc / ada / a-coinho.adb
blobb6c38b098b63134ab594e7f322f653b2e680f68c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ V E C T O R S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2011, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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 procedure Free is
33 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
35 ---------
36 -- "=" --
37 ---------
39 function "=" (Left, Right : Holder) return Boolean is
40 begin
41 if Left.Element = null and Right.Element = null then
42 return True;
44 elsif Left.Element /= null and Right.Element /= null then
45 return Left.Element.all = Right.Element.all;
47 else
48 return False;
49 end if;
50 end "=";
52 ------------
53 -- Adjust --
54 ------------
56 overriding procedure Adjust (Container : in out Holder) is
57 begin
58 if Container.Element /= null then
59 Container.Element := new Element_Type'(Container.Element.all);
60 end if;
62 Container.Busy := 0;
63 end Adjust;
65 ------------
66 -- Assign --
67 ------------
69 procedure Assign (Target : in out Holder; Source : Holder) is
70 begin
71 if Target.Busy /= 0 then
72 raise Program_Error with "attempt to tamper with elements";
73 end if;
75 if Target.Element /= Source.Element then
76 Free (Target.Element);
78 if Source.Element /= null then
79 Target.Element := new Element_Type'(Source.Element.all);
80 end if;
81 end if;
82 end Assign;
84 -----------
85 -- Clear --
86 -----------
88 procedure Clear (Container : in out Holder) is
89 begin
90 if Container.Busy /= 0 then
91 raise Program_Error with "attempt to tamper with elements";
92 end if;
94 Free (Container.Element);
95 end Clear;
97 ----------
98 -- Copy --
99 ----------
101 function Copy (Source : Holder) return Holder is
102 begin
103 if Source.Element = null then
104 return (AF.Controlled with null, 0);
105 else
106 return (AF.Controlled with new Element_Type'(Source.Element.all), 0);
107 end if;
108 end Copy;
110 -------------
111 -- Element --
112 -------------
114 function Element (Container : Holder) return Element_Type is
115 begin
116 if Container.Element = null then
117 raise Constraint_Error with "container is empty";
118 else
119 return Container.Element.all;
120 end if;
121 end Element;
123 --------------
124 -- Finalize --
125 --------------
127 overriding procedure Finalize (Container : in out Holder) is
128 begin
129 if Container.Busy /= 0 then
130 raise Program_Error with "attempt to tamper with elements";
131 end if;
133 Free (Container.Element);
134 end Finalize;
136 --------------
137 -- Is_Empty --
138 --------------
140 function Is_Empty (Container : Holder) return Boolean is
141 begin
142 return Container.Element = null;
143 end Is_Empty;
145 ----------
146 -- Move --
147 ----------
149 procedure Move (Target : in out Holder; Source : in out Holder) is
150 begin
151 if Target.Busy /= 0 then
152 raise Program_Error with "attempt to tamper with elements";
153 end if;
155 if Source.Busy /= 0 then
156 raise Program_Error with "attempt to tamper with elements";
157 end if;
159 if Target.Element /= Source.Element then
160 Free (Target.Element);
161 Target.Element := Source.Element;
162 Source.Element := null;
163 end if;
164 end Move;
166 -------------------
167 -- Query_Element --
168 -------------------
170 procedure Query_Element
171 (Container : Holder;
172 Process : not null access procedure (Element : Element_Type))
174 B : Natural renames Container'Unrestricted_Access.Busy;
176 begin
177 if Container.Element = null then
178 raise Constraint_Error with "container is empty";
179 end if;
181 B := B + 1;
183 begin
184 Process (Container.Element.all);
185 exception
186 when others =>
187 B := B - 1;
188 raise;
189 end;
191 B := B - 1;
192 end Query_Element;
194 ----------
195 -- Read --
196 ----------
198 procedure Read
199 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
200 Container : out Holder)
202 begin
203 Clear (Container);
205 if not Boolean'Input (Stream) then
206 Container.Element := new Element_Type'(Element_Type'Input (Stream));
207 end if;
208 end Read;
210 ---------------------
211 -- Replace_Element --
212 ---------------------
214 procedure Replace_Element
215 (Container : in out Holder;
216 New_Item : Element_Type)
218 begin
219 if Container.Busy /= 0 then
220 raise Program_Error with "attempt to tamper with elements";
221 end if;
223 Free (Container.Element);
224 Container.Element := new Element_Type'(New_Item);
225 end Replace_Element;
227 ---------------
228 -- To_Holder --
229 ---------------
231 function To_Holder (New_Item : Element_Type) return Holder is
232 begin
233 return (AF.Controlled with new Element_Type'(New_Item), 0);
234 end To_Holder;
236 --------------------
237 -- Update_Element --
238 --------------------
240 procedure Update_Element
241 (Container : Holder;
242 Process : not null access procedure (Element : in out Element_Type))
244 B : Natural renames Container'Unrestricted_Access.Busy;
246 begin
247 if Container.Element = null then
248 raise Constraint_Error with "container is empty";
249 end if;
251 B := B + 1;
253 begin
254 Process (Container.Element.all);
255 exception
256 when others =>
257 B := B - 1;
258 raise;
259 end;
261 B := B - 1;
262 end Update_Element;
264 -----------
265 -- Write --
266 -----------
268 procedure Write
269 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
270 Container : Holder)
272 begin
273 Boolean'Output (Stream, Container.Element = null);
275 if Container.Element /= null then
276 Element_Type'Output (Stream, Container.Element.all);
277 end if;
278 end Write;
280 end Ada.Containers.Indefinite_Holders;