Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Factored out swig typemaps

  • Loading branch information...
commit e1deb79c5a31eb272f4a9d0de4b2deb7470a680e 1 parent 37a6d05
@pjotrp authored
View
76 src/mappings/swig/ruby/example/ruby_example.i
@@ -1,79 +1,7 @@
%module example
-/* The following typemaps turn a function with definition:
- *
- * result = func(array)
- *
- * into a C call
- *
- * func(num, double *data, double *result)
- *
- * where num is digested from the array size. Note that arg1 is still
- * used as a function wide parameter.
- */
-
-%typemap(in, numinputs=0) int num {
- /* %typemap(in, numinputs=0) int num: ignore num input */
-}
-
-%typemap(in) (double* data, double *result) {
- /* %typemap(in) (double* data, double *result) */
- int i, num;
-
- if (!rb_obj_is_kind_of($input,rb_cArray))
- rb_raise(rb_eArgError, "Expected Array of values for $1_name");
- num = RARRAY($input)->len;
- $1 = (double *)malloc(num*sizeof(double));
- arg1 = num;
- for (i=0; i<num; ++i)
- ($1)[i] = rb_num2dbl(RARRAY($input)->ptr[i]);
- $2 = (double *)malloc(num*sizeof(double));
-}
-
-%typemap(freearg) (double* data, double *result) {
- /* %typemap(in) (double* data, double *result) */
- if ($1) free($1);
- if ($2) free($2);
-}
-
-%typemap(in) double* data {
- /* %typemap(in) double* data */
- int i, num;
-
- if (!rb_obj_is_kind_of($input,rb_cArray))
- rb_raise(rb_eArgError, "Expected Array of values for $1_name");
- num = RARRAY($input)->len;
- $1 = (double *)malloc(num*sizeof(double));
- arg1 = num;
- for (i=0; i<num; ++i)
- ($1)[i] = rb_num2dbl(RARRAY($input)->ptr[i]);
-}
-
-%typemap(freearg) double *data {
- /* %typemap(freearg) double *data */
- if ($1) free($1);
-}
-
-%typemap(out) double *result {
- /* %typemap(out) double *result: ignore */
-}
-
-%typemap(argout) double *result {
- /* %typemap(argout) double *result */
- int i;
- int num = arg1;
-
- /* example: printf("%f,%f",$1[0][0],$1[1][0]); */
- $result = rb_ary_new();
- for (i=0; i<num; i++)
- rb_ary_push($result,rb_float_new($1[i]));
-}
-
-%typemap(freearg) double *result {
- /* %typemap(freearg) double *result */
- if ($1) free($1);
-}
-
+%include ../../typemaps/ruby/biolib_datap_resultp.i
+%include ../../typemaps/ruby/biolib_resultp.i
%include ../../typemaps/ruby/biolib_matrix.i
%include ../../example.i
View
55 src/mappings/swig/typemaps/ruby/biolib_datap_resultp.i
@@ -0,0 +1,55 @@
+/* The following typemaps turn a function with definition:
+ *
+ * result = func(array)
+ *
+ * into a C call
+ *
+ * func(num, double *data, double *result)
+ *
+ * where num is digested from the array size. Note that arg1 is still
+ * used as a function wide parameter.
+ */
+
+%typemap(in, numinputs=0) int num {
+ /* %typemap(in, numinputs=0) int num: ignore num input */
+}
+
+%typemap(in) (double* data, double *result) {
+ /* %typemap(in) (double* data, double *result) */
+ int i, num;
+
+ if (!rb_obj_is_kind_of($input,rb_cArray))
+ rb_raise(rb_eArgError, "Expected Array of values for $1_name");
+ num = RARRAY($input)->len;
+ $1 = (double *)malloc(num*sizeof(double));
+ arg1 = num;
+ for (i=0; i<num; ++i)
+ ($1)[i] = rb_num2dbl(RARRAY($input)->ptr[i]);
+ $2 = (double *)malloc(num*sizeof(double));
+}
+
+%typemap(freearg) (double* data, double *result) {
+ /* %typemap(in) (double* data, double *result) */
+ if ($1) free($1);
+ if ($2) free($2);
+}
+
+%typemap(in) double* data {
+ /* %typemap(in) double* data */
+ int i, num;
+
+ if (!rb_obj_is_kind_of($input,rb_cArray))
+ rb_raise(rb_eArgError, "Expected Array of values for $1_name");
+ num = RARRAY($input)->len;
+ $1 = (double *)malloc(num*sizeof(double));
+ arg1 = num;
+ for (i=0; i<num; ++i)
+ ($1)[i] = rb_num2dbl(RARRAY($input)->ptr[i]);
+}
+
+%typemap(freearg) double *data {
+ /* %typemap(freearg) double *data */
+ if ($1) free($1);
+}
+
+
View
23 src/mappings/swig/typemaps/ruby/biolib_resultp.i
@@ -0,0 +1,23 @@
+/* Pass a result through an array pointer */
+
+%typemap(out) double *result {
+ /* %typemap(out) double *result: ignore */
+}
+
+%typemap(argout) double *result {
+ /* %typemap(argout) double *result */
+ int i;
+ int num = arg1;
+
+ /* example: printf("%f,%f",$1[0][0],$1[1][0]); */
+ $result = rb_ary_new();
+ for (i=0; i<num; i++)
+ rb_ary_push($result,rb_float_new($1[i]));
+}
+
+%typemap(freearg) double *result {
+ /* %typemap(freearg) double *result */
+ if ($1) free($1);
+}
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.