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



commit a31e73292bdae032176b6d26e332f399aca71919
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Sep 14 12:54:03 2018 -0500

    gda-lockable: ported to G_DECLARE/G_DEFINE
    
    Interface's method's name changed to help introspection.

 libgda/gda-connection.c            | 10 +++---
 libgda/gda-holder.c                | 10 +++---
 libgda/gda-lockable.c              | 74 ++++++++++----------------------------
 libgda/gda-lockable.h              | 18 +++-------
 libgda/sql-parser/gda-sql-parser.c | 10 +++---
 5 files changed, 39 insertions(+), 83 deletions(-)
---
diff --git a/libgda/gda-connection.c b/libgda/gda-connection.c
index 2485e49e5..c8f80664e 100644
--- a/libgda/gda-connection.c
+++ b/libgda/gda-connection.c
@@ -140,7 +140,7 @@ static void gda_connection_get_property (GObject *object,
                                         GParamSpec *pspec);
 
 /* GdaLockable interface */
-static void                 gda_connection_lockable_init (GdaLockableIface *iface);
+static void                 gda_connection_lockable_init (GdaLockableInterface *iface);
 static void                 gda_connection_lock      (GdaLockable *lockable);
 static gboolean             gda_connection_trylock   (GdaLockable *lockable);
 static void                 gda_connection_unlock    (GdaLockable *lockable);
@@ -416,11 +416,11 @@ gda_connection_class_init (GdaConnectionClass *klass)
 }
 
 static void
-gda_connection_lockable_init (GdaLockableIface *iface)
+gda_connection_lockable_init (GdaLockableInterface *iface)
 {
-       iface->i_lock = gda_connection_lock;
-       iface->i_trylock = gda_connection_trylock;
-       iface->i_unlock = gda_connection_unlock;
+       iface->lock = gda_connection_lock;
+       iface->trylock = gda_connection_trylock;
+       iface->unlock = gda_connection_unlock;
 }
 
 static void
diff --git a/libgda/gda-holder.c b/libgda/gda-holder.c
index d892643ef..ac1f9a24c 100644
--- a/libgda/gda-holder.c
+++ b/libgda/gda-holder.c
@@ -56,7 +56,7 @@ static void gda_holder_get_property (GObject *object,
                                     GParamSpec *pspec);
 
 /* GdaLockable interface */
-static void                 gda_holder_lockable_init (GdaLockableIface *iface);
+static void                 gda_holder_lockable_init (GdaLockableInterface *iface);
 static void                 gda_holder_lock      (GdaLockable *lockable);
 static gboolean             gda_holder_trylock   (GdaLockable *lockable);
 static void                 gda_holder_unlock    (GdaLockable *lockable);
@@ -336,11 +336,11 @@ gda_holder_class_init (GdaHolderClass *class)
 }
 
 static void
-gda_holder_lockable_init (GdaLockableIface *iface)
+gda_holder_lockable_init (GdaLockableInterface *iface)
 {
-       iface->i_lock = gda_holder_lock;
-       iface->i_trylock = gda_holder_trylock;
-       iface->i_unlock = gda_holder_unlock;
+       iface->lock = gda_holder_lock;
+       iface->trylock = gda_holder_trylock;
+       iface->unlock = gda_holder_unlock;
 }
 
 static void
diff --git a/libgda/gda-lockable.c b/libgda/gda-lockable.c
index 8679e4f66..34fce5188 100644
--- a/libgda/gda-lockable.c
+++ b/libgda/gda-lockable.c
@@ -23,51 +23,14 @@
 static GRecMutex init_rmutex;
 #define MUTEX_LOCK() g_rec_mutex_lock(&init_rmutex)
 #define MUTEX_UNLOCK() g_rec_mutex_unlock(&init_rmutex)
-static void gda_lockable_class_init (gpointer g_class);
 
-GType
-gda_lockable_get_type (void)
-{
-       static GType type = 0;
-
-       if (G_UNLIKELY (type == 0)) {
-               static const GTypeInfo info = {
-                       sizeof (GdaLockableIface),
-                       (GBaseInitFunc) gda_lockable_class_init,
-                       (GBaseFinalizeFunc) NULL,
-                       (GClassInitFunc) NULL,
-                       NULL,
-                       NULL,
-                       0,
-                       0,
-                       (GInstanceInitFunc) NULL,
-                       0
-               };
-               
-               MUTEX_LOCK();
-               if (type == 0) {
-                       type = g_type_register_static (G_TYPE_INTERFACE, "GdaLockable", &info, 0);
-                       g_type_interface_add_prerequisite (type, G_TYPE_OBJECT);
-               }
-               MUTEX_UNLOCK();
-       }
-       return type;
-}
+G_DEFINE_INTERFACE(GdaLockable, gda_lockable, G_TYPE_OBJECT)
 
-static void
-gda_lockable_class_init (G_GNUC_UNUSED gpointer g_class)
-{
-       static gboolean initialized = FALSE;
-
-       MUTEX_LOCK();
-       if (! initialized) {
-               initialized = TRUE;
-       }
-       MUTEX_UNLOCK();
-}
+void
+gda_lockable_default_init (GdaLockableInterface *iface) {}
 
 /**
- * gda_lockable_lock:
+ * gda_lockable_lock: (virtual )
  * @lockable: a #GdaLockable object.
  *
  * Locks @lockable. If it is already locked by another thread, the current thread will block until it is 
unlocked 
@@ -80,11 +43,11 @@ void
 gda_lockable_lock (GdaLockable *lockable)
 {
        g_return_if_fail (GDA_IS_LOCKABLE (lockable));
-       
-       if (GDA_LOCKABLE_GET_CLASS (lockable)->i_lock)
-               (GDA_LOCKABLE_GET_CLASS (lockable)->i_lock) (lockable);
+       GdaLockableInterface *iface = GDA_LOCKABLE_GET_IFACE (lockable);
+       if (iface->lock)
+               (iface->lock) (lockable);
        else
-               g_warning ("Internal implementation error: %s() method not implemented\n", "i_lock");
+               g_warning ("Internal implementation error: %s() method not implemented\n", "lock");
 }
 
 /**
@@ -103,13 +66,13 @@ gboolean
 gda_lockable_trylock (GdaLockable *lockable)
 {
        g_return_val_if_fail (GDA_IS_LOCKABLE (lockable), FALSE);
+       GdaLockableInterface *iface = GDA_LOCKABLE_GET_IFACE (lockable);
        
-       if (GDA_LOCKABLE_GET_CLASS (lockable)->i_trylock)
-               return (GDA_LOCKABLE_GET_CLASS (lockable)->i_trylock) (lockable);
-       else {
-               g_warning ("Internal implementation error: %s() method not implemented\n", "i_trylock");
-               return FALSE;
-       }
+       if (iface->trylock)
+               return (iface->trylock) (lockable);
+
+       g_warning ("Internal implementation error: %s() method not implemented\n", "trylock");
+       return FALSE;
 }
 
 /**
@@ -123,9 +86,10 @@ void
 gda_lockable_unlock (GdaLockable *lockable)
 {
        g_return_if_fail (GDA_IS_LOCKABLE (lockable));
-       
-       if (GDA_LOCKABLE_GET_CLASS (lockable)->i_unlock)
-               (GDA_LOCKABLE_GET_CLASS (lockable)->i_unlock) (lockable);
+       GdaLockableInterface *iface = GDA_LOCKABLE_GET_IFACE (lockable);
+
+       if (iface->unlock)
+               (iface->unlock) (lockable);
        else
-               g_warning ("Internal implementation error: %s() method not implemented\n", "i_unlock");
+               g_warning ("Internal implementation error: %s() method not implemented\n", "unlock");
 }
diff --git a/libgda/gda-lockable.h b/libgda/gda-lockable.h
index 2a84c5b4d..833945418 100644
--- a/libgda/gda-lockable.h
+++ b/libgda/gda-lockable.h
@@ -25,21 +25,15 @@
 G_BEGIN_DECLS
 
 #define GDA_TYPE_LOCKABLE            (gda_lockable_get_type())
-#define GDA_LOCKABLE(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GDA_TYPE_LOCKABLE, GdaLockable))
-#define GDA_IS_LOCKABLE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE (obj, GDA_TYPE_LOCKABLE))
-#define GDA_LOCKABLE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GDA_TYPE_LOCKABLE, 
GdaLockableIface))
-
-typedef struct _GdaLockableIface   GdaLockableIface;
-typedef struct _GdaLockable        GdaLockable;
-
+G_DECLARE_INTERFACE (GdaLockable, gda_lockable, GDA, LOCKABLE, GObject)
 /* struct for the interface */
-struct _GdaLockableIface {
+struct _GdaLockableInterface {
        GTypeInterface           g_iface;
 
        /* virtual table */
-       void                 (* i_lock)       (GdaLockable *lock);
-       gboolean             (* i_trylock)    (GdaLockable *lock);
-       void                 (* i_unlock)     (GdaLockable *lock);
+       void                 (* lock)       (GdaLockable *lock);
+       gboolean             (* trylock)    (GdaLockable *lock);
+       void                 (* unlock)     (GdaLockable *lock);
 };
 
 /**
@@ -54,8 +48,6 @@ struct _GdaLockableIface {
  * gda_lockable_trylock() and call gda_lockable_unlock() when the object is not used anymore.
  */
 
-GType      gda_lockable_get_type   (void) G_GNUC_CONST;
-
 void       gda_lockable_lock       (GdaLockable *lockable);
 gboolean   gda_lockable_trylock    (GdaLockable *lockable);
 void       gda_lockable_unlock     (GdaLockable *lockable);
diff --git a/libgda/sql-parser/gda-sql-parser.c b/libgda/sql-parser/gda-sql-parser.c
index af9f8446c..376c7d87a 100644
--- a/libgda/sql-parser/gda-sql-parser.c
+++ b/libgda/sql-parser/gda-sql-parser.c
@@ -51,7 +51,7 @@ static void gda_sql_parser_get_property (GObject *object,
                                        GParamSpec *pspec);
 
 /* GdaLockable interface */
-static void                 gda_sql_parser_lockable_init (GdaLockableIface *iface);
+static void                 gda_sql_parser_lockable_init (GdaLockableInterface *iface);
 static void                 gda_sql_parser_lock      (GdaLockable *lockable);
 static gboolean             gda_sql_parser_trylock   (GdaLockable *lockable);
 static void                 gda_sql_parser_unlock    (GdaLockable *lockable);
@@ -190,11 +190,11 @@ gda_sql_parser_class_init (GdaSqlParserClass * klass)
 }
 
 static void
-gda_sql_parser_lockable_init (GdaLockableIface *iface)
+gda_sql_parser_lockable_init (GdaLockableInterface *iface)
 {
-       iface->i_lock = gda_sql_parser_lock;
-       iface->i_trylock = gda_sql_parser_trylock;
-       iface->i_unlock = gda_sql_parser_unlock;
+       iface->lock = gda_sql_parser_lock;
+       iface->trylock = gda_sql_parser_trylock;
+       iface->unlock = gda_sql_parser_unlock;
 }
 
 static void


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