[libdazzle/wip/rishi/task-cache-NULL-value-copy-and-destroy] task-cache: Allow NULL as value_copy_func and value_destroy_func




commit 1c0903a68d04f885087c659ffb9839b419e688fe
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Mar 30 20:06:26 2021 +0200

    task-cache: Allow NULL as value_copy_func and value_destroy_func
    
    This makes it convenient to use raw gpointers as values. eg., when
    stuffing booleans and integer values as pointers in the cache.
    
    https://gitlab.gnome.org/GNOME/libdazzle/-/merge_requests/49

 src/cache/dzl-task-cache.c |  8 +++++--
 tests/test-task-cache.c    | 53 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 2 deletions(-)
---
diff --git a/src/cache/dzl-task-cache.c b/src/cache/dzl-task-cache.c
index be4300a..0af7ff3 100644
--- a/src/cache/dzl-task-cache.c
+++ b/src/cache/dzl-task-cache.c
@@ -718,8 +718,6 @@ dzl_task_cache_constructed (GObject *object)
 
   if ((self->key_equal_func == NULL) ||
       (self->key_hash_func == NULL) ||
-      (self->value_copy_func == NULL) ||
-      (self->value_destroy_func == NULL) ||
       (self->populate_callback == NULL))
     {
       g_error ("DzlTaskCache was configured improperly.");
@@ -732,6 +730,12 @@ dzl_task_cache_constructed (GObject *object)
   if (self->key_destroy_func == NULL)
     self->key_destroy_func = dzl_task_cache_dummy_destroy_func;
 
+  if (self->value_copy_func == NULL)
+    self->value_copy_func = dzl_task_cache_dummy_copy_func;
+
+  if (self->value_destroy_func == NULL)
+    self->value_destroy_func = dzl_task_cache_dummy_destroy_func;
+
   /*
    * This is where the cached result objects live.
    */
diff --git a/tests/test-task-cache.c b/tests/test-task-cache.c
index a23df59..abc2774 100644
--- a/tests/test-task-cache.c
+++ b/tests/test-task-cache.c
@@ -58,11 +58,64 @@ test_task_cache (void)
   g_assert (foo == NULL);
 }
 
+static void
+populate_callback_raw_value (DzlTaskCache  *self,
+                             gconstpointer  key,
+                             GTask         *task,
+                             gpointer       user_data)
+{
+  g_task_return_pointer (task, GINT_TO_POINTER ((gint) TRUE), NULL);
+}
+
+static void
+get_foo_raw_value_cb (GObject      *object,
+                      GAsyncResult *result,
+                      gpointer      user_data)
+{
+  GError *error = NULL;
+  gboolean value;
+  gpointer ret;
+
+  ret = dzl_task_cache_get_finish (cache, result, &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (ret);
+
+  value = (gboolean) GPOINTER_TO_INT (ret);
+  g_assert_true (value);
+
+  g_assert_true (dzl_task_cache_evict (cache, "foo"));
+
+  g_main_loop_quit (main_loop);
+}
+
+static void
+test_task_cache_raw_value (void)
+{
+  main_loop = g_main_loop_new (NULL, FALSE);
+  cache = dzl_task_cache_new (g_str_hash,
+                              g_str_equal,
+                              (GBoxedCopyFunc)g_strdup,
+                              (GBoxedFreeFunc)g_free,
+                              NULL,
+                              NULL,
+                              100 /* msec */,
+                              populate_callback_raw_value, NULL, NULL);
+
+  g_assert (!dzl_task_cache_peek (cache, "foo"));
+  g_assert (!dzl_task_cache_evict (cache, "foo"));
+
+  dzl_task_cache_get_async (cache, "foo", TRUE, NULL, get_foo_raw_value_cb, NULL);
+
+  g_main_loop_run (main_loop);
+  g_main_loop_unref (main_loop);
+}
+
 gint
 main (gint   argc,
       gchar *argv[])
 {
   g_test_init (&argc, &argv, NULL);
   g_test_add_func ("/Dazzle/TaskCache/basic", test_task_cache);
+  g_test_add_func ("/Dazzle/TaskCache/raw-value", test_task_cache_raw_value);
   return g_test_run ();
 }


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