Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / a-coinho.adb
blob0d0d40064e9d4071c5ecf0e2aaf5600510ac13d8
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
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 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2012, 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 declare
224 X : Element_Access := Container.Element;
226 -- Element allocator may need an accessibility check in case actual
227 -- type is class-wide or has access discriminants (RM 4.8(10.1) and
228 -- AI12-0035).
230 pragma Unsuppress (Accessibility_Check);
232 begin
233 Container.Element := new Element_Type'(New_Item);
234 Free (X);
235 end;
236 end Replace_Element;
238 ---------------
239 -- To_Holder --
240 ---------------
242 function To_Holder (New_Item : Element_Type) return Holder is
243 -- The element allocator may need an accessibility check in the case the
244 -- actual type is class-wide or has access discriminants (RM 4.8(10.1)
245 -- and AI12-0035).
247 pragma Unsuppress (Accessibility_Check);
249 begin
250 return (AF.Controlled with new Element_Type'(New_Item), 0);
251 end To_Holder;
253 --------------------
254 -- Update_Element --
255 --------------------
257 procedure Update_Element
258 (Container : Holder;
259 Process : not null access procedure (Element : in out Element_Type))
261 B : Natural renames Container'Unrestricted_Access.Busy;
263 begin
264 if Container.Element = null then
265 raise Constraint_Error with "container is empty";
266 end if;
268 B := B + 1;
270 begin
271 Process (Container.Element.all);
272 exception
273 when others =>
274 B := B - 1;
275 raise;
276 end;
278 B := B - 1;
279 end Update_Element;
281 -----------
282 -- Write --
283 -----------
285 procedure Write
286 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
287 Container : Holder)
289 begin
290 Boolean'Output (Stream, Container.Element = null);
292 if Container.Element /= null then
293 Element_Type'Output (Stream, Container.Element.all);
294 end if;
295 end Write;
297 end Ada.Containers.Indefinite_Holders;