[gnome-builder] configuration: add ide_configuration_get_internal_* API



commit 17fa553adb4def64b82ab64f86418512e4209318
Author: Christian Hergert <chergert redhat com>
Date:   Wed Nov 2 16:47:48 2016 -0700

    configuration: add ide_configuration_get_internal_* API
    
    This allows plugins to store additional information on an IdeConfiguration
    that does not require adding new public API to the configuration object.
    
    Think of this as a more introspection friendly g_object_set_data() API.
    
    The data held in these "internal" values are meant more for storing things
    needed at runtime, not things that are meant to be persisted to the
    buildconfig file. These values are not persisted to buildconfig files.

 libide/buildsystem/ide-configuration.c |  216 +++++++++++++++++++++++++++++---
 libide/buildsystem/ide-configuration.h |   20 +++
 tests/Makefile.am                      |    7 +
 tests/test-ide-configuration.c         |   72 +++++++++++
 4 files changed, 300 insertions(+), 15 deletions(-)
---
diff --git a/libide/buildsystem/ide-configuration.c b/libide/buildsystem/ide-configuration.c
index f013c56..6cf000f 100644
--- a/libide/buildsystem/ide-configuration.c
+++ b/libide/buildsystem/ide-configuration.c
@@ -48,6 +48,8 @@ struct _IdeConfiguration
   IdeBuildCommandQueue *prebuild;
   IdeBuildCommandQueue *postbuild;
 
+  GHashTable     *internal;
+
   gint            parallelism;
   guint           sequence;
 
@@ -83,6 +85,29 @@ static GParamSpec *properties [N_PROPS];
 static guint signals [LAST_SIGNAL];
 
 static void
+_value_free (gpointer data)
+{
+  GValue *value = data;
+
+  if (value != NULL)
+    {
+      g_value_unset (value);
+      g_slice_free (GValue, value);
+    }
+}
+
+static GValue *
+_value_new (GType type)
+{
+  GValue *value;
+
+  value = g_slice_new0 (GValue);
+  g_value_init (value, type);
+
+  return value;
+}
+
+static void
 ide_configuration_emit_changed (IdeConfiguration *self)
 {
   g_assert (IDE_IS_CONFIGURATION (self));
@@ -168,24 +193,27 @@ ide_configuration_constructed (GObject *object)
 
   G_OBJECT_CLASS (ide_configuration_parent_class)->constructed (object);
 
-  context = ide_object_get_context (IDE_OBJECT (self));
-  device_manager = ide_context_get_device_manager (context);
-  runtime_manager = ide_context_get_runtime_manager (context);
+  /* Allow ourselves to be run from unit tests without a valid context */
+  if (NULL != (context = ide_object_get_context (IDE_OBJECT (self))))
+    {
+      device_manager = ide_context_get_device_manager (context);
+      runtime_manager = ide_context_get_runtime_manager (context);
 
-  g_signal_connect_object (device_manager,
-                           "items-changed",
-                           G_CALLBACK (ide_configuration_device_manager_items_changed),
-                           self,
-                           G_CONNECT_SWAPPED);
+      g_signal_connect_object (device_manager,
+                               "items-changed",
+                               G_CALLBACK (ide_configuration_device_manager_items_changed),
+                               self,
+                               G_CONNECT_SWAPPED);
 
-  g_signal_connect_object (runtime_manager,
-                           "items-changed",
-                           G_CALLBACK (ide_configuration_runtime_manager_items_changed),
-                           self,
-                           G_CONNECT_SWAPPED);
+      g_signal_connect_object (runtime_manager,
+                               "items-changed",
+                               G_CALLBACK (ide_configuration_runtime_manager_items_changed),
+                               self,
+                               G_CONNECT_SWAPPED);
 
-  ide_configuration_device_manager_items_changed (self, 0, 0, 0, device_manager);
-  ide_configuration_runtime_manager_items_changed (self, 0, 0, 0, runtime_manager);
+      ide_configuration_device_manager_items_changed (self, 0, 0, 0, device_manager);
+      ide_configuration_runtime_manager_items_changed (self, 0, 0, 0, runtime_manager);
+    }
 }
 
 static void
@@ -197,6 +225,7 @@ ide_configuration_finalize (GObject *object)
   g_clear_object (&self->prebuild);
   g_clear_object (&self->postbuild);
 
+  g_clear_pointer (&self->internal, g_hash_table_unref);
   g_clear_pointer (&self->config_opts, g_free);
   g_clear_pointer (&self->device_id, g_free);
   g_clear_pointer (&self->display_name, g_free);
@@ -439,6 +468,8 @@ ide_configuration_init (IdeConfiguration *self)
   self->environment = ide_environment_new ();
   self->parallelism = -1;
 
+  self->internal = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _value_free);
+
   g_signal_connect_object (self->environment,
                            "items-changed",
                            G_CALLBACK (ide_configuration_environment_changed),
@@ -936,6 +967,9 @@ void
 _ide_configuration_set_prebuild (IdeConfiguration     *self,
                                  IdeBuildCommandQueue *prebuild)
 {
+  g_assert (IDE_IS_CONFIGURATION (self));
+  g_assert (!prebuild || IDE_IS_BUILD_COMMAND_QUEUE (prebuild));
+
   g_set_object (&self->prebuild, prebuild);
 }
 
@@ -943,5 +977,157 @@ void
 _ide_configuration_set_postbuild (IdeConfiguration     *self,
                                   IdeBuildCommandQueue *postbuild)
 {
+  g_assert (IDE_IS_CONFIGURATION (self));
+  g_assert (!postbuild || IDE_IS_BUILD_COMMAND_QUEUE (postbuild));
+
   g_set_object (&self->postbuild, postbuild);
 }
+
+static GValue *
+ide_configuration_reset_internal_value (IdeConfiguration *self,
+                                        const gchar      *key,
+                                        GType             type)
+{
+  GValue *v;
+
+  g_assert (IDE_IS_CONFIGURATION (self));
+  g_assert (key != NULL);
+  g_assert (type != G_TYPE_INVALID);
+
+  v = g_hash_table_lookup (self->internal, key);
+
+  if (v == NULL)
+    {
+      v = _value_new (type);
+      g_hash_table_insert (self->internal, g_strdup (key), v);
+    }
+  else
+    {
+      g_value_unset (v);
+      g_value_init (v, type);
+    }
+
+  return v;
+}
+
+const gchar *
+ide_configuration_get_internal_string (IdeConfiguration *self,
+                                       const gchar      *key)
+{
+  const GValue *v;
+
+  g_return_val_if_fail (IDE_IS_CONFIGURATION (self), NULL);
+  g_return_val_if_fail (key != NULL, NULL);
+
+  v = g_hash_table_lookup (self->internal, key);
+
+  if (v != NULL && G_VALUE_HOLDS_STRING (v))
+    return g_value_get_string (v);
+
+  return NULL;
+}
+
+void
+ide_configuration_set_internal_string (IdeConfiguration *self,
+                                       const gchar      *key,
+                                       const gchar      *value)
+{
+  GValue *v;
+
+  g_return_if_fail (IDE_IS_CONFIGURATION (self));
+  g_return_if_fail (key != NULL);
+
+  v = ide_configuration_reset_internal_value (self, key, G_TYPE_STRING);
+  g_value_set_string (v, value);
+}
+
+gboolean
+ide_configuration_get_internal_boolean (IdeConfiguration *self,
+                                        const gchar      *key)
+{
+  const GValue *v;
+
+  g_return_val_if_fail (IDE_IS_CONFIGURATION (self), FALSE);
+  g_return_val_if_fail (key != NULL, FALSE);
+
+  v = g_hash_table_lookup (self->internal, key);
+
+  if (v != NULL && G_VALUE_HOLDS_BOOLEAN (v))
+    return g_value_get_boolean (v);
+
+  return FALSE;
+}
+
+void
+ide_configuration_set_internal_boolean (IdeConfiguration  *self,
+                                        const gchar       *key,
+                                        gboolean           value)
+{
+  GValue *v;
+
+  g_return_if_fail (IDE_IS_CONFIGURATION (self));
+  g_return_if_fail (key != NULL);
+
+  v = ide_configuration_reset_internal_value (self, key, G_TYPE_BOOLEAN);
+  g_value_set_boolean (v, value);
+}
+
+gint
+ide_configuration_get_internal_int (IdeConfiguration *self,
+                                    const gchar      *key)
+{
+  const GValue *v;
+
+  g_return_val_if_fail (IDE_IS_CONFIGURATION (self), -1);
+  g_return_val_if_fail (key != NULL, -1);
+
+  v = g_hash_table_lookup (self->internal, key);
+
+  if (v != NULL && G_VALUE_HOLDS_INT (v))
+    return g_value_get_int (v);
+
+  return 0;
+}
+
+void
+ide_configuration_set_internal_int (IdeConfiguration *self,
+                                    const gchar      *key,
+                                    gint              value)
+{
+  GValue *v;
+
+  g_return_if_fail (IDE_IS_CONFIGURATION (self));
+  g_return_if_fail (key != NULL);
+
+  v = ide_configuration_reset_internal_value (self, key, G_TYPE_INT);
+  g_value_set_int (v, value);
+}
+
+gint64
+ide_configuration_get_internal_int64 (IdeConfiguration *self,
+                                      const gchar      *key)
+{
+  const GValue *v;
+
+  g_return_val_if_fail (IDE_IS_CONFIGURATION (self), -1);
+  g_return_val_if_fail (key != NULL, -1);
+
+  if (v != NULL && G_VALUE_HOLDS_INT64 (v))
+    return g_value_get_int64 (v);
+
+  return 0;
+}
+
+void
+ide_configuration_set_internal_int64 (IdeConfiguration *self,
+                                      const gchar      *key,
+                                      gint64            value)
+{
+  GValue *v;
+
+  g_return_if_fail (IDE_IS_CONFIGURATION (self));
+  g_return_if_fail (key != NULL);
+
+  v = ide_configuration_reset_internal_value (self, key, G_TYPE_INT64);
+  g_value_set_int64 (v, value);
+}
diff --git a/libide/buildsystem/ide-configuration.h b/libide/buildsystem/ide-configuration.h
index 8c2c422..d5c38f9 100644
--- a/libide/buildsystem/ide-configuration.h
+++ b/libide/buildsystem/ide-configuration.h
@@ -76,6 +76,26 @@ IdeConfiguration     *ide_configuration_duplicate        (IdeConfiguration  *sel
 guint                 ide_configuration_get_sequence     (IdeConfiguration  *self);
 IdeBuildCommandQueue *ide_configuration_get_prebuild     (IdeConfiguration  *self);
 IdeBuildCommandQueue *ide_configuration_get_postbuild    (IdeConfiguration  *self);
+const gchar          *ide_configuration_get_internal_string  (IdeConfiguration  *self,
+                                                              const gchar       *key);
+void                  ide_configuration_set_internal_string  (IdeConfiguration  *self,
+                                                              const gchar       *key,
+                                                              const gchar       *value);
+gboolean              ide_configuration_get_internal_boolean (IdeConfiguration  *self,
+                                                              const gchar       *key);
+void                  ide_configuration_set_internal_boolean (IdeConfiguration  *self,
+                                                              const gchar       *key,
+                                                              gboolean           value);
+gint                  ide_configuration_get_internal_int     (IdeConfiguration  *self,
+                                                              const gchar       *key);
+void                  ide_configuration_set_internal_int     (IdeConfiguration  *self,
+                                                              const gchar       *key,
+                                                              gint               value);
+gint64                ide_configuration_get_internal_int64   (IdeConfiguration  *self,
+                                                              const gchar       *key);
+void                  ide_configuration_set_internal_int64   (IdeConfiguration  *self,
+                                                              const gchar       *key,
+                                                              gint64             value);
 
 G_END_DECLS
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 84be119..77955d0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -91,6 +91,13 @@ test_ide_context_LDADD = $(tests_libs)
 test_ide_context_LDFLAGS = $(tests_ldflags)
 
 
+TESTS = test-ide-configuration
+test_ide_configuration_SOURCES = test-ide-configuration.c
+test_ide_configuration_CFLAGS = $(tests_cflags)
+test_ide_configuration_LDADD = $(tests_libs)
+test_ide_configuration_LDFLAGS = $(tests_ldflags)
+
+
 TESTS += test-ide-back-forward-list
 test_ide_back_forward_list_SOURCES = test-ide-back-forward-list.c
 test_ide_back_forward_list_CFLAGS = $(tests_cflags)
diff --git a/tests/test-ide-configuration.c b/tests/test-ide-configuration.c
new file mode 100644
index 0000000..f9cd713
--- /dev/null
+++ b/tests/test-ide-configuration.c
@@ -0,0 +1,72 @@
+/* test-configuration.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <ide.h>
+
+static void
+test_internal (void)
+{
+  g_autoptr(IdeConfiguration) configuration = NULL;
+
+  configuration = g_object_new (IDE_TYPE_CONFIGURATION,
+                                "id", "my-configuration",
+                                NULL);
+
+  ide_configuration_set_internal_string (configuration, "foo-string", NULL);
+  g_assert_cmpstr (ide_configuration_get_internal_string (configuration, "foo-string"), ==, NULL);
+  g_assert_cmpint (ide_configuration_get_internal_int (configuration, "foo-string"), ==, 0);
+  g_assert_cmpint (ide_configuration_get_internal_int64 (configuration, "foo-string"), ==, 0);
+  g_assert_cmpint (ide_configuration_get_internal_boolean (configuration, "foo-string"), ==, FALSE);
+
+  ide_configuration_set_internal_string (configuration, "foo-string", "foo");
+  g_assert_cmpstr (ide_configuration_get_internal_string (configuration, "foo-string"), ==, "foo");
+  g_assert_cmpint (ide_configuration_get_internal_int (configuration, "foo-string"), ==, 0);
+  g_assert_cmpint (ide_configuration_get_internal_int64 (configuration, "foo-string"), ==, 0);
+  g_assert_cmpint (ide_configuration_get_internal_boolean (configuration, "foo-string"), ==, FALSE);
+
+  ide_configuration_set_internal_int (configuration, "foo-string", 123);
+  g_assert_cmpstr (ide_configuration_get_internal_string (configuration, "foo-string"), ==, NULL);
+  g_assert_cmpint (ide_configuration_get_internal_int (configuration, "foo-string"), ==, 123);
+  g_assert_cmpint (ide_configuration_get_internal_int64 (configuration, "foo-string"), ==, 0);
+  g_assert_cmpint (ide_configuration_get_internal_boolean (configuration, "foo-string"), ==, FALSE);
+
+  ide_configuration_set_internal_int64 (configuration, "foo-string", 123);
+  g_assert_cmpstr (ide_configuration_get_internal_string (configuration, "foo-string"), ==, NULL);
+  g_assert_cmpint (ide_configuration_get_internal_int (configuration, "foo-string"), ==, 0);
+  g_assert_cmpint (ide_configuration_get_internal_int64 (configuration, "foo-string"), ==, 123);
+  g_assert_cmpint (ide_configuration_get_internal_boolean (configuration, "foo-string"), ==, FALSE);
+
+  ide_configuration_set_internal_boolean (configuration, "foo-string", TRUE);
+  g_assert_cmpstr (ide_configuration_get_internal_string (configuration, "foo-string"), ==, NULL);
+  g_assert_cmpint (ide_configuration_get_internal_int (configuration, "foo-string"), ==, 0);
+  g_assert_cmpint (ide_configuration_get_internal_int64 (configuration, "foo-string"), ==, 0);
+  g_assert_cmpint (ide_configuration_get_internal_boolean (configuration, "foo-string"), ==, TRUE);
+
+  g_object_add_weak_pointer (G_OBJECT (configuration), (gpointer *)&configuration);
+  g_object_unref (configuration);
+  g_assert (configuration == NULL);
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+  g_test_add_func ("/Ide/Configuration/internal", test_internal);
+  return g_test_run ();
+}


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