| 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-2025, 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 |
------------------------------------------------------------------------------ |
| 27 |
|
| 28 |
with Ada.Unchecked_Deallocation; |
| 29 |
with System.Put_Images; |
| 30 |
|
| 31 |
package body Ada.Containers.Indefinite_Holders is |
| 32 |
|
| 33 |
procedure Free is |
| 34 |
new Ada.Unchecked_Deallocation (Element_Type, Element_Access); |
| 35 |
|
| 36 |
--------- |
| 37 |
-- "=" -- |
| 38 |
--------- |
| 39 |
|
| 40 |
function "=" (Left, Right : Holder) return Boolean is |
| 41 |
begin |
| 42 |
if Left.Element = null and Right.Element = null then |
| 43 |
return True; |
| 44 |
elsif Left.Element /= null and Right.Element /= null then |
| 45 |
return Left.Element.all = Right.Element.all; |
| 46 |
else |
| 47 |
return False; |
| 48 |
end if; |
| 49 |
end "="; |
| 50 |
|
| 51 |
------------ |
| 52 |
-- Adjust -- |
| 53 |
------------ |
| 54 |
|
| 55 |
overriding procedure Adjust (Container : in out Holder) is |
| 56 |
begin |
| 57 |
if Container.Element /= null then |
| 58 |
Container.Element := new Element_Type'(Container.Element.all); |
| 59 |
end if; |
| 60 |
|
| 61 |
Container.Busy := 0; |
| 62 |
end Adjust; |
| 63 |
|
| 64 |
overriding procedure Adjust (Control : in out Reference_Control_Type) is |
| 65 |
begin |
| 66 |
if Control.Container /= null then |
| 67 |
declare |
| 68 |
B : Natural renames Control.Container.Busy; |
| 69 |
begin |
| 70 |
B := B + 1; |
| 71 |
end; |
| 72 |
end if; |
| 73 |
end Adjust; |
| 74 |
|
| 75 |
------------ |
| 76 |
-- Assign -- |
| 77 |
------------ |
| 78 |
|
| 79 |
procedure Assign (Target : in out Holder; Source : Holder) is |
| 80 |
begin |
| 81 |
if Target.Busy /= 0 then |
| 82 |
raise Program_Error with "attempt to tamper with elements"; |
| 83 |
end if; |
| 84 |
|
| 85 |
if Target.Element /= Source.Element then |
| 86 |
Free (Target.Element); |
| 87 |
|
| 88 |
if Source.Element /= null then |
| 89 |
Target.Element := new Element_Type'(Source.Element.all); |
| 90 |
end if; |
| 91 |
end if; |
| 92 |
end Assign; |
| 93 |
|
| 94 |
----------- |
| 95 |
-- Clear -- |
| 96 |
----------- |
| 97 |
|
| 98 |
procedure Clear (Container : in out Holder) is |
| 99 |
begin |
| 100 |
if Container.Busy /= 0 then |
| 101 |
raise Program_Error with "attempt to tamper with elements"; |
| 102 |
end if; |
| 103 |
|
| 104 |
Free (Container.Element); |
| 105 |
end Clear; |
| 106 |
|
| 107 |
------------------------ |
| 108 |
-- Constant_Reference -- |
| 109 |
------------------------ |
| 110 |
|
| 111 |
function Constant_Reference |
| 112 |
(Container : aliased Holder) return Constant_Reference_Type |
| 113 |
is |
| 114 |
Ref : constant Constant_Reference_Type := |
| 115 |
(Element => Container.Element.all'Access, |
| 116 |
Control => (Controlled with Container'Unrestricted_Access)); |
| 117 |
B : Natural renames Ref.Control.Container.Busy; |
| 118 |
begin |
| 119 |
B := B + 1; |
| 120 |
return Ref; |
| 121 |
end Constant_Reference; |
| 122 |
|
| 123 |
---------- |
| 124 |
-- Copy -- |
| 125 |
---------- |
| 126 |
|
| 127 |
function Copy (Source : Holder) return Holder is |
| 128 |
begin |
| 129 |
if Source.Element = null then |
| 130 |
return (Controlled with null, 0); |
| 131 |
else |
| 132 |
return (Controlled with new Element_Type'(Source.Element.all), 0); |
| 133 |
end if; |
| 134 |
end Copy; |
| 135 |
|
| 136 |
------------- |
| 137 |
-- Element -- |
| 138 |
------------- |
| 139 |
|
| 140 |
function Element (Container : Holder) return Element_Type is |
| 141 |
begin |
| 142 |
if Container.Element = null then |
| 143 |
raise Constraint_Error with "container is empty"; |
| 144 |
else |
| 145 |
return Container.Element.all; |
| 146 |
end if; |
| 147 |
end Element; |
| 148 |
|
| 149 |
-------------- |
| 150 |
-- Finalize -- |
| 151 |
-------------- |
| 152 |
|
| 153 |
overriding procedure Finalize (Container : in out Holder) is |
| 154 |
begin |
| 155 |
if Container.Busy /= 0 then |
| 156 |
raise Program_Error with "attempt to tamper with elements"; |
| 157 |
end if; |
| 158 |
|
| 159 |
Free (Container.Element); |
| 160 |
end Finalize; |
| 161 |
|
| 162 |
overriding procedure Finalize (Control : in out Reference_Control_Type) is |
| 163 |
begin |
| 164 |
if Control.Container /= null then |
| 165 |
declare |
| 166 |
B : Natural renames Control.Container.Busy; |
| 167 |
begin |
| 168 |
B := B - 1; |
| 169 |
end; |
| 170 |
end if; |
| 171 |
|
| 172 |
Control.Container := null; |
| 173 |
end Finalize; |
| 174 |
|
| 175 |
-------------- |
| 176 |
-- Is_Empty -- |
| 177 |
-------------- |
| 178 |
|
| 179 |
function Is_Empty (Container : Holder) return Boolean is |
| 180 |
begin |
| 181 |
return Container.Element = null; |
| 182 |
end Is_Empty; |
| 183 |
|
| 184 |
---------- |
| 185 |
-- Move -- |
| 186 |
---------- |
| 187 |
|
| 188 |
procedure Move (Target : in out Holder; Source : in out Holder) is |
| 189 |
begin |
| 190 |
if Target.Busy /= 0 then |
| 191 |
raise Program_Error with "attempt to tamper with elements"; |
| 192 |
end if; |
| 193 |
|
| 194 |
if Source.Busy /= 0 then |
| 195 |
raise Program_Error with "attempt to tamper with elements"; |
| 196 |
end if; |
| 197 |
|
| 198 |
if Target.Element /= Source.Element then |
| 199 |
Free (Target.Element); |
| 200 |
Target.Element := Source.Element; |
| 201 |
Source.Element := null; |
| 202 |
end if; |
| 203 |
end Move; |
| 204 |
|
| 205 |
------------------- |
| 206 |
-- Query_Element -- |
| 207 |
------------------- |
| 208 |
|
| 209 |
procedure Query_Element |
| 210 |
(Container : Holder; |
| 211 |
Process : not null access procedure (Element : Element_Type)) |
| 212 |
is |
| 213 |
B : Natural renames Container'Unrestricted_Access.Busy; |
| 214 |
|
| 215 |
begin |
| 216 |
if Container.Element = null then |
| 217 |
raise Constraint_Error with "container is empty"; |
| 218 |
end if; |
| 219 |
|
| 220 |
B := B + 1; |
| 221 |
|
| 222 |
begin |
| 223 |
Process (Container.Element.all); |
| 224 |
exception |
| 225 |
when others => |
| 226 |
B := B - 1; |
| 227 |
raise; |
| 228 |
end; |
| 229 |
|
| 230 |
B := B - 1; |
| 231 |
end Query_Element; |
| 232 |
|
| 233 |
--------------- |
| 234 |
-- Put_Image -- |
| 235 |
--------------- |
| 236 |
|
| 237 |
procedure Put_Image |
| 238 |
(S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Holder) |
| 239 |
is |
| 240 |
use System.Put_Images; |
| 241 |
begin |
| 242 |
Array_Before (S); |
| 243 |
if not Is_Empty (V) then |
| 244 |
Element_Type'Put_Image (S, Element (V)); |
| 245 |
end if; |
| 246 |
Array_After (S); |
| 247 |
end Put_Image; |
| 248 |
|
| 249 |
---------- |
| 250 |
-- Read -- |
| 251 |
---------- |
| 252 |
|
| 253 |
procedure Read |
| 254 |
(Stream : not null access Ada.Streams.Root_Stream_Type'Class; |
| 255 |
Container : out Holder) |
| 256 |
is |
| 257 |
begin |
| 258 |
Clear (Container); |
| 259 |
|
| 260 |
if not Boolean'Input (Stream) then |
| 261 |
Container.Element := new Element_Type'(Element_Type'Input (Stream)); |
| 262 |
end if; |
| 263 |
end Read; |
| 264 |
|
| 265 |
procedure Read |
| 266 |
(Stream : not null access Root_Stream_Type'Class; |
| 267 |
Item : out Constant_Reference_Type) |
| 268 |
is |
| 269 |
begin |
| 270 |
raise Program_Error with "attempt to stream reference"; |
| 271 |
end Read; |
| 272 |
|
| 273 |
procedure Read |
| 274 |
(Stream : not null access Root_Stream_Type'Class; |
| 275 |
Item : out Reference_Type) |
| 276 |
is |
| 277 |
begin |
| 278 |
raise Program_Error with "attempt to stream reference"; |
| 279 |
end Read; |
| 280 |
|
| 281 |
--------------- |
| 282 |
-- Reference -- |
| 283 |
--------------- |
| 284 |
|
| 285 |
function Reference |
| 286 |
(Container : aliased in out Holder) return Reference_Type |
| 287 |
is |
| 288 |
Ref : constant Reference_Type := |
| 289 |
(Element => Container.Element.all'Access, |
| 290 |
Control => (Controlled with Container'Unrestricted_Access)); |
| 291 |
begin |
| 292 |
Container.Busy := Container.Busy + 1; |
| 293 |
return Ref; |
| 294 |
end Reference; |
| 295 |
|
| 296 |
--------------------- |
| 297 |
-- Replace_Element -- |
| 298 |
--------------------- |
| 299 |
|
| 300 |
procedure Replace_Element |
| 301 |
(Container : in out Holder; |
| 302 |
New_Item : Element_Type) |
| 303 |
is |
| 304 |
begin |
| 305 |
if Container.Busy /= 0 then |
| 306 |
raise Program_Error with "attempt to tamper with elements"; |
| 307 |
end if; |
| 308 |
|
| 309 |
declare |
| 310 |
X : Element_Access := Container.Element; |
| 311 |
|
| 312 |
-- Element allocator may need an accessibility check in case actual |
| 313 |
-- type is class-wide or has access discriminants (RM 4.8(10.1) and |
| 314 |
-- AI12-0035). |
| 315 |
|
| 316 |
pragma Unsuppress (Accessibility_Check); |
| 317 |
|
| 318 |
begin |
| 319 |
Container.Element := new Element_Type'(New_Item); |
| 320 |
Free (X); |
| 321 |
end; |
| 322 |
end Replace_Element; |
| 323 |
|
| 324 |
---------- |
| 325 |
-- Swap -- |
| 326 |
---------- |
| 327 |
|
| 328 |
procedure Swap (Left, Right : in out Holder) is |
| 329 |
begin |
| 330 |
if Left.Busy /= 0 then |
| 331 |
raise Program_Error with "attempt to tamper with elements"; |
| 332 |
end if; |
| 333 |
|
| 334 |
if Right.Busy /= 0 then |
| 335 |
raise Program_Error with "attempt to tamper with elements"; |
| 336 |
end if; |
| 337 |
|
| 338 |
if Left.Element /= Right.Element then |
| 339 |
declare |
| 340 |
Tmp : constant Element_Access := Left.Element; |
| 341 |
begin |
| 342 |
Left.Element := Right.Element; |
| 343 |
Right.Element := Tmp; |
| 344 |
end; |
| 345 |
end if; |
| 346 |
end Swap; |
| 347 |
|
| 348 |
--------------- |
| 349 |
-- To_Holder -- |
| 350 |
--------------- |
| 351 |
|
| 352 |
function To_Holder (New_Item : Element_Type) return Holder is |
| 353 |
|
| 354 |
-- The element allocator may need an accessibility check in the case the |
| 355 |
-- actual type is class-wide or has access discriminants (RM 4.8(10.1) |
| 356 |
-- and AI12-0035). |
| 357 |
|
| 358 |
pragma Unsuppress (Accessibility_Check); |
| 359 |
|
| 360 |
begin |
| 361 |
return (Controlled with new Element_Type'(New_Item), 0); |
| 362 |
end To_Holder; |
| 363 |
|
| 364 |
-------------------- |
| 365 |
-- Update_Element -- |
| 366 |
-------------------- |
| 367 |
|
| 368 |
procedure Update_Element |
| 369 |
(Container : in out Holder; |
| 370 |
Process : not null access procedure (Element : in out Element_Type)) |
| 371 |
is |
| 372 |
B : Natural renames Container.Busy; |
| 373 |
|
| 374 |
begin |
| 375 |
if Container.Element = null then |
| 376 |
raise Constraint_Error with "container is empty"; |
| 377 |
end if; |
| 378 |
|
| 379 |
B := B + 1; |
| 380 |
|
| 381 |
begin |
| 382 |
Process (Container.Element.all); |
| 383 |
exception |
| 384 |
when others => |
| 385 |
B := B - 1; |
| 386 |
raise; |
| 387 |
end; |
| 388 |
|
| 389 |
B := B - 1; |
| 390 |
end Update_Element; |
| 391 |
|
| 392 |
----------- |
| 393 |
-- Write -- |
| 394 |
----------- |
| 395 |
|
| 396 |
procedure Write |
| 397 |
(Stream : not null access Ada.Streams.Root_Stream_Type'Class; |
| 398 |
Container : Holder) |
| 399 |
is |
| 400 |
begin |
| 401 |
Boolean'Output (Stream, Container.Element = null); |
| 402 |
|
| 403 |
if Container.Element /= null then |
| 404 |
Element_Type'Output (Stream, Container.Element.all); |
| 405 |
end if; |
| 406 |
end Write; |
| 407 |
|
| 408 |
procedure Write |
| 409 |
(Stream : not null access Root_Stream_Type'Class; |
| 410 |
Item : Reference_Type) |
| 411 |
is |
| 412 |
begin |
| 413 |
raise Program_Error with "attempt to stream reference"; |
| 414 |
end Write; |
| 415 |
|
| 416 |
procedure Write |
| 417 |
(Stream : not null access Root_Stream_Type'Class; |
| 418 |
Item : Constant_Reference_Type) |
| 419 |
is |
| 420 |
begin |
| 421 |
raise Program_Error with "attempt to stream reference"; |
| 422 |
end Write; |
| 423 |
|
| 424 |
end Ada.Containers.Indefinite_Holders; |