comp.lang.ada
 help / color / mirror / Atom feed
* Rep specs and conversion among derived types
@ 1993-04-11  4:12 Michael Feldman
  0 siblings, 0 replies; only message in thread
From: Michael Feldman @ 1993-04-11  4:12 UTC (permalink / raw)


Here's one to try on your favorite compiler, if you are interested in
storage reps. I discovered a section in the Ada Rationale a couple of years
ago, quite by accident, that pointed me in the direction of this program.
It never occurred to me to try an explicit conversion between related
types with different reps, but I relaized in the end that this was
indeed quite intentional.

Caution: this is a short program with lots of tricks. Try this at home, but
if you try it on a project, please encapsulate the hell out of it.

Pat yourself on the back if you understand this code. I've left it
uncommented to give you a nice challenge.

Good luck. Post strange results, please.

Mike Feldman
(see code following sig)
------------------------------------------------------------------------
Michael B. Feldman
co-chair, SIGAda Education Committee

Professor, Dept. of Electrical Engineering and Computer Science
School of Engineering and Applied Science
The George Washington University
Washington, DC 20052 USA
(202) 994-5253 (voice)
(202) 994-5296 (fax)
mfeldman@seas.gwu.edu (Internet)

"The most important thing is to be sincere, 
and once you've learned how to fake that, you've got it made." 
-- old show-business adage
------------------------------------------------------------------------
with UNCHECKED_CONVERSION;                                                     
 
with TEXT_IO;
procedure REPDEMO is                                                           
 

  type INT_32 is range -2**31..2**31-1;
  package MY_INT_IO is new TEXT_IO.INTEGER_IO(INT_32);

  type DIRECTION is (UP, DOWN, LEFT, RIGHT);                                   
 
  type D_ARRAY is ARRAY(1..4) of DIRECTION;                                    
 
  type PD_ARRAY is new D_ARRAY; pragma PACK(PD_ARRAY);                         
 
                                                                               
 
  function D_TO_INT is NEW UNCHECKED_CONVERSION                                
 
    (SOURCE => D_ARRAY,  TARGET => INT_32);
  function PD_TO_INT is NEW UNCHECKED_CONVERSION                               
 
    (SOURCE => PD_ARRAY, TARGET => INT_32);
                                                                               
 
  type NEWDIRECTION is new DIRECTION;                                          
 
  for NEWDIRECTION use (1,2,4,8);                                              
 
  type N_ARRAY is ARRAY(1..4) of NEWDIRECTION;                                 
 
  type PN_ARRAY is new N_ARRAY; pragma PACK(PN_ARRAY);                         
 
                                                                               
 
  function N_TO_INT is new UNCHECKED_CONVERSION                                
 
    (SOURCE => N_ARRAY,  TARGET => INT_32);
  function PN_TO_INT is new UNCHECKED_CONVERSION                               
 
    (SOURCE => PN_ARRAY, TARGET => INT_32);
                                                                               
 
  DIR: DIRECTION := UP;                                                        
 
  NEWDIR: DIRECTION := UP;                                                     
 
                                                                               
 
  D: D_ARRAY := (UP, DOWN, LEFT, RIGHT);                                       
 
  N: N_ARRAY;                                                                  
 
  PD: PD_ARRAY;                                                                
 
  PN: PN_ARRAY;                                                                
 
                                                                               
 
begin                                                                          
 
                                                                               
 
  N := (NEWDIRECTION(D(1)), NEWDIRECTION(D(2)),                                
 
        NEWDIRECTION(D(3)), NEWDIRECTION(D(4)));                               
 
  PD := PD_ARRAY(D);                                                           
 
  PN := PN_ARRAY(N);                                                           
 
                                                                               
 
  MY_INT_IO.PUT(D'SIZE); TEXT_IO.NEW_LINE;                                     
 
  MY_INT_IO.PUT(PD'SIZE); TEXT_IO.NEW_LINE;                                    
 
  MY_INT_IO.PUT(N'SIZE); TEXT_IO.NEW_LINE;                                     
 
  MY_INT_IO.PUT(PN'SIZE); TEXT_IO.NEW_LINE;                                    
 
  MY_INT_IO.PUT(DIR'SIZE); TEXT_IO.NEW_LINE;                                   
 
  MY_INT_IO.PUT(NEWDIR'SIZE); TEXT_IO.NEW_LINE;                                
 
                                                                               
 
  MY_INT_IO.PUT(ITEM => D_TO_INT(D),BASE=>16);                                 
 
  TEXT_IO.NEW_LINE;                                                            
 
  MY_INT_IO.PUT(ITEM => PD_TO_INT(PD),BASE=>16);                               
 
  TEXT_IO.NEW_LINE;                                                            
 
  MY_INT_IO.PUT(ITEM => N_TO_INT(N),BASE=>16);                                 
 
  TEXT_IO.NEW_LINE;                                                            
 
  MY_INT_IO.PUT(ITEM => PN_TO_INT(PN),BASE=>16);                               
 
  TEXT_IO.NEW_LINE;                                                            
 
                                                                               
 
end REPDEMO;                                                                   
 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1993-04-11  4:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-04-11  4:12 Rep specs and conversion among derived types Michael Feldman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox