[gnome-builder] egg-task-cache: add user data and destroy callbacks



commit 52e885999b15cbbd7034c7d8e0a31f6530b2323c
Author: Christian Hergert <christian hergert me>
Date:   Tue May 12 18:16:30 2015 -0700

    egg-task-cache: add user data and destroy callbacks
    
    Helps to avoid having to pass extra data in the cache key.

 contrib/egg/egg-task-cache.c |   39 +++++++++++++++++++++++++++++++++++++--
 contrib/egg/egg-task-cache.h |    8 ++++++--
 tests/test-egg-cache.c       |    7 ++++---
 3 files changed, 47 insertions(+), 7 deletions(-)
---
diff --git a/contrib/egg/egg-task-cache.c b/contrib/egg/egg-task-cache.c
index aac3dce..a9c0949 100644
--- a/contrib/egg/egg-task-cache.c
+++ b/contrib/egg/egg-task-cache.c
@@ -32,7 +32,10 @@ struct _EggTaskCache
   GEqualFunc            key_equal_func;
   GBoxedCopyFunc        key_copy_func;
   GBoxedFreeFunc        key_destroy_func;
+
   EggTaskCacheCallback  populate_callback;
+  gpointer              populate_callback_data;
+  GDestroyNotify        populate_callback_data_destroy;
 
   GHashTable           *cache;
   GHashTable           *in_flight;
@@ -72,6 +75,8 @@ enum {
   PROP_KEY_EQUAL_FUNC,
   PROP_KEY_HASH_FUNC,
   PROP_POPULATE_CALLBACK,
+  PROP_POPULATE_CALLBACK_DATA,
+  PROP_POPULATE_CALLBACK_DATA_DESTROY,
   PROP_TIME_TO_LIVE,
   LAST_PROP
 };
@@ -409,7 +414,7 @@ egg_task_cache_get_async (EggTaskCache        *self,
       g_hash_table_insert (self->in_flight,
                            self->key_copy_func ((gpointer)key),
                            GINT_TO_POINTER (TRUE));
-      self->populate_callback (self, key, fetch_task);
+      self->populate_callback (self, key, fetch_task, self->populate_callback_data);
 
       EGG_COUNTER_INC (in_flight);
     }
@@ -567,6 +572,12 @@ egg_task_cache_dispose (GObject *object)
       EGG_COUNTER_SUB (in_flight, count);
     }
 
+  if (self->populate_callback_data)
+    {
+      if (self->populate_callback_data_destroy)
+        self->populate_callback_data_destroy (self->populate_callback_data);
+    }
+
   G_OBJECT_CLASS (egg_task_cache_parent_class)->dispose (object);
 }
 
@@ -608,6 +619,14 @@ egg_task_cache_set_property (GObject      *object,
       self->populate_callback = g_value_get_pointer (value);
       break;
 
+    case PROP_POPULATE_CALLBACK_DATA:
+      self->populate_callback_data = g_value_get_pointer (value);
+      break;
+
+    case PROP_POPULATE_CALLBACK_DATA_DESTROY:
+      self->populate_callback_data_destroy = g_value_get_pointer (value);
+      break;
+
     case PROP_TIME_TO_LIVE:
       self->time_to_live_usec = (g_value_get_int64 (value) * 1000L);
       break;
@@ -657,6 +676,18 @@ egg_task_cache_class_init (EggTaskCacheClass *klass)
                          _("Populate Callback"),
                          (G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
 
+  gParamSpecs [PROP_POPULATE_CALLBACK_DATA] =
+    g_param_spec_pointer ("populate-callback-data",
+                         _("Populate Callback Data"),
+                         _("Populate Callback Data"),
+                         (G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  gParamSpecs [PROP_POPULATE_CALLBACK_DATA_DESTROY] =
+    g_param_spec_pointer ("populate-callback-data-destroy",
+                         _("Populate Callback Data Destroy"),
+                         _("Populate Callback Data Destroy"),
+                         (G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
   /**
    * EggTaskCache:time-to-live:
    *
@@ -691,8 +722,10 @@ egg_task_cache_new (GHashFunc            key_hash_func,
                     GEqualFunc           key_equal_func,
                     GBoxedCopyFunc       key_copy_func,
                     GBoxedFreeFunc       key_destroy_func,
+                    gint64               time_to_live,
                     EggTaskCacheCallback populate_callback,
-                    gint64               time_to_live)
+                    gpointer             populate_callback_data,
+                    GDestroyNotify       populate_callback_data_destroy)
 {
   g_return_val_if_fail (key_hash_func, NULL);
   g_return_val_if_fail (key_equal_func, NULL);
@@ -706,6 +739,8 @@ egg_task_cache_new (GHashFunc            key_hash_func,
                        "key-copy-func", key_copy_func,
                        "key-destroy-func", key_destroy_func,
                        "populate-callback", populate_callback,
+                       "populate-callback-data", populate_callback_data,
+                       "populate-callback-data-destroy", populate_callback_data_destroy,
                        "time-to-live", time_to_live,
                        NULL);
 }
diff --git a/contrib/egg/egg-task-cache.h b/contrib/egg/egg-task-cache.h
index 10068f4..4f0c587 100644
--- a/contrib/egg/egg-task-cache.h
+++ b/contrib/egg/egg-task-cache.h
@@ -32,6 +32,7 @@ G_DECLARE_FINAL_TYPE (EggTaskCache, egg_task_cache, EGG, TASK_CACHE, GObject)
  * @self: An #EggTaskCache.
  * @key: the key to fetch
  * @task: the task to be completed
+ * @user_data: user_data registered at initialization.
  *
  * #EggTaskCacheCallback is the prototype for a function to be executed to
  * populate a an item in the cache.
@@ -45,14 +46,17 @@ G_DECLARE_FINAL_TYPE (EggTaskCache, egg_task_cache, EGG, TASK_CACHE, GObject)
  */
 typedef void (*EggTaskCacheCallback) (EggTaskCache  *self,
                                       gconstpointer  key,
-                                      GTask         *task);
+                                      GTask         *task,
+                                      gpointer       user_data);
 
 EggTaskCache *egg_task_cache_new        (GHashFunc              key_hash_func,
                                          GEqualFunc             key_equal_func,
                                          GBoxedCopyFunc         key_copy_func,
                                          GBoxedFreeFunc         key_destroy_func,
+                                         gint64                 time_to_live_msec,
                                          EggTaskCacheCallback   populate_callback,
-                                         gint64                 time_to_live_msec);
+                                         gpointer               populate_callback_data,
+                                         GDestroyNotify         populate_callback_data_destroy);
 void          egg_task_cache_get_async  (EggTaskCache          *self,
                                          gconstpointer          key,
                                          GCancellable          *cancellable,
diff --git a/tests/test-egg-cache.c b/tests/test-egg-cache.c
index 0c90dd4..a2e7327 100644
--- a/tests/test-egg-cache.c
+++ b/tests/test-egg-cache.c
@@ -7,7 +7,8 @@ static GObject *foo;
 static void
 populate_callback (EggTaskCache  *self,
                    gconstpointer  key,
-                   GTask         *task)
+                   GTask         *task,
+                   gpointer       user_data)
 {
   foo = g_object_new (G_TYPE_OBJECT, NULL);
   g_object_add_weak_pointer (G_OBJECT (foo), (gpointer *)&foo);
@@ -41,8 +42,8 @@ test_task_cache (void)
                               g_str_equal,
                               (GBoxedCopyFunc)g_strdup,
                               (GBoxedFreeFunc)g_free,
-                              populate_callback,
-                              100 /* msec */);
+                              100 /* msec */,
+                              populate_callback, NULL, NULL);
 
   g_assert (!egg_task_cache_peek (cache, "foo"));
   g_assert (!egg_task_cache_evict (cache, "foo"));


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