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.

14 Generics<br />

This chapter looks at generics that enable parameterized re-usable code to be written. The degree of reuseability,<br />

however, will depend on the skill and foresight of the orig<strong>in</strong>ator.<br />

14.1 Generic functions and procedures<br />

The ma<strong>in</strong> problem <strong>in</strong> re-us<strong>in</strong>g code of previously written functions or procedures is that they are restricted to<br />

process specific types of values. For example, the function order developed as an exercise <strong>in</strong> Chapter 5 will only<br />

work for Float values. To be really useful to a programmer, this procedure should work for all objects for which<br />

a ‘greater than’ value can be def<strong>in</strong>ed. <strong>Ada</strong> allows the def<strong>in</strong>ition of generic functions or procedures. In this, the<br />

actual type(s) that are to be used are supplied by the user of the function or procedure. This is best illustrated by<br />

an example:<br />

generic<br />

type T is ( );<br />

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

--Specification<br />

--Any discrete type<br />

--Prototype ord<br />

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

Tmp : T;<br />

--Temporary<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 declaration of a generic function or procedure is split <strong>in</strong>to two components: a specification part that<br />

def<strong>in</strong>es the <strong>in</strong>terface to the outside world, and an implementation part that def<strong>in</strong>es the physical implementation. In<br />

the specification part, the type(s) that are to be used <strong>in</strong> the procedure are specified between the generic and the<br />

prototype l<strong>in</strong>e of the function or procedure. In this example a s<strong>in</strong>gle type T is to be supplied. The type T must be<br />

one of <strong>Ada</strong>’s discrete types. The ‘()’ <strong>in</strong> the declaration ‘type T is ()’ specifies this restriction. A full<br />

list of the restricted types to which a generic parameter can be constra<strong>in</strong>ed to are given <strong>in</strong> Section 14.2.<br />

To use this procedure the user must first <strong>in</strong>stantiate a procedure that will operate on a particular type. This is<br />

accomplished by the declaration:<br />

procedure Order is new G_Order( Natural ); --Instantiate order<br />

which def<strong>in</strong>es a procedure order which will put <strong>in</strong>to ascend<strong>in</strong>g order its two Natural parameters. It would, of<br />

course, be an error detected at compile-time to use the procedure order with any parameter of a type other than a<br />

Natural or a subtype of a Natural.<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!