[libgda] GdaPStmt: ported to G_DECLARE/G_DEFINE



commit 58a12d44ce71ad4a8b188e21bfef9a106cdc305d
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Wed Feb 27 16:49:04 2019 -0600

    GdaPStmt: ported to G_DECLARE/G_DEFINE

 libgda/providers-support/gda-pstmt.c               | 238 +++++++++------------
 libgda/providers-support/gda-pstmt.h               |  19 +-
 .../skel-implementation/capi/gda-capi-provider.c   |   2 +-
 3 files changed, 105 insertions(+), 154 deletions(-)
---
diff --git a/libgda/providers-support/gda-pstmt.c b/libgda/providers-support/gda-pstmt.c
index 748b2f6ff..c45b616c2 100644
--- a/libgda/providers-support/gda-pstmt.c
+++ b/libgda/providers-support/gda-pstmt.c
@@ -24,14 +24,9 @@
 #include "gda-pstmt.h"
 #include <string.h>
 
-static void gda_pstmt_class_init (GdaPStmtClass *klass);
-static void gda_pstmt_init       (GdaPStmt *pstmt, GdaPStmtClass *klass);
 static void gda_pstmt_dispose    (GObject *object);
-static void gda_pstmt_finalize   (GObject *object);
 
-static GObjectClass *parent_class = NULL;
-
-struct _GdaPStmtPrivate {
+typedef struct {
        GRecMutex mutex;
        GWeakRef gda_stmt_ref; /* holds a weak reference to #GdaStatement, or %NULL */
        gchar        *sql; /* actual SQL code used for this prepared statement, mem freed by GdaPStmt */
@@ -41,64 +36,31 @@ struct _GdaPStmtPrivate {
        gint          ncols;
        GType        *types; /* array of ncols types */
        GSList       *tmpl_columns; /* list of #GdaColumn objects which data models created from this prep. 
statement can copy */
-};
-
-/**
- * gda_pstmt_get_type:
- *
- * Returns: the #GType of GdaPStmt.
- */
-GType
-gda_pstmt_get_type (void)
-{
-       static GType type = 0;
+} GdaPStmtPrivate;
 
-       if (G_UNLIKELY (type == 0)) {
-               static GMutex registering;
-               static const GTypeInfo info = {
-                       sizeof (GdaPStmtClass),
-                       (GBaseInitFunc) NULL,
-                       (GBaseFinalizeFunc) NULL,
-                       (GClassInitFunc) gda_pstmt_class_init,
-                       NULL,
-                       NULL,
-                       sizeof (GdaPStmt),
-                       0,
-                       (GInstanceInitFunc) gda_pstmt_init,
-                       0
-               };
-
-               g_mutex_lock (&registering);
-               if (type == 0)
-                       type = g_type_register_static (G_TYPE_OBJECT, "GdaPStmt", &info, 
G_TYPE_FLAG_ABSTRACT);
-               g_mutex_unlock (&registering);
-       }
-       return type;
-}
+G_DEFINE_TYPE_WITH_PRIVATE (GdaPStmt, gda_pstmt, G_TYPE_OBJECT)
 
 static void 
 gda_pstmt_class_init (GdaPStmtClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
-       parent_class = g_type_class_peek_parent (klass);
 
        /* virtual functions */
        object_class->dispose = gda_pstmt_dispose;
-       object_class->finalize = gda_pstmt_finalize;
 }
 
 static void
-gda_pstmt_init (GdaPStmt *pstmt, G_GNUC_UNUSED GdaPStmtClass *klass)
+gda_pstmt_init (GdaPStmt *pstmt)
 {
        g_return_if_fail (GDA_IS_PSTMT (pstmt));
-       pstmt->priv = g_new0 (GdaPStmtPrivate, 1);
-       g_rec_mutex_init (& pstmt->priv->mutex);
-       g_weak_ref_init (&pstmt->priv->gda_stmt_ref, NULL);
-       pstmt->priv->sql = NULL;
-       pstmt->priv->param_ids = NULL;
-       pstmt->priv->ncols = -1;
-       pstmt->priv->types = NULL;
-       pstmt->priv->tmpl_columns = NULL;
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       g_rec_mutex_init (& priv->mutex);
+       g_weak_ref_init (&priv->gda_stmt_ref, NULL);
+       priv->sql = NULL;
+       priv->param_ids = NULL;
+       priv->ncols = -1;
+       priv->types = NULL;
+       priv->tmpl_columns = NULL;
 }
 
 /*
@@ -107,7 +69,8 @@ gda_pstmt_init (GdaPStmt *pstmt, G_GNUC_UNUSED GdaPStmtClass *klass)
 static void
 gda_stmt_reset_cb (GdaStatement *stmt, GdaPStmt *pstmt)
 {
-       g_rec_mutex_lock (& pstmt->priv->mutex);
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       g_rec_mutex_lock (& priv->mutex);
        if (stmt) {
                g_object_ref (stmt);
                g_signal_handlers_disconnect_by_func (G_OBJECT (stmt),
@@ -115,7 +78,7 @@ gda_stmt_reset_cb (GdaStatement *stmt, GdaPStmt *pstmt)
                g_object_unref (stmt);
        }
        else {
-               GdaStatement *stm = g_weak_ref_get (& pstmt->priv->gda_stmt_ref);
+               GdaStatement *stm = g_weak_ref_get (& priv->gda_stmt_ref);
                if (stm) {
                        g_signal_handlers_disconnect_by_func (G_OBJECT (stm),
                                                              G_CALLBACK (gda_stmt_reset_cb), pstmt);
@@ -123,54 +86,46 @@ gda_stmt_reset_cb (GdaStatement *stmt, GdaPStmt *pstmt)
                }
        }
 
-       g_weak_ref_set (& pstmt->priv->gda_stmt_ref, NULL);
-       g_rec_mutex_unlock (& pstmt->priv->mutex);
+       g_weak_ref_set (& priv->gda_stmt_ref, NULL);
+       g_rec_mutex_unlock (& priv->mutex);
 }
 
 static void
 gda_pstmt_dispose (GObject *object)
 {
        GdaPStmt *pstmt = (GdaPStmt *) object;
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
 
        gda_stmt_reset_cb (NULL, pstmt);
 
-       /* chain to parent class */
-       parent_class->dispose (object);
-}
-
-static void
-gda_pstmt_finalize (GObject *object)
-{
-       GdaPStmt *pstmt = (GdaPStmt *) object;
-
        /* free memory */
-       g_rec_mutex_clear (& pstmt->priv->mutex);
-       g_weak_ref_clear (&pstmt->priv->gda_stmt_ref);
+       g_rec_mutex_clear (& priv->mutex);
+       g_weak_ref_clear (&priv->gda_stmt_ref);
 
-       if (pstmt->priv->sql != NULL) {
-               g_free (pstmt->priv->sql);
-               pstmt->priv->sql = NULL;
+       if (priv->sql != NULL) {
+               g_free (priv->sql);
+               priv->sql = NULL;
        }
-       if (pstmt->priv->param_ids != NULL) {
-               g_slist_foreach (pstmt->priv->param_ids, (GFunc) g_free, NULL);
-               g_slist_free (pstmt->priv->param_ids);
-               pstmt->priv->param_ids = NULL;
+       if (priv->param_ids != NULL) {
+               g_slist_foreach (priv->param_ids, (GFunc) g_free, NULL);
+               g_slist_free (priv->param_ids);
+               priv->param_ids = NULL;
        }
-       if (pstmt->priv->types != NULL) {
-               g_free (pstmt->priv->types);
-               pstmt->priv->types = NULL;
+       if (priv->types != NULL) {
+               g_free (priv->types);
+               priv->types = NULL;
        }
-       if (pstmt->priv->tmpl_columns != NULL) {
-               g_slist_foreach (pstmt->priv->tmpl_columns, (GFunc) g_object_unref, NULL);
-               g_slist_free (pstmt->priv->tmpl_columns);
-               pstmt->priv->tmpl_columns = NULL;
+       if (priv->tmpl_columns != NULL) {
+               g_slist_foreach (priv->tmpl_columns, (GFunc) g_object_unref, NULL);
+               g_slist_free (priv->tmpl_columns);
+               priv->tmpl_columns = NULL;
        }
-       g_free (pstmt->priv);
 
        /* chain to parent class */
-       parent_class->finalize (object);
+       G_OBJECT_CLASS (gda_pstmt_parent_class)->dispose (object);
 }
 
+
 /**
  * gda_pstmt_set_gda_statement:
  * @pstmt: a #GdaPStmt object
@@ -183,25 +138,26 @@ gda_pstmt_set_gda_statement (GdaPStmt *pstmt, GdaStatement *stmt)
 {
        g_return_if_fail (GDA_IS_PSTMT (pstmt));
        g_return_if_fail (!stmt || GDA_IS_STATEMENT (stmt));
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
 
-       g_rec_mutex_lock (& pstmt->priv->mutex);
+       g_rec_mutex_lock (& priv->mutex);
        g_object_ref (stmt);
 
        GdaStatement *estmt;
-       estmt = g_weak_ref_get (& pstmt->priv->gda_stmt_ref);
+       estmt = g_weak_ref_get (& priv->gda_stmt_ref);
        if (estmt == stmt) {
                if (estmt)
                        g_object_unref (estmt);
-               g_rec_mutex_unlock (& pstmt->priv->mutex);
+               g_rec_mutex_unlock (& priv->mutex);
                return;
        }
 
        gda_stmt_reset_cb (NULL, pstmt);
 
-       g_weak_ref_set (& pstmt->priv->gda_stmt_ref, stmt);
+       g_weak_ref_set (& priv->gda_stmt_ref, stmt);
        g_signal_connect (G_OBJECT (stmt), "reset", G_CALLBACK (gda_stmt_reset_cb), pstmt);
        g_object_unref (stmt);
-       g_rec_mutex_unlock (& pstmt->priv->mutex);
+       g_rec_mutex_unlock (& priv->mutex);
 }
 
 /**
@@ -217,45 +173,47 @@ gda_pstmt_copy_contents (GdaPStmt *src, GdaPStmt *dest)
        GSList *list;
        g_return_if_fail (GDA_IS_PSTMT (src));
        g_return_if_fail (GDA_IS_PSTMT (dest));
-
-       g_rec_mutex_lock (& src->priv->mutex);
-       g_rec_mutex_lock (& dest->priv->mutex);
-
-       g_free (dest->priv->sql);
-       dest->priv->sql = NULL;
-       if (src->priv->sql)
-               dest->priv->sql = g_strdup (src->priv->sql);
-       if (dest->priv->param_ids) {
-               g_slist_foreach (dest->priv->param_ids, (GFunc) g_free, NULL);
-               g_slist_free (dest->priv->param_ids);
-               dest->priv->param_ids = NULL;
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (src);
+       GdaPStmtPrivate *dpriv = gda_pstmt_get_instance_private (dest);
+
+       g_rec_mutex_lock (& priv->mutex);
+       g_rec_mutex_lock (& dpriv->mutex);
+
+       g_free (dpriv->sql);
+       dpriv->sql = NULL;
+       if (priv->sql)
+               dpriv->sql = g_strdup (priv->sql);
+       if (dpriv->param_ids) {
+               g_slist_foreach (dpriv->param_ids, (GFunc) g_free, NULL);
+               g_slist_free (dpriv->param_ids);
+               dpriv->param_ids = NULL;
        }
-       for (list = src->priv->param_ids; list; list = list->next)
-               dest->priv->param_ids = g_slist_append (dest->priv->param_ids, g_strdup ((gchar *) 
list->data));
-       dest->priv->ncols = src->priv->ncols;
-       if (dest->priv->types != NULL) {
-               g_free (dest->priv->types);
+       for (list = priv->param_ids; list; list = list->next)
+               dpriv->param_ids = g_slist_append (dpriv->param_ids, g_strdup ((gchar *) list->data));
+       dpriv->ncols = priv->ncols;
+       if (dpriv->types != NULL) {
+               g_free (dpriv->types);
        }
-       dest->priv->types = NULL;
-       if (src->priv->types) {
-               dest->priv->types = g_new (GType, dest->priv->ncols);
-               memcpy (dest->priv->types, src->priv->types, sizeof (GType) * dest->priv->ncols); /* 
Flawfinder: ignore */
+       dpriv->types = NULL;
+       if (priv->types) {
+               dpriv->types = g_new (GType, dpriv->ncols);
+               memcpy (dpriv->types, priv->types, sizeof (GType) * dpriv->ncols); /* Flawfinder: ignore */
        }
-       if (src->priv->tmpl_columns) {
+       if (priv->tmpl_columns) {
                GSList *list;
-               for (list = src->priv->tmpl_columns; list; list = list->next)
-                       dest->priv->tmpl_columns = g_slist_append (dest->priv->tmpl_columns,
+               for (list = priv->tmpl_columns; list; list = list->next)
+                       dpriv->tmpl_columns = g_slist_append (dpriv->tmpl_columns,
                                                             gda_column_copy (GDA_COLUMN (list->data)));
        }
        GdaStatement *stmt;
-       stmt = g_weak_ref_get (& src->priv->gda_stmt_ref);
+       stmt = g_weak_ref_get (& priv->gda_stmt_ref);
        if (stmt) {
                gda_pstmt_set_gda_statement (dest, stmt);
                g_object_unref (stmt);
        }
 
-       g_rec_mutex_unlock (& src->priv->mutex);
-       g_rec_mutex_unlock (& dest->priv->mutex);
+       g_rec_mutex_unlock (& priv->mutex);
+       g_rec_mutex_unlock (& dpriv->mutex);
 
 }
 
@@ -274,10 +232,11 @@ GdaStatement *
 gda_pstmt_get_gda_statement (GdaPStmt *pstmt)
 {
        g_return_val_if_fail (GDA_IS_PSTMT (pstmt), NULL);
-       g_rec_mutex_lock (& pstmt->priv->mutex);
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       g_rec_mutex_lock (& priv->mutex);
        GdaStatement *stmt;
-       stmt = g_weak_ref_get (& pstmt->priv->gda_stmt_ref);
-       g_rec_mutex_unlock (& pstmt->priv->mutex);
+       stmt = g_weak_ref_get (& priv->gda_stmt_ref);
+       g_rec_mutex_unlock (& priv->mutex);
        return stmt;
 }
 /**
@@ -285,8 +244,9 @@ gda_pstmt_get_gda_statement (GdaPStmt *pstmt)
  * Actual SQL code used for this prepared statement, mem freed by GdaPStmt
  */
 gchar*
-gda_pstmt_get_sql (GdaPStmt *ps) {
-       return ps->priv->sql;
+gda_pstmt_get_sql (GdaPStmt *pstmt) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       return priv->sql;
 }
 /**
  * gda_pstmt_set_sql:
@@ -294,8 +254,9 @@ gda_pstmt_get_sql (GdaPStmt *ps) {
  * Set SQL code used for this prepared statement, mem freed by GdaPStmt
  */
 void
-gda_pstmt_set_sql (GdaPStmt *ps, const gchar *sql) {
-       ps->priv->sql = g_strdup (sql);
+gda_pstmt_set_sql (GdaPStmt *pstmt, const gchar *sql) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       priv->sql = g_strdup (sql);
 }
 /**
  *gda_pstmt_get_param_ids:
@@ -303,8 +264,9 @@ gda_pstmt_set_sql (GdaPStmt *ps, const gchar *sql) {
  * List of parameters' IDs (as gchar *)
  */
 GSList*
-gda_pstmt_get_param_ids (GdaPStmt *ps) {
-       return ps->priv->param_ids;
+gda_pstmt_get_param_ids (GdaPStmt *pstmt) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       return priv->param_ids;
 }
 
 /**
@@ -313,15 +275,17 @@ gda_pstmt_get_param_ids (GdaPStmt *ps) {
  * List of parameters' IDs (as gchar *), list is stolen
  */
 void
-gda_pstmt_set_param_ids    (GdaPStmt *ps, GSList* params) {
-       ps->priv->param_ids = params;
+gda_pstmt_set_param_ids    (GdaPStmt *pstmt, GSList* params) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       priv->param_ids = params;
 }
 /**
  * gda_pstmt_get_ncols:
  */
 gint
-gda_pstmt_get_ncols (GdaPStmt *ps) {
-       return ps->priv->ncols;
+gda_pstmt_get_ncols (GdaPStmt *pstmt) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       return priv->ncols;
 }
 /**
  * gda_pstmt_get_types:
@@ -330,8 +294,9 @@ gda_pstmt_get_ncols (GdaPStmt *ps) {
  * no be modified
  */
 GType*
-gda_pstmt_get_types (GdaPStmt *ps) {
-       return ps->priv->types;
+gda_pstmt_get_types (GdaPStmt *pstmt) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       return priv->types;
 }
 /**
  * gda_pstmt_set_cols:
@@ -340,9 +305,10 @@ gda_pstmt_get_types (GdaPStmt *ps) {
  * no be modified
  */
 void
-gda_pstmt_set_cols (GdaPStmt *ps, gint ncols, GType *types) {
-       ps->priv->ncols = ncols;
-       ps->priv->types = types;
+gda_pstmt_set_cols (GdaPStmt *pstmt, gint ncols, GType *types) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       priv->ncols = ncols;
+       priv->types = types;
 }
 /**
  *gda_pstmt_get_tmpl_columns:
@@ -351,8 +317,9 @@ gda_pstmt_set_cols (GdaPStmt *ps, gint ncols, GType *types) {
  * prepared statement can copy
  */
 GSList*
-gda_pstmt_get_tmpl_columns (GdaPStmt *ps) {
-       return ps->priv->tmpl_columns;
+gda_pstmt_get_tmpl_columns (GdaPStmt *pstmt) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       return priv->tmpl_columns;
 }
 /**
  *gda_pstmt_set_tmpl_columns:
@@ -362,6 +329,7 @@ gda_pstmt_get_tmpl_columns (GdaPStmt *ps) {
  * free it.
  */
 void
-gda_pstmt_set_tmpl_columns (GdaPStmt *ps, GSList* columns) {
-       ps->priv->tmpl_columns = columns;
+gda_pstmt_set_tmpl_columns (GdaPStmt *pstmt, GSList* columns) {
+       GdaPStmtPrivate *priv = gda_pstmt_get_instance_private (pstmt);
+       priv->tmpl_columns = columns;
 }
diff --git a/libgda/providers-support/gda-pstmt.h b/libgda/providers-support/gda-pstmt.h
index ba66c7171..46bbfeeb3 100644
--- a/libgda/providers-support/gda-pstmt.h
+++ b/libgda/providers-support/gda-pstmt.h
@@ -27,14 +27,8 @@
 G_BEGIN_DECLS
 
 #define GDA_TYPE_PSTMT            (gda_pstmt_get_type())
-#define GDA_PSTMT(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GDA_TYPE_PSTMT, GdaPStmt))
-#define GDA_PSTMT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GDA_TYPE_PSTMT, GdaPStmtClass))
-#define GDA_IS_PSTMT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE(obj, GDA_TYPE_PSTMT))
-#define GDA_IS_PSTMT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GDA_TYPE_PSTMT))
 
-typedef struct _GdaPStmt        GdaPStmt;
-typedef struct _GdaPStmtPrivate GdaPStmtPrivate;
-typedef struct _GdaPStmtClass   GdaPStmtClass;
+G_DECLARE_DERIVABLE_TYPE (GdaPStmt, gda_pstmt, GDA, PSTMT, GObject)
 
 /**
  * GdaPStmt:
@@ -45,16 +39,6 @@ typedef struct _GdaPStmtClass   GdaPStmtClass;
  * @tmpl_columns: (element-type Gda.Column): list of #GdaColumn objects which data models created from this 
prep. statement can copy
  *
  */
-struct _GdaPStmt {
-       GObject       object;
-
-       GdaPStmtPrivate *priv;
-
-       /*< private >*/
-       /* Padding for future expansion */
-       gpointer         _gda_reserved1;
-       gpointer         _gda_reserved2;
-};
 
 struct _GdaPStmtClass {
        GObjectClass  parent_class;
@@ -95,7 +79,6 @@ struct _GdaPStmtClass {
  * if a weak one (which means it will be lost if the #GdaStatement object is destroyed).
  */
 
-GType         gda_pstmt_get_type          (void) G_GNUC_CONST;
 void          gda_pstmt_set_gda_statement (GdaPStmt *pstmt, GdaStatement *stmt);
 void          gda_pstmt_copy_contents     (GdaPStmt *src, GdaPStmt *dest);
 GdaStatement *gda_pstmt_get_gda_statement (GdaPStmt *pstmt);
diff --git a/providers/skel-implementation/capi/gda-capi-provider.c 
b/providers/skel-implementation/capi/gda-capi-provider.c
index c7bb6acc6..4d3c2823a 100644
--- a/providers/skel-implementation/capi/gda-capi-provider.c
+++ b/providers/skel-implementation/capi/gda-capi-provider.c
@@ -1277,7 +1277,7 @@ gda_capi_provider_statement_execute (GdaServerProvider *provider, GdaConnection
                                gtps = (GdaPStmt *) tps;
 
                                /* keep @param_ids to avoid being cleared by gda_pstmt_copy_contents() */
-                               gda_pstmt_set_param_ids (prep_param_ids, gda_pstmt_get_param_ids (gtps));
+                               prep_param_ids = gda_pstmt_get_param_ids (gtps);
                                gda_pstmt_set_param_ids (gtps, NULL);
                                
                                /* actual copy */


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