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.

416 Appendix D<br />

with Unchecked_Deallocation;<br />

package body Class_Queue is<br />

procedure Dispose is<br />

new Unchecked_Deallocation( Node, P_Node );<br />

procedure Add( The:<strong>in</strong> out Queue; Item:<strong>in</strong> T ) is<br />

Tmp : P_Node := new Node'( Item, null );<br />

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

if The.No_Of > 0 then<br />

The.Tail.P_Next := Tmp; --Cha<strong>in</strong> <strong>in</strong><br />

else<br />

The.Head := Tmp; --Also head<br />

end if;<br />

The.Tail := Tmp; --New Tail<br />

The.No_Of := The.No_Of + 1; --Inc no.<br />

end Add;<br />

procedure Sub( The:<strong>in</strong> out Queue; Item :out T ) is<br />

Tmp : P_Node;<br />

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

if The.No_Of > 0 then<br />

Item := The.Head.Item; --Recovered item<br />

Tmp := The.Head; --Node f<strong>in</strong>ished with<br />

The.Head := The.Head.P_Next; --new head<br />

Dispose( Tmp ); --Free storage<br />

The.No_Of := The.No_Of - 1; --1 less <strong>in</strong> queue<br />

else<br />

raise Queue_Error;<br />

end if;<br />

end Sub;<br />

--Error<br />

procedure F<strong>in</strong>alize( The:<strong>in</strong> out Queue ) is<br />

Discard : T;<br />

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

for I <strong>in</strong> 1 .. The.No_Of loop --Free storage<br />

Sub( The, Discard );<br />

end loop;<br />

end F<strong>in</strong>alize;<br />

end Class_Queue;<br />

The <strong>in</strong>stantiation of an <strong>in</strong>teger <strong>in</strong>stance of the class Queue.<br />

with Class_Queue;<br />

package Class_Queue_Int is new Class_Queue(Integer);<br />

A small test program to test the class Queue.<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!