Commits

Barry Schwartz committed 772c000

Some fixes to the C++ wrappers.

Comments (0)

Files changed (2)

caml2geom_stubs.c

 //-------------------------------------------------------------------------
 
 #define OPAQUE_P(t, name) (*(t**)Data_custom_val((name)))
-#define OPAQUE_P_ALLOC(t, ops) caml_alloc_custom(&(ops), sizeof(t), 0, 1);
+#define OPAQUE_P_ALLOC(t, ops) caml_alloc_custom(&(ops), sizeof(t*), 0, 1);
 
 inline Geom::Translate *translate_from_ml_floats(value _x, value _y)
 {
 
 static struct custom_operations bezier_curve_ops = {
     (char *) "lib2geom.Bezier_curve",
-    custom_finalize_default,
+    finalize_bezier_curve,
     custom_compare_default,
     custom_hash_default,
     custom_serialize_default,
 {
     CAMLparam1(_p);
     CAMLlocal1(_t);
-    double x = Geom::Point(*(Geom::Point*)Data_custom_val(_p))[0];
-    double y = Geom::Point(*(Geom::Point*)Data_custom_val(_p))[1];
+    Geom::Point *pt = OPAQUE_P(Geom::Point, _p);
+    double x = (*pt)[0];
+    double y = (*pt)[1];
     _t = OPAQUE_P_ALLOC(Geom::Translate, translate_ops);
     OPAQUE_P(Geom::Translate, _t) = new Geom::Translate(x, y);
     CAMLreturn(_t);
     CAMLparam2(_bez0, _bez1);
     CAMLlocal1(_bezcurve);
     _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
-    *(Geom::BezierCurve**)Data_custom_val(_bezcurve) =
-        new Geom::BezierCurve(*(Geom::Bezier*)Data_custom_val(_bez0),
-                              *(Geom::Bezier*)Data_custom_val(_bez1));
+    OPAQUE_P(Geom::BezierCurve, _bezcurve) =
+        new Geom::BezierCurve(*OPAQUE_P(Geom::Bezier, _bez0),
+                              *OPAQUE_P(Geom::Bezier, _bez1));
     CAMLreturn(_bezcurve);
 }
 
 {
     CAMLparam2(_p0, _p1);
     CAMLlocal1(_bezcurve);
+    Geom::BezierCurve *bc = new Geom::BezierCurve(*OPAQUE_P(Geom::Point, _p0),
+                                                  *OPAQUE_P(Geom::Point, _p1));
     _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
-    *(Geom::BezierCurve**)Data_custom_val(_bezcurve) =
-        new Geom::BezierCurve(*(Geom::Point*)Data_custom_val(_p0),
-                              *(Geom::Point*)Data_custom_val(_p1));
+    OPAQUE_P(Geom::BezierCurve, _bezcurve) = bc;
     CAMLreturn(_bezcurve);
 }
 
 {
     CAMLparam3(_p0, _p1, _p2);
     CAMLlocal1(_bezcurve);
+    Geom::BezierCurve *bc = new Geom::BezierCurve(*OPAQUE_P(Geom::Point, _p0),
+                                                  *OPAQUE_P(Geom::Point, _p1),
+                                                  *OPAQUE_P(Geom::Point, _p2));
     _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
-    *(Geom::BezierCurve**)Data_custom_val(_bezcurve) =
-        new Geom::BezierCurve(*(Geom::Point*)Data_custom_val(_p0),
-                              *(Geom::Point*)Data_custom_val(_p1),
-                              *(Geom::Point*)Data_custom_val(_p2));
+    OPAQUE_P(Geom::BezierCurve, _bezcurve) = bc;
     CAMLreturn(_bezcurve);
 }
 
+#include <stdio.h>
 extern "C" CAMLprim value bezier_curve_of_four_points_wrapper(value _p0, value _p1, value _p2, value _p3)
 {
     CAMLparam4(_p0, _p1, _p2, _p3);
     CAMLlocal1(_bezcurve);
+    Geom::BezierCurve *bc = new Geom::BezierCurve(*OPAQUE_P(Geom::Point, _p0),
+                                                  *OPAQUE_P(Geom::Point, _p1),
+                                                  *OPAQUE_P(Geom::Point, _p2),
+                                                  *OPAQUE_P(Geom::Point, _p3));
     _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
-    *(Geom::BezierCurve**)Data_custom_val(_bezcurve) =
-        new Geom::BezierCurve(*(Geom::Point*)Data_custom_val(_p0),
-                              *(Geom::Point*)Data_custom_val(_p1),
-                              *(Geom::Point*)Data_custom_val(_p2),
-                              *(Geom::Point*)Data_custom_val(_p3));
+    OPAQUE_P(Geom::BezierCurve, _bezcurve) = bc;
     CAMLreturn(_bezcurve);
 }
 
 {
     CAMLparam1(_bezcurve);
     CAMLlocal1(_p);
+    Geom::BezierCurve *bc = OPAQUE_P(Geom::BezierCurve, _bezcurve);
+    Geom::Point pt = bc->initialPoint();
     _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);
-    *(Geom::Point*)Data_custom_val(_p) =
-        (*(Geom::BezierCurve**)Data_custom_val(_bezcurve))->initialPoint();
+    OPAQUE_P(Geom::Point, _p) = new Geom::Point(pt);
     CAMLreturn(_p);
 }
 
+#include <stdio.h>
 extern "C" CAMLprim value bezier_curve_final_point_wrapper(value _bezcurve)
 {
     CAMLparam1(_bezcurve);
     CAMLlocal1(_p);
+    Geom::BezierCurve *bc = OPAQUE_P(Geom::BezierCurve, _bezcurve);
+    Geom::Point pt = bc->finalPoint();
     _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);
-    *(Geom::Point*)Data_custom_val(_p) = OPAQUE_P(Geom::BezierCurve, _bezcurve)->finalPoint();
+    OPAQUE_P(Geom::Point, _p) = new Geom::Point(pt);
     CAMLreturn(_p);
 }
 
 
 let q = Point.make 0. 0.;;
 let _ = printf "(%f,%f) %f\n" (Point.coord q 0) (Point.coord q 1) (Point.length q);;
+let q = Point.default ();;
+let _ = printf "(%f,%f) %f\n" (Point.coord q 0) (Point.coord q 1) (Point.length q);;
 Point.set q (Point.make (-50.) 50.);;
 let _ = printf "(%f,%f) %f\n" (Point.coord q 0) (Point.coord q 1) (Point.length q);;
 
 (*-----------------------------------------------------------------------*)
 
 let bezcurve =
-  Bezier_curve.of_four_points (Point.make 0. 0.) (Point.make 10. 10.) (Point.make 15. 5.) (Point.make 20. 1.) ;;
+  Bezier_curve.of_four_points (Point.make (-1.) (-3.)) (Point.make 10. 10.) (Point.make 15. 5.) (Point.make 20. 1.) ;;
 let _ =
   printf "initial_point = (%f,%f)\n" (Point.coord (Bezier_curve.initial_point bezcurve) 0)
     (Point.coord (Bezier_curve.initial_point bezcurve) 1) ;;