1. -- 
  2. --  Copyright (c) 2009, 
  3. --  Reto Buerki, Adrian-Ken Rueegsegger 
  4. --  secunet SwissIT AG 
  5. -- 
  6. --  This file is part of Alog. 
  7. -- 
  8. --  Alog is free software; you can redistribute it and/or modify 
  9. --  it under the terms of the GNU Lesser General Public License as published 
  10. --  by the Free Software Foundation; either version 2.1 of the License, or 
  11. --  (at your option) any later version. 
  12. -- 
  13. --  Alog is distributed in the hope that it will be useful, 
  14. --  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15. --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  16. --  GNU Lesser General Public License for more details. 
  17. -- 
  18. --  You should have received a copy of the GNU Lesser General Public License 
  19. --  along with Alog; if not, write to the Free Software 
  20. --  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
  21. --  MA  02110-1301  USA 
  22. -- 
  23.  
  24. with Ada.Finalization; 
  25. with Ada.Containers.Indefinite_Ordered_Maps; 
  26.  
  27. generic 
  28.    type Key_Type (<>) is private; 
  29.    type Element_Type (<>) is limited private; 
  30.    type Element_Handle is access Element_Type; 
  31.  
  32.    with function "<" (Left, Right : Key_Type) return Boolean is <>; 
  33.  
  34. --  Controlled variant of a map. The memory of an element pointed to by a 
  35. --  previously inserted handle is freed upon calling Delete, Clear or during 
  36. --  finalization of the controlled map. Thus control over objects inserted into 
  37. --  this map resides with the controlled map. 
  38. package Alog.Controlled_Map is 
  39.  
  40.    pragma Preelaborate; 
  41.  
  42.    type Map is new Ada.Finalization.Limited_Controlled with private; 
  43.    --  A controlled map container. 
  44.  
  45.    procedure Insert 
  46.      (Container : in out Map; 
  47.       Key       :        Key_Type; 
  48.       New_Item  :        Element_Handle); 
  49.    --  Insert a new element handle with 'Key' into the controlled map. 
  50.  
  51.    function Element 
  52.      (Container : Map; 
  53.       Key       : Key_Type) 
  54.       return Element_Handle; 
  55.    --  Return a handle to an element identified by 'Key'. 
  56.  
  57.    procedure Delete 
  58.      (Container : in out Map; 
  59.       Key       :        Key_Type); 
  60.    --  Delete the element with key 'Key' from the map. Memory of the element is 
  61.    --  freed. 
  62.  
  63.    function Contains 
  64.      (Container : Map; 
  65.       Key       : Key_Type) 
  66.       return Boolean; 
  67.    --  Returns True if an element with key 'Key' is in the map. 
  68.  
  69.    function Is_Empty (Container : Map) return Boolean; 
  70.    --  Returns True if the map is empty. 
  71.  
  72.    procedure Clear (Container : in out Map); 
  73.    --  Remove all elements in the map. Memory of the elements is freed. 
  74.  
  75.    function Length (Container : Map) return Natural; 
  76.    --  Return the current element count. 
  77.  
  78.    procedure Iterate 
  79.      (Container : Map; 
  80.       Process   : not null access procedure (Handle : Element_Handle)); 
  81.    --  Iterate over all elements in the map and call the 'Process' procedure for 
  82.    --  each handle. 
  83.  
  84. private 
  85.  
  86.    overriding 
  87.    procedure Finalize (Container : in out Map); 
  88.    --  Clean up the the controlled map. This will Free all the memory occupied 
  89.    --  by the elements in the map. 
  90.  
  91.    package Map_Of_Elements_Package is new 
  92.      Ada.Containers.Indefinite_Ordered_Maps 
  93.        (Key_Type     => Key_Type, 
  94.         Element_Type => Element_Handle); 
  95.  
  96.    package MOEP renames Map_Of_Elements_Package; 
  97.  
  98.    type Map is new Ada.Finalization.Limited_Controlled with record 
  99.       Data : MOEP.Map; 
  100.    end record; 
  101.  
  102. end Alog.Controlled_Map;