[gjs: 28/45] [cairo] Improve reference counting



commit 189f77ac2b45e3cab1f725f6cd653bf330258b36
Author: Johan Dahlin <johan gnome org>
Date:   Fri Feb 26 18:57:00 2010 -0300

    [cairo] Improve reference counting
    
    Surface/patterns should refcount the cairo structures.
    Also add documentation and functions to create a pattern/structure
    given a cairo structure.

 modules/cairo-pattern.c |  119 +++++++++++++++++++++++++++++++++++------------
 modules/cairo-private.h |    4 ++
 modules/cairo-surface.c |  115 ++++++++++++++++++++++++++++++++++-----------
 3 files changed, 179 insertions(+), 59 deletions(-)
---
diff --git a/modules/cairo-pattern.c b/modules/cairo-pattern.c
index dc3ecf6..568fa70 100644
--- a/modules/cairo-pattern.c
+++ b/modules/cairo-pattern.c
@@ -42,32 +42,6 @@ gjs_cairo_pattern_finalize(JSContext *context,
     g_slice_free(GjsCairoPattern, priv);
 }
 
-/**
- * gjs_cairo_pattern_construct:
- * @context: the context
- * @object: object to construct
- * @pattern: cairo_pattern to attach to the object
- *
- * Constructs a pattern wrapper giving an empty JSObject and a
- * cairo pattern. A reference to @pattern will be taken.
- */
-void
-gjs_cairo_pattern_construct(JSContext       *context,
-                            JSObject        *obj,
-                            cairo_pattern_t *pattern)
-{
-    GjsCairoPattern *priv;
-
-    priv = g_slice_new0(GjsCairoPattern);
-
-    g_assert(priv_from_js(context, obj) == NULL);
-    JS_SetPrivate(context, obj, priv);
-
-    priv->context = context;
-    priv->object = obj;
-    priv->pattern = cairo_pattern_reference(pattern);
-}
-
 /* Properties */
 static JSPropertySpec gjs_cairo_pattern_proto_props[] = {
     { NULL }
@@ -108,24 +82,109 @@ static JSFunctionSpec gjs_cairo_pattern_proto_funcs[] = {
 };
 
 /* Public API */
+
+/**
+ * gjs_cairo_pattern_construct:
+ * @context: the context
+ * @object: object to construct
+ * @pattern: cairo_pattern to attach to the object
+ *
+ * Constructs a pattern wrapper giving an empty JSObject and a
+ * cairo pattern. A reference to @pattern will be taken.
+ *
+ * This is mainly used for subclasses where object is already created.
+ */
+void
+gjs_cairo_pattern_construct(JSContext       *context,
+                            JSObject        *object,
+                            cairo_pattern_t *pattern)
+{
+    GjsCairoPattern *priv;
+
+    g_return_if_fail(context != NULL);
+    g_return_if_fail(object != NULL);
+    g_return_if_fail(pattern != NULL);
+
+    priv = g_slice_new0(GjsCairoPattern);
+
+    g_assert(priv_from_js(context, object) == NULL);
+    JS_SetPrivate(context, object, priv);
+
+    priv->context = context;
+    priv->object = object;
+    priv->pattern = cairo_pattern_reference(pattern);
+}
+
+/**
+ * gjs_cairo_pattern_finalize:
+ * @context: the context
+ * @object: object to finalize
+ *
+ * Destroys the resources assoicated with a pattern wrapper.
+ *
+ * This is mainly used for subclasses.
+ */
+
 void
 gjs_cairo_pattern_finalize_pattern(JSContext *context,
-                                   JSObject  *obj)
+                                   JSObject  *object)
 {
     g_return_if_fail(context != NULL);
-    g_return_if_fail(obj != NULL);
+    g_return_if_fail(object != NULL);
+
+    gjs_cairo_pattern_finalize(context, object);
+}
+
+/**
+ * gjs_cairo_pattern_from_pattern:
+ * @context: the context
+ * @pattern: cairo_pattern to attach to the object
+ *
+ * Constructs a pattern wrapper given cairo pattern.
+ * A reference to @pattern will be taken.
+ *
+ */
+JSObject *
+gjs_cairo_pattern_from_pattern(JSContext       *context,
+                               cairo_pattern_t *pattern)
+{
+    JSObject *object;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(pattern != NULL, NULL);
+
+    object = JS_NewObject(context, &gjs_cairo_pattern_class, NULL, NULL);
+    if (!object) {
+        gjs_throw(context, "failed to create surface");
+        return NULL;
+    }
 
-    gjs_cairo_pattern_finalize(context, obj);
+    gjs_cairo_pattern_construct(context, object, pattern);
+
+    return object;
 }
 
+/**
+ * gjs_cairo_pattern_get_pattern:
+ * @context: the context
+ * @object: pattern wrapper
+ *
+ * Returns: the pattern attaches to the wrapper.
+ *
+ */
 cairo_pattern_t *
 gjs_cairo_pattern_get_pattern(JSContext *context,
-                              JSObject *object)
+                              JSObject  *object)
 {
     GjsCairoPattern *priv;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(object != NULL, NULL);
+
     priv = JS_GetPrivate(context, object);
     if (priv == NULL)
         return NULL;
+
     return priv->pattern;
 }
 
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index 4a2c25c..8dca928 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -58,6 +58,8 @@ void             gjs_cairo_surface_construct            (JSContext       *contex
                                                          cairo_surface_t *surface);
 void             gjs_cairo_surface_finalize_surface     (JSContext       *context,
                                                          JSObject        *object);
+JSObject *       gjs_cairo_surface_from_surface         (JSContext       *context,
+                                                         cairo_surface_t *surface);
 cairo_surface_t* gjs_cairo_surface_get_surface          (JSContext       *context,
                                                          JSObject        *object);
 
@@ -109,6 +111,8 @@ void             gjs_cairo_pattern_construct            (JSContext       *contex
                                                          cairo_pattern_t *pattern);
 void             gjs_cairo_pattern_finalize_pattern     (JSContext       *context,
                                                          JSObject        *object);
+JSObject*        gjs_cairo_pattern_from_pattern         (JSContext       *context,
+                                                         cairo_pattern_t *pattern);
 cairo_pattern_t* gjs_cairo_pattern_get_pattern          (JSContext       *context,
                                                          JSObject        *object);
 
diff --git a/modules/cairo-surface.c b/modules/cairo-surface.c
index 23e829f..6d3936d 100644
--- a/modules/cairo-surface.c
+++ b/modules/cairo-surface.c
@@ -42,32 +42,6 @@ gjs_cairo_surface_finalize(JSContext *context,
     g_slice_free(GjsCairoSurface, priv);
 }
 
-/**
- * gjs_cairo_surface_construct:
- * @context: the context
- * @object: object to construct
- * @surface: cairo_surface to attach to the object
- *
- * Constructs a surface wrapper giving an empty JSObject and a
- * cairo surface. A reference to @surface will be taken.
- */
-void
-gjs_cairo_surface_construct(JSContext       *context,
-                            JSObject        *object,
-                            cairo_surface_t *surface)
-{
-    GjsCairoSurface *priv;
-
-    priv = g_slice_new0(GjsCairoSurface);
-
-    g_assert(priv_from_js(context, object) == NULL);
-    JS_SetPrivate(context, object, priv);
-
-    priv->context = context;
-    priv->object = object;
-    priv->surface = cairo_surface_reference(surface);
-}
-
 /* Properties */
 static JSPropertySpec gjs_cairo_surface_proto_props[] = {
     { NULL }
@@ -139,21 +113,104 @@ static JSFunctionSpec gjs_cairo_surface_proto_funcs[] = {
 };
 
 /* Public API */
+
+/**
+ * gjs_cairo_surface_construct:
+ * @context: the context
+ * @object: object to construct
+ * @surface: cairo_surface to attach to the object
+ *
+ * Constructs a surface wrapper giving an empty JSObject and a
+ * cairo surface. A reference to @surface will be taken.
+ *
+ * This is mainly used for subclasses where object is already created.
+ */
+void
+gjs_cairo_surface_construct(JSContext       *context,
+                            JSObject        *object,
+                            cairo_surface_t *surface)
+{
+    GjsCairoSurface *priv;
+
+    g_return_if_fail(context != NULL);
+    g_return_if_fail(object != NULL);
+    g_return_if_fail(surface != NULL);
+
+    priv = g_slice_new0(GjsCairoSurface);
+
+    g_assert(priv_from_js(context, object) == NULL);
+    JS_SetPrivate(context, object, priv);
+
+    priv->context = context;
+    priv->object = object;
+    priv->surface = cairo_surface_reference(surface);
+}
+
+/**
+ * gjs_cairo_surface_finalize:
+ * @context: the context
+ * @object: object to finalize
+ *
+ * Destroys the resources assoicated with a surface wrapper.
+ *
+ * This is mainly used for subclasses.
+ */
 void
 gjs_cairo_surface_finalize_surface(JSContext *context,
-                                   JSObject  *obj)
+                                   JSObject  *object)
 {
     g_return_if_fail(context != NULL);
-    g_return_if_fail(obj != NULL);
+    g_return_if_fail(object != NULL);
 
-    gjs_cairo_surface_finalize(context, obj);
+    gjs_cairo_surface_finalize(context, object);
 }
 
+/**
+ * gjs_cairo_surface_from_surface:
+ * @context: the context
+ * @surface: cairo_surface to attach to the object
+ *
+ * Constructs a surface wrapper given cairo surface.
+ * A reference to @surface will be taken.
+ *
+ */
+JSObject *
+gjs_cairo_surface_from_surface(JSContext       *context,
+                               cairo_surface_t *surface)
+{
+    JSObject *object;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(surface != NULL, NULL);
+
+    object = JS_NewObject(context, &gjs_cairo_surface_class, NULL, NULL);
+    if (!object) {
+        gjs_throw(context, "failed to create surface");
+        return NULL;
+    }
+
+    gjs_cairo_surface_construct(context, object, surface);
+
+    return object;
+}
+
+/**
+ * gjs_cairo_surface_get_surface:
+ * @context: the context
+ * @object: surface wrapper
+ *
+ * Returns: the surface attaches to the wrapper.
+ *
+ */
 cairo_surface_t *
 gjs_cairo_surface_get_surface(JSContext *context,
                               JSObject *object)
 {
     GjsCairoSurface *priv;
+
+    g_return_val_if_fail(context != NULL, NULL);
+    g_return_val_if_fail(object != NULL, NULL);
+
     priv = JS_GetPrivate(context, object);
     if (priv == NULL)
         return NULL;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]