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.

362 Mixed language programm<strong>in</strong>g<br />

function To_C (Item : <strong>in</strong> Character) return Char;<br />

function To_<strong>Ada</strong> (Item : <strong>in</strong> Char ) return Character;<br />

Str<strong>in</strong>g conversions:<br />

function To_C ( Item : <strong>in</strong> Str<strong>in</strong>g;<br />

Append_Nul:<strong>in</strong> Boolean := True )<br />

return char_array;<br />

function To_<strong>Ada</strong> ( Item: <strong>in</strong> char_array;<br />

Trim_Nul : <strong>in</strong> Boolean := True )<br />

return Str<strong>in</strong>g;<br />

25.3 An <strong>Ada</strong> program call<strong>in</strong>g a C function<br />

The follow<strong>in</strong>g <strong>Ada</strong> program calls the C function triple. The C function triple returns as an <strong>in</strong>teger value triple<br />

the <strong>in</strong>teger value passed to it. Firstly an <strong>in</strong>terface function Triple is constructed that calls the C function<br />

triple.<br />

with Interfaces.C;<br />

use Interfaces.C;<br />

function Triple( N:<strong>in</strong> Integer ) return Integer is<br />

function C_Triple(N:<strong>in</strong> Int) return Int;<br />

pragma Import (C, C_Triple, "c_triple");<br />

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

return Integer( C_Triple( Int(N) ) );<br />

end Triple;<br />

Note: The use of the type def<strong>in</strong>ed <strong>in</strong> Interfaces.C s:<br />

Int<br />

Represents a C <strong>in</strong>t<br />

Integer The <strong>Ada</strong> <strong>in</strong>teger type.<br />

In the <strong>Ada</strong> <strong>in</strong>terface function the pragma import is used to request the compiler to l<strong>in</strong>k to an externally<br />

written procedure or function. In this case the function c_triple written <strong>in</strong> C.<br />

This <strong>in</strong>terface function triple is then called from a simple test program.<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Triple;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io;<br />

procedure Ma<strong>in</strong> is<br />

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

Put("3 Tripled is "); Put( Triple(3) ); New_L<strong>in</strong>e;<br />

end Ma<strong>in</strong>;<br />

The implementation of the function c_triple <strong>in</strong> C is:<br />

<strong>in</strong>t c_triple( <strong>in</strong>t n ) /* function to triple argument */<br />

{<br />

return n + n + n;<br />

}<br />

The above code when compiled and l<strong>in</strong>ked will produce the follow<strong>in</strong>g output.<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!