Cleanup: -Wnonnull-compare GCC warning
[blender.git] / source / blender / bmesh / intern / bmesh_iterators_inline.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): Joseph Eagar, Geoffrey Bantle, Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/intern/bmesh_iterators_inline.h
24  *  \ingroup bmesh
25  *
26  * BMesh inline iterator functions.
27  */
28
29 #ifndef __BMESH_ITERATORS_INLINE_H__
30 #define __BMESH_ITERATORS_INLINE_H__
31
32 /* inline here optimizes out the switch statement when called with
33  * constant values (which is very common), nicer for loop-in-loop situations */
34
35 /**
36  * \brief Iterator Step
37  *
38  * Calls an iterators step function to return the next element.
39  */
40 ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
41 BLI_INLINE void *BM_iter_step(BMIter *iter)
42 {
43         return iter->step(iter);
44 }
45
46
47 /**
48  * \brief Iterator Init
49  *
50  * Takes a bmesh iterator structure and fills
51  * it with the appropriate function pointers based
52  * upon its type.
53  */
54 ATTR_NONNULL(1)
55 BLI_INLINE bool BM_iter_init(BMIter *iter, BMesh *bm, const char itype, void *data)
56 {
57         /* int argtype; */
58         iter->itype = itype;
59
60         /* inlining optimizes out this switch when called with the defined type */
61         switch ((BMIterType)itype) {
62                 case BM_VERTS_OF_MESH:
63                         BLI_assert(bm != NULL);
64                         BLI_assert(data == NULL);
65                         iter->begin = (BMIter__begin_cb)bmiter__elem_of_mesh_begin;
66                         iter->step  = (BMIter__step_cb)bmiter__elem_of_mesh_step;
67                         iter->data.elem_of_mesh.pooliter.pool = bm->vpool;
68                         break;
69                 case BM_EDGES_OF_MESH:
70                         BLI_assert(bm != NULL);
71                         BLI_assert(data == NULL);
72                         iter->begin = (BMIter__begin_cb)bmiter__elem_of_mesh_begin;
73                         iter->step  = (BMIter__step_cb)bmiter__elem_of_mesh_step;
74                         iter->data.elem_of_mesh.pooliter.pool = bm->epool;
75                         break;
76                 case BM_FACES_OF_MESH:
77                         BLI_assert(bm != NULL);
78                         BLI_assert(data == NULL);
79                         iter->begin = (BMIter__begin_cb)bmiter__elem_of_mesh_begin;
80                         iter->step  = (BMIter__step_cb)bmiter__elem_of_mesh_step;
81                         iter->data.elem_of_mesh.pooliter.pool = bm->fpool;
82                         break;
83                 case BM_EDGES_OF_VERT:
84                         BLI_assert(data != NULL);
85                         BLI_assert(((BMElem *)data)->head.htype == BM_VERT);
86                         iter->begin = (BMIter__begin_cb)bmiter__edge_of_vert_begin;
87                         iter->step  = (BMIter__step_cb)bmiter__edge_of_vert_step;
88                         iter->data.edge_of_vert.vdata = (BMVert *)data;
89                         break;
90                 case BM_FACES_OF_VERT:
91                         BLI_assert(data != NULL);
92                         BLI_assert(((BMElem *)data)->head.htype == BM_VERT);
93                         iter->begin = (BMIter__begin_cb)bmiter__face_of_vert_begin;
94                         iter->step  = (BMIter__step_cb)bmiter__face_of_vert_step;
95                         iter->data.face_of_vert.vdata = (BMVert *)data;
96                         break;
97                 case BM_LOOPS_OF_VERT:
98                         BLI_assert(data != NULL);
99                         BLI_assert(((BMElem *)data)->head.htype == BM_VERT);
100                         iter->begin = (BMIter__begin_cb)bmiter__loop_of_vert_begin;
101                         iter->step  = (BMIter__step_cb)bmiter__loop_of_vert_step;
102                         iter->data.loop_of_vert.vdata = (BMVert *)data;
103                         break;
104                 case BM_VERTS_OF_EDGE:
105                         BLI_assert(data != NULL);
106                         BLI_assert(((BMElem *)data)->head.htype == BM_EDGE);
107                         iter->begin = (BMIter__begin_cb)bmiter__vert_of_edge_begin;
108                         iter->step  = (BMIter__step_cb)bmiter__vert_of_edge_step;
109                         iter->data.vert_of_edge.edata = (BMEdge *)data;
110                         break;
111                 case BM_FACES_OF_EDGE:
112                         BLI_assert(data != NULL);
113                         BLI_assert(((BMElem *)data)->head.htype == BM_EDGE);
114                         iter->begin = (BMIter__begin_cb)bmiter__face_of_edge_begin;
115                         iter->step  = (BMIter__step_cb)bmiter__face_of_edge_step;
116                         iter->data.face_of_edge.edata = (BMEdge *)data;
117                         break;
118                 case BM_VERTS_OF_FACE:
119                         BLI_assert(data != NULL);
120                         BLI_assert(((BMElem *)data)->head.htype == BM_FACE);
121                         iter->begin = (BMIter__begin_cb)bmiter__vert_of_face_begin;
122                         iter->step  = (BMIter__step_cb)bmiter__vert_of_face_step;
123                         iter->data.vert_of_face.pdata = (BMFace *)data;
124                         break;
125                 case BM_EDGES_OF_FACE:
126                         BLI_assert(data != NULL);
127                         BLI_assert(((BMElem *)data)->head.htype == BM_FACE);
128                         iter->begin = (BMIter__begin_cb)bmiter__edge_of_face_begin;
129                         iter->step  = (BMIter__step_cb)bmiter__edge_of_face_step;
130                         iter->data.edge_of_face.pdata = (BMFace *)data;
131                         break;
132                 case BM_LOOPS_OF_FACE:
133                         BLI_assert(data != NULL);
134                         BLI_assert(((BMElem *)data)->head.htype == BM_FACE);
135                         iter->begin = (BMIter__begin_cb)bmiter__loop_of_face_begin;
136                         iter->step  = (BMIter__step_cb)bmiter__loop_of_face_step;
137                         iter->data.loop_of_face.pdata = (BMFace *)data;
138                         break;
139                 case BM_LOOPS_OF_LOOP:
140                         BLI_assert(data != NULL);
141                         BLI_assert(((BMElem *)data)->head.htype == BM_LOOP);
142                         iter->begin = (BMIter__begin_cb)bmiter__loop_of_loop_begin;
143                         iter->step  = (BMIter__step_cb)bmiter__loop_of_loop_step;
144                         iter->data.loop_of_loop.ldata = (BMLoop *)data;
145                         break;
146                 case BM_LOOPS_OF_EDGE:
147                         BLI_assert(data != NULL);
148                         BLI_assert(((BMElem *)data)->head.htype == BM_EDGE);
149                         iter->begin = (BMIter__begin_cb)bmiter__loop_of_edge_begin;
150                         iter->step  = (BMIter__step_cb)bmiter__loop_of_edge_step;
151                         iter->data.loop_of_edge.edata = (BMEdge *)data;
152                         break;
153                 default:
154                         /* should never happen */
155                         BLI_assert(0);
156                         return false;
157                         break;
158         }
159         
160         iter->begin(iter);
161
162         return true;
163 }
164
165 /**
166  * \brief Iterator New
167  *
168  * Takes a bmesh iterator structure and fills
169  * it with the appropriate function pointers based
170  * upon its type and then calls BMeshIter_step()
171  * to return the first element of the iterator.
172  *
173  */
174 ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
175 BLI_INLINE void *BM_iter_new(BMIter *iter, BMesh *bm, const char itype, void *data)
176 {
177         if (LIKELY(BM_iter_init(iter, bm, itype, data))) {
178                 return BM_iter_step(iter);
179         }
180         else {
181                 return NULL;
182         }
183 }
184
185 /**
186  * \brief Parallel (threaded) iterator, only available for most basic itertypes (verts/edges/faces of mesh).
187  *
188  * Uses BLI_task_parallel_mempool to iterate over all items of underlying matching mempool.
189  *
190  * \note You have to include BLI_task.h before BMesh includes to be able to use this function!
191  */
192
193 #ifdef __BLI_TASK_H__
194
195 ATTR_NONNULL(1)
196 BLI_INLINE void BM_iter_parallel(
197         BMesh *bm, const char itype, TaskParallelMempoolFunc func, void *userdata, const bool use_threading)
198 {
199         /* inlining optimizes out this switch when called with the defined type */
200         switch ((BMIterType)itype) {
201                 case BM_VERTS_OF_MESH:
202                         BLI_task_parallel_mempool(bm->vpool, userdata, func, use_threading);
203                         break;
204                 case BM_EDGES_OF_MESH:
205                         BLI_task_parallel_mempool(bm->epool, userdata, func, use_threading);
206                         break;
207                 case BM_FACES_OF_MESH:
208                         BLI_task_parallel_mempool(bm->fpool, userdata, func, use_threading);
209                         break;
210                 default:
211                         /* should never happen */
212                         BLI_assert(0);
213                         break;
214         }
215 }
216
217 #endif  /* __BLI_TASK_H__ */
218
219 #endif /* __BMESH_ITERATORS_INLINE_H__ */