[Numpy-svn] r2710 - in trunk/numpy/f2py/lib: . research research/rat

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Jun 30 08:29:04 CDT 2006


Author: pearu
Date: 2006-06-30 08:28:59 -0500 (Fri, 30 Jun 2006)
New Revision: 2710

Added:
   trunk/numpy/f2py/lib/research/
   trunk/numpy/f2py/lib/research/rat/
   trunk/numpy/f2py/lib/research/rat/main.c
   trunk/numpy/f2py/lib/research/rat/main.f90
   trunk/numpy/f2py/lib/research/rat/rational.f90
Log:
Workout a way to access Fortran derived objects from C.

Added: trunk/numpy/f2py/lib/research/rat/main.c
===================================================================
--- trunk/numpy/f2py/lib/research/rat/main.c	2006-06-30 12:21:17 UTC (rev 2709)
+++ trunk/numpy/f2py/lib/research/rat/main.c	2006-06-30 13:28:59 UTC (rev 2710)
@@ -0,0 +1,38 @@
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+
+typedef void* obj_type;
+typedef obj_type (*rat_create_func)(void);
+typedef obj_type (*rat_add_func)(obj_type, obj_type);
+typedef void (*rat_show_func)(obj_type);
+typedef void (*rat_set_func)(obj_type, int*, int*);
+
+static void** rational_funcs;
+
+static void set_f90_funcs2(int* n,...) {
+  int i;
+  va_list ap;
+  //printf("In set_f90_funcs n=%d\n",(*n));
+  va_start(ap,n);
+  rational_funcs = (void*)malloc((*n)*sizeof(void*));
+  for (i=0;i<(*n);i++)
+    rational_funcs[i] = va_arg(ap,void*);
+  va_end(ap);
+}
+
+int main(void) {
+  init_f90_funcs_(set_f90_funcs2);
+  rat_create_func rat_create = rational_funcs[0];
+  rat_show_func rat_show = rational_funcs[1];
+  rat_set_func rat_set = rational_funcs[2];
+  rat_add_func rat_add = rational_funcs[3];
+
+  obj_type obj_ptr = NULL;
+  obj_ptr = (*rat_create)();
+  int n=2,d=3;
+  (*rat_set)(obj_ptr, &n, &d);
+  (*rat_show)(obj_ptr);
+  (*rat_show)((*rat_add)(obj_ptr,obj_ptr));
+}

Added: trunk/numpy/f2py/lib/research/rat/main.f90
===================================================================
--- trunk/numpy/f2py/lib/research/rat/main.f90	2006-06-30 12:21:17 UTC (rev 2709)
+++ trunk/numpy/f2py/lib/research/rat/main.f90	2006-06-30 13:28:59 UTC (rev 2710)
@@ -0,0 +1,16 @@
+
+program a
+
+  use rational
+
+  type(rat_struct), pointer :: r
+
+  r => rat_create()
+
+  print*,r
+
+  call rat_set(r, 3,4)
+
+  call rat_show(r)
+
+end program a

Added: trunk/numpy/f2py/lib/research/rat/rational.f90
===================================================================
--- trunk/numpy/f2py/lib/research/rat/rational.f90	2006-06-30 12:21:17 UTC (rev 2709)
+++ trunk/numpy/f2py/lib/research/rat/rational.f90	2006-06-30 13:28:59 UTC (rev 2710)
@@ -0,0 +1,48 @@
+
+module rational
+
+  implicit none
+  
+  type, public :: rat_struct
+     integer :: numer ! numerator
+     integer :: denom ! denominator
+  end type rat_struct
+
+  contains
+
+    function rat_create() result (obj)
+      type(rat_struct), pointer :: obj
+      print*,'In rat_create'
+      allocate(obj)
+      call rat_set(obj, 0, 1)
+    end function rat_create
+
+     subroutine rat_set(obj, n, d)
+       type(rat_struct) :: obj
+       integer :: n,d
+       print*,'In rat_set'
+       obj % numer = n
+       obj % denom = d       
+     end subroutine rat_set
+
+    subroutine rat_show(obj)
+      type(rat_struct)  :: obj
+      print*,'In rat_show'
+      print*, "object numer,denom = ",obj % numer, obj % denom
+    end subroutine rat_show
+
+    function rat_add(a,b) result (ab)
+      type(rat_struct), pointer :: ab
+      type(rat_struct)  :: a,b
+      allocate(ab)
+      ab % numer = a % numer * b % denom + b % numer * a % denom
+      ab % denom = a % denom * b % denom
+    end function rat_add
+
+end module rational
+
+subroutine init_f90_funcs(set_f90_funcs)
+  use rational
+  external set_f90_funcs
+  call set_f90_funcs(4, rat_create, rat_show, rat_set, rat_add)
+end subroutine init_f90_funcs



More information about the Numpy-svn mailing list