agpl_1.0.0_b5da3320/src/agpl-counter-multi.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Finalization;
with Ada.Strings.Hash;
use  Ada;

package Agpl.Counter.Multi is

   pragma Preelaborate;

   ------------------------------------------------------------------------
   -- Object                                                             --
   ------------------------------------------------------------------------
   type Object is tagged limited private;
   pragma Preelaborable_Initialization (Object);

   ------------------------------------------------------------------------
   -- Add                                                                --
   ------------------------------------------------------------------------
   --  Creates it if doesn't exists with value Increment
   procedure Add (This : in out Object; Key : in String; Increment : in Integer := 1);

   ------------------------------------------------------------------------
   -- Reset                                                              --
   ------------------------------------------------------------------------
   procedure Reset (This : in out Object; Key : in String; Val : in Integer := 0);

   ------------------------------------------------------------------------
   -- Val                                                                --
   ------------------------------------------------------------------------
   function  Val (This : in Object; Key : in String) return Integer;

   function Val (This : Object; Key : String; Default : Integer) return Integer;

   ------------------------------------------------------------------------
   -- Max_Key                                                            --
   ------------------------------------------------------------------------
   function  Max_Key (This : in Object) return String; -- Key with highest value

private

   package Counter_Map is new Ada.Containers.Indefinite_Hashed_Maps
     (String,
      Counter.Object_Access,
      Ada.Strings.Hash,
      "=",
      "=");

   ------------------------------------------------------------------------
   -- Safe                                                               --
   ------------------------------------------------------------------------
   protected type Safe_Object (Parent : access Object) is
      --  Creates it if doesn't exists with value Increment
      procedure Add (Key : in String; Increment : in Integer := 1);
      procedure Reset (Key : in String; Val     : in Integer := 0);
      function  Val (Key : in String) return Integer;
      function  Val (Key : in String; Default : in Integer) return Integer;
      function  Max_Key return String; -- Key with highest value
      procedure Destroy;
   private
      Values     : aliased Counter_Map.Map;
   end Safe_Object;

   type Object is new Finalization.Limited_Controlled with record
      Safe : Safe_Object (Object'Access);
   end record;

   procedure Finalize (This : in out Object);

end Agpl.Counter.Multi;