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.

Tasks 297<br />

The implementation of the above program is split <strong>in</strong>to two procedures. The procedure Do_Copy does the<br />

actual work of copy<strong>in</strong>g between the two files.<br />

with <strong>Ada</strong>.Text_Io, Pack_Threads, Pack_Types;<br />

use <strong>Ada</strong>.Text_Io, Pack_Threads, Pack_Types;<br />

procedure Do_Copy(From:<strong>in</strong> Str<strong>in</strong>g; To:<strong>in</strong> Str<strong>in</strong>g) is<br />

type State is ( Open_File, Create_File );<br />

Fd_In : P_File_Type := new <strong>Ada</strong>.Text_Io.File_Type;<br />

Fd_Out : P_File_Type := new <strong>Ada</strong>.Text_Io.File_Type;<br />

Mode : State := Open_File;<br />

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

Open( File=>Fd_In.all, Mode=>In_File, Name=>From);<br />

Mode := Create_File;<br />

Create(File=>Fd_Out.all, Mode=>Out_File, Name=>To);<br />

declare<br />

Buffers : P_PT_Buffer := new PT_Buffer ;<br />

Reader : Task_Read( Buffers, Fd_In );<br />

Writer : Task_Write( Buffers, Fd_Out );<br />

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

Reader.F<strong>in</strong>ish; Close( Fd_In.all ); --F<strong>in</strong>ish reader task<br />

Writer.F<strong>in</strong>ish; Close( Fd_Out.all ); --F<strong>in</strong>ish writer task<br />

end;<br />

exception<br />

when Name_Error =><br />

case Mode is<br />

when Open_File =><br />

Put("Problem open<strong>in</strong>g file " & From ); New_L<strong>in</strong>e;<br />

when Create_File =><br />

Put("Problem creat<strong>in</strong>g file " & To ); New_L<strong>in</strong>e;<br />

end case;<br />

when Task<strong>in</strong>g_Error =><br />

Put("Task error <strong>in</strong> ma<strong>in</strong> program"); New_L<strong>in</strong>e;<br />

end Do_Copy;<br />

Note: Explicit de-referenc<strong>in</strong>g of <strong>in</strong>stances of a File_Type is achieved us<strong>in</strong>g .all.<br />

The procedure copy extracts the arguments for the copy operation.<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Command_L<strong>in</strong>e, Do_Copy;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Command_L<strong>in</strong>e;<br />

procedure Copy is<br />

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

if Argument_Count = 2 then<br />

Do_Copy ( Argument(1), Argument(2) );<br />

else<br />

Put("Usage: copy from to"); New_L<strong>in</strong>e;<br />

end if;<br />

end Copy;<br />

When a po<strong>in</strong>ter to a protected type (for example, PT_Buffer) is elaborated, no object is created. The<br />

creation of an <strong>in</strong>stance of the protected type PT_Buffer is performed us<strong>in</strong>g new as follows:<br />

Buffers : P_PT_Buffer := new PT_Buffer ;<br />

Note:<br />

As a protected object is limited, us<strong>in</strong>g an access value is one way of mak<strong>in</strong>g the protected object visible<br />

to several program units.<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!