gstiterator.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* GStreamer
  2. * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
  3. * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
  4. *
  5. * gstiterator.h: Header for GstIterator
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #ifndef __GST_ITERATOR_H__
  23. #define __GST_ITERATOR_H__
  24. #include <glib-object.h> /* for GValue in the fold */
  25. #include <gst/gstconfig.h>
  26. G_BEGIN_DECLS
  27. #define GST_TYPE_ITERATOR (gst_iterator_get_type ())
  28. /**
  29. * GstIteratorResult:
  30. * @GST_ITERATOR_DONE: No more items in the iterator
  31. * @GST_ITERATOR_OK: An item was retrieved
  32. * @GST_ITERATOR_RESYNC: Datastructure changed while iterating
  33. * @GST_ITERATOR_ERROR: An error happened
  34. *
  35. * The result of gst_iterator_next().
  36. */
  37. typedef enum {
  38. GST_ITERATOR_DONE = 0,
  39. GST_ITERATOR_OK = 1,
  40. GST_ITERATOR_RESYNC = 2,
  41. GST_ITERATOR_ERROR = 3
  42. } GstIteratorResult;
  43. typedef struct _GstIterator GstIterator;
  44. /**
  45. * GstIteratorItem:
  46. * @GST_ITERATOR_ITEM_SKIP: Skip this item
  47. * @GST_ITERATOR_ITEM_PASS: Return item
  48. * @GST_ITERATOR_ITEM_END: Stop after this item.
  49. *
  50. * The result of a #GstIteratorItemFunction.
  51. */
  52. typedef enum {
  53. GST_ITERATOR_ITEM_SKIP = 0,
  54. GST_ITERATOR_ITEM_PASS = 1,
  55. GST_ITERATOR_ITEM_END = 2
  56. } GstIteratorItem;
  57. /**
  58. * GstIteratorCopyFunction:
  59. * @it: The original iterator
  60. * @copy: The copied iterator
  61. *
  62. * This function will be called when creating a copy of @it and should
  63. * create a copy of all custom iterator fields or increase their
  64. * reference counts.
  65. */
  66. typedef void (*GstIteratorCopyFunction) (const GstIterator *it, GstIterator *copy);
  67. /**
  68. * GstIteratorItemFunction:
  69. * @it: the iterator
  70. * @item: the item being retrieved.
  71. *
  72. * The function that will be called after the next item of the iterator
  73. * has been retrieved. This function can be used to skip items or stop
  74. * the iterator.
  75. *
  76. * The function will be called with the iterator lock held.
  77. *
  78. * Returns: the result of the operation.
  79. */
  80. typedef GstIteratorItem (*GstIteratorItemFunction) (GstIterator *it, const GValue * item);
  81. /**
  82. * GstIteratorNextFunction:
  83. * @it: the iterator
  84. * @result: a pointer to hold the next item
  85. *
  86. * The function that will be called when the next element of the iterator
  87. * should be retrieved.
  88. *
  89. * Implementors of a #GstIterator should implement this
  90. * function and pass it to the constructor of the custom iterator.
  91. * The function will be called with the iterator lock held.
  92. *
  93. * Returns: the result of the operation.
  94. */
  95. typedef GstIteratorResult (*GstIteratorNextFunction) (GstIterator *it, GValue *result);
  96. /**
  97. * GstIteratorResyncFunction:
  98. * @it: the iterator
  99. *
  100. * This function will be called whenever a concurrent update happened
  101. * to the iterated datastructure. The implementor of the iterator should
  102. * restart the iterator from the beginning and clean up any state it might
  103. * have.
  104. *
  105. * Implementors of a #GstIterator should implement this
  106. * function and pass it to the constructor of the custom iterator.
  107. * The function will be called with the iterator lock held.
  108. */
  109. typedef void (*GstIteratorResyncFunction) (GstIterator *it);
  110. /**
  111. * GstIteratorFreeFunction:
  112. * @it: the iterator
  113. *
  114. * This function will be called when the iterator is freed.
  115. *
  116. * Implementors of a #GstIterator should implement this
  117. * function and pass it to the constructor of the custom iterator.
  118. * The function will be called with the iterator lock held.
  119. */
  120. typedef void (*GstIteratorFreeFunction) (GstIterator *it);
  121. /**
  122. * GstIteratorForeachFunction:
  123. * @item: The item
  124. * @user_data: User data
  125. *
  126. * A function that is called by gst_iterator_foreach() for every element.
  127. */
  128. typedef void (*GstIteratorForeachFunction) (const GValue * item, gpointer user_data);
  129. /**
  130. * GstIteratorFoldFunction:
  131. * @item: the item to fold
  132. * @ret: a #GValue collecting the result
  133. * @user_data: data passed to gst_iterator_fold()
  134. *
  135. * A function to be passed to gst_iterator_fold().
  136. *
  137. * Returns: %TRUE if the fold should continue, %FALSE if it should stop.
  138. */
  139. typedef gboolean (*GstIteratorFoldFunction) (const GValue * item, GValue * ret, gpointer user_data);
  140. /**
  141. * GST_ITERATOR:
  142. * @it: the #GstIterator value
  143. *
  144. * Macro to cast to a #GstIterator
  145. */
  146. #define GST_ITERATOR(it) ((GstIterator*)(it))
  147. /**
  148. * GST_ITERATOR_LOCK:
  149. * @it: the #GstIterator to get the lock of
  150. *
  151. * Macro to get the lock protecting the datastructure being iterated.
  152. */
  153. #define GST_ITERATOR_LOCK(it) (GST_ITERATOR(it)->lock)
  154. /**
  155. * GST_ITERATOR_COOKIE:
  156. * @it: the #GstIterator to get the cookie of
  157. *
  158. * Macro to get the cookie of a #GstIterator. The cookie of the
  159. * iterator is the value of the master cookie when the iterator
  160. * was created.
  161. * Whenever the iterator is iterated, the value is compared to the
  162. * value of the master cookie. If they are different, a concurrent
  163. * modification happened to the iterator and a resync is needed.
  164. */
  165. #define GST_ITERATOR_COOKIE(it) (GST_ITERATOR(it)->cookie)
  166. /**
  167. * GST_ITERATOR_ORIG_COOKIE:
  168. * @it: the #GstIterator to get the master cookie of
  169. *
  170. * Macro to get a pointer to where the master cookie is stored. The
  171. * master cookie protects the structure being iterated and gets updated
  172. * whenever the datastructure changes.
  173. */
  174. #define GST_ITERATOR_ORIG_COOKIE(it) (GST_ITERATOR(it)->master_cookie)
  175. /**
  176. * GstIterator:
  177. * @copy: The function to copy the iterator
  178. * @next: The function to get the next item in the iterator
  179. * @item: The function to be called for each item retrieved
  180. * @resync: The function to call when a resync is needed.
  181. * @free: The function to call when the iterator is freed
  182. * @pushed: The iterator that is currently pushed with gst_iterator_push()
  183. * @type: The type of the object that this iterator will return
  184. * @lock: The lock protecting the data structure and the cookie.
  185. * @cookie: The cookie; the value of the master_cookie when this iterator was
  186. * created.
  187. * @master_cookie: A pointer to the master cookie.
  188. * @size: the size of the iterator
  189. *
  190. * #GstIterator base structure. The values of this structure are
  191. * protected for subclasses, use the methods to use the #GstIterator.
  192. */
  193. struct _GstIterator {
  194. /*< protected >*/
  195. GstIteratorCopyFunction copy;
  196. GstIteratorNextFunction next;
  197. GstIteratorItemFunction item;
  198. GstIteratorResyncFunction resync;
  199. GstIteratorFreeFunction free;
  200. GstIterator *pushed; /* pushed iterator */
  201. GType type;
  202. GMutex *lock;
  203. guint32 cookie; /* cookie of the iterator */
  204. guint32 *master_cookie; /* pointer to guint32 holding the cookie when this
  205. iterator was created */
  206. guint size;
  207. /*< private >*/
  208. gpointer _gst_reserved[GST_PADDING];
  209. };
  210. GType gst_iterator_get_type (void);
  211. /* creating iterators */
  212. GstIterator* gst_iterator_new (guint size,
  213. GType type,
  214. GMutex *lock,
  215. guint32 *master_cookie,
  216. GstIteratorCopyFunction copy,
  217. GstIteratorNextFunction next,
  218. GstIteratorItemFunction item,
  219. GstIteratorResyncFunction resync,
  220. GstIteratorFreeFunction free) G_GNUC_MALLOC;
  221. GstIterator* gst_iterator_new_list (GType type,
  222. GMutex *lock,
  223. guint32 *master_cookie,
  224. GList **list,
  225. GObject * owner,
  226. GstIteratorItemFunction item) G_GNUC_MALLOC;
  227. GstIterator* gst_iterator_new_single (GType type,
  228. const GValue * object) G_GNUC_MALLOC;
  229. GstIterator* gst_iterator_copy (const GstIterator *it) G_GNUC_MALLOC;
  230. /* using iterators */
  231. GstIteratorResult gst_iterator_next (GstIterator *it, GValue * elem);
  232. void gst_iterator_resync (GstIterator *it);
  233. void gst_iterator_free (GstIterator *it);
  234. void gst_iterator_push (GstIterator *it, GstIterator *other);
  235. /* higher-order functions that operate on iterators */
  236. GstIterator* gst_iterator_filter (GstIterator *it, GCompareFunc func,
  237. const GValue * user_data) G_GNUC_MALLOC;
  238. GstIteratorResult gst_iterator_fold (GstIterator *it,
  239. GstIteratorFoldFunction func,
  240. GValue *ret, gpointer user_data);
  241. GstIteratorResult gst_iterator_foreach (GstIterator *it,
  242. GstIteratorForeachFunction func, gpointer user_data);
  243. gboolean gst_iterator_find_custom (GstIterator *it, GCompareFunc func,
  244. GValue *elem, gpointer user_data);
  245. #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
  246. G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstIterator, gst_iterator_free)
  247. #endif
  248. G_END_DECLS
  249. #endif /* __GST_ITERATOR_H__ */