| 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 _ H O L D E R S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2015-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_Conversion; |
| 29 |
with System.Put_Images; |
| 30 |
|
| 31 |
package body Ada.Containers.Bounded_Holders is |
| 32 |
|
| 33 |
function Size_In_Storage_Elements (Element : Element_Type) return Natural; |
| 34 |
-- This returns the size of Element in storage units. It raises an |
| 35 |
-- exception if the size is not a multiple of Storage_Unit, or if the size |
| 36 |
-- is too big. |
| 37 |
|
| 38 |
------------------------------ |
| 39 |
-- Size_In_Storage_Elements -- |
| 40 |
------------------------------ |
| 41 |
|
| 42 |
function Size_In_Storage_Elements (Element : Element_Type) return Natural is |
| 43 |
Max_Size : Natural renames Max_Size_In_Storage_Elements; |
| 44 |
|
| 45 |
begin |
| 46 |
return S : constant Natural := Element'Size / System.Storage_Unit do |
| 47 |
pragma Assert |
| 48 |
(Element'Size mod System.Storage_Unit = 0, |
| 49 |
"Size must be a multiple of Storage_Unit"); |
| 50 |
|
| 51 |
pragma Assert |
| 52 |
(S <= Max_Size, "Size is too big:" & S'Img & " >" & Max_Size'Img); |
| 53 |
end return; |
| 54 |
end Size_In_Storage_Elements; |
| 55 |
|
| 56 |
function Cast is new |
| 57 |
Ada.Unchecked_Conversion (System.Address, Element_Access); |
| 58 |
|
| 59 |
--------- |
| 60 |
-- "=" -- |
| 61 |
--------- |
| 62 |
|
| 63 |
function "=" (Left, Right : Holder) return Boolean is |
| 64 |
begin |
| 65 |
return Get (Left) = Get (Right); |
| 66 |
end "="; |
| 67 |
|
| 68 |
------------------------ |
| 69 |
-- Constant_Reference -- |
| 70 |
------------------------ |
| 71 |
|
| 72 |
function Constant_Reference |
| 73 |
(Container : aliased Holder) return not null access constant Element_Type |
| 74 |
is |
| 75 |
begin |
| 76 |
return Cast (Container'Address); |
| 77 |
end Constant_Reference; |
| 78 |
|
| 79 |
--------- |
| 80 |
-- Get -- |
| 81 |
--------- |
| 82 |
|
| 83 |
function Get (Container : Holder) return Element_Type is |
| 84 |
begin |
| 85 |
return Cast (Container'Address).all; |
| 86 |
end Get; |
| 87 |
|
| 88 |
--------------- |
| 89 |
-- Put_Image -- |
| 90 |
--------------- |
| 91 |
|
| 92 |
procedure Put_Image |
| 93 |
(S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Holder) |
| 94 |
is |
| 95 |
use System.Put_Images; |
| 96 |
begin |
| 97 |
Array_Before (S); |
| 98 |
Element_Type'Put_Image (S, Get (V)); |
| 99 |
Array_After (S); |
| 100 |
end Put_Image; |
| 101 |
|
| 102 |
--------------- |
| 103 |
-- Reference -- |
| 104 |
--------------- |
| 105 |
|
| 106 |
function Reference |
| 107 |
(Container : not null access Holder) return not null access Element_Type |
| 108 |
is |
| 109 |
begin |
| 110 |
return Cast (Container.all'Address); |
| 111 |
end Reference; |
| 112 |
|
| 113 |
--------- |
| 114 |
-- Set -- |
| 115 |
--------- |
| 116 |
|
| 117 |
procedure Set (Container : in out Holder; New_Item : Element_Type) is |
| 118 |
Storage : Storage_Array |
| 119 |
(1 .. Size_In_Storage_Elements (New_Item)) with |
| 120 |
Address => New_Item'Address; |
| 121 |
begin |
| 122 |
Container.Data (Storage'Range) := Storage; |
| 123 |
end Set; |
| 124 |
|
| 125 |
--------------- |
| 126 |
-- To_Holder -- |
| 127 |
--------------- |
| 128 |
|
| 129 |
function To_Holder (New_Item : Element_Type) return Holder is |
| 130 |
begin |
| 131 |
return Result : Holder do |
| 132 |
Set (Result, New_Item); |
| 133 |
end return; |
| 134 |
end To_Holder; |
| 135 |
|
| 136 |
end Ada.Containers.Bounded_Holders; |