FortWrap
FortWrap version 0.9 released 8/18/2010
FortWrap is a python script that parses Fortran 90/95/200X source
files and generates wrapper code for interfacing with the original
Fortran code from C++. FortWrap is compatible with
both g95
and gfortran and is
intended to be used with Fortran code that takes an object oriented
approach and makes use of Fortran derived types. The resulting
wrapper code provides a C++ interface that wraps the Fortran derived
types with C++ proxy classes.
For example, consider the following Fortran code:
MODULE m
TYPE Object
...
END TYPE Object
CONTAINS
SUBROUTINE object_ctor(o, n, x)
TYPE (Object) :: o
INTEGER, INTENT(in) :: n
REAL, INTENT(in) :: x(n)
...
END SUBROUTINE object_ctor
SUBROUTINE foo(o)
TYPE (Object) :: o
...
END SUBROUTINE foo
END MODULE m
After wrapping with FortWrap, these routines can be utilized in a
natural way from C++:
std::vector<float> x(10);
... // Define values for x
Object o(&x); // C++ constructor: automatically calls object_ctor
o.foo();
Additional examples can be found here
FortWrap can also be used as an intermediary step for wrapping
Fortran code with Swig. The C++
header files generated by FortWrap can be directly processed by
Swig using the -c++ option. This allows you to take full
advantage of Swig's powerful capabilities for wrapping C++ code.
For example, FortWrap can be used with Swig to create an object
oriented interface in python. This works particularly well with
Swig's -keyword argument for handling Fortran optional
arguments.
FortWrap may be downloaded
from https://sourceforge.net/projects/fortwrap/.
- Derived Types: Transparent translation of Fortran derived types into C++ classes. This is the main objective of FortWrap.
- Translation of Fortran "ctor" functions into C++ constructors
- Fortran "dtor" functions automatically called by C++ destructor
- Optional arguments: Fortran optional arguments are fully
supported, with automatic NULL default values in C++.
- Procedure pointers: C++ function pointers may be passed
natively where Fortran expects a procedure pointer. Right now this
requires that the Fortran procedure pointer have an
explicit ABSTRACT INTERFACE
- Arrays: By default, one-dimensional arrays are
translated into C++ vector containers. Subroutine arguments used
to define the Fortran array size are automatically calculated
based on the C++ vector.
- Matrices: A "FortranMatrix" C++ class is provided for
interfacing with two-dimensional Fortran arrays (matrices). This
class takes care to store data internally in Fortran order.
- Strings with INTENT(IN) or INTENT(OUT)
and explicit length parameter are wrapped using C++ strings or
character arrays.
- "Top-level" (a.k.a. global or non-module) procedures are wrapped
- Name mangling support for both gfortran and g95 compilers
- Where possible, pass by value is used in C++ (e.g. scalar
arguments that are not optional)
- Doxygen comments: Doxygen-style comments used for
Fortran symbols (derived types, subroutines, arguments) are
transferred to C++ doxygen comments in the wrapper code.
- Automatic handling of Fortran PUBLIC
and PRIVATE statements. By default, only PUBLIC
routines are wrapped.
- Generates clean, human-readable wrapper code
Many features of Fortran 90/95/2003 are not supported by FortWrap.
In some cases (e.g. assumed shape arrays) this is because the Fortran
language standard does not provide an interoperability mechanism.
In most situations, it is possible to get FortWrap to wrap these
types of routines by providing a Fortran wrapper that is interoperable
and calls the target routine.
The following argument types/constructs/features are not
supported:
- ALLOCATABLE or POINTER arguments
- Assumed shape arrays (a declaration that looks like INTEGER
:: X(:)). The Fortran standard does not provide an
interoperability mechanism for assumed shape arrays.
- Strings with INTENT(INOUT). Arrays of strings
- Arrays of a derived type. These can be wrapped by creating a
new derived type to contain an array of the derived type of
interest. Create an "append" function in Fortran that accepts
only scalars but allows you to add items to the array container
one at a time.
- Fortran functions with non-primitive return types are not
wrapped.
- COMPLEX data type is not supported
- CLASS (polymorphic) variables. This Fortran language
features is not supported by g95 and is currently making its way
online with gfortran. Unfortunately, the Fortran 2003 standard
does not allow C_LOC to be used on CLASS pointers, so
there is not a standard-compliant way of working with these
variables. However, experimental support for FortWrap to generate
polymorphic C++ wrappers around Fortran CLASS constructs
is already underway, although it relies on gfortran-specific
compiler implementation details.
- Type bound procedures. Those that were not declared
as NOPASS require that the firrst argument be declared
using CLASS instead of TYPE. FortWrap does not
currently handle CLASS: see above.
Note that FortWrap can still wrap procedures that use unsupported
arguments if those arguments are optional. In these cases, the
offending arguments are hidden from the generated interface
code.