gcc/
[official-gcc.git] / gcc / ada / a-coboho.ads
blob7e6933e22deefdcc7967742790fb11362ff6a82f
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 -- S p e c --
8 -- --
9 -- Copyright (C) 2014, Free Software Foundation, Inc. --
10 -- --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
14 -- --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
21 -- --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
25 -- --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
30 ------------------------------------------------------------------------------
32 private with System;
34 generic
35 type Element_Type (<>) is private;
36 Max_Size_In_Storage_Elements : Natural :=
37 Element_Type'Max_Size_In_Storage_Elements;
38 with function "=" (Left, Right : Element_Type) return Boolean is <>;
40 package Ada.Containers.Bounded_Holders is
41 -- This package is patterned after Ada.Containers.Indefinite_Holders. It is
42 -- used to treat indefinite subtypes as definite, but without using heap
43 -- allocation. For example, you might like to say:
45 -- type A is array (...) of T'Class; -- illegal
47 -- Instead, you can instantiate this package with Element_Type => T'Class,
48 -- and say:
50 -- type A is array (...) of Holder;
52 -- Each object of type Holder is allocated Max_Size_In_Storage_Elements
53 -- bytes. If you try to create a holder from an object of type Element_Type
54 -- that is too big, an exception is raised. This applies to To_Holder and
55 -- Replace_Element. If you pass an Element_Type object that is smaller than
56 -- Max_Size_In_Storage_Elements, it works fine, but some space is wasted.
58 -- Element_Type must not be an unconstrained array type. It can be a
59 -- class-wide type or a type with non-defaulted discriminants.
61 -- The 'Size of each Element_Type object must be a multiple of
62 -- System.Storage_Unit; e.g. creating Holders from 5-bit objects won't
63 -- work.
65 type Holder is private;
67 function "=" (Left, Right : Holder) return Boolean;
69 function To_Holder (New_Item : Element_Type) return Holder;
70 function "+" (New_Item : Element_Type) return Holder renames To_Holder;
72 function Get (Container : Holder) return Element_Type;
74 procedure Set (Container : in out Holder; New_Item : Element_Type);
76 private
78 -- The implementation uses low-level tricks (Address clauses and unchecked
79 -- conversions of access types) to treat the elements as storage arrays.
81 pragma Assert (Element_Type'Alignment <= Standard'Maximum_Alignment);
82 -- This prevents elements with a user-specified Alignment that is too big
84 type Storage_Element is mod System.Storage_Unit;
85 type Storage_Array is array (Positive range <>) of Storage_Element;
86 type Holder is record
87 Data : Storage_Array (1 .. Max_Size_In_Storage_Elements);
88 end record
89 with Alignment => Standard'Maximum_Alignment;
90 -- We would like to say "Alignment => Element_Type'Alignment", but that
91 -- is illegal because it's not static, so we use the maximum possible
92 -- (default) alignment instead.
94 type Element_Access is access all Element_Type;
95 pragma Assert (Element_Access'Size = Standard'Address_Size,
96 "cannot instantiate with an array type");
97 -- If Element_Access is a fat pointer, Element_Type must be an
98 -- unconstrained array, which is not allowed. Arrays won't work, because
99 -- the 'Address of an array points to the first element, thus losing the
100 -- bounds.
102 pragma No_Strict_Aliasing (Element_Access);
103 -- Needed because we are unchecked-converting from Address to
104 -- Element_Access (see package body), which is a violation of the
105 -- normal aliasing rules enforced by gcc.
107 end Ada.Containers.Bounded_Holders;