@@ -1890,9 +1890,92 @@ void should_be_wrapped(UnknownType*);
1890
1890
1891
1891
# # Cross-language polymorphism using directors
1892
1892
1893
+ The "director" capability in SWIG allows C++ classes to be subclassed by a
1894
+ user in the target language to enable inversion of control through overridden
1895
+ C++ virtual functions. For Fortran, this means that a SWIG-wrapped
1896
+ derived type can be *extended* by an application code so that C++ code can send
1897
+ data to a native Fortran type-bound procedure and receive data back.
1898
+
1899
+ Enabling this advanced and still highly experimental feature requires extra
1900
+ SWIG directives, including a special setup argument in the `%module`
1901
+ declaration.
1902
+ ` ` ` swig
1903
+ %module(directors="1") example
1904
+
1905
+ %feature("director") Base;
1906
+
1907
+ %inline %{
1908
+ class Base {
1909
+ public:
1910
+ virtual ~Base() {}
1911
+ virtual int apply(int x) const = 0;
1912
+ };
1913
+
1914
+ int apply(const Base& b, int x) {
1915
+ return b.apply(x);
1916
+ }
1917
+ %}
1918
+ ` ` `
1919
+
1920
+ This allows a user application to declare an extended type such as :
1921
+ ` ` ` fortran
1922
+ module mymod
1923
+ use, intrinsic :: ISO_C_BINDING
1924
+ use ISO_FORTRAN_ENV
1925
+ use example, only : Base
1926
+ implicit none
1927
+
1928
+ type, extends(Base), public :: MyDerived
1929
+ integer(C_INT) :: multiply_by = 1
1930
+ integer(C_INT) :: add_to = 0
1931
+ contains
1932
+ procedure :: apply => MyDerived_apply
1933
+ end type MyDerived
1934
+ contains
1935
+
1936
+ function MyDerived_apply(self, x) &
1937
+ result(myresult)
1938
+ use, intrinsic :: ISO_C_BINDING
1939
+ class(MyDerived), intent(in) :: self
1940
+ integer(C_INT), intent(in) :: x
1941
+ integer(C_INT) :: myresult
1942
+
1943
+ myresult = x * self%multiply_by + self%add_to
1944
+ end function
1945
+ end module
1946
+ ` ` `
1947
+
1948
+ The overridden procedure can be called either from Fortran *or* through
1949
+ existing C++ library code.
1950
+ ` ` ` fortran
1951
+ subroutine test_director_int
1952
+ use director_simple
1953
+ use director_simple_mod
1954
+ use, intrinsic :: ISO_C_BINDING
1955
+ type(MyDerived), target :: myclass
1956
+
1957
+ ! Allocate and set up callbacks for Base class
1958
+ call swig_initialize(myclass, source=Base())
1959
+ myclass%multiply_by = 2
1960
+ myclass%add_to = 1
1961
+
1962
+ ! Direct Fortran call
1963
+ ASSERT(myclass%apply(10_c_int) == 21_c_int)
1964
+ ! Call through C director
1965
+ ASSERT(apply(myclass, 10_c_int) == 21_c_int)
1966
+
1967
+ call myclass%release()
1968
+ end subroutine
1969
+ ` ` `
1970
+
1971
+ # ## Limitations
1972
+
1973
+ Currently only fundamental types are supported.
1974
+
1893
1975
Fortran has much less introspection than Java, Python, and other less static
1894
1976
languages. Therefore director methods can't automatically detect whether the
1895
- target language (Fortran) overrides a particular method.
1977
+ target language (Fortran) overrides a particular method. **Therefore you must
1978
+ currently override all virtual methods.**
1896
1979
1897
1980
Note that a bug in GCC prevents versions before 8 from using the
1898
1981
` --std=f2003` flag (see [GNU bug 84924](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84924)).
0 commit comments