Update Changelog
[profile/ivi/libgee.git] / gee / iterator.c
1 /* iterator.c generated by valac 0.18.0, the Vala compiler
2  * generated from iterator.vala, do not modify */
3
4 /* iterator.vala
5  *
6  * Copyright (C) 2007-2008  Jürg Billeter
7  * Copyright (C) 2009  Didier Villevalois, Maciej Piechotka
8  * Copyright (C) 2010-2011  Maciej Piechotka
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
23  *
24  * Author:
25  *      Jürg Billeter <j@bitron.ch>
26  *      Maciej Piechotka <uzytkownik2@gmail.com>
27  *      Didier 'Ptitjes Villevalois <ptitjes@free.fr>
28  */
29
30 #include <glib.h>
31 #include <glib-object.h>
32
33
34 #define GEE_TYPE_TRAVERSABLE (gee_traversable_get_type ())
35 #define GEE_TRAVERSABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_TRAVERSABLE, GeeTraversable))
36 #define GEE_IS_TRAVERSABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_TRAVERSABLE))
37 #define GEE_TRAVERSABLE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_TRAVERSABLE, GeeTraversableIface))
38
39 typedef struct _GeeTraversable GeeTraversable;
40 typedef struct _GeeTraversableIface GeeTraversableIface;
41
42 #define GEE_TRAVERSABLE_TYPE_STREAM (gee_traversable_stream_get_type ())
43
44 #define GEE_TYPE_LAZY (gee_lazy_get_type ())
45 #define GEE_LAZY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_LAZY, GeeLazy))
46 #define GEE_LAZY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEE_TYPE_LAZY, GeeLazyClass))
47 #define GEE_IS_LAZY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_LAZY))
48 #define GEE_IS_LAZY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEE_TYPE_LAZY))
49 #define GEE_LAZY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEE_TYPE_LAZY, GeeLazyClass))
50
51 typedef struct _GeeLazy GeeLazy;
52 typedef struct _GeeLazyClass GeeLazyClass;
53
54 #define GEE_TYPE_ITERATOR (gee_iterator_get_type ())
55 #define GEE_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_ITERATOR, GeeIterator))
56 #define GEE_IS_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_ITERATOR))
57 #define GEE_ITERATOR_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_ITERATOR, GeeIteratorIface))
58
59 typedef struct _GeeIterator GeeIterator;
60 typedef struct _GeeIteratorIface GeeIteratorIface;
61
62 #define GEE_TYPE_UNFOLD_ITERATOR (gee_unfold_iterator_get_type ())
63 #define GEE_UNFOLD_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_UNFOLD_ITERATOR, GeeUnfoldIterator))
64 #define GEE_UNFOLD_ITERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEE_TYPE_UNFOLD_ITERATOR, GeeUnfoldIteratorClass))
65 #define GEE_IS_UNFOLD_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_UNFOLD_ITERATOR))
66 #define GEE_IS_UNFOLD_ITERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEE_TYPE_UNFOLD_ITERATOR))
67 #define GEE_UNFOLD_ITERATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEE_TYPE_UNFOLD_ITERATOR, GeeUnfoldIteratorClass))
68
69 typedef struct _GeeUnfoldIterator GeeUnfoldIterator;
70 typedef struct _GeeUnfoldIteratorClass GeeUnfoldIteratorClass;
71 #define _gee_lazy_unref0(var) ((var == NULL) ? NULL : (var = (gee_lazy_unref (var), NULL)))
72 #define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
73 typedef struct _Block6Data Block6Data;
74 #define _g_destroy_func0(var) (((var == NULL) || (g_destroy_func == NULL)) ? NULL : (var = (g_destroy_func (var), NULL)))
75
76 typedef gboolean (*GeeForallFunc) (gpointer g, void* user_data);
77 typedef enum  {
78         GEE_TRAVERSABLE_STREAM_YIELD,
79         GEE_TRAVERSABLE_STREAM_CONTINUE,
80         GEE_TRAVERSABLE_STREAM_END
81 } GeeTraversableStream;
82
83 typedef GeeTraversableStream (*GeeStreamFunc) (GeeTraversableStream state, GeeLazy* g, GeeLazy** lazy, void* user_data);
84 typedef gpointer (*GeeFoldFunc) (gpointer g, gpointer a, void* user_data);
85 typedef gpointer (*GeeMapFunc) (gpointer g, void* user_data);
86 typedef gboolean (*GeePredicate) (gconstpointer g, void* user_data);
87 struct _GeeTraversableIface {
88         GTypeInterface parent_iface;
89         GType (*get_g_type) (GeeTraversable* self);
90         GBoxedCopyFunc (*get_g_dup_func) (GeeTraversable* self);
91         GDestroyNotify (*get_g_destroy_func) (GeeTraversable* self);
92         gboolean (*foreach) (GeeTraversable* self, GeeForallFunc f, void* f_target);
93         GeeIterator* (*stream) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeStreamFunc f, void* f_target, GDestroyNotify f_target_destroy_notify);
94         gpointer (*fold) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeFoldFunc f, void* f_target, gpointer seed);
95         GeeIterator* (*map) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeMapFunc f, void* f_target);
96         GeeIterator* (*scan) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeFoldFunc f, void* f_target, gpointer seed);
97         GeeIterator* (*filter) (GeeTraversable* self, GeePredicate pred, void* pred_target, GDestroyNotify pred_target_destroy_notify);
98         GeeIterator* (*chop) (GeeTraversable* self, gint offset, gint length);
99         GType (*get_element_type) (GeeTraversable* self);
100 };
101
102 struct _GeeIteratorIface {
103         GTypeInterface parent_iface;
104         gboolean (*next) (GeeIterator* self);
105         gboolean (*has_next) (GeeIterator* self);
106         gpointer (*get) (GeeIterator* self);
107         void (*remove) (GeeIterator* self);
108         gboolean (*get_valid) (GeeIterator* self);
109         gboolean (*get_read_only) (GeeIterator* self);
110 };
111
112 typedef GeeLazy* (*GeeUnfoldFunc) (void* user_data);
113 struct _Block6Data {
114         int _ref_count_;
115         GType g_type;
116         GBoxedCopyFunc g_dup_func;
117         GDestroyNotify g_destroy_func;
118         GeeIterator* current;
119         GeeIterator* iters;
120 };
121
122
123
124 GType gee_traversable_stream_get_type (void) G_GNUC_CONST;
125 gpointer gee_lazy_ref (gpointer instance);
126 void gee_lazy_unref (gpointer instance);
127 GParamSpec* gee_param_spec_lazy (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
128 void gee_value_set_lazy (GValue* value, gpointer v_object);
129 void gee_value_take_lazy (GValue* value, gpointer v_object);
130 gpointer gee_value_get_lazy (const GValue* value);
131 GType gee_lazy_get_type (void) G_GNUC_CONST;
132 GType gee_traversable_get_type (void) G_GNUC_CONST;
133 GType gee_iterator_get_type (void) G_GNUC_CONST;
134 gboolean gee_iterator_next (GeeIterator* self);
135 gboolean gee_iterator_has_next (GeeIterator* self);
136 gpointer gee_iterator_get (GeeIterator* self);
137 void gee_iterator_remove (GeeIterator* self);
138 GeeIterator* gee_iterator_unfold (GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeUnfoldFunc f, void* f_target, GDestroyNotify f_target_destroy_notify, GeeLazy* current);
139 GeeUnfoldIterator* gee_unfold_iterator_new (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeUnfoldFunc func, void* func_target, GDestroyNotify func_target_destroy_notify, GeeLazy* current);
140 GeeUnfoldIterator* gee_unfold_iterator_construct (GType object_type, GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeUnfoldFunc func, void* func_target, GDestroyNotify func_target_destroy_notify, GeeLazy* current);
141 GType gee_unfold_iterator_get_type (void) G_GNUC_CONST;
142 GeeIterator* gee_iterator_concat (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeIterator* iters);
143 static Block6Data* block6_data_ref (Block6Data* _data6_);
144 static void block6_data_unref (void * _userdata_);
145 gboolean gee_iterator_get_valid (GeeIterator* self);
146 static GeeLazy* __lambda2_ (Block6Data* _data6_);
147 GeeLazy* gee_lazy_new_from_value (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, gconstpointer item);
148 GeeLazy* gee_lazy_construct_from_value (GType object_type, GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, gconstpointer item);
149 static GeeLazy* ___lambda2__gee_unfold_func (gpointer self);
150 gboolean gee_iterator_get_read_only (GeeIterator* self);
151
152
153 /**
154  * Advances to the next element in the iteration.
155  *
156  * @return ``true`` if the iterator has a next element
157  */
158 gboolean gee_iterator_next (GeeIterator* self) {
159         g_return_val_if_fail (self != NULL, FALSE);
160         return GEE_ITERATOR_GET_INTERFACE (self)->next (self);
161 }
162
163
164 /**
165  * Checks whether there is a next element in the iteration.
166  *
167  * @return ``true`` if the iterator has a next element
168  */
169 gboolean gee_iterator_has_next (GeeIterator* self) {
170         g_return_val_if_fail (self != NULL, FALSE);
171         return GEE_ITERATOR_GET_INTERFACE (self)->has_next (self);
172 }
173
174
175 /**
176  * Returns the current element in the iteration.
177  *
178  * @return the current element in the iteration
179  */
180 gpointer gee_iterator_get (GeeIterator* self) {
181         g_return_val_if_fail (self != NULL, NULL);
182         return GEE_ITERATOR_GET_INTERFACE (self)->get (self);
183 }
184
185
186 /**
187  * Removes the current element in the iteration. The cursor is set in an
188  * in-between state. Both {@link get} and {@link remove} will fail until
189  * the next move of the cursor (calling {@link next}).
190  */
191 void gee_iterator_remove (GeeIterator* self) {
192         g_return_if_fail (self != NULL);
193         GEE_ITERATOR_GET_INTERFACE (self)->remove (self);
194 }
195
196
197 /**
198  * Create iterator from unfolding function. The lazy value is
199  * force-evaluated before progressing to next element.
200  *
201  * @param f Unfolding function
202  * @param current If iterator is to be valid it contains the current value of it
203  */
204 GeeIterator* gee_iterator_unfold (GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeUnfoldFunc f, void* f_target, GDestroyNotify f_target_destroy_notify, GeeLazy* current) {
205         GeeIterator* result = NULL;
206         GeeUnfoldFunc _tmp0_;
207         void* _tmp0__target;
208         GDestroyNotify _tmp0__target_destroy_notify;
209         GeeLazy* _tmp1_;
210         GeeUnfoldIterator* _tmp2_;
211         _tmp0_ = f;
212         _tmp0__target = f_target;
213         _tmp0__target_destroy_notify = f_target_destroy_notify;
214         f_target_destroy_notify = NULL;
215         _tmp1_ = current;
216         current = NULL;
217         _tmp2_ = gee_unfold_iterator_new (a_type, (GBoxedCopyFunc) a_dup_func, a_destroy_func, _tmp0_, _tmp0__target, _tmp0__target_destroy_notify, _tmp1_);
218         result = (GeeIterator*) _tmp2_;
219         (f_target_destroy_notify == NULL) ? NULL : (f_target_destroy_notify (f_target), NULL);
220         f = NULL;
221         f_target = NULL;
222         f_target_destroy_notify = NULL;
223         _gee_lazy_unref0 (current);
224         return result;
225 }
226
227
228 /**
229  * Concatinate iterators.
230  *
231  * @param iters Iterators of iterators
232  * @return Iterator containg values of each iterator
233  */
234 static gpointer _g_object_ref0 (gpointer self) {
235         return self ? g_object_ref (self) : NULL;
236 }
237
238
239 static Block6Data* block6_data_ref (Block6Data* _data6_) {
240         g_atomic_int_inc (&_data6_->_ref_count_);
241         return _data6_;
242 }
243
244
245 static void block6_data_unref (void * _userdata_) {
246         Block6Data* _data6_;
247         _data6_ = (Block6Data*) _userdata_;
248         if (g_atomic_int_dec_and_test (&_data6_->_ref_count_)) {
249                 GType g_type;
250                 GBoxedCopyFunc g_dup_func;
251                 GDestroyNotify g_destroy_func;
252                 g_type = _data6_->g_type;
253                 g_dup_func = _data6_->g_dup_func;
254                 g_destroy_func = _data6_->g_destroy_func;
255                 _g_object_unref0 (_data6_->current);
256                 _g_object_unref0 (_data6_->iters);
257                 g_slice_free (Block6Data, _data6_);
258         }
259 }
260
261
262 static GeeLazy* __lambda2_ (Block6Data* _data6_) {
263         GType g_type;
264         GBoxedCopyFunc g_dup_func;
265         GDestroyNotify g_destroy_func;
266         GeeLazy* result = NULL;
267         g_type = _data6_->g_type;
268         g_dup_func = _data6_->g_dup_func;
269         g_destroy_func = _data6_->g_destroy_func;
270         while (TRUE) {
271                 GeeIterator* _tmp0_;
272                 _tmp0_ = _data6_->current;
273                 if (_tmp0_ == NULL) {
274                         GeeIterator* _tmp1_;
275                         gboolean _tmp2_ = FALSE;
276                         _tmp1_ = _data6_->iters;
277                         _tmp2_ = gee_iterator_next (_tmp1_);
278                         if (_tmp2_) {
279                                 GeeIterator* _tmp3_;
280                                 gpointer _tmp4_ = NULL;
281                                 _tmp3_ = _data6_->iters;
282                                 _tmp4_ = gee_iterator_get (_tmp3_);
283                                 _g_object_unref0 (_data6_->current);
284                                 _data6_->current = (GeeIterator*) _tmp4_;
285                         } else {
286                                 result = NULL;
287                                 return result;
288                         }
289                 } else {
290                         GeeIterator* _tmp5_;
291                         gboolean _tmp6_ = FALSE;
292                         _tmp5_ = _data6_->current;
293                         _tmp6_ = gee_iterator_next (_tmp5_);
294                         if (_tmp6_) {
295                                 GeeIterator* _tmp7_;
296                                 gpointer _tmp8_ = NULL;
297                                 gpointer _tmp9_;
298                                 GeeLazy* _tmp10_;
299                                 GeeLazy* _tmp11_;
300                                 _tmp7_ = _data6_->current;
301                                 _tmp8_ = gee_iterator_get (_tmp7_);
302                                 _tmp9_ = _tmp8_;
303                                 _tmp10_ = gee_lazy_new_from_value (g_type, (GBoxedCopyFunc) g_dup_func, g_destroy_func, _tmp9_);
304                                 _tmp11_ = _tmp10_;
305                                 _g_destroy_func0 (_tmp9_);
306                                 result = _tmp11_;
307                                 return result;
308                         } else {
309                                 _g_object_unref0 (_data6_->current);
310                                 _data6_->current = NULL;
311                         }
312                 }
313         }
314 }
315
316
317 static GeeLazy* ___lambda2__gee_unfold_func (gpointer self) {
318         GeeLazy* result;
319         result = __lambda2_ (self);
320         return result;
321 }
322
323
324 GeeIterator* gee_iterator_concat (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeIterator* iters) {
325         GeeIterator* result = NULL;
326         Block6Data* _data6_;
327         GeeIterator* _tmp0_;
328         GeeIterator* _tmp1_;
329         GeeIterator* _tmp2_;
330         gboolean _tmp3_;
331         gboolean _tmp4_;
332         GeeIterator* _tmp7_ = NULL;
333         g_return_val_if_fail (iters != NULL, NULL);
334         _data6_ = g_slice_new0 (Block6Data);
335         _data6_->_ref_count_ = 1;
336         _data6_->g_type = g_type;
337         _data6_->g_dup_func = g_dup_func;
338         _data6_->g_destroy_func = g_destroy_func;
339         _tmp0_ = iters;
340         _tmp1_ = _g_object_ref0 (_tmp0_);
341         _g_object_unref0 (_data6_->iters);
342         _data6_->iters = _tmp1_;
343         _data6_->current = NULL;
344         _tmp2_ = _data6_->iters;
345         _tmp3_ = gee_iterator_get_valid (_tmp2_);
346         _tmp4_ = _tmp3_;
347         if (_tmp4_) {
348                 GeeIterator* _tmp5_;
349                 gpointer _tmp6_ = NULL;
350                 _tmp5_ = _data6_->iters;
351                 _tmp6_ = gee_iterator_get (_tmp5_);
352                 _g_object_unref0 (_data6_->current);
353                 _data6_->current = (GeeIterator*) _tmp6_;
354         }
355         _tmp7_ = gee_iterator_unfold (g_type, (GBoxedCopyFunc) g_dup_func, g_destroy_func, ___lambda2__gee_unfold_func, block6_data_ref (_data6_), block6_data_unref, NULL);
356         result = _tmp7_;
357         block6_data_unref (_data6_);
358         _data6_ = NULL;
359         return result;
360 }
361
362
363 gboolean gee_iterator_get_valid (GeeIterator* self) {
364         g_return_val_if_fail (self != NULL, FALSE);
365         return GEE_ITERATOR_GET_INTERFACE (self)->get_valid (self);
366 }
367
368
369 gboolean gee_iterator_get_read_only (GeeIterator* self) {
370         g_return_val_if_fail (self != NULL, FALSE);
371         return GEE_ITERATOR_GET_INTERFACE (self)->get_read_only (self);
372 }
373
374
375 static void gee_iterator_base_init (GeeIteratorIface * iface) {
376         static gboolean initialized = FALSE;
377         if (!initialized) {
378                 initialized = TRUE;
379                 /**
380                  * Determines wheather the call to {@link get} is legal. It is false at the
381                  * beginning and after {@link remove} call and true otherwise.
382                  */
383                 g_object_interface_install_property (iface, g_param_spec_boolean ("valid", "valid", "valid", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
384                 /**
385                  * Determines wheather the call to {@link remove} is legal assuming the
386                  * iterator is valid. The value must not change in runtime hence the user
387                  * of iterator may cache it.
388                  */
389                 g_object_interface_install_property (iface, g_param_spec_boolean ("read-only", "read-only", "read-only", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
390         }
391 }
392
393
394 /**
395  * An iterator over a collection.
396  *
397  * Gee's iterators are "on-track" iterators. They always point to an item
398  * except before the first call to {@link next}, or, when an
399  * item has been removed, until the next call to {@link next}.
400  *
401  * Please note that when the iterator is out of track, neither {@link get} nor
402  * {@link remove} are defined and both will fail. After the next call to
403  * {@link next}, they will be defined again.
404  */
405 GType gee_iterator_get_type (void) {
406         static volatile gsize gee_iterator_type_id__volatile = 0;
407         if (g_once_init_enter (&gee_iterator_type_id__volatile)) {
408                 static const GTypeInfo g_define_type_info = { sizeof (GeeIteratorIface), (GBaseInitFunc) gee_iterator_base_init, (GBaseFinalizeFunc) NULL, (GClassInitFunc) NULL, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
409                 GType gee_iterator_type_id;
410                 gee_iterator_type_id = g_type_register_static (G_TYPE_INTERFACE, "GeeIterator", &g_define_type_info, 0);
411                 g_type_interface_add_prerequisite (gee_iterator_type_id, G_TYPE_OBJECT);
412                 g_type_interface_add_prerequisite (gee_iterator_type_id, GEE_TYPE_TRAVERSABLE);
413                 g_once_init_leave (&gee_iterator_type_id__volatile, gee_iterator_type_id);
414         }
415         return gee_iterator_type_id__volatile;
416 }
417
418
419