gnoga_2.1.2_5f127c56/deps/zanyblue/src/parameters/zanyblue-parameters-scopes.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
 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
--  -*- coding: utf-8 -*-
--
--  ZanyBlue, an Ada library and framework for finite element analysis.
--
--  Copyright (c) 2012, 2016, Michael Rohan <mrohan@zanyblue.com>
--  All rights reserved.
--
--  Redistribution and use in source and binary forms, with or without
--  modification, are permitted provided that the following conditions
--  are met:
--
--    * Redistributions of source code must retain the above copyright
--      notice, this list of conditions and the following disclaimer.
--
--    * Redistributions in binary form must reproduce the above copyright
--      notice, this list of conditions and the following disclaimer in the
--      documentation and/or other materials provided with the distribution.
--
--    * Neither the name of ZanyBlue nor the names of its contributors may
--      be used to endorse or promote products derived from this software
--      without specific prior written permission.
--
--  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
--  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
--  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
--  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
--  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
--  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
--  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
--  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
--  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
--  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--

--
--  The ZanyBlue.Parameters.Scopes implements a stack based parameter sets
--  with scoping, i.e., local parameters to a scope.  Changes made to
--  parameters with a scope do not impact parameter values in enclosing
--  scopes.  Scopes are started with New_Scope and ended with End_Scope.
--  Access to parameter values will traverse the set of defined scopes
--  until a value is found.  E.g.,
--
--    New_Scope
--       Set (A, "B")
--       Get (A)          => Return "B"
--       New_Scope
--          Get (A)       => Return "B"
--          Set (A, "C")
--          Get (A)       => Return "C"
--       End_Scope
--       Get (A)          => Return "B"
--    End_Scope
--
--  For list types, the functionality uses scope accumulation, i.e.,
--  the value return for a list parameter is a concatenation of values
--  for all scopes, e.g.,
--
--    New_Scope
--       Set (A, [1, 2])
--       Get (A)          => Return [1, 2]
--       New_Scope
--          Get (A)       => Return [1, 2]
--          Set (A, [3])
--          Get (A)       => Return [3, 1, 2]
--       End_Scope
--       Get (A)          => Return [1, 2]
--    End_Scope
--
--  In a list context, values that are not lists are convert to singleton
--  lists, e.g.
--
--    New_Scope
--       Set (A, [1, 2])
--       Get (A)          => Return [1, 2]
--       New_Scope
--          Get (A)       => Return [1, 2]
--          Set (A, 3)
--          Get (A)       => Return [3, 1, 2]   (List context, Get_List)
--          Get (A)       => 3                  (Integer context, Get_Integer)
--       End_Scope
--       Get (A)          => Return [1, 2]
--    End_Scope
--
--  This accumulation can be disabled by passing False for the Deep argument
--  of Get_List.
--
--  As a side effect, getting a parameter in a list context always returns
--  a value.  If the parameter is not defined in any scope, the empty list is
--  returned.
--
--  The Increment is a special update operation and will, if invoked with
--  the Deep option, increment the parameter value in all scopes, e.g.,
--
--    New_Scope
--       Set (N, 1)
--       Get (A)          => Return 1
--       New_Scope
--          Set (N, 1)
--          Get (N)       => Return 1
--          Incr (N)      => 2
--       End_Scope
--       Get (N)          => 2          (N of parent scope also incremented)
--    End_Scope
--

with Ada.Calendar;
with UXStrings.Text_IO;
with ZanyBlue.Parameters.Sets;
with ZanyBlue.Parameters.Values;

private with Ada.Containers.Indefinite_Vectors;

package ZanyBlue.Parameters.Scopes is

   use Ada.Calendar;
   use ZanyBlue.Parameters.Values;
   use ZanyBlue.Parameters.Sets;

   type Parameter_Stack_Type is tagged private;
   --  The type used to represent scoped parameter values.

   procedure New_Scope (Param_Stack : in out Parameter_Stack_Type);
   --  Create a new scope.  The scope is given the name "PARAMS-NNN" where
   --  NNN is the level of the scope in the stack of scopes.  The name is
   --  normally only seen if the stack is written to an XML file.
   --
   --  The New_Scope function defines a single parameter in the new scope
   --  named "_level" with an integer value of the level with in the stack.
   --

   procedure End_Scope (Param_Stack : in out Parameter_Stack_Type);
   --  Return to the enclosing scope.  Changes to local parameters are lost
   --  and the previous values are made available.

   function Is_Defined
     (Param_Stack : Parameter_Stack_Type;
      Name        : String;
      Any_Scope   : Boolean := True)
      return Boolean;
   --  Is a parameter defined, either for the current scope or in one of the
   --  enclosing scopes.  If Any_Scope is False, only the current scope
   --  is checked.

   function Get
     (Param_Stack : Parameter_Stack_Type;
      Name        : String)
      return Value_Type'Class;
   --  General parameter query routine.  This can be used to handle user
   --  defined parameter types.

   function Get_Boolean
     (Param_Stack : Parameter_Stack_Type;
      Name        : String)
      return Boolean;
   --  Get the value of a Boolean parameter.  Not_A_Boolean_Error is raised
   --  if the value is not a Boolean.

   function Get_Float
     (Param_Stack : Parameter_Stack_Type;
      Name        : String)
      return Float;
   --  Get the value of a Float parameter.  Not_A_Real_Error is raised
   --  if the value is not a Float.

   function Get_Integer
     (Param_Stack : Parameter_Stack_Type;
      Name        : String)
      return Integer;
   --  Get the value of an Integer parameter.  Not_An_Integer_Error is raised
   --  if the value is not a Integer.

   function Get_List
     (Param_Stack : Parameter_Stack_Type;
      Name        : String;
      Deep        : Boolean)
      return List_Type;
   --  Get the value of a List parameter.  If Deep is False, only the current
   --  scope is queried.  If Deep is True, the all scopes are queried and the
   --  the result is an accumulation of all values found.

   function Get_Time
     (Param_Stack : Parameter_Stack_Type;
      Name        : String)
      return Time;
   --  Get the value of a Time parameter.  Not_A_Time_Error is raised
   --  if the value is not a Time.

   function Get_String
     (Param_Stack : Parameter_Stack_Type;
      Name        : String)
      return String;
   --  Get the value of a String parameter.  All types support conversion to
   --  string so a value is always returned unless the parameter is not
   --  defined (Not_Defined_Error is raised in this case).

   procedure Increment
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      By_Amount   :        Integer := 1;
      Deep        :        Boolean := True);
   --  Increment an Integer parameter.  If Deep is True, then all instances
   --  of the parameter are increment, i.e., all scopes.

   procedure Append
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      Value       :        String);
   --  Append a value to the list parameter.  If the current parameter is not
   --  a list value, it converted (Get_String of the value) to a list prior to
   --  appending.

   procedure Prepend
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      Value       :        String);
   --  Prepend a value to the list parameter.  If the current parameter is not
   --  a list value, it converted (Get_String of the value) to a list prior to
   --  prepending.

   procedure Set
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      Value       :        Value_Type'Class);
   --  General parameter set routine.  This can be used to handle user
   --  defined parameter types.

   procedure Set_Boolean
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      Value       :        Boolean);
   --  Set a parameter to a Boolean value.

   procedure Set_Integer
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      Value       :        Integer);
   --  Set a parameter to an Integer value.

   procedure Set_Float
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      Value       :        Float);
   --  Set a parameter to a Float value.

   procedure Set_String
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      Value       :        String);
   --  Set a parameter to a String value.

   procedure Set_Time
     (Param_Stack : in out Parameter_Stack_Type;
      Name        :        String;
      Value       :        Time);
   --  Set a parameter to a Time value.

   procedure Dump
     (Param_Stack : Parameter_Stack_Type;
      Destination : UXStrings.Text_IO.File_Type;
      All_Scopes  : Boolean);
   --  Dump the set of parameters defined in a parameter set to a file.  This
   --  is a debugging routine: no XML quoting is currently performed on the
   --  parameter values so the general XML might not be valid XML!

--     procedure Dump
--       (Param_Stack : Parameter_Stack_Type;
--        Destination : Ada.Wide_Text_IO.File_Type;
--        All_Scopes  : Boolean);
   --  Dump the set of parameters defined in a parameter set to a file.  This
   --  is a debugging routine: no XML quoting is currently performed on the
   --  parameter values so the general XML might not be valid XML!

private

   package Parameter_Set_Vectors is new Ada.Containers.Indefinite_Vectors
     (Index_Type => Positive, Element_Type => Parameter_Set_Type);

   type Parameter_Stack_Type is tagged record
      Values : Parameter_Set_Vectors.Vector;
   end record;

end ZanyBlue.Parameters.Scopes;