File tree Expand file tree Collapse file tree 1 file changed +26
-7
lines changed
Examples/test-suite/fortran Expand file tree Collapse file tree 1 file changed +26
-7
lines changed Original file line number Diff line number Diff line change 2
2
3
3
#include " fassert.h"
4
4
5
+ module funcptr_mod
6
+ use , intrinsic :: ISO_C_BINDING
7
+ implicit none
8
+ contains
9
+
10
+ function myexp (left , right ) bind(C) &
11
+ result(fresult)
12
+ use , intrinsic :: ISO_C_BINDING
13
+ integer (C_INT), intent (in ), value :: left
14
+ integer (C_INT), intent (in ), value :: right
15
+ integer (C_INT) :: fresult
16
+
17
+ fresult = left ** right
18
+ end function
19
+
20
+ end module
21
+
5
22
program funcptr_runme
6
23
use funcptr
24
+ use funcptr_mod
7
25
use ISO_C_BINDING
8
26
implicit none
9
- type (C_FUNPTR) :: fp
27
+ procedure (SWIGTYPE_f_int_int__int), pointer :: fp
10
28
11
- ! Add
12
- call set_handle(0 , fp)
29
+ ! Get the C++ function handle, which starts out as 'add'
30
+ fp = > get_funcvar()
31
+ ASSERT(fp(2 , 3 ) == 5 )
13
32
ASSERT(do_op(2 , 3 , fp) == 5 )
14
33
15
- ! Subtract
16
- call set_handle( 1 , fp )
17
- call set_funcvar(fp)
18
- ASSERT(do_op(2 , 3 , get_funcvar()) == - 1 )
34
+ ! Set the function handle
35
+ call set_funcvar(myexp )
36
+ ! Get it back, then call it from C++
37
+ ASSERT(do_op(2 , 3 , get_funcvar()) == 8 )
19
38
20
39
end program
21
40
You can’t perform that action at this time.
0 commit comments