[template-glib] scope: add simplified interface to import into scope



commit 506d096045b2358ff42b3002dfeb82d46481a95d
Author: Christian Hergert <chergert redhat com>
Date:   Tue May 3 15:52:55 2022 -0700

    scope: add simplified interface to import into scope

 src/tmpl-scope.c | 35 +++++++++++++++++++++++++++++++++++
 src/tmpl-scope.h |  4 ++++
 2 files changed, 39 insertions(+)
---
diff --git a/src/tmpl-scope.c b/src/tmpl-scope.c
index 2b62acf..aa80ced 100644
--- a/src/tmpl-scope.c
+++ b/src/tmpl-scope.c
@@ -16,6 +16,9 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "config.h"
+
+#include "tmpl-gi-private.h"
 #include "tmpl-scope.h"
 #include "tmpl-symbol.h"
 
@@ -402,3 +405,35 @@ tmpl_scope_set_resolver (TmplScope         *self,
       self->resolver_destroy = destroy;
     }
 }
+
+/**
+ * tmpl_scope_require:
+ * @self: a #TmplScope
+ * @namespace_: the namespace to import into the scope
+ * @version: (nullable): the version of @namespace_ to import
+ *
+ * Imports @namespace_ into @self so it can be used by expressions.
+ *
+ * Returns: %TRUE if successful; otherwise %FALSE
+ */
+gboolean
+tmpl_scope_require (TmplScope    *self,
+                    const char   *namespace_,
+                    const char   *version)
+{
+  GITypelib *typelib;
+  GValue value = G_VALUE_INIT;
+
+  g_return_val_if_fail (self != NULL, FALSE);
+  g_return_val_if_fail (namespace_ != NULL, FALSE);
+
+  if (!(typelib = g_irepository_require (NULL, namespace_, version, 0, NULL)))
+    return FALSE;
+
+  g_value_init (&value, TMPL_TYPE_TYPELIB);
+  g_value_set_pointer (&value, typelib);
+  tmpl_scope_set_value (self, namespace_, &value);
+  g_value_unset (&value);
+
+  return TRUE;
+}
diff --git a/src/tmpl-scope.h b/src/tmpl-scope.h
index dde37cd..67c1211 100644
--- a/src/tmpl-scope.h
+++ b/src/tmpl-scope.h
@@ -92,6 +92,10 @@ void        tmpl_scope_set_resolver    (TmplScope         *self,
                                         TmplScopeResolver  resolver,
                                         gpointer           user_data,
                                         GDestroyNotify     destroy);
+TMPL_AVAILABLE_IN_3_36
+gboolean    tmpl_scope_require         (TmplScope         *self,
+                                        const char        *namespace_,
+                                        const char        *version);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (TmplScope, tmpl_scope_unref)
 


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