awa_2.4.0_59135a52/ada-ado/src/ado-objects-cache.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
-----------------------------------------------------------------------
--  objects.cache -- Object cache
--  Copyright (C) 2009, 2010, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------

with Ada.Finalization;

with Ada.Containers.Indefinite_Hashed_Sets;

--  The <b>ADO.Objects.Cache</b> holds a cache of object records.
--  The cache maintains a set of objects that were loaded for a given session.
--
--  The cache is not thread-safe.  It is not intended to be shared directly.
package ADO.Objects.Cache is

   type Object_Cache is limited private;

   --  Insert the object in the cache.
   --  The reference counter associated with the object is incremented.
   procedure Insert (Cache  : in out Object_Cache;
                     Object : in Object_Record_Access);

   --  Insert the object in the cache
   --  The reference counter associated with the object is incremented.
   procedure Insert (Cache  : in out Object_Cache;
                     Object : in Object_Ref'Class);

   --  Check if the object is contained in the cache
   function Contains (Cache : in Object_Cache;
                      Key   : in Object_Key) return Boolean;

   function Find (Cache : in Object_Cache;
                  Key   : in Object_Key) return Object_Record_Access;

   procedure Find (Cache  : in Object_Cache;
                   Object : in out Object_Ref'Class;
                   Key    : in Object_Key);

   --  Remove the object from the cache.  The reference counter associated
   --  with the object is decremented.
   --  Do nothing if the object is not in the cache.
   procedure Remove (Cache  : in out Object_Cache;
                     Object : in Object_Record_Access);

   --  Remove the object from the cache.
   --  Do nothing if the object is not in the cache.
   procedure Remove (Cache  : in out Object_Cache;
                     Object : in Object_Ref);

   --  Remove the object from the cache.
   --  Do nothing if the object is not in the cache.
   procedure Remove (Cache  : in out Object_Cache;
                     Object : in Object_Key);

   --  Remove all object in the cache.
   procedure Remove_All (Cache : in out Object_Cache);

private

   use Ada.Containers;

   --  The cache is a <b>Hashed_Set</b> in which the object record pointers
   --  are stored.  The hash and comparison methods are based on the object key
   --  and object type.
   function Hash (Item : Object_Record_Access) return Hash_Type;

   --  Compare whether the two objects pointed to by Left and Right have the same
   --  object key.
   function Equivalent_Elements (Left, Right : Object_Record_Access)
                               return Boolean;

   package Object_Set is new Indefinite_Hashed_Sets
     (Element_Type        => Object_Record_Access,
      Hash                => Hash,
      Equivalent_Elements => Equivalent_Elements);

   type Object_Cache is new Ada.Finalization.Limited_Controlled with record
      Objects : Object_Set.Set;
   end record;

   --  Finalize the object cache by removing all entries
   overriding
   procedure Finalize (Cache : in out Object_Cache);

end ADO.Objects.Cache;