1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 /** \file
23  * \ingroup bli
24  */
25 
26 #include "BLI_compiler_attrs.h"
27 #include "BLI_utildefines.h"
28 #include "DNA_listBase.h"
29 // struct ListBase;
30 // struct LinkData;
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT
37     ATTR_NONNULL(1);
38 int BLI_findstringindex(const struct ListBase *listbase,
39                         const char *id,
40                         const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
41 
42 /* find forwards */
43 void *BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT
44     ATTR_NONNULL(1);
45 void *BLI_findstring(const struct ListBase *listbase,
46                      const char *id,
47                      const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
48 void *BLI_findstring_ptr(const struct ListBase *listbase,
49                          const char *id,
50                          const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
51 void *BLI_findptr(const struct ListBase *listbase,
52                   const void *ptr,
53                   const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
54 void *BLI_listbase_bytes_find(const ListBase *listbase,
55                               const void *bytes,
56                               const size_t bytes_size,
57                               const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 2);
58 
59 /* find backwards */
60 void *BLI_rfindlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT
61     ATTR_NONNULL(1);
62 void *BLI_rfindstring(const struct ListBase *listbase,
63                       const char *id,
64                       const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
65 void *BLI_rfindstring_ptr(const struct ListBase *listbase,
66                           const char *id,
67                           const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
68 void *BLI_rfindptr(const struct ListBase *listbase,
69                    const void *ptr,
70                    const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
71 void *BLI_listbase_bytes_rfind(const ListBase *listbase,
72                                const void *bytes,
73                                const size_t bytes_size,
74                                const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 2);
75 
76 void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1);
77 void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
78 void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
79 bool BLI_remlink_safe(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
80 void *BLI_pophead(ListBase *listbase) ATTR_NONNULL(1);
81 void *BLI_poptail(ListBase *listbase) ATTR_NONNULL(1);
82 
83 void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
84 void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink)
85     ATTR_NONNULL(1);
86 void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink)
87     ATTR_NONNULL(1);
88 void BLI_insertlinkreplace(ListBase *listbase, void *vreplacelink, void *vnewlink)
89     ATTR_NONNULL(1, 2, 3);
90 void BLI_listbase_sort(struct ListBase *listbase, int (*cmp)(const void *, const void *))
91     ATTR_NONNULL(1, 2);
92 void BLI_listbase_sort_r(ListBase *listbase,
93                          int (*cmp)(void *, const void *, const void *),
94                          void *thunk) ATTR_NONNULL(1, 2);
95 bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL();
96 bool BLI_listbase_move_index(ListBase *listbase, int from, int to) ATTR_NONNULL();
97 void BLI_freelist(struct ListBase *listbase) ATTR_NONNULL(1);
98 int BLI_listbase_count_at_most(const struct ListBase *listbase,
99                                const int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
100 int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
101 void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
102 
103 void BLI_listbase_swaplinks(struct ListBase *listbase, void *vlinka, void *vlinkb)
104     ATTR_NONNULL(1, 2);
105 void BLI_listbases_swaplinks(struct ListBase *listbasea,
106                              struct ListBase *listbaseb,
107                              void *vlinka,
108                              void *vlinkb) ATTR_NONNULL(2, 3);
109 
110 void BLI_movelisttolist(struct ListBase *dst, struct ListBase *src) ATTR_NONNULL(1, 2);
111 void BLI_movelisttolist_reverse(struct ListBase *dst, struct ListBase *src) ATTR_NONNULL(1, 2);
112 void BLI_duplicatelist(struct ListBase *dst, const struct ListBase *src) ATTR_NONNULL(1, 2);
113 void BLI_listbase_reverse(struct ListBase *lb) ATTR_NONNULL(1);
114 void BLI_listbase_rotate_first(struct ListBase *lb, void *vlink) ATTR_NONNULL(1, 2);
115 void BLI_listbase_rotate_last(struct ListBase *lb, void *vlink) ATTR_NONNULL(1, 2);
116 
117 /**
118  * Utility functions to avoid first/last references inline all over.
119  */
BLI_listbase_is_single(const struct ListBase * lb)120 BLI_INLINE bool BLI_listbase_is_single(const struct ListBase *lb)
121 {
122   return (lb->first && lb->first == lb->last);
123 }
BLI_listbase_is_empty(const struct ListBase * lb)124 BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
125 {
126   return (lb->first == (void *)0);
127 }
BLI_listbase_clear(struct ListBase * lb)128 BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
129 {
130   lb->first = lb->last = (void *)0;
131 }
132 
133 /* create a generic list node containing link to provided data */
134 struct LinkData *BLI_genericNodeN(void *data);
135 
136 /**
137  * Does a full loop on the list, with any value acting as first
138  * (handy for cycling items)
139  *
140  * \code{.c}
141  *
142  * LISTBASE_CIRCULAR_FORWARD_BEGIN(listbase, item, item_init)
143  * {
144  *     ...operate on marker...
145  * }
146  * LISTBASE_CIRCULAR_FORWARD_END (listbase, item, item_init);
147  *
148  * \endcode
149  */
150 #define LISTBASE_CIRCULAR_FORWARD_BEGIN(lb, lb_iter, lb_init) \
151   if ((lb)->first && (lb_init || (lb_init = (lb)->first))) { \
152     lb_iter = lb_init; \
153     do {
154 #define LISTBASE_CIRCULAR_FORWARD_END(lb, lb_iter, lb_init) \
155   } \
156   while ((lb_iter = (lb_iter)->next ? (lb_iter)->next : (lb)->first), (lb_iter != lb_init)) \
157     ; \
158   } \
159   ((void)0)
160 
161 #define LISTBASE_CIRCULAR_BACKWARD_BEGIN(lb, lb_iter, lb_init) \
162   if ((lb)->last && (lb_init || (lb_init = (lb)->last))) { \
163     lb_iter = lb_init; \
164     do {
165 #define LISTBASE_CIRCULAR_BACKWARD_END(lb, lb_iter, lb_init) \
166   } \
167   while ((lb_iter = (lb_iter)->prev ? (lb_iter)->prev : (lb)->last), (lb_iter != lb_init)) \
168     ; \
169   } \
170   ((void)0)
171 
172 #define LISTBASE_FOREACH(type, var, list) \
173   for (type var = (type)((list)->first); var != NULL; var = (type)(((Link *)(var))->next))
174 
175 /**
176  * A version of #LISTBASE_FOREACH that supports incrementing an index variable at every step.
177  * Including this in the macro helps prevent mistakes where "continue" mistakenly skips the
178  * incrementation.
179  */
180 #define LISTBASE_FOREACH_INDEX(type, var, list, index_var) \
181   for (type var = (((void)(index_var = 0)), (type)((list)->first)); var != NULL; \
182        var = (type)(((Link *)(var))->next), index_var++)
183 
184 #define LISTBASE_FOREACH_BACKWARD(type, var, list) \
185   for (type var = (type)((list)->last); var != NULL; var = (type)(((Link *)(var))->prev))
186 
187 /** A version of #LISTBASE_FOREACH that supports removing the item we're looping over. */
188 #define LISTBASE_FOREACH_MUTABLE(type, var, list) \
189   for (type var = (type)((list)->first), *var##_iter_next; \
190        ((var != NULL) ? ((void)(var##_iter_next = (type)(((Link *)(var))->next)), 1) : 0); \
191        var = var##_iter_next)
192 
193 /** A version of #LISTBASE_FOREACH_BACKWARD that supports removing the item we're looping over. */
194 #define LISTBASE_FOREACH_BACKWARD_MUTABLE(type, var, list) \
195   for (type var = (type)((list)->last), *var##_iter_prev; \
196        ((var != NULL) ? ((void)(var##_iter_prev = (type)(((Link *)(var))->prev)), 1) : 0); \
197        var = var##_iter_prev)
198 
199 #ifdef __cplusplus
200 }
201 #endif
202