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.

198 Child libraries<br />

1.1.2 Implementation techniques for a generic package<br />

The implementation of a generic package is usually performed by one of the follow<strong>in</strong>g mechanisms:<br />

• A new package is generated for each unique <strong>in</strong>stantiation of the generic package. This is sometimes<br />

referred to as the macro implementation.<br />

• A s<strong>in</strong>gle code body is used which can cater for different formal types.<br />

14.4 Generic formal subprograms<br />

When specify<strong>in</strong>g a generic formal type, the compiler must know how to perform operations on an <strong>in</strong>stance of this<br />

type. For example, if the formal type is specified as private, then any <strong>Ada</strong> private type can be used. The<br />

implementor of the body of the generic package may wish to use <strong>in</strong>stances of this type <strong>in</strong> a comparison. For<br />

example, as part of a logical expression:<br />

if Instance_Of_Formal_Type > Another_Instance_Of_Formal_type then<br />

...<br />

end if;<br />

For this to be allowed, the type passed must allow, <strong>in</strong> this case, for the ">" operation to be performed between<br />

<strong>in</strong>stances of the generic type. To enforce this contract the specification part of the generic procedure, function or<br />

package must <strong>in</strong>clude a generic formal parameter for the ">" logical operation. Remember, the type passed may<br />

not have ">" def<strong>in</strong>ed between <strong>in</strong>stances of the type. For example, the class Account does not provide any<br />

comparison operators between <strong>in</strong>stances of an Account.<br />

The follow<strong>in</strong>g generic procedure which orders its formal parameters of type private is def<strong>in</strong>ed with a<br />

formal subprogram specification for ">".<br />

generic<br />

--Specification<br />

type T is private; --Any non limited type<br />

with function ">" ( A, B:<strong>in</strong> T )<br />

return Boolean is ; --Need def for ><br />

procedure G_Order( A,B:<strong>in</strong> out T ); --Prototype G_Order<br />

procedure G_Order( A,B:<strong>in</strong> out T ) is --Implementation G_Order<br />

Tmp : T;<br />

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

if A > B then<br />

--Compare<br />

Tmp := A; A := B; B := Tmp; --Swap<br />

end if;<br />

end G_Order; --<br />

The generic formal subprogram:<br />

with function ">" ( A, B:<strong>in</strong> T )<br />

return Boolean is ; --Need def for ><br />

specifies that on <strong>in</strong>stantiation of the package, a def<strong>in</strong>ition for ">" between <strong>in</strong>stances of the formal parameter T<br />

must be provided. The part of the generic formal subprogram specifies that this formal parameter has as a<br />

default value of the current def<strong>in</strong>ition for ">" at the po<strong>in</strong>t of <strong>in</strong>stantiation of the generic.<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!