[libgit2-glib] Make GgitCommitParents implement vala iterator protocol



commit 75d73662b48ba81cb9ecfe10b4817cbfcc99af05
Author: Jesse van den Kieboom <jessevdk gmail com>
Date:   Sun Dec 14 19:15:20 2014 +0100

    Make GgitCommitParents implement vala iterator protocol

 examples/walk.c                    |    4 +-
 libgit2-glib/Makefile.am           |    2 +
 libgit2-glib/ggit-commit-parents.c |  213 ++++++++++++++++++++++++++++++++++++
 libgit2-glib/ggit-commit-parents.h |   77 +++++++++++++
 libgit2-glib/ggit-commit.c         |  138 +-----------------------
 libgit2-glib/ggit-commit.h         |   14 ---
 libgit2-glib/ggit.h.in             |    1 +
 7 files changed, 297 insertions(+), 152 deletions(-)
---
diff --git a/examples/walk.c b/examples/walk.c
index 6327ada..2494fcc 100644
--- a/examples/walk.c
+++ b/examples/walk.c
@@ -166,7 +166,7 @@ main (int   argc,
 
                commit_parents = ggit_commit_get_parents (commit);
 
-               if (ggit_commit_parents_size (commit_parents) > 0)
+               if (ggit_commit_parents_get_size (commit_parents) > 0)
                {
                        GgitCommit *parent_commit;
                        GgitTree *commit_tree;
@@ -194,7 +194,7 @@ main (int   argc,
 
                g_print ("----------------------------------------\n");
 
-               ggit_commit_parents_unref (commit_parents);
+               g_object_unref (commit_parents);
                g_free (committer_str);
                g_free (author_str);
                g_object_unref (committer);
diff --git a/libgit2-glib/Makefile.am b/libgit2-glib/Makefile.am
index 51fcc7d..3dd0f65 100644
--- a/libgit2-glib/Makefile.am
+++ b/libgit2-glib/Makefile.am
@@ -26,6 +26,7 @@ INST_H_FILES =                                        \
        ggit-cherry-pick-options.h              \
        ggit-clone-options.h                    \
        ggit-commit.h                           \
+       ggit-commit-parents.h                   \
        ggit-config.h                           \
        ggit-config-entry.h                     \
        ggit-cred.h                             \
@@ -90,6 +91,7 @@ C_FILES =                                     \
        ggit-cherry-pick-options.c              \
        ggit-clone-options.c                    \
        ggit-commit.c                           \
+       ggit-commit-parents.c                   \
        ggit-config.c                           \
        ggit-config-entry.c                     \
        ggit-convert.c                          \
diff --git a/libgit2-glib/ggit-commit-parents.c b/libgit2-glib/ggit-commit-parents.c
new file mode 100644
index 0000000..962883d
--- /dev/null
+++ b/libgit2-glib/ggit-commit-parents.c
@@ -0,0 +1,213 @@
+/*
+ * ggit-commit.c
+ * This file is part of libgit2-glib
+ *
+ * Copyright (C) 2014 - Jesse van den Kieboom
+ *
+ * libgit2-glib is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * libgit2-glib 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <git2.h>
+
+#include "ggit-commit-parents.h"
+#include "ggit-commit.h"
+#include "ggit-oid.h"
+
+#define GGIT_COMMIT_PARENTS_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), 
GGIT_TYPE_COMMIT_PARENTS, GgitCommitParentsPrivate))
+
+struct _GgitCommitParentsPrivate
+{
+       GgitCommit *commit;
+};
+
+enum
+{
+       PROP_0,
+       PROP_COMMIT,
+       PROP_SIZE
+};
+
+G_DEFINE_TYPE (GgitCommitParents, ggit_commit_parents, G_TYPE_OBJECT)
+
+static void
+ggit_commit_parents_get_property (GObject    *object,
+                                                                                                             
                          guint       prop_id,
+                                                                                                             
                          GValue     *value,
+                                                                                                             
                          GParamSpec *pspec)
+{
+       GgitCommitParents *parents = GGIT_COMMIT_PARENTS (object);
+
+       switch (prop_id)
+       {
+               case PROP_COMMIT:
+                       g_value_set_object (value, parents->priv->commit);
+                       break;
+               case PROP_SIZE:
+                       g_value_set_uint (value, ggit_commit_parents_get_size (parents));
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+ggit_commit_parents_set_property (GObject      *object,
+                                                                                                             
                          guint         prop_id,
+                                                                                                             
                          const GValue *value,
+                                                                                                             
                          GParamSpec   *pspec)
+{
+       GgitCommitParentsPrivate *priv = GGIT_COMMIT_PARENTS (object)->priv;
+
+       switch (prop_id)
+       {
+               case PROP_COMMIT:
+                       priv->commit = g_value_dup_object (value);
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+GgitCommitParents *
+ggit_commit_parents_new (GgitCommit *commit)
+{
+       return g_object_new (GGIT_TYPE_COMMIT_PARENTS, "commit", commit, NULL);
+}
+
+static void
+ggit_commit_parents_finalize (GObject *object)
+{
+       GgitCommitParents *parents = GGIT_COMMIT_PARENTS (object);
+
+       g_object_unref (parents->priv->commit);
+
+       G_OBJECT_CLASS (ggit_commit_parents_parent_class)->finalize (object);
+}
+
+static void
+ggit_commit_parents_class_init (GgitCommitParentsClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->finalize = ggit_commit_parents_finalize;
+       object_class->get_property = ggit_commit_parents_get_property;
+       object_class->set_property = ggit_commit_parents_set_property;
+
+       g_object_class_install_property (object_class,
+                                        PROP_COMMIT,
+                                        g_param_spec_object ("commit",
+                                                             "Commit",
+                                                             "The commit for the parents collection",
+                                                             GGIT_TYPE_COMMIT,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY |
+                                                             G_PARAM_STATIC_STRINGS));
+
+       g_object_class_install_property (object_class,
+                                        PROP_SIZE,
+                                        g_param_spec_uint ("size",
+                                                           "Size",
+                                                           "The size of the parents collection",
+                                                           0,
+                                                           G_MAXUINT,
+                                                           0,
+                                                           G_PARAM_READABLE |
+                                                           G_PARAM_STATIC_STRINGS));
+
+       g_type_class_add_private (object_class, sizeof (GgitCommitParentsPrivate));
+}
+
+static void
+ggit_commit_parents_init (GgitCommitParents *self)
+{
+       self->priv = GGIT_COMMIT_PARENTS_GET_PRIVATE (self);
+}
+
+/**
+ * ggit_commit_parents_get_size:
+ * @parents: a #GgitCommitParents.
+ *
+ * Get the number of parents in the parents collection.
+ *
+ * Returns: the number of parents.
+ *
+ **/
+guint
+ggit_commit_parents_get_size (GgitCommitParents *parents)
+{
+       git_commit *c;
+
+       g_return_val_if_fail (GGIT_IS_COMMIT_PARENTS (parents), 0);
+
+       c = _ggit_native_get (parents->priv->commit);
+       return (guint)git_commit_parentcount (c);
+}
+
+/**
+ * ggit_commit_parents_get:
+ * @parents: a #GgitCommitParents.
+ * @idx: the parent index.
+ *
+ * Get the #GgitCommit of a parent.
+ *
+ * Returns: (transfer full): a #GgitCommit.
+ *
+ **/
+GgitCommit *
+ggit_commit_parents_get (GgitCommitParents *parents,
+                                                                                                guint        
      idx)
+{
+       git_commit *c;
+       git_commit *p;
+
+       g_return_val_if_fail (GGIT_IS_COMMIT_PARENTS (parents), NULL);
+
+       c = _ggit_native_get (parents->priv->commit);
+
+       if (git_commit_parent (&p, c, idx) == GIT_OK)
+       {
+               return _ggit_commit_wrap (p, TRUE);
+       }
+
+       return NULL;
+}
+
+/**
+ * ggit_commit_parents_get_id:
+ * @parents: a #GgitCommitParents.
+ * @idx: the parent index.
+ *
+ * Get the #GgitOId of a parent.
+ *
+ * Returns: (transfer full): a #GgitOId.
+ *
+ **/
+GgitOId *
+ggit_commit_parents_get_id (GgitCommitParents *parents,
+                                                                                                             
  guint              idx)
+{
+       git_commit *c;
+       const git_oid *oid;
+
+       g_return_val_if_fail (GGIT_IS_COMMIT_PARENTS (parents), NULL);
+
+       c = _ggit_native_get (parents->priv->commit);
+
+       oid = git_commit_parent_id (c, idx);
+       return _ggit_oid_wrap (oid);
+}
+
+/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-commit-parents.h b/libgit2-glib/ggit-commit-parents.h
new file mode 100644
index 0000000..6713ab3
--- /dev/null
+++ b/libgit2-glib/ggit-commit-parents.h
@@ -0,0 +1,77 @@
+/*
+ * ggit-commit.h
+ * This file is part of libgit2-glib
+ *
+ * Copyright (C) 2014 - Jesse van den Kieboom
+ *
+ * libgit2-glib is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * libgit2-glib 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef __GGIT_COMMIT_PARENTS_H__
+#define __GGIT_COMMIT_PARENTS_H__
+
+#include <glib-object.h>
+
+#include <libgit2-glib/ggit-types.h>
+
+G_BEGIN_DECLS
+
+#define GGIT_TYPE_COMMIT_PARENTS    (ggit_commit_parents_get_type ())
+#define GGIT_COMMIT_PARENTS(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GGIT_TYPE_COMMIT_PARENTS, 
GgitCommitParents))
+#define GGIT_COMMIT_PARENTS_CLASS(klass)  (G_TYPE_CHECK_CLASS_CAST ((klass), GGIT_TYPE_COMMIT_PARENTS, 
GgitCommitParentsClass))
+#define GGIT_IS_COMMIT_PARENTS(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GGIT_TYPE_COMMIT_PARENTS))
+#define GGIT_IS_COMMIT_PARENTS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GGIT_TYPE_COMMIT_PARENTS))
+#define GGIT_COMMIT_PARENTS_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GGIT_TYPE_COMMIT_PARENTS, 
GgitCommitParentsClass))
+
+typedef struct _GgitCommitParentsClass   GgitCommitParentsClass;
+typedef struct _GgitCommitParentsPrivate GgitCommitParentsPrivate;
+
+struct _GgitCommitParents
+{
+  /*< private >*/
+  GObject parent;
+
+  /* priv padding */
+  GgitCommitParentsPrivate *priv;
+};
+
+/**
+ * GgitCommitParentsClass:
+ * @parent_class: The parent class.
+ *
+ * The class structure for #GgitCommitParentClass.
+ */
+struct _GgitCommitParentsClass
+{
+  /*< private >*/
+  GObjectClass parent_class;
+};
+
+GType                ggit_commit_parents_get_type     (void) G_GNUC_CONST;
+GgitCommitParents   *ggit_commit_parents_new          (GgitCommit        *commit);
+
+guint                ggit_commit_parents_get_size     (GgitCommitParents *parents);
+
+GgitCommit          *ggit_commit_parents_get          (GgitCommitParents *parents,
+                                                       guint              idx);
+
+GgitOId             *ggit_commit_parents_get_id       (GgitCommitParents *parents,
+                                                       guint              idx);
+
+G_END_DECLS
+
+#endif /* __GGIT_COMMIT_PARENTS_H__ */
+
+/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-commit.c b/libgit2-glib/ggit-commit.c
index 84d7051..6d9f388 100644
--- a/libgit2-glib/ggit-commit.c
+++ b/libgit2-glib/ggit-commit.c
@@ -26,15 +26,10 @@
 #include "ggit-oid.h"
 #include "ggit-convert.h"
 #include "ggit-tree.h"
+#include "ggit-commit-parents.h"
 
 #define GGIT_COMMIT_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GGIT_TYPE_COMMIT, 
GgitCommitPrivate))
 
-struct _GgitCommitParents
-{
-       GgitCommit *commit;
-       gint ref_count;
-};
-
 struct _GgitCommitPrivate
 {
        gchar *message_utf8;
@@ -43,61 +38,6 @@ struct _GgitCommitPrivate
 
 G_DEFINE_TYPE (GgitCommit, ggit_commit, GGIT_TYPE_OBJECT)
 
-G_DEFINE_BOXED_TYPE (GgitCommitParents,
-                     ggit_commit_parents,
-                     ggit_commit_parents_ref,
-                     ggit_commit_parents_unref)
-
-static GgitCommitParents *
-ggit_commit_parents_wrap (GgitCommit *commit)
-{
-       GgitCommitParents *ret;
-
-       ret = g_slice_new (GgitCommitParents);
-       ret->commit = g_object_ref (commit);
-       ret->ref_count = 1;
-
-       return ret;
-}
-
-/**
- * ggit_commit_parents_ref:
- * @parents: a #GgitCommitParents.
- *
- * Atomically increments the reference count of @parents by one.
- * This function is MT-safe and may be called from any thread.
- *
- * Returns: a #GgitCommitParents.
- **/
-GgitCommitParents *
-ggit_commit_parents_ref (GgitCommitParents *parents)
-{
-       g_return_val_if_fail (parents != NULL, NULL);
-
-       g_atomic_int_inc (&parents->ref_count);
-
-       return parents;
-}
-
-/**
- * ggit_commit_parents_unref:
- * @parents: a #GgitCommitParents.
- *
- * Atomically decrements the reference count of @parents by one.
- * If the reference count drops to 0, @parents is freed.
- **/
-void
-ggit_commit_parents_unref (GgitCommitParents *parents)
-{
-       g_return_if_fail (parents != NULL);
-
-       if (g_atomic_int_dec_and_test (&parents->ref_count))
-       {
-               g_clear_object (&parents->commit);
-               g_slice_free (GgitCommitParents, parents);
-       }
-}
-
 static void
 ggit_commit_finalize (GObject *object)
 {
@@ -319,81 +259,7 @@ ggit_commit_get_parents (GgitCommit *commit)
 {
        g_return_val_if_fail (GGIT_IS_COMMIT (commit), NULL);
 
-       return ggit_commit_parents_wrap (commit);
-}
-
-/**
- * ggit_commit_parents_size:
- * @parents: a #GgitCommitParents.
- *
- * Get the number of parents in the parents collection.
- *
- * Returns: the number of parents.
- *
- **/
-guint
-ggit_commit_parents_size (GgitCommitParents *parents)
-{
-       git_commit *c;
-
-       g_return_val_if_fail (parents != NULL, 0);
-
-       c = _ggit_native_get (parents->commit);
-       return (guint)git_commit_parentcount (c);
-}
-
-/**
- * ggit_commit_parents_get:
- * @parents: a #GgitCommitParents.
- * @idx: the parent index.
- *
- * Get the #GgitCommit of a parent.
- *
- * Returns: (transfer full): a #GgitCommit.
- *
- **/
-GgitCommit *
-ggit_commit_parents_get (GgitCommitParents *parents,
-                         guint              idx)
-{
-       git_commit *c;
-       git_commit *p;
-
-       g_return_val_if_fail (parents != NULL, NULL);
-
-       c = _ggit_native_get (parents->commit);
-
-       if (git_commit_parent (&p, c, idx) == GIT_OK)
-       {
-               return _ggit_commit_wrap (p, TRUE);
-       }
-
-       return NULL;
-}
-
-/**
- * ggit_commit_parents_get_id:
- * @parents: a #GgitCommitParents.
- * @idx: the parent index.
- *
- * Get the #GgitOId of a parent.
- *
- * Returns: (transfer full): a #GgitOId.
- *
- **/
-GgitOId *
-ggit_commit_parents_get_id (GgitCommitParents *parents,
-                            guint              idx)
-{
-       git_commit *c;
-       const git_oid *oid;
-
-       g_return_val_if_fail (parents != NULL, NULL);
-
-       c = _ggit_native_get (parents->commit);
-
-       oid = git_commit_parent_id (c, idx);
-       return _ggit_oid_wrap (oid);
+       return ggit_commit_parents_new (commit);
 }
 
 /**
diff --git a/libgit2-glib/ggit-commit.h b/libgit2-glib/ggit-commit.h
index d1a7635..0ce3ede 100644
--- a/libgit2-glib/ggit-commit.h
+++ b/libgit2-glib/ggit-commit.h
@@ -62,7 +62,6 @@ struct _GgitCommitClass
 };
 
 GType                ggit_commit_get_type             (void) G_GNUC_CONST;
-GType                ggit_commit_parents_get_type     (void) G_GNUC_CONST;
 
 GgitCommit          *_ggit_commit_wrap                (git_commit        *commit,
                                                        gboolean           owned);
@@ -79,19 +78,6 @@ GgitSignature       *ggit_commit_get_author           (GgitCommit        *commit
 
 GgitCommitParents   *ggit_commit_get_parents          (GgitCommit        *commit);
 
-
-
-GgitCommitParents   *ggit_commit_parents_ref          (GgitCommitParents *parents);
-void                 ggit_commit_parents_unref        (GgitCommitParents *parents);
-
-guint                ggit_commit_parents_size         (GgitCommitParents *parents);
-
-GgitCommit          *ggit_commit_parents_get          (GgitCommitParents *parents,
-                                                       guint              idx);
-
-GgitOId             *ggit_commit_parents_get_id       (GgitCommitParents *parents,
-                                                       guint              idx);
-
 GgitTree            *ggit_commit_get_tree             (GgitCommit        *commit);
 
 GgitOId             *ggit_commit_get_tree_id          (GgitCommit        *commit);
diff --git a/libgit2-glib/ggit.h.in b/libgit2-glib/ggit.h.in
index 9093a40..eac36ca 100644
--- a/libgit2-glib/ggit.h.in
+++ b/libgit2-glib/ggit.h.in
@@ -27,6 +27,7 @@
 #include <libgit2-glib/ggit-branch.h>
 #include <libgit2-glib/ggit-clone-options.h>
 #include <libgit2-glib/ggit-commit.h>
+#include <libgit2-glib/ggit-commit-parents.h>
 #include <libgit2-glib/ggit-config-entry.h>
 #include <libgit2-glib/ggit-config.h>
 #include <libgit2-glib/ggit-cred.h>


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