Merge branch 'blender2.7'
[blender.git] / source / blender / blenlib / BLI_iterator.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Dalai Felinto
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #ifndef __BLI_ITERATOR_H__
24 #define __BLI_ITERATOR_H__
25
26 /** \file BLI_iterator.h
27  *  \ingroup bli
28  */
29
30 typedef struct BLI_Iterator {
31         void *current; /* current pointer we iterate over */
32         void *data;    /* stored data required for this iterator */
33         bool skip;
34         bool valid;
35 } BLI_Iterator;
36
37 typedef void (*IteratorCb)(BLI_Iterator *iter);
38 typedef void (*IteratorBeginCb)(BLI_Iterator *iter, void *data_in);
39
40 #define ITER_BEGIN(callback_begin, callback_next, callback_end, _data_in, _type, _instance) \
41 {                                                                                    \
42         _type _instance;                                                                 \
43         IteratorCb callback_end_func = callback_end;                                     \
44         BLI_Iterator iter_macro;                                                         \
45         iter_macro.skip = false;                                                         \
46         iter_macro.valid = true;                                                         \
47         for (callback_begin(&iter_macro, (_data_in));                                    \
48              iter_macro.valid;                                                           \
49              callback_next(&iter_macro))                                                 \
50         {                                                                                \
51                 if (iter_macro.skip) {                                                       \
52                         iter_macro.skip = false;                                                 \
53                         continue;                                                                \
54                 }                                                                            \
55                 _instance = (_type ) iter_macro.current;
56
57 #define ITER_END                                                                     \
58         }                                                                                \
59         callback_end_func(&iter_macro);                                                  \
60 } ((void)0)
61
62 #endif /* __BLI_ITERATOR_H__ */