agpl_1.0.0_b5da3320/src/agpl-reflection.adb

  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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Unchecked_Deallocation;
with Agpl.Strings; use Agpl.Strings;

package body Agpl.Reflection is

   type Datum_Access is access all Datum'Class;

   package Value_Maps is new
     Ada.Containers.Indefinite_Ordered_Maps (String, Datum_Access);

   protected Values is
      procedure Add (Name : String; D : in out Datum'Class);
      procedure Set (Name, Value : String);
      function  Get (Name        : String) return String;
      function Get_All return Value_Maps.Map;
   private
      Map : Value_Maps.Map;
   end Values;

   ------------
   -- Values --
   ------------

   protected body Values is

   ---------
   -- Add --
   ---------

      procedure Add (Name : String; D : in out Datum'Class) is
      begin
         Map.Include (To_Lower (Name), D'Unchecked_Access);
      end Add;

      ---------
      -- Set --
      ---------

      procedure Set (Name, Value : String) is
      begin
         Map.Element (To_Lower (Name)).all.Set (Value);
      end Set;

      ---------
      -- Get --
      ---------

      function  Get (Name : String) return String is
      begin
         return Map.Element (To_Lower (Name)).Get;
      end Get;

      -------------
      -- Get_All --
      -------------

      function Get_All return Value_Maps.Map is
      begin
         return Map;
      end Get_All;

   end Values;

   ----------
   -- Base --
   ----------

   package body Base is

      ---------
      -- Set --
      ---------

      procedure Set (D : in out Object; Val : String) is
      begin
         D.Val.Set (Value (Val));
      end Set;

      ---------
      -- Set --
      ---------

      procedure Set (D : in out Object; Val : Basetype) is
      begin
         D.Val.Set (Val);
      end Set;

      ---------
      -- Get --
      ---------

      function Get (D : Object) return String is
      begin
         return Image (D.Val.Get);
      end Get;

      -----------
      -- Value --
      -----------

      function Value (Name : String;
                      B    : Basetype) return Object is
      begin
         return O : Object (new String'(Name)) do
            O.Val.Set (B);
         end return;
      end Value;

      -----------
      -- Value --
      -----------

      function Value (D : Object) return Basetype is
      begin
         return D.Val.Get;
      end Value;

      ----------------
      -- Initialize --
      ----------------

      procedure Initialize (D : in out Object) is
      begin
         Register (D.Name.all, D);
      end Initialize;

      --------------
      -- Finalize --
      --------------

      procedure Finalize (D : in out Object) is
         procedure Free is new Ada.Unchecked_Deallocation (String,
                                                           Name_Access);
         X : Name_Access := D.Name;
      begin
         Free (X);
      end Finalize;

   end Base;

   --------------
   -- Register --
   --------------

   procedure Register (Name : String; D : in out Datum'Class) is
   begin
      Values.Add (Name, D);
   end Register;

   ---------
   -- Set --
   ---------

   procedure Set (Name, Value : String) is
   begin
      Values.Set (Name, Value);
   end Set;

   ---------
   -- Get --
   ---------

   function Get (Name : String) return String is
   begin
      return Values.Get (Name);
   end Get;

   -------------
   -- Get_All --
   -------------

   function  Get_All return Containers.String_String_Maps.Map is
      Result : Containers.String_String_Maps.Map;
      use Value_Maps;
      procedure Add (I : Cursor) is
      begin
         Result.Insert (Key (I), Element (I).Get);
      end Add;
   begin
      Values.Get_All.Iterate (Add'Access);
      return Result;
   end Get_All;

end Agpl.Reflection;