[cairo] [rcairo] hide free functions and cleanup ruby <-> C convert functions

Kouhei Sutou kou at cozmixng.org
Sat Sep 10 00:46:42 PDT 2005


Hi,

I made a patch which hides free functions and cleanup
ruby <-> C convert functions.

  * rename ruby <-> C convert functions name.

    ruby -> C: rb_cairo_XXX_from_ruby_object().
    C -> ruby: rb_cairo_XXX_to_ruby_object().

  * provide ruby <-> C convert macros.

    ruby -> C: RVAL2CRXXX()
    C -> ruby: CRXXX2RVAL()

  * _SELF use RVAL2CRXXX()

  * rb_cairo_XXX_from_ruby_object() uses rb_obj_is_kind_of()
    instead of 'CLASS_OF() != rb_cCairo_XXX'.

  * add 'static' or remove free functions.

Thanks,
--
kou
-------------- next part --------------
Index: packages/cairo/ext/rb_cairo.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo.c,v
retrieving revision 1.4
diff -u -p -r1.4 rb_cairo.c
--- packages/cairo/ext/rb_cairo.c	9 Sep 2005 11:28:02 -0000	1.4
+++ packages/cairo/ext/rb_cairo.c	10 Sep 2005 08:06:18 -0000
@@ -11,6 +11,8 @@
 
 #include "rb_cairo.h"
 
+VALUE rb_mCairo;
+
 extern void Init_cairo_context (void);
 extern void Init_cairo_matrix (void);
 extern void Init_cairo_surface (void);
Index: packages/cairo/ext/rb_cairo.h
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo.h,v
retrieving revision 1.3
diff -u -p -r1.3 rb_cairo.h
--- packages/cairo/ext/rb_cairo.h	9 Sep 2005 11:28:02 -0000	1.3
+++ packages/cairo/ext/rb_cairo.h	10 Sep 2005 08:06:18 -0000
@@ -27,33 +27,56 @@ extern VALUE rb_cCairo_TextExtents;
 extern VALUE rb_cCairo_Glyph;
 extern VALUE rb_cCairo_Surface;
 
-VALUE    rb_cairo_new_from  (cairo_t *cr);
-cairo_t *rb_v_to_cairo_t    (VALUE value);
-void     rb_free_context    (void *ptr);
+#define RVAL2CRCONTEXT(obj)     (rb_cairo_from_ruby_object(obj))
+#define CRCONTEXT2RVAL(cr)      (rb_cairo_to_ruby_object(cr))
 
-void rb_cairo_raise_exception (cairo_status_t  status);
+#define RVAL2CRMATRIX(obj)      (rb_cairo_matrix_from_ruby_object(obj))
+#define CRMATRIX2RVAL(matrix)   (rb_cairo_matrix_to_ruby_object(matrix))
 
-cairo_matrix_t *rb_v_to_cairo_matrix_t (VALUE value);
-VALUE           rb_cairo_matrix_wrap   (cairo_matrix_t *matrix);
-void            rb_free_matrix         (void *ptr);
+#define RVAL2CRPATTERN(obj)     (rb_cairo_pattern_from_ruby_object(obj))
+#define CRPATTERN2RVAL(pattern) (rb_cairo_pattern_to_ruby_object(pattern))
 
-VALUE            rb_cairo_pattern_wrap   (cairo_pattern_t *pat);
-cairo_pattern_t *rb_v_to_cairo_pattern_t (VALUE value);
-void             rb_free_pattern         (void *ptr);
+#define RVAL2CRFONTFACE(obj)    (rb_cairo_font_face_from_ruby_object(obj))
+#define CRFONTFACE2RVAL(face)   (rb_cairo_font_face_to_ruby_object(face))
 
-cairo_font_face_t *value_to_font_face   (VALUE value);
-void               rb_free_font_face    (void *ptr);
+#define RVAL2CRFONTEXTENTS(obj) (rb_cairo_font_extents_from_ruby_object(obj))
+#define CRFONTEXTENTS2RVAL(ext) (rb_cairo_font_extents_to_ruby_object(ext))
 
-cairo_font_extents_t *rb_v_to_cairo_font_extents_t (VALUE value);
-void                  rb_free_font_extents         (void *ptr);
+#define RVAL2CRTEXTEXTENTS(obj) (rb_cairo_text_extents_from_ruby_object(obj))
+#define CRTEXTEXTENTS2RVAL(ext) (rb_cairo_text_extents_to_ruby_object(ext))
 
-cairo_text_extents_t *rb_v_to_cairo_text_extents_t (VALUE value);
-void                  rb_free_text_extents         (void *ptr);
+#define RVAL2CRGLYPH(obj)       (rb_cairo_glyph_from_ruby_object(obj))
+#define CRGLYPH2RVAL(glyph)     (rb_cairo_glyph_to_ruby_object(glyph))
 
-cairo_glyph_t *rb_v_to_cairo_glyph_t (VALUE value);
-void           rb_free_glyph         (void *ptr);
+#define RVAL2CRSURFACE(obj)     (rb_cairo_surface_from_ruby_object(obj))
+#define CRSURFACE2RVAL(surface) (rb_cairo_surface_to_ruby_object(surface))
 
-cairo_surface_t *rb_v_to_cairo_surface_t (VALUE value);
-void             rb_free_surface         (void *ptr);
+cairo_t              *rb_cairo_from_ruby_object              (VALUE obj);
+VALUE                 rb_cairo_to_ruby_object                (cairo_t *cr);
+
+cairo_matrix_t       *rb_cairo_matrix_from_ruby_object       (VALUE obj);
+VALUE                 rb_cairo_matrix_to_ruby_object         (cairo_matrix_t *matrix);
+
+cairo_pattern_t      *rb_cairo_pattern_from_ruby_object      (VALUE obj);
+VALUE                 rb_cairo_pattern_to_ruby_object        (cairo_pattern_t *pat);
+
+cairo_font_face_t    *rb_cairo_font_face_from_ruby_object    (VALUE obj);
+VALUE                 rb_cairo_font_face_to_ruby_object      (cairo_font_face_t *face);
+
+cairo_font_extents_t *rb_cairo_font_extents_from_ruby_object (VALUE obj);
+VALUE                 rb_cairo_font_extents_to_ruby_object   (cairo_font_extents_t *extents);
+
+cairo_text_extents_t *rb_cairo_text_extents_from_ruby_object (VALUE obj);
+VALUE                 rb_cairo_text_extents_to_ruby_object   (cairo_text_extents_t *extents);
+
+cairo_glyph_t        *rb_cairo_glyph_from_ruby_object        (VALUE obj);
+VALUE                 rb_cairo_glyph_to_ruby_object          (cairo_glyph_t *glyph);
+
+cairo_surface_t      *rb_cairo_surface_from_ruby_object      (VALUE obj);
+VALUE                 rb_cairo_surface_to_ruby_object        (cairo_surface_t *surface);
+
+
+
+void rb_cairo_raise_exception (cairo_status_t status);
 
 #endif
Index: packages/cairo/ext/rb_cairo_context.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo_context.c,v
retrieving revision 1.8
diff -u -p -r1.8 rb_cairo_context.c
--- packages/cairo/ext/rb_cairo_context.c	9 Sep 2005 11:28:02 -0000	1.8
+++ packages/cairo/ext/rb_cairo_context.c	10 Sep 2005 08:06:19 -0000
@@ -10,17 +10,19 @@
 
 #include "rb_cairo.h"
 
-#define _SELF  (DATA_PTR(self))
+VALUE rb_cCairo_Context;
+
+#define _SELF  (RVAL2CRCONTEXT(self))
 
 cairo_t *
-rb_v_to_cairo_t (VALUE value)
+rb_cairo_from_ruby_object (VALUE obj)
 {
   cairo_t  *context;
-  if (CLASS_OF (value) != rb_cCairo_Context)
+  if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_Context)))
     {
       rb_raise (rb_eTypeError, "not a cairo graphics context");
     }
-  Data_Get_Struct (value, cairo_t, context);
+  Data_Get_Struct (obj, cairo_t, context);
   return context;
 }
 
@@ -50,7 +52,7 @@ check_context_status (cairo_t *context)
     }
 }
 
-void
+static void
 rb_free_context (void *ptr)
 {
   if (ptr)
@@ -59,6 +61,13 @@ rb_free_context (void *ptr)
     }
 }
 
+VALUE
+rb_cairo_to_ruby_object (cairo_t *cr)
+{
+  cairo_reference (cr);
+  return Data_Wrap_Struct (rb_cCairo_Context, NULL, rb_free_context, cr);
+}
+
 static    VALUE
 rb_cairo_new (VALUE klass,
               VALUE target_v)
@@ -66,12 +75,14 @@ rb_cairo_new (VALUE klass,
   cairo_surface_t *target;
   cairo_t         *cr;
 
-  target = rb_v_to_cairo_surface_t (target_v);
+  target = RVAL2CRSURFACE (target_v);
   cr     = cairo_create (target);
 
   if (cr)
     {
-      return Data_Wrap_Struct (rb_cCairo_Context, NULL, rb_free_context, cr);
+      VALUE rb_cr = CRCONTEXT2RVAL (cr);
+      cairo_destroy (cr);
+      return rb_cr;
     }
   else
     {
@@ -80,13 +91,6 @@ rb_cairo_new (VALUE klass,
     }
 }
 
-VALUE
-rb_cairo_new_from (cairo_t *cr)
-{
-  cairo_reference (cr);
-  return Data_Wrap_Struct (rb_cCairo_Context, NULL, rb_free_context, cr);
-}
-
 static    VALUE
 rb_cairo_save (VALUE self)
 {
@@ -158,7 +162,7 @@ rb_cairo_set_source_surface (VALUE self,
                              VALUE width_v,
                              VALUE height_v)
 {
-  cairo_surface_t *surface = rb_v_to_cairo_surface_t (surface_v);
+  cairo_surface_t *surface = RVAL2CRSURFACE (surface_v);
   int              width  = NUM2INT (width_v);
   int              height = NUM2INT (height_v);
   cairo_set_source_surface (_SELF, surface, width, height);
@@ -171,7 +175,7 @@ rb_cairo_set_source (VALUE self,
                      VALUE pattern_v)
 {
   cairo_pattern_t *pattern;
-  pattern = rb_v_to_cairo_pattern_t (pattern_v);
+  pattern = RVAL2CRPATTERN (pattern_v);
 
   cairo_set_source (_SELF, pattern);
   check_context_status (_SELF);
@@ -182,9 +186,7 @@ rb_cairo_set_source (VALUE self,
 static    VALUE
 rb_cairo_get_source (VALUE self)
 {
-  cairo_pattern_t *pattern;
-  pattern = cairo_get_source (_SELF);
-  return rb_cairo_pattern_wrap  (pattern);
+  return CRPATTERN2RVAL (cairo_get_source (_SELF));
 }
 
 static    VALUE
@@ -329,7 +331,7 @@ static    VALUE
 rb_cairo_transform (VALUE self,
                     VALUE xform)
 {
-  cairo_transform (_SELF, rb_v_to_cairo_matrix_t (xform));
+  cairo_transform (_SELF, RVAL2CRMATRIX (xform));
   check_context_status (_SELF);
   return self;
 }
@@ -338,7 +340,7 @@ static    VALUE
 rb_cairo_set_matrix (VALUE self,
                      VALUE xform)
 {
-  cairo_set_matrix (_SELF, rb_v_to_cairo_matrix_t (xform));
+  cairo_set_matrix (_SELF, RVAL2CRMATRIX (xform));
   check_context_status (_SELF);
   return self;
 }
@@ -713,7 +715,7 @@ rb_cairo_set_font_matrix (VALUE self,
                           VALUE matrix)
 {
   cairo_set_font_matrix (_SELF,
-                         rb_v_to_cairo_matrix_t (matrix));
+                         RVAL2CRMATRIX (matrix));
   return self;
 }
 
@@ -747,7 +749,7 @@ rb_cairo_show_glyphs (VALUE self,
   for (i=0; i< count; i++)
     {
       memcpy ( (char *) &glyphs[i],
-               (char *) DATA_PTR (rb_ary_entry (glyphs_v, i)),
+               (char *) RVAL2CRGLYPH (rb_ary_entry (glyphs_v, i)),
                sizeof (cairo_glyph_t));
     }
 
@@ -765,12 +767,12 @@ rb_cairo_get_font_face (VALUE self)
   xform = cairo_get_font_face (_SELF);
   if (xform)
     {
+      VALUE rb_xform = CRFONTFACE2RVAL (xform);
       if (cairo_status (_SELF))
         {
-          rb_free_font_face (xform);
           rb_cairo_raise_exception (cairo_status (_SELF));
         }
-      return Data_Wrap_Struct (rb_cCairo_FontFace, NULL, rb_free_font_face, xform);
+      return rb_xform;
     }
   else
     {
@@ -784,7 +786,7 @@ rb_cairo_set_font_face (VALUE self,
                         VALUE xform)
 {
   cairo_set_font_face (_SELF,
-                       value_to_font_face (xform));
+                       RVAL2CRFONTFACE (xform));
   check_context_status (_SELF);
   return self;
 }
@@ -819,7 +821,7 @@ rb_cairo_glyph_path (VALUE self,
   for (i=0; i< count; i++)
     {
       memcpy ( (char *) &glyphs[i],
-               (char *) DATA_PTR (rb_ary_entry (glyphs_v, i)),
+               (char *) RVAL2CRGLYPH (rb_ary_entry (glyphs_v, i)),
                sizeof (cairo_glyph_t));
     }
 
@@ -900,40 +902,20 @@ rb_cairo_get_miter_limit (VALUE self)
 static    VALUE
 rb_cairo_get_matrix (VALUE self)
 {
-  cairo_matrix_t *matrix = malloc (sizeof (cairo_matrix_t));
-  if (matrix)
+  cairo_matrix_t matrix;
+  cairo_get_matrix (_SELF, &matrix);
+  if (cairo_status (_SELF))
     {
-      cairo_get_matrix (_SELF, matrix);
-      if (cairo_status (_SELF))
-        {
-          rb_free_matrix (matrix);
-          rb_cairo_raise_exception (cairo_status (_SELF));
-        }
-      return rb_cairo_matrix_wrap (matrix);
-    }
-  else
-    {
-      rb_raise (rb_eNoMemError, "out of memory");
-      return Qundef;
+      rb_cairo_raise_exception (cairo_status (_SELF));
     }
+  return CRMATRIX2RVAL (&matrix);
 }
 
 
 static    VALUE
 rb_cairo_get_target (VALUE self)
 {
-  cairo_surface_t *surface;
-  surface = cairo_get_target (_SELF);
-  if (surface)
-    {
-      cairo_surface_reference (surface);
-      return Data_Wrap_Struct (rb_cCairo_Surface, NULL, rb_free_surface,
-                               surface);
-    }
-  else
-    {
-      return Qnil;
-    }
+  return CRSURFACE2RVAL (cairo_get_target (_SELF));
 }
 
 void
Index: packages/cairo/ext/rb_cairo_font_extents.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo_font_extents.c,v
retrieving revision 1.2
diff -u -p -r1.2 rb_cairo_font_extents.c
--- packages/cairo/ext/rb_cairo_font_extents.c	9 Sep 2005 11:28:02 -0000	1.2
+++ packages/cairo/ext/rb_cairo_font_extents.c	10 Sep 2005 08:06:20 -0000
@@ -11,45 +11,49 @@
 
 #include "rb_cairo.h"
 
-#define _SELF  ((cairo_font_extents_t *)DATA_PTR(self))
+VALUE rb_cCairo_FontExtents;
+
+#define _SELF  (RVAL2CRFONTEXTENTS(self))
 
 cairo_font_extents_t *
-rb_v_to_cairo_font_extents_t (VALUE value)
+rb_cairo_font_extents_from_ruby_object (VALUE obj)
 {
   cairo_font_extents_t *xform;
-  if (CLASS_OF (value) != rb_cCairo_FontExtents)
+  if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_FontExtents)))
     {
       rb_raise (rb_eTypeError, "not a cairo font_extents");
     }
-  Data_Get_Struct (value, cairo_font_extents_t, xform);
+  Data_Get_Struct (obj, cairo_font_extents_t, xform);
   return xform;
 }
 
-void
-rb_free_font_extents (void *ptr)
+VALUE
+rb_cairo_font_extents_to_ruby_object (cairo_font_extents_t *extents)
 {
-  if (ptr)
+  if (extents)
     {
-      free ((cairo_font_extents_t *) ptr);
+      cairo_font_extents_t *new_extents = ALLOC (cairo_font_extents_t);
+      *new_extents = *extents;
+      return Data_Wrap_Struct (rb_cCairo_FontExtents, NULL, -1, new_extents);
+    }
+  else
+    {
+      return Qnil;
     }
 }
 
 static    VALUE
 rb_cairo_font_extents_new (VALUE klass)
 {
-  cairo_font_extents_t *font_extents;
-  VALUE font_extents_v;
-
-  font_extents = ALLOC (cairo_font_extents_t);
-  font_extents->ascent        = 0.0;
-  font_extents->descent       = 0.0;
-  font_extents->height        = 0.0;
-  font_extents->max_x_advance = 0.0;
-  font_extents->max_y_advance = 0.0;
+  cairo_font_extents_t font_extents;
 
-  font_extents_v = Data_Wrap_Struct (rb_cCairo_FontExtents, NULL, free, font_extents);
+  font_extents.ascent        = 0.0;
+  font_extents.descent       = 0.0;
+  font_extents.height        = 0.0;
+  font_extents.max_x_advance = 0.0;
+  font_extents.max_y_advance = 0.0;
 
-  return font_extents_v;
+  return CRFONTEXTENTS2RVAL (&font_extents);
 }
 
 static    VALUE
Index: packages/cairo/ext/rb_cairo_font_face.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo_font_face.c,v
retrieving revision 1.2
diff -u -p -r1.2 rb_cairo_font_face.c
--- packages/cairo/ext/rb_cairo_font_face.c	9 Sep 2005 11:28:02 -0000	1.2
+++ packages/cairo/ext/rb_cairo_font_face.c	10 Sep 2005 08:06:20 -0000
@@ -11,49 +11,55 @@
 
 #include "rb_cairo.h"
 
-#define _SELF  (DATA_PTR(self))
+VALUE rb_cCairo_FontFace;
+
+#define _SELF  (RVAL2CRFONTFACE(self))
 
 cairo_font_face_t *
-value_to_font_face (VALUE value)
+rb_cairo_font_face_from_ruby_object (VALUE obj)
 {
   cairo_font_face_t *xform;
-  if (CLASS_OF (value) != rb_cCairo_FontFace)
+  if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_FontFace)))
     {
       rb_raise (rb_eTypeError, "not a cairo font face");
     }
-  Data_Get_Struct (value, cairo_font_face_t, xform);
+  Data_Get_Struct (obj, cairo_font_face_t, xform);
   return xform;
 }
 
-void
-rb_free_font_face (void *ptr)
+VALUE
+rb_cairo_font_face_to_ruby_object (cairo_font_face_t *face)
 {
-  if (ptr)
+  if (face)
+    {
+      cairo_font_face_reference (face);
+      return Data_Wrap_Struct (rb_cCairo_FontFace, NULL,
+                               cairo_font_face_destroy, face);
+    }
+  else
     {
-//      cairo_font_destroy ((cairo_font_t *) ptr);
+      return Qnil;
     }
 }
 
+
 #if 0
 static    VALUE
 rb_cairo_font_extents (VALUE self,
                        VALUE font_matrix_v)
 {
-  cairo_matrix_t       *font_matrix = rb_v_to_cairo_matrix_t (font_matrix_v);
-  cairo_font_extents_t *extents;
+  cairo_matrix_t       *font_matrix = RVAL2CRAMTRIX (font_matrix_v);
+  cairo_font_extents_t  extents;
   cairo_status_t        status;
 
-  extents = malloc (sizeof (cairo_font_extents_t));
-  if (extents)
+  status = cairo_font_extents (_SELF, font_matrix, &extents);
+  if (status)
     {
-      status = cairo_font_extents (_SELF, font_matrix, extents);
-      /* XXX; check status */
-      return Data_Wrap_Struct (rb_cCairo_FontExtents, NULL, rb_free_font_extents, extents);
+      rb_cairo_raise_exception (status);
     }
   else
     {
-      rb_raise (rb_eNoMemError, "unable to allocate font extents");
-      return Qundef;
+      return CRFONTEXTENTS2RVAL (&extents);
     }
 }
 
@@ -62,22 +68,13 @@ rb_cairo_font_glyph_extents (VALUE self,
                              VALUE font_matrix_v,
                              VALUE glyphs_v)
 {
-  cairo_matrix_t       *font_matrix = rb_v_to_cairo_matrix_t (font_matrix_v);
-  cairo_text_extents_t *extents;
+  cairo_matrix_t       *font_matrix = RVAL2CRMATRIX (font_matrix_v);
+  cairo_text_extents_t  extents;
   cairo_status_t        status;
 
-  extents = malloc (sizeof (cairo_text_extents_t));
-  if (extents)
-    {
-      /* XXX: unwrap glyph array */
-      cairo_font_glyph_extents (_SELF, font_matrix, NULL, 0, extents);
-      return Data_Wrap_Struct (rb_cCairo_TextExtents, NULL, free, extents);
-    }
-  else
-    {
-      rb_raise (rb_eNoMemError, "unable to allocate font extents");
-      return Qundef;
-    }
+  /* XXX: unwrap glyph array */
+  cairo_font_glyph_extents (_SELF, font_matrix, NULL, 0, &extents);
+  return CRTEXTEXTENTS2RVAL (&extents);
 }
 #endif
 
Index: packages/cairo/ext/rb_cairo_glyph.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo_glyph.c,v
retrieving revision 1.2
diff -u -p -r1.2 rb_cairo_glyph.c
--- packages/cairo/ext/rb_cairo_glyph.c	9 Sep 2005 11:28:02 -0000	1.2
+++ packages/cairo/ext/rb_cairo_glyph.c	10 Sep 2005 08:06:20 -0000
@@ -11,43 +11,47 @@
 
 #include "rb_cairo.h"
 
-#define _SELF  ((cairo_glyph_t *)DATA_PTR(self))
+VALUE rb_cCairo_Glyph;
+
+#define _SELF  (RVAL2CRGLYPH(self))
 
 cairo_glyph_t *
-rb_v_to_cairo_glyph_t (VALUE value)
+rb_cairo_glyph_from_ruby_object (VALUE obj)
 {
   cairo_glyph_t *xform;
-  if (CLASS_OF (value) != rb_cCairo_Glyph)
+  if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_Glyph)))
     {
       rb_raise (rb_eTypeError, "not a cairo glyph");
     }
-  Data_Get_Struct (value, cairo_glyph_t, xform);
+  Data_Get_Struct (obj, cairo_glyph_t, xform);
   return xform;
 }
 
-void
-rb_free_glyph (void *ptr)
+VALUE
+rb_cairo_glyph_to_ruby_object (cairo_glyph_t *glyph)
 {
-  if (ptr)
+  if (glyph)
     {
-      free ((cairo_glyph_t *) ptr);
+      cairo_glyph_t *new_glyph = ALLOC (cairo_glyph_t);
+      *new_glyph = *glyph;
+      return Data_Wrap_Struct (rb_cCairo_Glyph, NULL, -1, new_glyph);
+    }
+  else
+    {
+      return Qnil;
     }
 }
 
 static    VALUE
 rb_cairo_glyph_new (VALUE klass)
 {
-  cairo_glyph_t *glyph;
-  VALUE glyph_v;
-
-  glyph = ALLOC (cairo_glyph_t);
-  glyph->index = 0;
-  glyph->x     = 0.0;
-  glyph->y     = 0.0;
+  cairo_glyph_t glyph;
 
-  glyph_v = Data_Wrap_Struct (rb_cCairo_Glyph, NULL, free, glyph);
+  glyph.index = 0;
+  glyph.x     = 0.0;
+  glyph.y     = 0.0;
 
-  return glyph_v;
+  return CRGLYPH2RVAL (&glyph);
 }
 
 static    VALUE
Index: packages/cairo/ext/rb_cairo_matrix.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo_matrix.c,v
retrieving revision 1.6
diff -u -p -r1.6 rb_cairo_matrix.c
--- packages/cairo/ext/rb_cairo_matrix.c	9 Sep 2005 11:28:02 -0000	1.6
+++ packages/cairo/ext/rb_cairo_matrix.c	10 Sep 2005 08:06:20 -0000
@@ -11,41 +11,34 @@
 
 #include "rb_cairo.h"
 
-#define _SELF  (DATA_PTR(self))
+VALUE rb_cCairo_Matrix;
+
+#define _SELF  (RVAL2CRMATRIX(self))
 
 cairo_matrix_t *
-rb_v_to_cairo_matrix_t (VALUE value)
+rb_cairo_matrix_from_ruby_object (VALUE obj)
 {
   cairo_matrix_t *xform;
-  if (CLASS_OF (value) != rb_cCairo_Matrix)
+  if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_Matrix)))
     {
       rb_raise (rb_eTypeError, "not a cairo matrix");
     }
-  Data_Get_Struct (value, cairo_matrix_t, xform);
+  Data_Get_Struct (obj, cairo_matrix_t, xform);
   return xform;
 }
 
-void
-rb_free_matrix (void *ptr)
-{
-  if (ptr)
-    {
-      free (ptr);
-    }
-}
-
-
 VALUE
-rb_cairo_matrix_wrap (cairo_matrix_t *matrix)
+rb_cairo_matrix_to_ruby_object (cairo_matrix_t *matrix)
 {
   if (matrix)
     {
-      return Data_Wrap_Struct (rb_cCairo_Matrix, NULL, rb_free_matrix, matrix);
+      cairo_matrix_t *new_matrix = ALLOC (cairo_matrix_t);
+      *new_matrix = *matrix;
+      return Data_Wrap_Struct (rb_cCairo_Matrix, NULL, -1, new_matrix);
     }
   else
     {
-      rb_raise (rb_eArgError, "unable to wrap matrix");
-      return Qundef;
+      return Qnil;
     }
 }  
 
@@ -75,26 +68,16 @@ float_array (double   *values,
 static    VALUE
 rb_cairo_matrix_new (VALUE klass)
 {
-  cairo_matrix_t *matrix;
+  cairo_matrix_t matrix;
   
-  matrix = malloc (sizeof (cairo_matrix_t));
-
-  if (matrix)
-    {
-      cairo_matrix_init_identity (matrix);
-      return rb_cairo_matrix_wrap (matrix);
-    }
-  else
-    {
-      rb_raise (rb_eNoMemError, "unable to allocate matrix");
-      return Qundef;
-    }
+  cairo_matrix_init_identity (&matrix);
+  return CRMATRIX2RVAL (&matrix);
 }
 
 static    VALUE
 rb_cairo_matrix_copy (VALUE self, VALUE other)
 {
-  cairo_matrix_t *matrix = rb_v_to_cairo_matrix_t (other);
+  cairo_matrix_t *matrix = RVAL2CRMATRIX (other);
   
   cairo_matrix_init (_SELF,
                      matrix->xx, matrix->yx,
@@ -183,8 +166,8 @@ rb_cairo_matrix_multiply (VALUE self,
                           VALUE b)
 {
   cairo_matrix_multiply (_SELF,
-                         rb_v_to_cairo_matrix_t (a),
-                         rb_v_to_cairo_matrix_t (b));
+                         RVAL2CRMATRIX (a),
+                         RVAL2CRMATRIX (b));
   return self;
 }
 
Index: packages/cairo/ext/rb_cairo_pattern.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo_pattern.c,v
retrieving revision 1.4
diff -u -p -r1.4 rb_cairo_pattern.c
--- packages/cairo/ext/rb_cairo_pattern.c	9 Sep 2005 11:28:02 -0000	1.4
+++ packages/cairo/ext/rb_cairo_pattern.c	10 Sep 2005 08:06:20 -0000
@@ -10,21 +10,23 @@
 
 #include "rb_cairo.h"
 
-#define _SELF  (DATA_PTR(self))
+VALUE rb_cCairo_Pattern;
+
+#define _SELF  (RVAL2CRPATTERN(self))
 
 cairo_pattern_t *
-rb_v_to_cairo_pattern_t (VALUE value)
+rb_cairo_pattern_from_ruby_object (VALUE obj)
 {
   cairo_pattern_t *pattern;
-  if (CLASS_OF (value) != rb_cCairo_Pattern)
+  if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_Pattern)))
     {
       rb_raise (rb_eTypeError, "not a cairo pattern");
     }
-  Data_Get_Struct (value, cairo_pattern_t, pattern);
+  Data_Get_Struct (obj, cairo_pattern_t, pattern);
   return pattern;
 }
 
-void
+static void
 rb_free_pattern (void *ptr)
 {
   if (ptr)
@@ -33,9 +35,8 @@ rb_free_pattern (void *ptr)
     }
 }
 
-#if 0
 VALUE
-rb_cairo_pattern_wrap (cairo_pattern_t *pat)
+rb_cairo_pattern_to_ruby_object (cairo_pattern_t *pat)
 {
   if (pat)
     {
@@ -44,11 +45,11 @@ rb_cairo_pattern_wrap (cairo_pattern_t *
     }
   else
     {
-      rb_raise (rb_eNoMemError, "unable to wrap pattern");
-      return Qundef;
+      return Qnil;
     }
-}  
+}
 
+#if 0
 VALUE
 rb_cairo_pattern_create_linear (VALUE klass,
                                 VALUE x0_v, VALUE y0_v,
Index: packages/cairo/ext/rb_cairo_surface.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo_surface.c,v
retrieving revision 1.4
diff -u -p -r1.4 rb_cairo_surface.c
--- packages/cairo/ext/rb_cairo_surface.c	9 Sep 2005 11:28:02 -0000	1.4
+++ packages/cairo/ext/rb_cairo_surface.c	10 Sep 2005 08:06:20 -0000
@@ -23,7 +23,9 @@
 #include <cairo-ps.h>
 #endif
 
-#define _SELF  (DATA_PTR(self))
+VALUE rb_cCairo_Surface;
+
+#define _SELF  (RVAL2CRSURFACE(self))
 
 static cairo_status_t
 rfile_write (void         *closure,
@@ -49,20 +51,19 @@ rfile_destroy_closure (void *closure)
   fptr = closure;
 }
 
-
 cairo_surface_t *
-rb_v_to_cairo_surface_t (VALUE value)
+rb_cairo_surface_from_ruby_object (VALUE obj)
 {
   cairo_surface_t *surface;
-  if (CLASS_OF (value) != rb_cCairo_Surface)
+  if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_Surface)))
     {
       rb_raise (rb_eTypeError, "not a cairo surface");
     }
-  Data_Get_Struct (value, cairo_surface_t, surface);
+  Data_Get_Struct (obj, cairo_surface_t, surface);
   return surface;
 }
 
-void
+static void
 rb_free_surface (void *ptr)
 {
   if (ptr)
@@ -71,6 +72,20 @@ rb_free_surface (void *ptr)
     }
 }
 
+VALUE
+rb_cairo_surface_to_ruby_object (cairo_surface_t *surface)
+{
+  if (surface)
+    {
+      cairo_surface_reference (surface);
+      return Data_Wrap_Struct (rb_cCairo_Surface, NULL, rb_free_surface, surface);
+    }
+  else
+    {
+      return Qnil;
+    }
+}
+
 static    VALUE
 rb_cairo_surface_new (VALUE klass,
                       VALUE format_v,
@@ -102,8 +117,9 @@ rb_cairo_surface_new (VALUE klass,
     cairo_image_surface_create ((cairo_format_t) format, width, height);
   if (surface)
     {
-      return Data_Wrap_Struct (rb_cCairo_Surface, NULL, rb_free_surface,
-                               surface);
+      VALUE rb_surface = CRSURFACE2RVAL (surface);
+      cairo_surface_destroy (surface);
+      return rb_surface;
     }
   else
     {
Index: packages/cairo/ext/rb_cairo_text_extents.c
===================================================================
RCS file: /cvs/cairo/rcairo/packages/cairo/ext/rb_cairo_text_extents.c,v
retrieving revision 1.2
diff -u -p -r1.2 rb_cairo_text_extents.c
--- packages/cairo/ext/rb_cairo_text_extents.c	9 Sep 2005 11:28:02 -0000	1.2
+++ packages/cairo/ext/rb_cairo_text_extents.c	10 Sep 2005 08:06:20 -0000
@@ -8,48 +8,52 @@
  *
  */
 
-#define _SELF  ((cairo_text_extents_t *)DATA_PTR(self))
-
 #include "rb_cairo.h"
 
+VALUE rb_cCairo_TextExtents;
+
+#define _SELF  (RVAL2CRTEXTEXTENTS(self))
+
 cairo_text_extents_t *
-rb_v_to_cairo_text_extents_t (VALUE value)
+rb_cairo_text_extents_from_ruby_object (VALUE obj)
 {
   cairo_text_extents_t *xform;
-  if (CLASS_OF (value) != rb_cCairo_TextExtents)
+  if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_TextExtents)))
     {
       rb_raise (rb_eTypeError, "not a cairo text_extents");
     }
-  Data_Get_Struct (value, cairo_text_extents_t, xform);
+  Data_Get_Struct (obj, cairo_text_extents_t, xform);
   return xform;
 }
 
-void
-rb_free_text_extents (void *ptr)
+VALUE
+rb_cairo_text_extents_to_ruby_object (cairo_text_extents_t *extents)
 {
-  if (ptr)
+  if (extents)
     {
-      free ((cairo_text_extents_t *) ptr);
+      cairo_text_extents_t *new_extents = ALLOC (cairo_text_extents_t);
+      *new_extents = *extents;
+      return Data_Wrap_Struct (rb_cCairo_TextExtents, NULL, -1, new_extents);
+    }
+  else
+    {
+      return Qnil;
     }
 }
 
 static    VALUE
 rb_cairo_text_extents_new (VALUE klass)
 {
-  cairo_text_extents_t *text_extents;
-  VALUE text_extents_v;
-
-  text_extents = ALLOC (cairo_text_extents_t);
-  text_extents->x_bearing = 0.0;
-  text_extents->y_bearing = 0.0;
-  text_extents->width     = 0.0;
-  text_extents->height    = 0.0;
-  text_extents->x_advance = 0.0;
-  text_extents->y_advance = 0.0;
+  cairo_text_extents_t text_extents;
 
-  text_extents_v = Data_Wrap_Struct (rb_cCairo_TextExtents, NULL, free, text_extents);
+  text_extents.x_bearing = 0.0;
+  text_extents.y_bearing = 0.0;
+  text_extents.width     = 0.0;
+  text_extents.height    = 0.0;
+  text_extents.x_advance = 0.0;
+  text_extents.y_advance = 0.0;
 
-  return text_extents_v;
+  return CRTEXTEXTENTS2RVAL (&text_extents);
 }
 
 static    VALUE


More information about the cairo mailing list