18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

System programm<strong>in</strong>g 307<br />

21.1.1 Putt<strong>in</strong>g it all together<br />

To pr<strong>in</strong>t the <strong>in</strong>ternational telephone code for France the follow<strong>in</strong>g code can be used:<br />

with System, System.Storage_Elements;<br />

use System, System.Storage_Elements;<br />

procedure Ma<strong>in</strong> is<br />

type Country is (USA, France, UK, Australia);<br />

for Colour'Size use Integer'Size;<br />

for Country use (USA=>1, France=>33, UK=>44, Australia=>61);<br />

function Idc is new Unchecked_Conversion( Country, Integer );<br />

beg<strong>in</strong><br />

Put( "International diall<strong>in</strong>g code for France is ");<br />

Put( Idc(France) );<br />

New_L<strong>in</strong>e;<br />

end Ma<strong>in</strong>;<br />

which when run, would produce the follow<strong>in</strong>g results:<br />

International diall<strong>in</strong>g code for France is 33<br />

It would also be convenient to also <strong>in</strong>clude Canada <strong>in</strong> the Country enumeration for telephone codes.<br />

However, as Canada has the same country code as the USA, this cannot be done directly. The reason for this is<br />

that two enumerations may not have the same physical representation. The way round this is to def<strong>in</strong>e a renam<strong>in</strong>g<br />

for Canada as follows:<br />

function Canada return Country renames USA;<br />

which def<strong>in</strong>es Canada as a function that returns the enumeration USA as its result.<br />

21.2 B<strong>in</strong>d<strong>in</strong>g an object to a specific address<br />

In some limited situations it is necessary to read or write from absolute locations <strong>in</strong> memory. In the historic<br />

operat<strong>in</strong>g system MS DOS the time of day is stored <strong>in</strong> locations (<strong>in</strong> hexadecimal) 46E and 46C. The exact<br />

specification of what is stored is as follows:<br />

Location (hexadecimal)<br />

046E - 046F<br />

046C - 046D<br />

Contents<br />

The time of day <strong>in</strong> hours.<br />

The ticks past the current hour.<br />

Each tick is 5/91 seconds.<br />

An object may be bound to an absolute location with the for use clause. For example, to b<strong>in</strong>d the <strong>in</strong>teger<br />

variable Time_High to the absolute location 16#46E# the follow<strong>in</strong>g declaration can be used:<br />

Time_High_Address : constant Address := To_Address( 16#046C# );<br />

type Time is range 0 .. 65365; --Unsigned<br />

for Time'Size use 16;<br />

-- <strong>in</strong> 2 bytes<br />

Time_High: Time;<br />

for Time_High'Address use Time_Low_Address;<br />

Note:<br />

Time is a type describ<strong>in</strong>g a 16 bit unsigned <strong>in</strong>teger.<br />

The address 16#046E# must be of type Address that is def<strong>in</strong>ed <strong>in</strong> the package System. The child<br />

package System.Storage_elements conta<strong>in</strong>s the function to_address which converts an<br />

<strong>in</strong>teger <strong>in</strong>to an address.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!