/source/blender/blenlib/BLI_listbase.h

https://github.com/mgschwan/blensor · C Header · 141 lines · 72 code · 18 blank · 51 comment · 15 complexity · 7e19168dbf28a7aef5f129ccf2dabe56 MD5 · raw file

  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. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. #ifndef __BLI_LISTBASE_H__
  28. #define __BLI_LISTBASE_H__
  29. /** \file BLI_listbase.h
  30. * \ingroup bli
  31. */
  32. #include "BLI_compiler_attrs.h"
  33. #include "BLI_utildefines.h"
  34. #include "DNA_listBase.h"
  35. //struct ListBase;
  36. //struct LinkData;
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  41. int BLI_findstringindex(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  42. /* find forwards */
  43. void *BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  44. void *BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  45. void *BLI_findstring_ptr(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  46. void *BLI_findptr(const struct ListBase *listbase, const void *ptr, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  47. void *BLI_listbase_bytes_find(const ListBase *listbase, const void *bytes, const size_t bytes_size, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 2);
  48. /* find backwards */
  49. void *BLI_rfindlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  50. void *BLI_rfindstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  51. void *BLI_rfindstring_ptr(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  52. void *BLI_rfindptr(const struct ListBase *listbase, const void *ptr, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  53. void *BLI_listbase_bytes_rfind(const ListBase *listbase, const void *bytes, const size_t bytes_size, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 2);
  54. void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1);
  55. void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
  56. void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
  57. bool BLI_remlink_safe(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
  58. void *BLI_pophead(ListBase *listbase) ATTR_NONNULL(1);
  59. void *BLI_poptail(ListBase *listbase) ATTR_NONNULL(1);
  60. void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
  61. void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1);
  62. void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1);
  63. void BLI_insertlinkreplace(ListBase *listbase, void *v_l_src, void *v_l_dst) ATTR_NONNULL(1, 2, 3);
  64. void BLI_listbase_sort(struct ListBase *listbase, int (*cmp)(const void *, const void *)) ATTR_NONNULL(1, 2);
  65. void BLI_listbase_sort_r(ListBase *listbase, int (*cmp)(void *, const void *, const void *), void *thunk) ATTR_NONNULL(1, 2);
  66. bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL();
  67. void BLI_freelist(struct ListBase *listbase) ATTR_NONNULL(1);
  68. int BLI_listbase_count_ex(const struct ListBase *listbase, const int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  69. int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
  70. void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
  71. void BLI_listbase_swaplinks(struct ListBase *listbase, void *vlinka, void *vlinkb) ATTR_NONNULL(1, 2);
  72. void BLI_listbases_swaplinks(struct ListBase *listbasea, struct ListBase *listbaseb, void *vlinka, void *vlinkb) ATTR_NONNULL(2, 3);
  73. void BLI_movelisttolist(struct ListBase *dst, struct ListBase *src) ATTR_NONNULL(1, 2);
  74. void BLI_duplicatelist(struct ListBase *dst, const struct ListBase *src) ATTR_NONNULL(1, 2);
  75. void BLI_listbase_reverse(struct ListBase *lb) ATTR_NONNULL(1);
  76. void BLI_listbase_rotate_first(struct ListBase *lb, void *vlink) ATTR_NONNULL(1, 2);
  77. void BLI_listbase_rotate_last(struct ListBase *lb, void *vlink) ATTR_NONNULL(1, 2);
  78. /**
  79. * Utility functions to avoid first/last references inline all over.
  80. */
  81. BLI_INLINE bool BLI_listbase_is_single(const struct ListBase *lb) { return (lb->first && lb->first == lb->last); }
  82. BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb) { return (lb->first == (void *)0); }
  83. BLI_INLINE void BLI_listbase_clear(struct ListBase *lb) { lb->first = lb->last = (void *)0; }
  84. /* create a generic list node containing link to provided data */
  85. struct LinkData *BLI_genericNodeN(void *data);
  86. /**
  87. * Does a full loop on the list, with any value acting as first
  88. * (handy for cycling items)
  89. *
  90. * \code{.c}
  91. *
  92. * LISTBASE_CIRCULAR_FORWARD_BEGIN(listbase, item, item_init)
  93. * {
  94. * ...operate on marker...
  95. * }
  96. * LISTBASE_CIRCULAR_FORWARD_END (listbase, item, item_init);
  97. *
  98. * \endcode
  99. */
  100. #define LISTBASE_CIRCULAR_FORWARD_BEGIN(lb, lb_iter, lb_init) \
  101. if ((lb)->first && (lb_init || (lb_init = (lb)->first))) { \
  102. lb_iter = lb_init; \
  103. do {
  104. #define LISTBASE_CIRCULAR_FORWARD_END(lb, lb_iter, lb_init) \
  105. } while ((lb_iter = (lb_iter)->next ? (lb_iter)->next : (lb)->first), \
  106. (lb_iter != lb_init)); \
  107. }
  108. #define LISTBASE_CIRCULAR_BACKWARD_BEGIN(lb, lb_iter, lb_init) \
  109. if ((lb)->last && (lb_init || (lb_init = (lb)->last))) { \
  110. lb_iter = lb_init; \
  111. do {
  112. #define LISTBASE_CIRCULAR_BACKWARD_END(lb, lb_iter, lb_init) \
  113. } while ((lb_iter = (lb_iter)->prev ? (lb_iter)->prev : (lb)->last), \
  114. (lb_iter != lb_init)); \
  115. }
  116. #define LISTBASE_FOREACH(type, var, list) \
  117. for (type var = (type)((list)->first); \
  118. var != NULL; \
  119. var = (type)(((Link*)(var))->next))
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif /* __BLI_LISTBASE_H__ */