Elsevier Science Home
Computer Physics Communications Program Library
Full text online from Science Direct
Programs in Physics & Physical Chemistry
CPC Home

[Licence| Download | New Version Template] aenu_v1_0.tar.gz(893 Kbytes)
Manuscript Title: An Adaptor for C++ callbacks with C and Fortran Libraries
Authors: J. Broeckhove, K. Vanmechelen
Program title: Adapt2rfp
Catalogue identifier: AENU_v1_0
Distribution format: tar.gz
Journal reference: Comput. Phys. Commun. 184(2013)824
Programming language: C++.
Computer: All.
Operating system: All.
Keywords: C++ callbacks, C libraries, Fortran libraries, C++ template programming.
PACS: P89.20.Ff, 07.05.Bx.
Classification: 6.5, 4.14.

Nature of problem:
Object-oriented programming using C++ is increasingly being adopted in the development of scientific codes. A recurrent issue in this regard, is the interaction of newly developed codes with existing legacy libraries written in C or Fortran. Often, one needs to pass raw function pointers to such libraries' procedures for callback purposes. This is problematic as it conflicts with one of the cornerstones of object-oriented programming, the association of functions and data through objects. Currently ad-hoc approaches are used to deal with this issue, but these are error-prone and lack reusability.

Solution method:
Recursive template instantiation is used to generate instantiations of wrapper templates. These template classes provide a static forwarding function that can be converted to a raw function pointer. The necessary provisions are in place to deal with variations in call signatures.

The present adaptor implementation can handle callable entities with signatures of at most nine parameters. Other implementations supporting more parameters can be generated but require the Boost macro library. The code of the adaptor implementation (a single header file) fails to compile on compilers pre-dating the introduction of TR1 C++ library extension. E.g. for the gcc suite one needs version 4.3 (released early 2008) or above.

Unusual features:
The inclusion of a single header file adapt2rfp.h suffices to integrate the solution in an existing software project.

Running time:
A call through a raw function pointer returned by the adaptor adds on the order of 30 machine instructions to forward the call through the adaptor's wrapper structure. Due to the static nature of these forwarding calls, the instruction count can be heavily optimized by the compiler.