2015-05-20 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-coboho.adb
blob4ea0fa047aa4c8b3f7365a4d2dc7458c97b88868
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) 2014, 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;
29 with Ada.Assertions; use Ada.Assertions;
31 package body Ada.Containers.Bounded_Holders is
33 pragma Annotate (CodePeer, Skip_Analysis);
35 function Size_In_Storage_Elements (Element : Element_Type) return Natural is
36 (Element'Size / System.Storage_Unit)
37 with Pre =>
38 (Element'Size mod System.Storage_Unit = 0 or else
39 raise Assertion_Error with "Size must be a multiple of Storage_Unit")
40 and then
41 (Element'Size / System.Storage_Unit <= Max_Size_In_Storage_Elements
42 or else raise Assertion_Error with "Size is too big");
43 -- This returns the size of Element in storage units. It raises an
44 -- exception if the size is not a multiple of Storage_Unit, or if the size
45 -- is too big.
47 function Cast is new
48 Unchecked_Conversion (System.Address, Element_Access);
50 ---------
51 -- "=" --
52 ---------
54 function "=" (Left, Right : Holder) return Boolean is
55 begin
56 return Get (Left) = Get (Right);
57 end "=";
59 -------------
60 -- Element --
61 -------------
63 function Get (Container : Holder) return Element_Type is
64 begin
65 return Cast (Container'Address).all;
66 end Get;
68 ---------------------
69 -- Replace_Element --
70 ---------------------
72 procedure Set (Container : in out Holder; New_Item : Element_Type) is
73 Storage : Storage_Array
74 (1 .. Size_In_Storage_Elements (New_Item)) with
75 Address => New_Item'Address;
76 begin
77 Container.Data (Storage'Range) := Storage;
78 end Set;
80 ---------------
81 -- To_Holder --
82 ---------------
84 function To_Holder (New_Item : Element_Type) return Holder is
85 begin
86 return Result : Holder do
87 Set (Result, New_Item);
88 end return;
89 end To_Holder;
91 end Ada.Containers.Bounded_Holders;