Ada 95 Quality and Style Guide Chapter 7

Chapter 7: Portability - TOC - 7.6 REPRESENTATION CLAUSES AND IMPLEMENTATION-DEPENDENT FEATURES

7.6.4 Interfacing to Foreign Languages

guideline

  • Use the package Interfaces and its language-defined child packages rather than implementation-specific mechanisms.
  • Consider using pragma Import rather than access-to-subprogram types for interfacing to subprograms in other languages.
  • Isolate all subprograms employing pragmas Import, Export, and Convention to implementation-specific (interface) package bodies.

  • example

    This example shows how to interface with the following cube root function written in C:

    double cbrt (double x);
    ------------------------------------------------------------------------------
    
    package Math_Utilities is
    
       Argument_Error : exception;
    
       function Cube_Root (X : Float) return Float;
    
       ...
    
    end Math_Utilities;
       
    ------------------------------------------------------------------------------
    with Interfaces.C;
    package body Math_Utilities is
    
       function Cube_Root (X : Float) return Float is
    
          function C_Cbrt (X : Interfaces.C.Double) return Interfaces.C.Double;
          pragma Import (Convention    => C,
                         Entity        => C_Cbrt,
                         External_Name => "cbrt");
    
       begin
          if X < 0.0 then
             raise Argument_Error;
          else
             return Float (C_Cbrt (Interfaces.C.Double (X)));
          end if;
       end Cube_Root;
    
       ...
    
    end Math_Utilities;
    
    

    rationale

    For static interfacing to subprograms in other languages, the pragma Import provides a better solution than access to subprograms because no indirection is required. The pragma Interface (Ada Reference Manual 1983) has been replaced by pragmas Import, Export, and Convention. Annex B of the Rationale (1995) discusses how to use these pragmas in conjunction with the access-to-subprogram types in interfacing to other languages.

    Access to subprogram types is useful for implementing callbacks in a separate subsystem, such as the X Window system.

    The problems with interfacing to foreign languages are complex. These problems include pragma syntax differences, conventions for linking/binding Ada to other languages, and mapping Ada variables to foreign language variables. By hiding these dependencies within interface packages, the amount of code modification can be reduced.

    exceptions

    It is often necessary to interact with other languages, if only an assembly language, to reach certain hardware features. In these cases, clearly comment the requirements and limitations of the interface and pragma Import, Export, and Conventions usage.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography