ada_util_0d266031/src/sys/serialize/util-serialize-contexts.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
-----------------------------------------------------------------------
--  util-serialize-contexts -- Contexts for serialization framework
--  Copyright (C) 2010, 2011 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--  SPDX-License-Identifier: Apache-2.0
-----------------------------------------------------------------------
with Ada.Containers;
with Ada.Containers.Hashed_Maps;
with Ada.Finalization;
package Util.Serialize.Contexts is

   No_Data : exception;

   --  ------------------------------
   --  Context data key
   --  ------------------------------
   type Data_Key is private;

   --  Allocate a unique data key for a mapper.
   procedure Allocate (Key : out Data_Key);

   --  ------------------------------
   --  Data context for the mapper
   --  ------------------------------
   type Data is tagged limited private;
   type Data_Access is access all Data'Class;

   --  Finalize the data object when it is removed from the reader context.
   procedure Finalize (Object : in out Data) is null;

   --  ------------------------------
   --  Reader context
   --  ------------------------------
   type Context is new Ada.Finalization.Limited_Controlled with private;

   --  Get the data object associated with the given key.
   --  Raises No_Data exception if there is no data object.
   function Get_Data (Ctx : in Context;
                      Key : in Data_Key) return Data_Access;

   --  Set the data object associated with the given key.
   --  Free the previous data object if there was one.
   --  The data object will be freed if the context is destroyed.
   procedure Set_Data (Ctx     : in out Context;
                       Key     : in Data_Key;
                       Content : in Data_Access);

private

   type Data_Key is new Integer;

   function Hash (Key : in Data_Key) return Ada.Containers.Hash_Type;

   type Data is tagged limited null record;

   package Data_Map is new Ada.Containers.Hashed_Maps
     (Key_Type        => Data_Key,
      Element_Type    => Data_Access,
      Hash            => Hash,
      Equivalent_Keys => "=");

   type Context is new Ada.Finalization.Limited_Controlled with record
      Data : Data_Map.Map;
   end record;

   --  Free the context data.
   overriding
   procedure Finalize (Ctx : in out Context);

end Util.Serialize.Contexts;