File : a-coboho.adb


   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 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  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 Unchecked_Conversion;
  29 
  30 package body Ada.Containers.Bounded_Holders is
  31 
  32    function Size_In_Storage_Elements (Element : Element_Type) return Natural;
  33    --  This returns the size of Element in storage units. It raises an
  34    --  exception if the size is not a multiple of Storage_Unit, or if the size
  35    --  is too big.
  36 
  37    ------------------------------
  38    -- Size_In_Storage_Elements --
  39    ------------------------------
  40 
  41    function Size_In_Storage_Elements (Element : Element_Type) return Natural is
  42       Max_Size : Natural renames Max_Size_In_Storage_Elements;
  43 
  44    begin
  45       return S : constant Natural := Element'Size / System.Storage_Unit do
  46          pragma Assert
  47            (Element'Size mod System.Storage_Unit = 0,
  48             "Size must be a multiple of Storage_Unit");
  49 
  50          pragma Assert
  51            (S <= Max_Size, "Size is too big:" & S'Img & " >" & Max_Size'Img);
  52       end return;
  53    end Size_In_Storage_Elements;
  54 
  55    function Cast is new
  56      Unchecked_Conversion (System.Address, Element_Access);
  57 
  58    ---------
  59    -- "=" --
  60    ---------
  61 
  62    function "=" (Left, Right : Holder) return Boolean is
  63    begin
  64       return Get (Left) = Get (Right);
  65    end "=";
  66 
  67    -------------
  68    -- Element --
  69    -------------
  70 
  71    function Get (Container : Holder) return Element_Type is
  72    begin
  73       return Cast (Container'Address).all;
  74    end Get;
  75 
  76    ---------
  77    -- Set --
  78    ---------
  79 
  80    procedure Set (Container : in out Holder; New_Item  : Element_Type) is
  81       Storage : Storage_Array
  82         (1 .. Size_In_Storage_Elements (New_Item)) with
  83           Address => New_Item'Address;
  84    begin
  85       Container.Data (Storage'Range) := Storage;
  86    end Set;
  87 
  88    ---------------
  89    -- To_Holder --
  90    ---------------
  91 
  92    function To_Holder (New_Item : Element_Type) return Holder is
  93    begin
  94       return Result : Holder do
  95          Set (Result, New_Item);
  96       end return;
  97    end To_Holder;
  98 
  99 end Ada.Containers.Bounded_Holders;