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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

414 Appendix D<br />

generic<br />

type Store_Index is private; --<br />

type Store_Element is private; --<br />

package Class_Store is<br />

type Store is limited private; --NO copy<strong>in</strong>g<br />

Not_There, Full : exception;<br />

procedure Add ( The:<strong>in</strong> out Store;<br />

Index:<strong>in</strong> Store_Index;<br />

Item:<strong>in</strong> Store_Element);<br />

function Deliver( The:<strong>in</strong> Store;<br />

Index:<strong>in</strong> Store_Index )<br />

return Store_Element;<br />

private<br />

Max_Store : constant := 10;<br />

type Store_R_Index is range 0 .. Max_Store;<br />

subtype Store_R_Range is Store_R_Index range 1 .. Max_Store;<br />

type Store_Record is record<br />

Index: Store_Index; --Index<br />

Item : Store_Element; --Data item<br />

end record;<br />

type Store_Array is array( Store_R_Range ) of Store_Record;<br />

type Store is limited record<br />

Data : Store_Array;<br />

Items: Store_R_Index := 0;<br />

end record;<br />

end Class_Store;<br />

A possible implementation of the Class Store is:<br />

package body Class_Store is<br />

procedure Add ( The:<strong>in</strong> out Store;<br />

Index:<strong>in</strong> Store_Index;<br />

Item:<strong>in</strong> Store_Element) is<br />

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

if The.Items < Max_Store then<br />

The.Items := The.Items + 1;<br />

The.Data(The.Items) := ( Index, Item );<br />

else<br />

raise Full;<br />

end if;<br />

end Add;<br />

function Deliver( The:<strong>in</strong> Store;<br />

Index:<strong>in</strong> Store_Index)<br />

return Store_Element is<br />

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

for I <strong>in</strong> 1 .. Store_R_Range(Max_Store) loop<br />

if The.Data(I).Index = Index then<br />

return The.Data(I).Item;<br />

end if;<br />

end loop;<br />

raise Not_There;<br />

end Deliver;<br />

end Class_Store;<br />

package Pack_Types is<br />

subtype Name is Str<strong>in</strong>g( 1..5 );<br />

end Pack_Types;<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!