[template-glib] add gobject introspection support



commit 329182e5764f2606fe118a06941f64608e10a23b
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jan 11 06:09:31 2016 -0800

    add gobject introspection support

 build-aux/Makefile.am.enums |   52 +++++++++++++++++++++++++++++++++++++
 configure.ac                |    7 +++++
 examples/simple.c           |    2 +-
 examples/simple.py          |   26 ++++++++++++++++++
 src/Makefile.am             |   60 +++++++++++++++++++++++++++++++++++++++++++
 src/main.c                  |    2 +-
 src/tmpl-enums.c.in         |   42 ++++++++++++++++++++++++++++++
 src/tmpl-enums.h.in         |   24 +++++++++++++++++
 src/tmpl-error.h            |    2 +-
 src/tmpl-expr-eval.c        |    2 +-
 src/tmpl-expr-parser.y      |    6 ++++
 src/tmpl-expr-types.h       |   20 +++++--------
 src/tmpl-scope.c            |   44 ++++++++++++++++++++++++++++++-
 src/tmpl-scope.h            |   21 ++++++++-------
 src/tmpl-symbol.c           |   16 +++++++++++
 src/tmpl-symbol.h           |    2 +-
 src/tmpl-template-locator.c |   10 +++++++
 src/tmpl-template.c         |    4 +-
 18 files changed, 312 insertions(+), 30 deletions(-)
---
diff --git a/build-aux/Makefile.am.enums b/build-aux/Makefile.am.enums
new file mode 100644
index 0000000..2fd69d5
--- /dev/null
+++ b/build-aux/Makefile.am.enums
@@ -0,0 +1,52 @@
+# Rules for generating enumeration types using glib-mkenums
+#
+# Define:
+#      glib_enum_h = header template file
+#      glib_enum_c = source template file
+#      glib_enum_headers = list of headers to parse
+#
+# before including Makefile.am.enums. You will also need to have
+# the following targets already defined:
+#
+#      CLEANFILES
+#      DISTCLEANFILES
+#      BUILT_SOURCES
+#      EXTRA_DIST
+#
+# Author: Emmanuele Bassi <ebassi linux intel com>
+
+# Basic sanity checks
+$(if $(GLIB_MKENUMS),,$(error Need to define GLIB_MKENUMS))
+
+$(if $(or $(glib_enum_h), \
+          $(glib_enum_c)),, \
+    $(error Need to define glib_enum_h and glib_enum_c))
+
+$(if $(glib_enum_headers),,$(error Need to define glib_enum_headers))
+
+enum_tmpl_h=$(addprefix $(srcdir)/, $(glib_enum_h:.h=.h.in))
+enum_tmpl_c=$(addprefix $(srcdir)/, $(glib_enum_c:.c=.c.in))
+enum_headers=$(addprefix $(srcdir)/, $(glib_enum_headers))
+
+CLEANFILES += stamp-enum-types
+DISTCLEANFILES += $(glib_enum_h) $(glib_enum_c)
+BUILT_SOURCES += $(glib_enum_h) $(glib_enum_c)
+EXTRA_DIST += $(enum_tmpl_h) $(enum_tmpl_c)
+
+stamp-enum-types: $(enum_headers) $(enum_tmpl_h)
+       $(AM_V_GEN)$(GLIB_MKENUMS) \
+               --template $(enum_tmpl_h) \
+       $(enum_headers) > xgen-eh \
+       && (cmp -s xgen-eh $(glib_enum_h) || cp -f xgen-eh $(glib_enum_h)) \
+       && rm -f xgen-eh \
+       && echo timestamp > $(@F)
+
+$(glib_enum_h): stamp-enum-types
+       @true
+
+$(glib_enum_c): $(enum_headers) $(enum_tmpl_h) $(enum_tmpl_c)
+       $(AM_V_GEN)$(GLIB_MKENUMS) \
+               --template $(enum_tmpl_c) \
+       $(enum_headers) > xgen-ec \
+       && cp -f xgen-ec $(glib_enum_c) \
+       && rm -f xgen-ec
diff --git a/configure.ac b/configure.ac
index 3cd77ae..3e7faa0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -226,6 +226,13 @@ AS_IF([test "$enable_profiling" = "yes"],[
 AC_MSG_RESULT([$enable_profiling])
 
 
+
+dnl ***********************************************************************
+dnl Enable GObject Introspection bindings
+dnl ***********************************************************************
+GOBJECT_INTROSPECTION_CHECK([1.42.0])
+
+
 dnl ***********************************************************************
 dnl Process .in Files
 dnl ***********************************************************************
diff --git a/examples/simple.c b/examples/simple.c
index 8ebb44e..72f0ac8 100644
--- a/examples/simple.c
+++ b/examples/simple.c
@@ -32,7 +32,7 @@ main (gint   argc,
    * Now create our scope used to expand the template,
    * and assign the "title" variable in the scope.
    */
-  scope = tmpl_scope_new (NULL);
+  scope = tmpl_scope_new ();
 
   symbol = tmpl_scope_get (scope, "title");
   tmpl_symbol_assign_string (symbol, "My Title");
diff --git a/examples/simple.py b/examples/simple.py
new file mode 100644
index 0000000..87fbcfe
--- /dev/null
+++ b/examples/simple.py
@@ -0,0 +1,26 @@
+import gi
+
+gi.require_version('Template', '1.0')
+
+from gi.repository import Gio
+from gi.repository import Template
+
+# Get our file to process
+file = Gio.File.new_for_path('simple.tmpl')
+
+# Create a new template and parse our input file
+tmpl = Template.Template()
+tmpl.parse_file(file, None)
+
+# Create scope for expansion
+scope = Template.Scope.new()
+
+# Create and assign "title" variable in scope
+title = scope.get('title')
+title.assign_string('Example Title')
+
+# Write to stdout
+stream = Gio.UnixOutputStream.new(0, False)
+
+# Expand the template into stream
+tmpl.expand(stream, scope, None)
diff --git a/src/Makefile.am b/src/Makefile.am
index e65b4be..138aa39 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,3 +1,7 @@
+DISTCLEANFILES =
+CLEANFILES =
+BUILT_SOURCES =
+
 bin_PROGRAMS = template-glib-eval
 lib_LTLIBRARIES = libtemplate-glib-1.0.la
 
@@ -36,6 +40,8 @@ libtemplate_glib_1_0_la_SOURCES = \
        tmpl-condition-node.c \
        tmpl-condition-node.h \
        tmpl-debug.h \
+       tmpl-enums.c \
+       tmpl-enums.h \
        tmpl-error.c \
        tmpl-expr-eval.c \
        tmpl-expr-node.c \
@@ -71,6 +77,60 @@ libtemplate_glib_1_0_la_SOURCES = \
        tmpl-util.c \
        $(NULL)
 
+glib_enum_h = tmpl-enums.h
+glib_enum_c = tmpl-enums.c
+glib_enum_headers = \
+       tmpl-error.h \
+       tmpl-expr-types.h \
+       $(NULL)
+include $(top_srcdir)/build-aux/Makefile.am.enums
+
+if HAVE_INTROSPECTION
+-include $(INTROSPECTION_MAKEFILE)
+
+INTROSPECTION_GIRS =
+INTROSPECTION_SCANNER_ARGS = --add-include-path=$(srcdir) --warn-all
+INTROSPECTION_COMPILER_ARGS = --includedir=$(srcdir)
+
+introspection_sources = \
+       tmpl-enums.c \
+       tmpl-enums.h \
+       tmpl-error.h \
+       tmpl-expr.c \
+       tmpl-expr.h \
+       tmpl-expr-types.h \
+       tmpl-scope.c \
+       tmpl-scope.h \
+       tmpl-symbol.c \
+       tmpl-symbol.h \
+       tmpl-template-locator.c \
+       tmpl-template-locator.h \
+       tmpl-template.c \
+       tmpl-template.h \
+       $(NULL)
+
+Template-1.0.gir: libtemplate-glib-1.0.la
+Template_1_0_gir_INCLUDES = Gio-2.0
+Template_1_0_gir_CFLAGS = $(libtemplate_glib_1_0_la_CFLAGS)
+Template_1_0_gir_LIBS = libtemplate-glib-1.0.la
+Template_1_0_gir_FILES = $(introspection_sources)
+Template_1_0_gir_SCANNERFLAGS = \
+       -n Template \
+       --identifier-prefix Tmpl \
+       --symbol-prefix tmpl \
+       -DTMPL_GLIB_COMPILATION \
+       $(NULL)
+INTROSPECTION_GIRS += Template-1.0.gir
+
+girdir = $(datadir)/gir-1.0
+gir_DATA = $(INTROSPECTION_GIRS)
+
+typelibdir = $(libdir)/girepository-1.0
+typelib_DATA = $(INTROSPECTION_GIRS:.gir=.typelib)
+
+CLEANFILES += $(gir_DATA) $(typelib_DATA)
+endif
+
 GITIGNOREFILES = \
        tmpl-expr-parser.c \
        tmpl-expr-parser.h \
diff --git a/src/main.c b/src/main.c
index 2789a3b..76de746 100644
--- a/src/main.c
+++ b/src/main.c
@@ -45,7 +45,7 @@ main (gint   argc,
   tmpl_template_locator_prepend_search_path (locator, ".");
   tmpl = tmpl_template_new (locator);
   file = g_file_new_for_commandline_arg (argv [1]);
-  scope = tmpl_scope_new (NULL);
+  scope = tmpl_scope_new ();
 
   if (!tmpl_template_parse_file (tmpl, file, NULL, &error))
     {
diff --git a/src/tmpl-enums.c.in b/src/tmpl-enums.c.in
new file mode 100644
index 0000000..e94c599
--- /dev/null
+++ b/src/tmpl-enums.c.in
@@ -0,0 +1,42 @@
+/*** BEGIN file-header ***/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "tmpl-enums.h"
+
+#include "tmpl-error.h"
+#include "tmpl-expr-types.h"
+
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+/* enumerations from "@filename@" */
+/*** END file-production ***/
+
+/*** BEGIN value-header ***/
+GType
+ enum_name@_get_type (void)
+{
+    static GType etype = 0;
+    if (G_UNLIKELY(etype == 0)) {
+        static const G Type@Value values[] = {
+/*** END value-header ***/
+
+/*** BEGIN value-production ***/
+            { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
+/*** END value-production ***/
+
+/*** BEGIN value-tail ***/
+            { 0, NULL, NULL }
+        };
+        etype = g_ type@_register_static (g_intern_static_string ("@EnumName@"), values);
+    }
+    return etype;
+}
+
+/*** END value-tail ***/
+
+/*** BEGIN file-tail ***/
+
+/*** END file-tail ***/
diff --git a/src/tmpl-enums.h.in b/src/tmpl-enums.h.in
new file mode 100644
index 0000000..3722cdc
--- /dev/null
+++ b/src/tmpl-enums.h.in
@@ -0,0 +1,24 @@
+/*** BEGIN file-header ***/
+#ifndef __IDE_ENUMS_H__
+#define __IDE_ENUMS_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+
+/* enumerations from "@filename@" */
+/*** END file-production ***/
+
+/*** BEGIN value-header ***/
+GType @enum_name _get_type (void);
+#define @ENUMPREFIX _TYPE_@ENUMSHORT@ (@enum_name _get_type ())
+/*** END value-header ***/
+
+/*** BEGIN file-tail ***/
+G_END_DECLS
+
+#endif /* __IDE_ENUMS_H__ */
+/*** END file-tail ***/
diff --git a/src/tmpl-error.h b/src/tmpl-error.h
index d3b379f..b475293 100644
--- a/src/tmpl-error.h
+++ b/src/tmpl-error.h
@@ -49,7 +49,7 @@ typedef enum
   TMPL_ERROR_NOT_IMPLEMENTED,
   TMPL_ERROR_NOT_A_VALUE,
   TMPL_ERROR_NOT_A_FUNCTION,
-} GisError;
+} TmplError;
 
 GQuark tmpl_error_quark (void);
 
diff --git a/src/tmpl-expr-eval.c b/src/tmpl-expr-eval.c
index 0e85045..0b7ac5e 100644
--- a/src/tmpl-expr-eval.c
+++ b/src/tmpl-expr-eval.c
@@ -700,7 +700,7 @@ tmpl_expr_user_fn_call_eval (TmplExprUserFnCall  *node,
   expr = tmpl_symbol_get_expr (symbol, &args);
   n_args = args != NULL ? args->len : 0;
 
-  local_scope = tmpl_scope_new (scope);
+  local_scope = tmpl_scope_new_with_parent (scope);
 
   params = node->params;
 
diff --git a/src/tmpl-expr-parser.y b/src/tmpl-expr-parser.y
index 3f8f5aa..1ed01c1 100644
--- a/src/tmpl-expr-parser.y
+++ b/src/tmpl-expr-parser.y
@@ -86,11 +86,17 @@ expr: /* nothing */ EOL {
     YYACCEPT;
   }
   | FUNC NAME '(' symlist ')' '=' list EOL {
+    /* todo: add ast node to define the expr on the scope
+     * when evaluated.
+     */
     //tmpl_scope_add_user_func (parser->scope, $2, $4, $7);
     parser->ast = NULL;
     YYACCEPT;
   }
   | FUNC NAME '(' ')' '=' list EOL {
+    /* todo: add ast node to define the expr on the scope
+     * when evaluated.
+     */
     //tmpl_scope_add_user_func (parser->scope, $2, NULL, $6);
     parser->ast = NULL;
     YYACCEPT;
diff --git a/src/tmpl-expr-types.h b/src/tmpl-expr-types.h
index 141fd80..1dab4cd 100644
--- a/src/tmpl-expr-types.h
+++ b/src/tmpl-expr-types.h
@@ -25,14 +25,13 @@
 
 #include <glib-object.h>
 
+#include "tmpl-enums.h"
+
 G_BEGIN_DECLS
 
-#define TMPL_TYPE_EXPR         (tmpl_expr_get_type())
-#define TMPL_TYPE_EXPR_BUILTIN (tmpl_expr_builtin_get_type())
-#define TMPL_TYPE_EXPR_TYPE    (tmpl_expr_type_get_type())
-#define TMPL_TYPE_SCOPE        (tmpl_scope_get_type())
-#define TMPL_TYPE_SYMBOL       (tmpl_symbol_get_type())
-#define TMPL_TYPE_SYMBOL_TYPE  (tmpl_symbol_type_get_type())
+#define TMPL_TYPE_EXPR   (tmpl_expr_get_type())
+#define TMPL_TYPE_SCOPE  (tmpl_scope_get_type())
+#define TMPL_TYPE_SYMBOL (tmpl_symbol_get_type())
 
 typedef union  _TmplExpr   TmplExpr;
 typedef struct _TmplScope  TmplScope;
@@ -85,12 +84,9 @@ typedef enum
   TMPL_EXPR_BUILTIN_SQRT,
 } TmplExprBuiltin;
 
-GType tmpl_expr_builtin_get_type (void);
-GType tmpl_expr_type_get_type    (void);
-GType tmpl_expr_get_type         (void);
-GType tmpl_scope_get_type        (void);
-GType tmpl_symbol_get_type       (void);
-GType tmpl_symbol_type_get_type  (void);
+GType tmpl_expr_get_type   (void);
+GType tmpl_scope_get_type  (void);
+GType tmpl_symbol_get_type (void);
 
 G_END_DECLS
 
diff --git a/src/tmpl-scope.c b/src/tmpl-scope.c
index edc4510..f1c892a 100644
--- a/src/tmpl-scope.c
+++ b/src/tmpl-scope.c
@@ -53,8 +53,36 @@ tmpl_scope_unref (TmplScope *self)
     }
 }
 
+/**
+ * tmpl_scope_new:
+ *
+ * Creates a new scope to contain variables and custom expressions,
+ *
+ * Returns: (transfer full): A newly created #TmplScope.
+ */
+TmplScope *
+tmpl_scope_new (void)
+{
+  TmplScope *self;
+
+  self = g_slice_new0 (TmplScope);
+  self->ref_count = 1;
+  self->parent = NULL;
+
+  return self;
+}
+
+/**
+ * tmpl_scope_new_with_parent:
+ * @parent: (nullable): An optional parent scope
+ *
+ * Creates a new scope to contain variables and custom expressions,
+ * If @parent is set, the parent scope will be inherited.
+ *
+ * Returns: (transfer full): A newly created #TmplScope.
+ */
 TmplScope *
-tmpl_scope_new (TmplScope *parent)
+tmpl_scope_new_with_parent (TmplScope *parent)
 {
   TmplScope *self;
 
@@ -111,6 +139,13 @@ tmpl_scope_get_full (TmplScope   *self,
   return symbol;
 }
 
+/**
+ * tmpl_scope_get:
+ *
+ * If the symbol could not be found, it will be allocated.
+ *
+ * Returns: (transfer none): A #TmplSymbol.
+ */
 TmplSymbol *
 tmpl_scope_get (TmplScope   *self,
                 const gchar *name)
@@ -118,6 +153,13 @@ tmpl_scope_get (TmplScope   *self,
   return tmpl_scope_get_full (self, name, TRUE);
 }
 
+/**
+ * tmpl_scope_peek:
+ *
+ * If the symbol could not be found, %NULL is returned.
+ *
+ * Returns: (transfer none) (nullable): A #TmplSymbol or %NULL.
+ */
 TmplSymbol *
 tmpl_scope_peek (TmplScope   *self,
                  const gchar *name)
diff --git a/src/tmpl-scope.h b/src/tmpl-scope.h
index 03f0686..60efd9a 100644
--- a/src/tmpl-scope.h
+++ b/src/tmpl-scope.h
@@ -27,16 +27,17 @@
 
 G_BEGIN_DECLS
 
-TmplScope  *tmpl_scope_new   (TmplScope   *parent);
-TmplScope  *tmpl_scope_ref   (TmplScope   *self);
-void        tmpl_scope_unref (TmplScope   *self);
-TmplSymbol *tmpl_scope_peek  (TmplScope   *self,
-                              const gchar *name);
-TmplSymbol *tmpl_scope_get   (TmplScope   *self,
-                              const gchar *name);
-void        tmpl_scope_set   (TmplScope   *self,
-                              const gchar *name,
-                              TmplSymbol  *symbol);
+TmplScope  *tmpl_scope_new             (void);
+TmplScope  *tmpl_scope_new_with_parent (TmplScope   *parent);
+TmplScope  *tmpl_scope_ref             (TmplScope   *self);
+void        tmpl_scope_unref           (TmplScope   *self);
+TmplSymbol *tmpl_scope_peek            (TmplScope   *self,
+                                        const gchar *name);
+TmplSymbol *tmpl_scope_get             (TmplScope   *self,
+                                        const gchar *name);
+void        tmpl_scope_set             (TmplScope   *self,
+                                        const gchar *name,
+                                        TmplSymbol  *symbol);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (TmplScope, tmpl_scope_unref)
 
diff --git a/src/tmpl-symbol.c b/src/tmpl-symbol.c
index 0736774..78339c7 100644
--- a/src/tmpl-symbol.c
+++ b/src/tmpl-symbol.c
@@ -100,6 +100,15 @@ tmpl_symbol_assign_value (TmplSymbol   *self,
     }
 }
 
+/**
+ * tmpl_symbol_assign_expr: (skip)
+ * @self: A #TmplSymbol.
+ * @expr: (nullable): An expression to assign, or %NULL.
+ * params: (element-type utf8): A #GPtrArray of strings.
+ *
+ * Sets the symbol as a %TMPL_SYMBOL_EXPR with the given ordered and
+ * named parameters.
+ */
 void
 tmpl_symbol_assign_expr (TmplSymbol *self,
                          TmplExpr   *expr,
@@ -126,6 +135,13 @@ tmpl_symbol_get_symbol_type (TmplSymbol *self)
   return self->type;
 }
 
+/**
+ * tmpl_symbol_get_expr:
+ * @self: A #TmplSymbol
+ * @params: (out) (element-type utf8) (transfer none) (nullable): A list of parameters
+ *
+ * Returns: (transfer none): A #TmplExpr.
+ */
 TmplExpr *
 tmpl_symbol_get_expr (TmplSymbol  *self,
                       GPtrArray  **params)
diff --git a/src/tmpl-symbol.h b/src/tmpl-symbol.h
index 92921ec..cb21d0f 100644
--- a/src/tmpl-symbol.h
+++ b/src/tmpl-symbol.h
@@ -34,7 +34,7 @@ TmplSymbolType  tmpl_symbol_get_symbol_type (TmplSymbol   *self);
 void            tmpl_symbol_get_value       (TmplSymbol   *self,
                                              GValue       *value);
 TmplExpr       *tmpl_symbol_get_expr        (TmplSymbol   *self,
-                                             GPtrArray   **args);
+                                             GPtrArray   **params);
 void            tmpl_symbol_assign_value    (TmplSymbol   *self,
                                              const GValue *value);
 void            tmpl_symbol_assign_boolean  (TmplSymbol   *self,
diff --git a/src/tmpl-template-locator.c b/src/tmpl-template-locator.c
index f31b8f5..8fc6b5a 100644
--- a/src/tmpl-template-locator.c
+++ b/src/tmpl-template-locator.c
@@ -185,6 +185,16 @@ tmpl_template_locator_new (void)
   return g_object_new (TMPL_TYPE_TEMPLATE_LOCATOR, NULL);
 }
 
+/**
+ * tmpl_template_locator_locate:
+ * @self: A #TmplTemplateLocator.
+ * @path: a relative path to the file
+ *
+ * This will resolve the relative path using the search paths found within
+ * the template loader.
+ *
+ * Returns: (transfer full): A #GInputStream or %NULL and @error is set.
+ */
 GInputStream *
 tmpl_template_locator_locate (TmplTemplateLocator  *self,
                               const gchar          *path,
diff --git a/src/tmpl-template.c b/src/tmpl-template.c
index a20ab47..f30efdd 100644
--- a/src/tmpl-template.c
+++ b/src/tmpl-template.c
@@ -341,7 +341,7 @@ tmpl_template_expand_visitor (TmplNode *node,
         {
           TmplIterator iter;
           TmplScope *old_scope = state->scope;
-          TmplScope *new_scope = tmpl_scope_new (old_scope);
+          TmplScope *new_scope = tmpl_scope_new_with_parent (old_scope);
           TmplSymbol *symbol;
 
           state->scope = new_scope;
@@ -420,7 +420,7 @@ tmpl_template_expand (TmplTemplate  *self,
 
 /**
  * tmpl_template_get_locator:
- * @template: A #TmplTemplate
+ * @self: A #TmplTemplate
  *
  * Gets the template locator used when resolving template includes.
  *


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