[latexila] Build messages: use GQueue instead of GNode



commit dd8e19f55d0754142d93d63843dd972d8e9ac102
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Sep 28 21:11:13 2014 +0200

    Build messages: use GQueue instead of GNode

 docs/reference/latexila-sections.txt               |    1 -
 src/liblatexila/latexila-build-job.c               |    2 +-
 src/liblatexila/latexila-build-view.c              |   19 ++++++---
 src/liblatexila/latexila-build-view.h              |   20 +++++++--
 .../latexila-post-processor-all-output.c           |   19 ++++-----
 src/liblatexila/latexila-post-processor-latex.c    |   17 +++-----
 src/liblatexila/latexila-post-processor.c          |   42 +++-----------------
 src/liblatexila/latexila-post-processor.h          |    6 +--
 8 files changed, 52 insertions(+), 74 deletions(-)
---
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index d273b9b..a589d6c 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -139,7 +139,6 @@ latexila_build_view_get_type
 <TITLE>LatexilaPostProcessor</TITLE>
 LatexilaPostProcessor
 LatexilaPostProcessorType
-latexila_build_messages_free
 latexila_post_processor_get_name_from_type
 latexila_post_processor_get_type_from_name
 latexila_post_processor_process_async
diff --git a/src/liblatexila/latexila-build-job.c b/src/liblatexila/latexila-build-job.c
index 0600993..96668bb 100644
--- a/src/liblatexila/latexila-build-job.c
+++ b/src/liblatexila/latexila-build-job.c
@@ -390,7 +390,7 @@ post_processor_cb (LatexilaPostProcessor *pp,
                    GAsyncResult          *result,
                    LatexilaBuildJob      *build_job)
 {
-  const GNode *messages;
+  const GQueue *messages;
 
   latexila_post_processor_process_finish (pp, result);
 
diff --git a/src/liblatexila/latexila-build-view.c b/src/liblatexila/latexila-build-view.c
index ec6b7b7..5a72249 100644
--- a/src/liblatexila/latexila-build-view.c
+++ b/src/liblatexila/latexila-build-view.c
@@ -132,6 +132,10 @@ latexila_build_msg_free (LatexilaBuildMsg *build_msg)
     {
       g_free (build_msg->text);
       g_free (build_msg->filename);
+
+      if (build_msg->children != NULL)
+        g_queue_free_full (build_msg->children, (GDestroyNotify) latexila_build_msg_free);
+
       g_slice_free (LatexilaBuildMsg, build_msg);
     }
 }
@@ -789,25 +793,28 @@ latexila_build_view_append_single_message (LatexilaBuildView *build_view,
 void
 latexila_build_view_append_messages (LatexilaBuildView *build_view,
                                      GtkTreeIter       *parent,
-                                     const GNode       *messages,
+                                     const GQueue      *messages,
                                      gboolean           expand)
 {
-  const GNode *node;
+  GList *l;
+
+  if (messages == NULL)
+    return;
 
-  for (node = messages; node != NULL; node = node->next)
+  for (l = messages->head; l != NULL; l = l->next)
     {
       GtkTreeIter child;
-      LatexilaBuildMsg *build_msg = node->data;
+      LatexilaBuildMsg *build_msg = l->data;
 
       g_assert (build_msg != NULL);
 
       child = latexila_build_view_append_single_message (build_view, parent, build_msg);
 
-      if (node->children != NULL)
+      if (build_msg->children != NULL)
         {
           latexila_build_view_append_messages (build_view,
                                                &child,
-                                               node->children,
+                                               build_msg->children,
                                                build_msg->expand);
         }
     }
diff --git a/src/liblatexila/latexila-build-view.h b/src/liblatexila/latexila-build-view.h
index 92b2ed6..b4ece6a 100644
--- a/src/liblatexila/latexila-build-view.h
+++ b/src/liblatexila/latexila-build-view.h
@@ -79,15 +79,13 @@ typedef enum
  * @filename: reference to a certain file.
  * @start_line: reference to a line in the file. -1 to unset.
  * @end_line: reference to a line in the file. -1 to unset.
- * @expand: if the message has children, whether to show them by default.
+ * @children: list of children of type #LatexilaBuildMsg.
+ * @expand: if the message has children, whether to initially show them.
  *
  * A build message, one line in the #GtkTreeView. If a @filename is provided,
  * the file will be opened when the user clicks on the message. If @start_line
  * and @end_line are provided, the lines between the two positions will be
  * selected.
- *
- * The @expand field assumes that a #LatexilaBuildMsg is included in a #GNode or
- * similar N-ary tree structure.
  */
 struct _LatexilaBuildMsg
 {
@@ -96,6 +94,18 @@ struct _LatexilaBuildMsg
   gchar *filename;
   gint start_line;
   gint end_line;
+
+  /* There are several reasons to use a GQueue:
+   * 1. A GQueue is convenient for appending at the end of the list.
+   * 2. An external GNode containing the build messages is not convenient for
+   * running sub-post-processors, for example the latex post-processor inside
+   * latexmk. A GNode has a reference to its parent, so it's more difficult to
+   * embed the messages of a sub-post-processor. Moreover, we don't need to know
+   * the parent, a GNode uses useless memory. A GSList would use even less
+   * memory, but it's less convenient to use.
+   */
+  GQueue *children;
+
   guint expand : 1;
 };
 
@@ -143,7 +153,7 @@ GtkTreeIter           latexila_build_view_append_single_message     (LatexilaBui
 
 void                  latexila_build_view_append_messages           (LatexilaBuildView *build_view,
                                                                      GtkTreeIter       *parent,
-                                                                     const GNode       *messages,
+                                                                     const GQueue      *messages,
                                                                      gboolean           expand);
 
 void                  latexila_build_view_remove_children           (LatexilaBuildView *build_view,
diff --git a/src/liblatexila/latexila-post-processor-all-output.c 
b/src/liblatexila/latexila-post-processor-all-output.c
index 392ab44..ce208b6 100644
--- a/src/liblatexila/latexila-post-processor-all-output.c
+++ b/src/liblatexila/latexila-post-processor-all-output.c
@@ -30,11 +30,12 @@
 
 struct _LatexilaPostProcessorAllOutputPrivate
 {
-  GNode *messages;
-  GNode *last_message;
+  GQueue *messages;
 };
 
-G_DEFINE_TYPE_WITH_PRIVATE (LatexilaPostProcessorAllOutput, latexila_post_processor_all_output, 
LATEXILA_TYPE_POST_PROCESSOR)
+G_DEFINE_TYPE_WITH_PRIVATE (LatexilaPostProcessorAllOutput,
+                            latexila_post_processor_all_output,
+                            LATEXILA_TYPE_POST_PROCESSOR)
 
 static void
 latexila_post_processor_all_output_process_lines (LatexilaPostProcessor  *post_processor,
@@ -49,20 +50,18 @@ latexila_post_processor_all_output_process_lines (LatexilaPostProcessor  *post_p
       msg->text = lines[i];
       msg->type = LATEXILA_BUILD_MSG_TYPE_INFO;
 
-      pp->priv->last_message = g_node_insert_data_after (pp->priv->messages,
-                                                         pp->priv->last_message,
-                                                         msg);
+      g_queue_push_tail (pp->priv->messages, msg);
     }
 
   g_free (lines);
 }
 
-static const GNode *
+static const GQueue *
 latexila_post_processor_all_output_get_messages (LatexilaPostProcessor *post_processor)
 {
   LatexilaPostProcessorAllOutput *pp = LATEXILA_POST_PROCESSOR_ALL_OUTPUT (post_processor);
 
-  return pp->priv->messages->children;
+  return pp->priv->messages;
 }
 
 static void
@@ -70,7 +69,7 @@ latexila_post_processor_all_output_finalize (GObject *object)
 {
   LatexilaPostProcessorAllOutput *pp = LATEXILA_POST_PROCESSOR_ALL_OUTPUT (object);
 
-  latexila_build_messages_free (pp->priv->messages);
+  g_queue_free_full (pp->priv->messages, (GDestroyNotify) latexila_build_msg_free);
 
   G_OBJECT_CLASS (latexila_post_processor_all_output_parent_class)->finalize (object);
 }
@@ -92,7 +91,7 @@ latexila_post_processor_all_output_init (LatexilaPostProcessorAllOutput *pp)
 {
   pp->priv = latexila_post_processor_all_output_get_instance_private (pp);
 
-  pp->priv->messages = g_node_new (NULL);
+  pp->priv->messages = g_queue_new ();
 }
 
 /**
diff --git a/src/liblatexila/latexila-post-processor-latex.c b/src/liblatexila/latexila-post-processor-latex.c
index 68dc44c..7d8194c 100644
--- a/src/liblatexila/latexila-post-processor-latex.c
+++ b/src/liblatexila/latexila-post-processor-latex.c
@@ -85,10 +85,7 @@ struct _File
 
 struct _LatexilaPostProcessorLatexPrivate
 {
-  GNode *messages;
-
-  /* The last message is used to insert in O(1) at the end of 'messages'. */
-  GNode *last_message;
+  GQueue *messages;
 
   /* Current message. */
   LatexilaBuildMsg *cur_msg;
@@ -279,9 +276,7 @@ add_message (LatexilaPostProcessorLatex *pp,
       break;
     }
 
-  pp->priv->last_message = g_node_insert_data_after (pp->priv->messages,
-                                                     pp->priv->last_message,
-                                                     cur_msg);
+  g_queue_push_tail (pp->priv->messages, cur_msg);
 
   pp->priv->cur_msg = latexila_build_msg_new ();
 
@@ -1378,12 +1373,12 @@ latexila_post_processor_latex_process_lines (LatexilaPostProcessor  *post_proces
   g_free (lines);
 }
 
-static const GNode *
+static const GQueue *
 latexila_post_processor_latex_get_messages (LatexilaPostProcessor *post_processor)
 {
   LatexilaPostProcessorLatex *pp = LATEXILA_POST_PROCESSOR_LATEX (post_processor);
 
-  return pp->priv->messages->children;
+  return pp->priv->messages;
 }
 
 static void
@@ -1391,7 +1386,7 @@ latexila_post_processor_latex_finalize (GObject *object)
 {
   LatexilaPostProcessorLatex *pp = LATEXILA_POST_PROCESSOR_LATEX (object);
 
-  latexila_build_messages_free (pp->priv->messages);
+  g_queue_free_full (pp->priv->messages, (GDestroyNotify) latexila_build_msg_free);
 
   if (pp->priv->cur_msg != NULL)
     latexila_build_msg_free (pp->priv->cur_msg);
@@ -1428,7 +1423,7 @@ latexila_post_processor_latex_init (LatexilaPostProcessorLatex *pp)
 {
   pp->priv = latexila_post_processor_latex_get_instance_private (pp);
 
-  pp->priv->messages = g_node_new (NULL);
+  pp->priv->messages = g_queue_new ();
   pp->priv->cur_msg = latexila_build_msg_new ();
   pp->priv->state = STATE_START;
 }
diff --git a/src/liblatexila/latexila-post-processor.c b/src/liblatexila/latexila-post-processor.c
index 74f69c4..a270a02 100644
--- a/src/liblatexila/latexila-post-processor.c
+++ b/src/liblatexila/latexila-post-processor.c
@@ -134,36 +134,6 @@ latexila_post_processor_get_name_from_type (LatexilaPostProcessorType type)
     }
 }
 
-static gboolean
-free_build_msg (GNode    *node,
-                gpointer  user_data)
-{
-  latexila_build_msg_free (node->data);
-  return FALSE;
-}
-
-/**
- * latexila_build_messages_free:
- * @build_messages: a tree of #LatexilaBuildMsg's.
- *
- * Frees a tree of #LatexilaBuildMsg's.
- */
-void
-latexila_build_messages_free (GNode *build_messages)
-{
-  if (build_messages != NULL)
-    {
-      g_node_traverse (build_messages,
-                       G_POST_ORDER,
-                       G_TRAVERSE_ALL,
-                       -1,
-                       free_build_msg,
-                       NULL);
-
-      g_node_destroy (build_messages);
-    }
-}
-
 static void
 latexila_post_processor_get_property (GObject    *object,
                                       guint       prop_id,
@@ -257,7 +227,7 @@ latexila_post_processor_end_default (LatexilaPostProcessor *pp)
   /* Do nothing. */
 }
 
-static const GNode *
+static const GQueue *
 latexila_post_processor_get_messages_default (LatexilaPostProcessor *pp)
 {
   return NULL;
@@ -514,18 +484,18 @@ latexila_post_processor_process_finish (LatexilaPostProcessor *pp,
  * Obviously if the build view is passed to the post-processor, the latexmk
  * post-processor can output its messages only at the end. But another reason to
  * not pass the build view is for the unit tests. It is easier for the unit
- * tests to check the returned #GNode than analyzing a #GtkTreeView. Of course
- * it would be possible to keep also the messages in a #GNode and have this
+ * tests to check the returned #GQueue than analyzing a #GtkTreeView. Of course
+ * it would be possible to keep also the messages in a #GQueue and have this
  * function only for the unit tests, but it takes more memory (unless a custom
  * #GtkTreeModel is implemented), or another function is needed to configure
- * whether a #GNode is kept in memory or not... It becomes a little too
+ * whether a #GQueue is kept in memory or not... It becomes a little too
  * complicated, and doesn't really worth the effort as most users use latexmk.
  *
- * The current solution is "good enough".
+ * The current solution is "good enough". And "good enough" is... "good enough".
  *
  * Returns: the tree of filtered messages.
  */
-const GNode *
+const GQueue *
 latexila_post_processor_get_messages (LatexilaPostProcessor *pp)
 {
   g_return_val_if_fail (LATEXILA_IS_POST_PROCESSOR (pp), NULL);
diff --git a/src/liblatexila/latexila-post-processor.h b/src/liblatexila/latexila-post-processor.h
index 43e02d4..0df0650 100644
--- a/src/liblatexila/latexila-post-processor.h
+++ b/src/liblatexila/latexila-post-processor.h
@@ -83,7 +83,7 @@ struct _LatexilaPostProcessorClass
   /* Get the build messages. The elements are of type "LatexilaBuildMsg *".
    * This function is called after end().
    */
-  const GNode * (* get_messages) (LatexilaPostProcessor *pp);
+  const GQueue * (* get_messages) (LatexilaPostProcessor *pp);
 };
 
 GType                   latexila_post_processor_get_type              (void) G_GNUC_CONST;
@@ -93,8 +93,6 @@ gboolean                latexila_post_processor_get_type_from_name    (const gch
 
 const gchar *           latexila_post_processor_get_name_from_type    (LatexilaPostProcessorType type);
 
-void                    latexila_build_messages_free                  (GNode *build_messages);
-
 void                    latexila_post_processor_process_async         (LatexilaPostProcessor *pp,
                                                                        GFile                 *file,
                                                                        GInputStream          *stream,
@@ -105,7 +103,7 @@ void                    latexila_post_processor_process_async         (LatexilaP
 void                    latexila_post_processor_process_finish        (LatexilaPostProcessor *pp,
                                                                        GAsyncResult          *result);
 
-const GNode *           latexila_post_processor_get_messages          (LatexilaPostProcessor *pp);
+const GQueue *          latexila_post_processor_get_messages          (LatexilaPostProcessor *pp);
 
 G_END_DECLS
 


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