--  Enumeration representation clauses (D196): values are positions, and memory (a field of
--  a laid-out record), 'Enum_Rep, 'Enum_Val and Unchecked_Conversion use the codes; and
--  Unchecked_Conversion between records, arrays and scalars, component by component.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Conversion;
with Interfaces; use Interfaces;
procedure Main is
   type Speed is (Off, Slow, Fast);
   for Speed use (Off => 0, Slow => 5, Fast => 200);
   type Control is record
      On : Boolean;
      S  : Speed;
   end record with Size => 16;
   for Control use record
      On at 0 range 0 .. 0;
      S  at 1 range 0 .. 7;
   end record;
   type Bytes is array (1 .. 4) of Unsigned_8;
   type Pair is record
      Lo, Hi : Unsigned_16;
   end record;
   function To_Byte is new Ada.Unchecked_Conversion (Speed, Unsigned_8);
   function To_Speed is new Ada.Unchecked_Conversion (Unsigned_8, Speed);
   function To_Word is new Ada.Unchecked_Conversion (Control, Unsigned_16);
   function To_Bytes is new Ada.Unchecked_Conversion (Pair, Bytes);
   function To_Pair is new Ada.Unchecked_Conversion (Bytes, Pair);
   function To_U32 is new Ada.Unchecked_Conversion (Bytes, Unsigned_32);
   S : constant Speed := Fast;
   C : constant Control := (On => True, S => Slow);
   B : constant Bytes := To_Bytes ((Lo => 16#0201#, Hi => 16#0403#));
   Arr : constant array (1 .. 3) of Speed := (Off, Fast, Slow);
begin
   Put_Line (Unsigned_8'Image (To_Byte (S)) & " " & Speed'Image (To_Speed (5)) & Integer'Image (Speed'Enum_Rep (Fast)) & " " & Speed'Image (Speed'Enum_Val (200)) & Integer'Image (S'Enum_Rep));
   Put_Line (Unsigned_16'Image (To_Word (C)) & " " & Speed'Image (C.S) & Boolean'Image (S > C.S) & " " & Speed'Image (Arr (2)) & " " & Speed'Image (Speed'Succ (C.S)));
   Put_Line (Unsigned_8'Image (B (1)) & Unsigned_8'Image (B (4)) & Unsigned_16'Image (To_Pair (B).Hi) & Unsigned_32'Image (To_U32 (B)));
end Main;
