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