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.

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

This chapter describes how code written <strong>in</strong> another language can be called from an <strong>Ada</strong> program. This<br />

allows the <strong>Ada</strong> programmer to take advantage of the wealth of code previously written <strong>in</strong> other languages.<br />

25.1 L<strong>in</strong>k<strong>in</strong>g to other code<br />

The designers of <strong>Ada</strong> <strong>95</strong> realised that if the language was to prosper then it must co-exist <strong>in</strong> a world where code<br />

was written <strong>in</strong> languages other than <strong>Ada</strong>. <strong>Ada</strong> provides mechanisms that allow code written <strong>in</strong> the programm<strong>in</strong>g<br />

languages C, Fortran and COBOL to be directly called. Other languages such as C++ may also be called us<strong>in</strong>g the<br />

C <strong>in</strong>terface.<br />

25.2 Selected types and functions from Interfaces.C<br />

The follow<strong>in</strong>g are a selection of types and functions from the package Interfaces.C. This <strong>Ada</strong> package<br />

allows the call<strong>in</strong>g of functions written <strong>in</strong> the C language. In particular mechanisms are provided to convert<br />

between <strong>in</strong>stance of <strong>Ada</strong> types and <strong>in</strong>stances of C types.<br />

25.2.1 Integer, character and float<strong>in</strong>g po<strong>in</strong>t types<br />

By us<strong>in</strong>g the follow<strong>in</strong>g types an <strong>Ada</strong> variable or expression can be converted to a form compatible with the C<br />

language.<br />

Integer Types Char types Float<strong>in</strong>g po<strong>in</strong>t types<br />

Char<br />

Wchar_T<br />

Int<br />

Short<br />

Long<br />

Size_T<br />

C_Float<br />

Double<br />

Long_Double.<br />

For example to pass the <strong>in</strong>teger value Item as a parameter to a C function that expects a long double the<br />

follow<strong>in</strong>g expression can be used: Long_Double(Item).<br />

25.2.2 C Str<strong>in</strong>g type<br />

The follow<strong>in</strong>g declaration:<br />

type Char_Array is Array (Size_T range ) of aliased Char;<br />

is used to declare a C array of characters. In C an array of characters term<strong>in</strong>ated by the null character is used to<br />

represent a str<strong>in</strong>g. For example, the follow<strong>in</strong>g will declare a str<strong>in</strong>g Name conta<strong>in</strong><strong>in</strong>g the text "Miranda" that<br />

may be passed to a C function that requires a C str<strong>in</strong>g parameter.<br />

Name : constant Char_Array := "Miranda" & nul;<br />

Note:<br />

25.2.3 Selected functions<br />

The use of nul to represent the null character. Remember null is an <strong>Ada</strong> reserved word.<br />

Character conversions:<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!