[gst-cvs] gstreamer: iterator: API: Add gst_iterator_new_single()

Sebastian Dröge slomo at kemper.freedesktop.org
Wed Aug 26 07:47:31 PDT 2009


Module: gstreamer
Branch: master
Commit: b2cab40745625f1b6f5bd4d9dd9d17f79ee42004
URL:    http://cgit.freedesktop.org/gstreamer/gstreamer/commit/?id=b2cab40745625f1b6f5bd4d9dd9d17f79ee42004

Author: Sebastian Dröge <sebastian.droege at collabora.co.uk>
Date:   Wed Aug 26 16:39:19 2009 +0200

iterator: API: Add gst_iterator_new_single()

This allows "iteration" over a single object of some type,
which happens often for the GstPadIterIntLinksFunction for example.

---

 docs/gst/gstreamer-sections.txt |    2 +
 gst/gstiterator.c               |   77 +++++++++++++++++++++++++++++++++++++++
 gst/gstiterator.h               |   18 +++++++++
 3 files changed, 97 insertions(+), 0 deletions(-)

diff --git a/docs/gst/gstreamer-sections.txt b/docs/gst/gstreamer-sections.txt
index 004c5b5..eb743d6 100644
--- a/docs/gst/gstreamer-sections.txt
+++ b/docs/gst/gstreamer-sections.txt
@@ -1069,6 +1069,7 @@ GstIteratorItemFunction
 GstIteratorResyncFunction
 GstIteratorFreeFunction
 GstIteratorFoldFunction
+GstCopyFunction
 
 GST_ITERATOR
 GST_ITERATOR_LOCK
@@ -1077,6 +1078,7 @@ GST_ITERATOR_ORIG_COOKIE
 
 gst_iterator_new
 gst_iterator_new_list
+gst_iterator_new_single
 
 gst_iterator_next
 gst_iterator_resync
diff --git a/gst/gstiterator.c b/gst/gstiterator.c
index b3121e0..e8bd745 100644
--- a/gst/gstiterator.c
+++ b/gst/gstiterator.c
@@ -658,3 +658,80 @@ gst_iterator_find_custom (GstIterator * it, GCompareFunc func,
   /* no need to unset, it's just a pointer */
   return g_value_get_pointer (&ret);
 }
+
+typedef struct
+{
+  GstIterator parent;
+  gpointer object;
+  GstCopyFunction copy;
+  GFreeFunc free;
+  gboolean visited;
+} GstSingleObjectIterator;
+
+static guint32 _single_object_dummy_cookie = 0;
+
+static GstIteratorResult
+gst_single_object_iterator_iterator_next (GstSingleObjectIterator * it,
+    gpointer * result)
+{
+  if (it->visited) {
+    *result = NULL;
+    return GST_ITERATOR_DONE;
+  }
+
+  *result = it->copy (it->object);
+  return GST_ITERATOR_OK;
+}
+
+static void
+gst_single_object_iterator_resync (GstSingleObjectIterator * it)
+{
+  it->visited = FALSE;
+}
+
+static void
+gst_single_object_iterator_free (GstSingleObjectIterator * it)
+{
+  it->free (it->object);
+  g_free (it);
+}
+
+/**
+ * gst_iterator_new:
+ * @type: #GType of the passed object
+ * @object: object that this iterator should return
+ * @copy: Function that returns a copy of @object or increases it's refcount
+ * @free: Function to be called for freeing @object
+ *
+ * This #GstIterator is a convenient iterator for the common
+ * case where a #GstIterator needs to be returned but only
+ * a single object has the be considered. This happens often
+ * for the #GstPadIterIntLinkFunction.
+ *
+ * Since: 0.10.25
+ *
+ */
+GstIterator *
+gst_iterator_new_single (GType type, gpointer object, GstCopyFunction copy,
+    GFreeFunc free)
+{
+  GstSingleObjectIterator *result;
+
+  g_return_val_if_fail (object != NULL, NULL);
+  g_return_val_if_fail (copy != NULL, NULL);
+  g_return_val_if_fail (free != NULL, NULL);
+
+  result = (GstSingleObjectIterator *)
+      gst_iterator_new (sizeof (GstSingleObjectIterator),
+      G_TYPE_FROM_INSTANCE (object), NULL, &_single_object_dummy_cookie,
+      (GstIteratorNextFunction) gst_single_object_iterator_iterator_next, NULL,
+      (GstIteratorResyncFunction) gst_single_object_iterator_resync,
+      (GstIteratorFreeFunction) gst_single_object_iterator_free);
+
+  result->object = copy (object);
+  result->copy = copy;
+  result->free = free;
+  result->visited = FALSE;
+
+  return (GstIterator *) result;
+}
diff --git a/gst/gstiterator.h b/gst/gstiterator.h
index 6376be1..ca6b32b 100644
--- a/gst/gstiterator.h
+++ b/gst/gstiterator.h
@@ -138,6 +138,19 @@ typedef void		  (*GstIteratorFreeFunction)	(GstIterator *it);
 typedef gboolean	  (*GstIteratorFoldFunction)    (gpointer item, GValue *ret, gpointer user_data);
 
 /**
+ * GstCopyFunction:
+ * @object: The object to copy
+ *
+ * A function to create a copy of some object or
+ * increase it's reference count.
+ *
+ * Returns: a copy of the object or the same object with increased reference count
+ *
+ * Since: 0.10.25
+ */
+typedef gpointer          (*GstCopyFunction)             (gpointer object);
+
+/**
  * GST_ITERATOR:
  * @it: the #GstIterator value
  *
@@ -226,6 +239,11 @@ GstIterator*		gst_iterator_new_list		(GType type,
 							 GstIteratorItemFunction item,
 							 GstIteratorDisposeFunction free);
 
+GstIterator*            gst_iterator_new_single         (GType type,
+                                                         gpointer object,
+                                                         GstCopyFunction copy,
+                                                         GFreeFunc free);
+
 /* using iterators */
 GstIteratorResult	gst_iterator_next		(GstIterator *it, gpointer *elem);
 void			gst_iterator_resync		(GstIterator *it);





More information about the Gstreamer-commits mailing list