1. with Ada.Streams;                       use Ada.Streams; 
  2. with Objects;                           use Objects; 
  3. with Values;                            use Values; 
  4.  
  5. private with Ada.Containers.Indefinite_Ordered_Maps; 
  6. private with Ada.Strings.Less_Case_Insensitive; 
  7.  
  8. package Associations is 
  9.  
  10.     -- An Association is an ordered map of strings to Value objects. Names are 
  11.     -- case insensitive. Associations do not subclass the Value class to avoid 
  12.     -- the possibility of nesting. 
  13.     type Association is new Object with private; 
  14.     type A_Association is access all Association'Class; 
  15.  
  16.     -- Creates a new empty association. 
  17.     function Create_Association return A_Association; 
  18.     pragma Postcondition( Create_Association'Result /= null ); 
  19.  
  20.     -- Returns the named value in the association as a boolean. An exception is 
  21.     -- raised if the value is not found or if it can't be converted to a 
  22.     -- boolean. 
  23.     function As_Boolean( this : access Association; name : String ) return Boolean; 
  24.     pragma Precondition( name'Length > 0 ); 
  25.  
  26.     -- Returns the named value in the association as an integer. An exception is 
  27.     -- raised if the value is not found or if it can't be converted to an 
  28.     -- integer. 
  29.     function As_Integer( this : access Association; name : String ) return Integer; 
  30.     pragma Precondition( name'Length > 0 ); 
  31.  
  32.     -- Returns the named value in the association as a string. An exception is 
  33.     -- raised if the value is not found or if it can't be converted to a string. 
  34.     function As_String( this : access Association; name : String ) return String; 
  35.     pragma Precondition( name'Length > 0 ); 
  36.  
  37.     -- Iterate through the name/value pairs in the association. The iteration 
  38.     -- will be in alphabetical order of names. 
  39.     procedure Iterate( this    : access Association; 
  40.                        examine : not null access procedure( name : String; value : A_Value ) ); 
  41.  
  42.     -- Returns the number of name/value pairs in the assocation. 
  43.     function Length( this : access Association ) return Natural; 
  44.  
  45.     -- Reads an Association from a stream. This should not be called directly. 
  46.     function Object_Input( stream : access Root_Stream_Type'Class ) return Association; 
  47.  
  48.     -- Removes the named value from the association. 
  49.     procedure Remove( this : access Association; name : String ); 
  50.     pragma Precondition( name'Length > 0 ); 
  51.  
  52.     -- Sets a value in the association by name. 
  53.     procedure Set_Value( this : access Association; name : String; val : Boolean ); 
  54.     pragma Precondition( name'Length > 0 ); 
  55.  
  56.     -- Sets a value in the association by name. 
  57.     procedure Set_Value( this : access Association; name : String; val : Integer ); 
  58.     pragma Precondition( name'Length > 0 ); 
  59.  
  60.     -- Sets a value in the association by name. 
  61.     procedure Set_Value( this : access Association; name : String; val : String ); 
  62.     pragma Precondition( name'Length > 0 ); 
  63.  
  64.     -- Sets a value in the association by name, consuming 'val'. 
  65.     procedure Set_Value( this : access Association; name : String; val : in out A_Value ); 
  66.     pragma Precondition( name'Length > 0 ); 
  67.     pragma Precondition( val /= null ); 
  68.     pragma Postcondition( val = null ); 
  69.  
  70.     -- Returns a deep copy of the assocation. 
  71.     function Copy( src : A_Association ) return A_Association; 
  72.     pragma Postcondition( Copy'Result /= src or else src = null ); 
  73.  
  74.     -- Deletes the association. 
  75.     procedure Delete( this : in out A_Association ); 
  76.     pragma Postcondition( this = null ); 
  77.  
  78. private 
  79.  
  80.     package Value_Maps is new 
  81.         Ada.Containers.Indefinite_Ordered_Maps( String, A_Value, 
  82.                                                 Ada.Strings.Less_Case_Insensitive, 
  83.                                                 "=" ); 
  84.     use Value_Maps; 
  85.  
  86.     type Association is new Object with 
  87.         record 
  88.             pairs : Value_Maps.Map; 
  89.         end record; 
  90.  
  91.     procedure Adjust( this : access Association ); 
  92.  
  93.     procedure Delete( this : in out Association ); 
  94.  
  95.     for Association'Input use Object_Input; 
  96.  
  97.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out Association ); 
  98.     for Association'Read use Object_Read; 
  99.  
  100.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : Association ); 
  101.     for Association'Write use Object_Write; 
  102.  
  103.     function A_Association_Input( stream : access Root_Stream_Type'Class ) return A_Association; 
  104.     for A_Association'Input use A_Association_Input; 
  105.  
  106.     procedure A_Association_Output( stream : access Root_Stream_Type'Class; obj : A_Association ); 
  107.     for A_Association'Output use A_Association_Output; 
  108.  
  109.     procedure A_Association_Read( stream : access Root_Stream_Type'Class; obj : out A_Association ); 
  110.     for A_Association'Read use A_Association_Read; 
  111.  
  112.     procedure A_Association_Write( stream : access Root_Stream_Type'Class; obj : A_Association ); 
  113.     for A_Association'Write use A_Association_Write; 
  114.  
  115. end Associations;