[libgda] gda-sql-parser: ported to G_DECLARE/G_DEFINE



commit 15829114ee06e9efa97079226c184b7f206f8fcf
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Sep 27 14:54:37 2018 -0500

    gda-sql-parser: ported to G_DECLARE/G_DEFINE

 libgda/sql-parser/gda-sql-parser-private.h |  1 -
 libgda/sql-parser/gda-sql-parser.c         | 70 ++++++++----------------------
 libgda/sql-parser/gda-sql-parser.h         | 14 +-----
 3 files changed, 20 insertions(+), 65 deletions(-)
---
diff --git a/libgda/sql-parser/gda-sql-parser-private.h b/libgda/sql-parser/gda-sql-parser-private.h
index dfe9fcade..b9b27321f 100644
--- a/libgda/sql-parser/gda-sql-parser-private.h
+++ b/libgda/sql-parser/gda-sql-parser-private.h
@@ -70,7 +70,6 @@ typedef struct {
        gpointer _gda_reserved1;
        gpointer _gda_reserved2;
 } GdaSqlParserPrivate;
-#define gda_sql_parser_get_instance_private(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, GDA_TYPE_SQL_PARSER, 
GdaSqlParserPrivate)
 
 G_END_DECLS
 
diff --git a/libgda/sql-parser/gda-sql-parser.c b/libgda/sql-parser/gda-sql-parser.c
index 376c7d87a..d4531f094 100644
--- a/libgda/sql-parser/gda-sql-parser.c
+++ b/libgda/sql-parser/gda-sql-parser.c
@@ -36,8 +36,6 @@
 /*
  * Main static functions
  */
-static void gda_sql_parser_class_init (GdaSqlParserClass *klass);
-static void gda_sql_parser_init (GdaSqlParser *stmt);
 static void gda_sql_parser_dispose (GObject *object);
 static void gda_sql_parser_finalize (GObject *object);
 
@@ -56,9 +54,6 @@ static void                 gda_sql_parser_lock      (GdaLockable *lockable);
 static gboolean             gda_sql_parser_trylock   (GdaLockable *lockable);
 static void                 gda_sql_parser_unlock    (GdaLockable *lockable);
 
-/* get a pointer to the parents to be able to call their destructor */
-static GObjectClass  *parent_class = NULL;
-
 static void gda_sql_parser_reset (GdaSqlParser *parser);
 static GValue *tokenizer_get_next_token (GdaSqlParser *parser);
 
@@ -81,6 +76,11 @@ void priv_gda_sql_parserTrace (void*, char *);
 void priv_gda_sql_parser (void*, int, GValue *, GdaSqlParserIface *);
 
 
+
+G_DEFINE_TYPE_WITH_CODE (GdaSqlParser, gda_sql_parser, G_TYPE_OBJECT,
+                         G_ADD_PRIVATE (GdaSqlParser)
+                         G_IMPLEMENT_INTERFACE (GDA_TYPE_LOCKABLE, gda_sql_parser_lockable_init))
+
 /* signals */
 enum
 {
@@ -111,49 +111,10 @@ GQuark gda_sql_parser_error_quark (void)
 }
 
 
-GType
-gda_sql_parser_get_type (void)
-{
-       static GType type = 0;
-
-       if (G_UNLIKELY (type == 0)) {
-               static GMutex registering;
-               static const GTypeInfo info = {
-                       sizeof (GdaSqlParserClass),
-                       (GBaseInitFunc) NULL,
-                       (GBaseFinalizeFunc) NULL,
-                       (GClassInitFunc) gda_sql_parser_class_init,
-                       NULL,
-                       NULL,
-                       sizeof (GdaSqlParser),
-                       0,
-                       (GInstanceInitFunc) gda_sql_parser_init,
-                       0
-               };
-
-               static GInterfaceInfo lockable_info = {
-                        (GInterfaceInitFunc) gda_sql_parser_lockable_init,
-                       NULL,
-                        NULL
-                };
-
-               g_mutex_lock (&registering);
-               if (type == 0) {
-                       type = g_type_register_static (G_TYPE_OBJECT, "GdaSqlParser", &info, 0);
-                       g_type_add_interface_static (type, GDA_TYPE_LOCKABLE, &lockable_info);
-               }
-               g_mutex_unlock (&registering);
-       }
-       return type;
-}
-
 static void
 gda_sql_parser_class_init (GdaSqlParserClass * klass)
 {
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
-       parent_class = g_type_class_peek_parent (klass);
-
-       g_type_class_add_private (object_class, sizeof (GdaSqlParserPrivate));
 
        object_class->dispose = gda_sql_parser_dispose;
        object_class->finalize = gda_sql_parser_finalize;
@@ -201,12 +162,14 @@ static void
 gda_sql_parser_reset (GdaSqlParser *parser)
 {
        GdaSqlParserPrivate *priv = gda_sql_parser_get_instance_private (parser);
-       g_free (priv->sql);
+       if (priv->sql)
+               g_free (priv->sql);
        priv->sql = NULL;
        g_array_free (priv->passed_tokens, TRUE);
        priv->passed_tokens = g_array_new (FALSE, FALSE, sizeof (gint));
 
-       g_free (priv->error_msg);
+       if (priv->error_msg)
+               g_free (priv->error_msg);
        priv->error_msg = NULL;
        priv->error_line = 0;
        priv->error_col = 0;
@@ -293,9 +256,16 @@ gda_sql_parser_dispose (GObject *object)
 
        parser = GDA_SQL_PARSER (object);
        GdaSqlParserPrivate *priv = gda_sql_parser_get_instance_private (parser);
+       gda_sql_parser_reset (parser);
+       if (priv->context)
+               g_free (priv->context);
+       priv->context = NULL;
+       if (priv->passed_tokens)
+               g_array_free (priv->passed_tokens, TRUE);
+       priv->passed_tokens = NULL;
 
        /* parent class */
-       parent_class->dispose (object);
+       G_OBJECT_CLASS (gda_sql_parser_parent_class)->dispose (object);
 }
 
 static void
@@ -312,8 +282,6 @@ gda_sql_parser_finalize (GObject *object)
        GdaSqlParserClass *klass;
 
        klass = (GdaSqlParserClass*) G_OBJECT_GET_CLASS (parser);
-       gda_sql_parser_reset (parser);
-       g_free (priv->context);
 
        if (klass->delim_alloc) {
                g_assert (klass->delim_free);
@@ -328,12 +296,10 @@ gda_sql_parser_finalize (GObject *object)
        else
                priv_gda_sql_parserFree (priv->lemon_parser, g_free);
 
-       g_array_free (priv->passed_tokens, TRUE);
-
        g_rec_mutex_clear (& (priv->mutex));
 
        /* parent class */
-       parent_class->finalize (object);
+       G_OBJECT_CLASS (gda_sql_parser_parent_class)->finalize (object);
 }
 
 
diff --git a/libgda/sql-parser/gda-sql-parser.h b/libgda/sql-parser/gda-sql-parser.h
index 8f133a904..cd5817bb9 100644
--- a/libgda/sql-parser/gda-sql-parser.h
+++ b/libgda/sql-parser/gda-sql-parser.h
@@ -30,11 +30,6 @@
 
 G_BEGIN_DECLS
 
-#define GDA_TYPE_SQL_PARSER          (gda_sql_parser_get_type())
-#define GDA_SQL_PARSER(obj)          G_TYPE_CHECK_INSTANCE_CAST (obj, gda_sql_parser_get_type(), 
GdaSqlParser)
-#define GDA_SQL_PARSER_CLASS(klass)  G_TYPE_CHECK_CLASS_CAST (klass, gda_sql_parser_get_type (), 
GdaSqlParserClass)
-#define GDA_IS_SQL_PARSER(obj)       G_TYPE_CHECK_INSTANCE_TYPE (obj, gda_sql_parser_get_type ())
-
 /* error reporting */
 extern GQuark gda_sql_parser_error_quark (void);
 #define GDA_SQL_PARSER_ERROR gda_sql_parser_error_quark ()
@@ -59,12 +54,6 @@ typedef enum {
         GDA_SQL_PARSER_FLAVOUR_POSTGRESQL = 4
 } GdaSqlParserFlavour;
 
-/* struct for the object's data */
-struct _GdaSqlParser
-{
-       GObject              object;
-};
-
 /* interface with the Lemon parser */
 typedef struct _GdaSqlParserIface
 {
@@ -77,6 +66,8 @@ typedef struct _GdaSqlParserIface
        gpointer         _gda_reserved2;
 } GdaSqlParserIface;
 
+#define GDA_TYPE_SQL_PARSER          (gda_sql_parser_get_type())
+G_DECLARE_DERIVABLE_TYPE(GdaSqlParser, gda_sql_parser, GDA, SQL_PARSER, GObject)
 /* struct for the object's class */
 struct _GdaSqlParserClass
 {
@@ -183,7 +174,6 @@ struct _GdaSqlParserClass
  * The #GdaSqlParser object implements its own locking mechanism so it is thread-safe.
  */
 
-GType               gda_sql_parser_get_type               (void) G_GNUC_CONST;
 GdaSqlParser       *gda_sql_parser_new                    (void);
 
 GdaStatement       *gda_sql_parser_parse_string           (GdaSqlParser *parser, 


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