2015-10-20 Steve Baird <baird@adacore.com>
[official-gcc.git] / gcc / ada / a-coboho.adb
blob590e807dd32c1864e70b3908e4a9ebd430029800
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, 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 Unchecked_Conversion;
30 package body Ada.Containers.Bounded_Holders is
32 pragma Annotate (CodePeer, Skip_Analysis);
34 function Size_In_Storage_Elements (Element : Element_Type) return Natural;
35 -- This returns the size of Element in storage units. It raises an
36 -- exception if the size is not a multiple of Storage_Unit, or if the size
37 -- is too big.
39 ------------------------------
40 -- Size_In_Storage_Elements --
41 ------------------------------
43 function Size_In_Storage_Elements (Element : Element_Type) return Natural is
44 Max_Size : Natural renames Max_Size_In_Storage_Elements;
46 begin
47 return S : constant Natural := Element'Size / System.Storage_Unit do
48 pragma Assert
49 (Element'Size mod System.Storage_Unit = 0,
50 "Size must be a multiple of Storage_Unit");
52 pragma Assert
53 (S <= Max_Size, "Size is too big:" & S'Img & " >" & Max_Size'Img);
54 end return;
55 end Size_In_Storage_Elements;
57 function Cast is new
58 Unchecked_Conversion (System.Address, Element_Access);
60 ---------
61 -- "=" --
62 ---------
64 function "=" (Left, Right : Holder) return Boolean is
65 begin
66 return Get (Left) = Get (Right);
67 end "=";
69 -------------
70 -- Element --
71 -------------
73 function Get (Container : Holder) return Element_Type is
74 begin
75 return Cast (Container'Address).all;
76 end Get;
78 ---------
79 -- Set --
80 ---------
82 procedure Set (Container : in out Holder; New_Item : Element_Type) is
83 Storage : Storage_Array
84 (1 .. Size_In_Storage_Elements (New_Item)) with
85 Address => New_Item'Address;
86 begin
87 Container.Data (Storage'Range) := Storage;
88 end Set;
90 ---------------
91 -- To_Holder --
92 ---------------
94 function To_Holder (New_Item : Element_Type) return Holder is
95 begin
96 return Result : Holder do
97 Set (Result, New_Item);
98 end return;
99 end To_Holder;
101 end Ada.Containers.Bounded_Holders;