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. We present a generic adaptor that is able to wrap any callable C++ entity and provide a raw function pointer that is compatible with C or Fortran library routines. This allows for an object-oriented style of programming, while interfacing with legacy libraries in a straightforward manner. Program summaryProgram title: Adapt2rfpCatalogue identifier: AENU_v1_0Program summary URL:http://cpc.cs.qub.ac.uk/summaries/AENU_v1_0.htmlProgram obtainable from: CPC Program Library, Queen’s University, Belfast, N. IrelandLicensing provisions: Standard CPC licence, http://cpc.cs.qub.ac.uk/licence /licence.htmlNo. of lines in distributed program, including test data, etc.: 76802No. of bytes in distributed program, including test data, etc.: 915389Distribution format: tar.gzProgramming language: C++.Computer: All.Operating system: All.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.Restrictions: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. For example, 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 for integrating 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.
Read full abstract