1. with Ada.Streams;                       use Ada.Streams; 
  2. with Objects;                           use Objects; 
  3.  
  4. private with Ada.Strings.Unbounded; 
  5.  
  6. package Values is 
  7.  
  8.     -- A Value represents a generic value of type boolean, integer or string. 
  9.     -- The value can be converted to other types in certain cases. Values can 
  10.     -- also be copied and written to a stream. 
  11.     type Value is abstract new Object with private; 
  12.     type A_Value is access all Value'Class; 
  13.  
  14.     -- Creates a new boolean Value. 
  15.     function Create_Value( val : Boolean ) return A_Value; 
  16.     pragma Postcondition( Create_Value'Result /= null ); 
  17.  
  18.     -- Creates a new integer Value. 
  19.     function Create_Value( val : Integer ) return A_Value; 
  20.     pragma Postcondition( Create_Value'Result /= null ); 
  21.  
  22.     -- Creates a new string Value. 
  23.     function Create_Value( val : String ) return A_Value; 
  24.     pragma Postcondition( Create_Value'Result /= null ); 
  25.  
  26.     -- Returns the Value as a boolean. Raises INVALID_CONVERSION if the value 
  27.     -- can't be converted to a boolean. 
  28.     function As_Boolean( this : access Value ) return Boolean; 
  29.  
  30.     -- Returns the Value as a boolean. Raises INVALID_CONVERSION if the value 
  31.     -- can't be converted to an integer. 
  32.     function As_Integer( this : access Value ) return Integer; 
  33.  
  34.     -- Returns the Value as a boolean. Raises INVALID_CONVERSION if the value 
  35.     -- can't be converted to a string. 
  36.     function As_String( this : access Value ) return String; 
  37.  
  38.     function Object_Input( stream : access Root_Stream_Type'Class ) return Value is abstract; 
  39.  
  40.     -- Returns a copy of 'src'. 
  41.     function Copy( src : A_Value ) return A_Value; 
  42.     pragma Postcondition( Copy'Result /= src or else src = null ); 
  43.  
  44.     -- Deletes the Value. 
  45.     procedure Delete( this : in out A_Value ); 
  46.     pragma Postcondition( this = null ); 
  47.  
  48.     ---------------------------------------------------------------------------- 
  49.  
  50.     VALUE_NOT_FOUND, 
  51.     INVALID_CONVERSION : exception; 
  52.  
  53. private 
  54.  
  55.     use Ada.Strings.Unbounded; 
  56.  
  57.     type Value is abstract new Object with null record; 
  58.  
  59.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out Value ); 
  60.     for Value'Read use Object_Read; 
  61.  
  62.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : Value ); 
  63.     for Value'Write use Object_Write; 
  64.  
  65.     function A_Value_Input( stream : access Root_Stream_Type'Class ) return A_Value; 
  66.     for A_Value'Input use A_Value_Input; 
  67.  
  68.     procedure A_Value_Output( stream : access Root_Stream_Type'Class; obj : A_Value ); 
  69.     for A_Value'Output use A_Value_Output; 
  70.  
  71.     procedure A_Value_Read( stream : access Root_Stream_Type'Class; obj : out A_Value ); 
  72.     for A_Value'Read use A_Value_Read; 
  73.  
  74.     procedure A_Value_Write( stream : access Root_Stream_Type'Class; obj : A_Value ); 
  75.     for A_Value'Write use A_Value_Write; 
  76.  
  77.     ---------------------------------------------------------------------------- 
  78.  
  79.     type Val_Bool is new Value with 
  80.         record 
  81.             bool : Boolean; 
  82.         end record; 
  83.  
  84.     -- Returns the value as a boolean. 
  85.     function As_Boolean( this : access Val_Bool ) return Boolean; 
  86.  
  87.     -- Returns 1 or 0 if the value is True or False, respectively. 
  88.     function As_Integer( this : access Val_Bool ) return Integer; 
  89.  
  90.     -- Returns "True" or "False". 
  91.     function As_String( this : access Val_Bool ) return String; 
  92.  
  93.     function Object_Input( stream : access Root_Stream_Type'Class ) return Val_Bool; 
  94.     for Val_Bool'Input use Object_Input; 
  95.  
  96.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out Val_Bool ); 
  97.     for Val_Bool'Read use Object_Read; 
  98.  
  99.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : Val_Bool ); 
  100.     for Val_Bool'Write use Object_Write; 
  101.  
  102.     -- Returns "True" or "False". 
  103.     function To_String( this : access Val_Bool ) return String; 
  104.  
  105.     ---------------------------------------------------------------------------- 
  106.  
  107.     type Val_Int is new Value with 
  108.         record 
  109.             int : Integer; 
  110.         end record; 
  111.  
  112.     -- Returns True if the value is non-zero. 
  113.     function As_Boolean( this : access Val_Int ) return Boolean; 
  114.  
  115.     -- Returns the value as an integer. 
  116.     function As_Integer( this : access Val_Int ) return Integer; 
  117.  
  118.     -- Returns a string representation of the integer. 
  119.     function As_String( this : access Val_Int ) return String; 
  120.  
  121.     function Object_Input( stream : access Root_Stream_Type'Class ) return Val_Int; 
  122.     for Val_Int'Input use Object_Input; 
  123.  
  124.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out Val_Int ); 
  125.     for Val_Int'Read use Object_Read; 
  126.  
  127.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : Val_Int ); 
  128.     for Val_Int'Write use Object_Write; 
  129.  
  130.     -- Returns a string representation of the integer. 
  131.     function To_String( this : access Val_Int ) return String; 
  132.  
  133.     ---------------------------------------------------------------------------- 
  134.  
  135.     type Val_String is new Value with 
  136.         record 
  137.             str : Unbounded_String; 
  138.         end record; 
  139.  
  140.     procedure Adjust( this : access Val_String ); 
  141.  
  142.     -- Returns the string value. 
  143.     function As_String( this : access Val_String ) return String; 
  144.  
  145.     function Object_Input( stream : access Root_Stream_Type'Class ) return Val_String; 
  146.     for Val_String'Input use Object_Input; 
  147.  
  148.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out Val_String ); 
  149.     for Val_String'Read use Object_Read; 
  150.  
  151.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : Val_String ); 
  152.     for Val_String'Write use Object_Write; 
  153.  
  154.     -- Returns the string value surrounded by double quotes. 
  155.     function To_String( this : access Val_String ) return String; 
  156.  
  157. end Values;