[libgda] gda-row: ported to G_DECLARE/G_DEFINE



commit 5bdac20391698be2e57fd16ffc60d91108e67584
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Sep 14 13:36:49 2018 -0500

    gda-row: ported to G_DECLARE/G_DEFINE

 libgda/gda-row.c | 238 +++++++++++++++++++++++--------------------------------
 libgda/gda-row.h |  18 +----
 2 files changed, 103 insertions(+), 153 deletions(-)
---
diff --git a/libgda/gda-row.c b/libgda/gda-row.c
index fa5233791..65ed76350 100644
--- a/libgda/gda-row.c
+++ b/libgda/gda-row.c
@@ -11,7 +11,7 @@
  * Copyright (C) 2006 - 2008 Murray Cumming <murrayc murrayc com>
  * Copyright (C) 2007 Leonardo Boshell <lb kmc com co>
  * Copyright (C) 2010 David King <davidk openismus com>
- * Copyright (C) 2011 Daniel Espinosa <despinosa src gnome org>
+ * Copyright (C) 2011, 2018 Daniel Espinosa <esodan gmail com>
  * Copyright (C) 2015 Corentin Noël <corentin elementary io>
  *
  * This library is free software; you can redistribute it and/or
@@ -37,14 +37,15 @@
 
 #define PARENT_TYPE G_TYPE_OBJECT
 
-struct _GdaRowPrivate {
+typedef struct {
   GdaDataModel *model; /* can be NULL */
   guint         model_row;
 
   GValue       *fields; /* GValues */
   GError      **errors; /* GError for each invalid value at the same position */
   guint         nfields;
-};
+} GdaRowPrivate;
+G_DEFINE_TYPE_WITH_PRIVATE (GdaRow, gda_row, G_TYPE_OBJECT)
 
 /* properties */
 enum
@@ -54,8 +55,6 @@ enum
   PROP_MODEL_ROW
 };
 
-static void gda_row_class_init (GdaRowClass *klass);
-static void gda_row_init       (GdaRow *row, GdaRowClass *klass);
 static void gda_row_finalize   (GObject *object);
 static void gda_row_dispose    (GObject *object);
 
@@ -68,14 +67,10 @@ static void gda_row_get_property (GObject *object,
                                  GValue *value,
                                  GParamSpec *pspec);
 
-static GObjectClass *parent_class = NULL;
-
 static void
 gda_row_class_init (GdaRowClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
-       
-       parent_class = g_type_class_peek_parent (klass);
 
        object_class->finalize = gda_row_finalize;
        object_class->dispose = gda_row_dispose;
@@ -101,16 +96,16 @@ gda_row_class_init (GdaRowClass *klass)
 }
 
 static void
-gda_row_init (GdaRow *row, G_GNUC_UNUSED GdaRowClass *klass)
+gda_row_init (GdaRow *row)
 {
        g_return_if_fail (GDA_IS_ROW (row));
-       
-       row->priv = g_new0 (GdaRowPrivate, 1);
-       row->priv->model = NULL;
-  row->priv->model_row = 0;
-       row->priv->fields = NULL;
-       row->priv->errors = NULL;
-       row->priv->nfields = 0;
+
+       GdaRowPrivate *priv = gda_row_get_instance_private (row);
+       priv->model = NULL;
+  priv->model_row = 0;
+       priv->fields = NULL;
+       priv->errors = NULL;
+       priv->nfields = 0;
 }
 
 static void
@@ -120,80 +115,76 @@ gda_row_dispose (GObject *object)
        
        g_return_if_fail (GDA_IS_ROW (row));
                
-       parent_class->finalize (object);
+       G_OBJECT_CLASS (gda_row_parent_class)->dispose (object);
 }
 
 static void
 gda_row_finalize (GObject *object)
 {
        GdaRow *row = (GdaRow *) object;
-       
+
        g_return_if_fail (GDA_IS_ROW (row));
+       GdaRowPrivate *priv = gda_row_get_instance_private (row);
        
-       if (row->priv) {
-    if (row->priv->model == NULL) {
-                 guint i;
-
-                 for (i = 0; i < row->priv->nfields; i++) {
-                         gda_value_set_null (&(row->priv->fields [i]));
-                         if (row->priv->errors && row->priv->errors [i])
-                                 g_error_free (row->priv->errors [i]);
-                 }
-                 g_free (row->priv->fields);
-                 g_free (row->priv->errors);
-
-                 g_free (row->priv);
-    } else {
-      g_object_unref (row->priv->model);
-      row->priv->model = NULL;
-    }
-               row->priv = NULL;
+       if (priv->model == NULL) {
+               guint i;
+
+               for (i = 0; i < priv->nfields; i++) {
+                       gda_value_set_null (&(priv->fields [i]));
+                       if (priv->errors && priv->errors [i])
+                               g_error_free (priv->errors [i]);
+               }
+               g_free (priv->fields);
+               g_free (priv->errors);
+
+       } else {
+               g_object_unref (priv->model);
+               priv->model = NULL;
        }
-       
-       parent_class->finalize (object);
+
+       G_OBJECT_CLASS (gda_row_parent_class)->finalize (object);
 }
 
 static void
 gda_row_set_property (GObject *object,
-                     guint param_id,
-                     const GValue *value,
-                     GParamSpec *pspec)
+                    guint param_id,
+                    const GValue *value,
+                    GParamSpec *pspec)
 {
-        GdaRow *row;
-
-        row = GDA_ROW (object);
-        if (row->priv) {
-                switch (param_id) {
-               case PROP_NB_VALUES:
-      if (row->priv->model != NULL) {
-        g_warning (_("Can't set a number of values, because a data model is in use"));
-        break;
-      }
-                       guint i;
-                       g_return_if_fail (!row->priv->fields);
-
-                       row->priv->nfields = (guint) g_value_get_int (value);
-                       row->priv->fields = g_new0 (GValue, row->priv->nfields);
-                       for (i = 0; i < row->priv->nfields; i++)
-                               gda_value_set_null (& (row->priv->fields [i]));
-                       break;
+    GdaRow *row;
+
+    row = GDA_ROW (object);
+    GdaRowPrivate *priv = gda_row_get_instance_private (row);
+    switch (param_id) {
+    case PROP_NB_VALUES:
+            if (priv->model != NULL) {
+                    g_warning (_("Can't set a number of values, because a data model is in use"));
+                    break;
+            }
+            guint i;
+            g_return_if_fail (!priv->fields);
+
+            priv->nfields = (guint) g_value_get_int (value);
+            priv->fields = g_new0 (GValue, priv->nfields);
+            for (i = 0; i < priv->nfields; i++)
+                    gda_value_set_null (& (priv->fields [i]));
+            break;
     case PROP_MODEL:
-      if (row->priv->nfields != 0) {
+      if (priv->nfields != 0) {
         break;
       }
-      row->priv->model = GDA_DATA_MODEL(g_object_ref (g_value_get_object (value)));
+      priv->model = GDA_DATA_MODEL(g_object_ref (g_value_get_object (value)));
       break;
     case PROP_MODEL_ROW:
-      if (row->priv->nfields != 0) {
+      if (priv->nfields != 0) {
         break;
       }
-      row->priv->model_row = g_value_get_uint (value);
+      priv->model_row = g_value_get_uint (value);
       break;
-               default:
-                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
-                       break;
-                }
-        }
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+        break;
+    }
 }
 
 static void
@@ -202,53 +193,24 @@ gda_row_get_property (GObject *object,
                      GValue *value,
                      GParamSpec *pspec)
 {
-        GdaRow *row;
+       GdaRow *row;
 
-        row = GDA_ROW (object);
-        if (row->priv) {
-                switch (param_id) {
+       row = GDA_ROW (object);
+       GdaRowPrivate *priv = gda_row_get_instance_private (row);
+       switch (param_id) {
                case PROP_NB_VALUES:
-                       g_value_set_int (value, row->priv->nfields);
+                       g_value_set_int (value, priv->nfields);
                        break;
                case PROP_MODEL:
-                       g_value_set_object (value, row->priv->model);
+                       g_value_set_object (value, priv->model);
                        break;
                case PROP_MODEL_ROW:
-                       g_value_set_uint (value, row->priv->model_row);
+                       g_value_set_uint (value, priv->model_row);
                        break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                        break;
-                }
-        }
-}
-
-GType
-gda_row_get_type (void)
-{
-       static GType type = 0;
-
-       if (G_UNLIKELY (type == 0)) {
-               static GMutex registering;
-               static const GTypeInfo info = {
-                       sizeof (GdaRowClass),
-                       (GBaseInitFunc) NULL,
-                       (GBaseFinalizeFunc) NULL,
-                       (GClassInitFunc) gda_row_class_init,
-                       NULL,
-                       NULL,
-                       sizeof (GdaRow),
-                       0,
-                       (GInstanceInitFunc) gda_row_init,
-                       0
-               };
-               g_mutex_lock (&registering);
-               if (type == 0)
-                       type = g_type_register_static (PARENT_TYPE, "GdaRow", &info, 0);
-               g_mutex_unlock (&registering);
        }
-
-       return type;
 }
 
 /**
@@ -262,8 +224,7 @@ gda_row_get_type (void)
 GdaRow *
 gda_row_new (gint count)
 {
-        g_return_val_if_fail (count > 0, NULL);
-       
+       g_return_val_if_fail (count > 0, NULL);
        return (GdaRow*) g_object_new (GDA_TYPE_ROW, "nb-values", count, NULL);
 }
 
@@ -300,14 +261,15 @@ GValue *
 gda_row_get_value (GdaRow *row, gint num)
 {
   g_return_val_if_fail (GDA_IS_ROW (row), NULL);
-  if (row->priv->model == NULL) {
-    g_return_val_if_fail ((num >= 0) && ((guint) num < row->priv->nfields), NULL);
-    return & (row->priv->fields[num]);
+  GdaRowPrivate *priv = gda_row_get_instance_private (row);
+  if (priv->model == NULL) {
+    g_return_val_if_fail ((num >= 0) && ((guint) num < priv->nfields), NULL);
+    return & (priv->fields[num]);
   } else {
     GError *error = NULL;
     const GValue *value = NULL;
-    g_return_val_if_fail ((num >= 0) && (num < gda_data_model_get_n_columns (row->priv->model)), NULL);
-    value = gda_data_model_get_value_at (row->priv->model, num, row->priv->model_row, &error);
+    g_return_val_if_fail ((num >= 0) && (num < gda_data_model_get_n_columns (priv->model)), NULL);
+    value = gda_data_model_get_value_at (priv->model, num, priv->model_row, &error);
     if (value == NULL) {
       g_warning (_("No value can be retrieved from data model's row: %s"),
                  error ? (error->message ? error->message : _("No Detail")) : _("No error was set"));
@@ -346,35 +308,36 @@ gda_row_invalidate_value_e (GdaRow *row, GValue *value, GError *error)
 {
        gda_value_set_null (value);
        G_VALUE_TYPE (value) = G_TYPE_INVALID;
+       GdaRowPrivate *priv = gda_row_get_instance_private (row);
        if (error) {
                guint i;
-               if (! row->priv->errors)
-                       row->priv->errors = g_new0 (GError*, row->priv->nfields);
-               for (i = 0; i < row->priv->nfields; i++) {
-                       if (& (row->priv->fields[i]) == value) {
-                               if (row->priv->errors [i])
-                                       g_error_free (row->priv->errors [i]);
-                               row->priv->errors [i] = error;
+               if (! priv->errors)
+                       priv->errors = g_new0 (GError*, priv->nfields);
+               for (i = 0; i < priv->nfields; i++) {
+                       if (& (priv->fields[i]) == value) {
+                               if (priv->errors [i])
+                                       g_error_free (priv->errors [i]);
+                               priv->errors [i] = error;
                                break;
                        }
                }
-               if (i == row->priv->nfields) {
+               if (i == priv->nfields) {
                        g_error_free (error);
                        g_warning (_("Value not found in row!"));
                }
        }
-       else if (row->priv->errors) {
+       else if (priv->errors) {
                guint i;
-               for (i = 0; i < row->priv->nfields; i++) {
-                       if (& (row->priv->fields[i]) == value) {
-                               if (row->priv->errors [i]) {
-                                       g_error_free (row->priv->errors [i]);
-                                       row->priv->errors [i] = NULL;
+               for (i = 0; i < priv->nfields; i++) {
+                       if (& (priv->fields[i]) == value) {
+                               if (priv->errors [i]) {
+                                       g_error_free (priv->errors [i]);
+                                       priv->errors [i] = NULL;
                                }
                                break;
                        }
                }
-               if (i == row->priv->nfields)
+               if (i == priv->nfields)
                        g_warning (_("Value not found in row!"));
        }
 }
@@ -415,16 +378,17 @@ gda_row_value_is_valid_e (GdaRow *row, GValue *value, GError **error)
 {
        gboolean valid;
        valid = (G_VALUE_TYPE (value) == G_TYPE_INVALID) ? FALSE : TRUE;
-       if (!valid && row->priv->errors && error) {
+       GdaRowPrivate *priv = gda_row_get_instance_private (row);
+       if (!valid && priv->errors && error) {
                guint i;
-               for (i = 0; i < row->priv->nfields; i++) {
-                       if (& (row->priv->fields[i]) == value) {
-                               if (row->priv->errors [i])
-                                       g_propagate_error (error, g_error_copy (row->priv->errors [i]));
+               for (i = 0; i < priv->nfields; i++) {
+                       if (& (priv->fields[i]) == value) {
+                               if (priv->errors [i])
+                                       g_propagate_error (error, g_error_copy (priv->errors [i]));
                                break;
                        }
                }
-               if (i == row->priv->nfields)
+               if (i == priv->nfields)
                        g_warning (_("Value not found in row!"));
        }
        return valid;
@@ -440,8 +404,8 @@ gint
 gda_row_get_length (GdaRow *row)
 {
   g_return_val_if_fail (GDA_IS_ROW (row), 0);
-  g_return_val_if_fail (row->priv, 0);
-  if (row->priv->model == NULL)
-    return row->priv->nfields;
-  return gda_data_model_get_n_columns (row->priv->model);
+  GdaRowPrivate *priv = gda_row_get_instance_private (row);
+  if (priv->model == NULL)
+    return priv->nfields;
+  return gda_data_model_get_n_columns (priv->model);
 }
diff --git a/libgda/gda-row.h b/libgda/gda-row.h
index 4d9f14d10..0ad5be1d4 100644
--- a/libgda/gda-row.h
+++ b/libgda/gda-row.h
@@ -7,6 +7,7 @@
  * Copyright (C) 2004 Paisa  Seeluangsawat <paisa users sf net>
  * Copyright (C) 2005 Bas Driessen <bas driessen xobas com>
  * Copyright (C) 2005 Álvaro Peńa <alvaropg telefonica net>
+ * Copyright (C) 2018 Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -33,20 +34,7 @@
 G_BEGIN_DECLS
 
 #define GDA_TYPE_ROW            (gda_row_get_type())
-#define GDA_ROW(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GDA_TYPE_ROW, GdaRow))
-#define GDA_ROW_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GDA_TYPE_ROW, GdaRowClass))
-#define GDA_IS_ROW(obj)         (G_TYPE_CHECK_INSTANCE_TYPE (obj, GDA_TYPE_ROW))
-#define GDA_IS_ROW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDA_TYPE_ROW))
-
-typedef struct _GdaRow        GdaRow;
-typedef struct _GdaRowClass   GdaRowClass;
-typedef struct _GdaRowPrivate GdaRowPrivate;
-
-struct _GdaRow {
-       GObject        object;
-       GdaRowPrivate *priv;
-};
-
+G_DECLARE_DERIVABLE_TYPE(GdaRow, gda_row, GDA, ROW, GObject)
 struct _GdaRowClass {
        GObjectClass   parent_class;
 
@@ -72,8 +60,6 @@ struct _GdaRowClass {
  * when executing a SELECT statement.
  */
 
-GType         gda_row_get_type       (void) G_GNUC_CONST;
-
 GdaRow       *gda_row_new            (gint count);
 GdaRow       *gda_row_new_from_data_model (GdaDataModel *model, guint row);
 gint          gda_row_get_length     (GdaRow *row);


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