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