BKE_idprop.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. #ifndef __BKE_IDPROP_H__
  23. #define __BKE_IDPROP_H__
  24. /** \file BKE_idprop.h
  25. * \ingroup bke
  26. * \author Joseph Eagar
  27. */
  28. #include "DNA_ID.h"
  29. #include "BLI_compiler_attrs.h"
  30. struct IDProperty;
  31. struct ID;
  32. typedef union IDPropertyTemplate {
  33. int i;
  34. float f;
  35. double d;
  36. struct {
  37. const char *str;
  38. int len;
  39. char subtype;
  40. } string;
  41. struct ID *id;
  42. struct {
  43. int len;
  44. char type;
  45. } array;
  46. struct {
  47. int matvec_size;
  48. const float *example;
  49. } matrix_or_vector;
  50. } IDPropertyTemplate;
  51. /* ----------- Property Array Type ---------- */
  52. IDProperty *IDP_NewIDPArray(const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
  53. IDProperty *IDP_CopyIDPArray(const IDProperty *array) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
  54. /* shallow copies item */
  55. void IDP_SetIndexArray(struct IDProperty *prop, int index, struct IDProperty *item) ATTR_NONNULL();
  56. struct IDProperty *IDP_GetIndexArray(struct IDProperty *prop, int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
  57. void IDP_AppendArray(struct IDProperty *prop, struct IDProperty *item);
  58. void IDP_ResizeIDPArray(struct IDProperty *prop, int len);
  59. /* ----------- Numeric Array Type ----------- */
  60. /*this function works for strings too!*/
  61. void IDP_ResizeArray(struct IDProperty *prop, int newlen);
  62. void IDP_FreeArray(struct IDProperty *prop);
  63. /* ---------- String Type ------------ */
  64. IDProperty *IDP_NewString(const char *st, const char *name, int maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2 /* 'name 'arg */); /* maxlen excludes '\0' */
  65. void IDP_AssignString(struct IDProperty *prop, const char *st, int maxlen) ATTR_NONNULL(); /* maxlen excludes '\0' */
  66. void IDP_ConcatStringC(struct IDProperty *prop, const char *st) ATTR_NONNULL();
  67. void IDP_ConcatString(struct IDProperty *str1, struct IDProperty *append) ATTR_NONNULL();
  68. void IDP_FreeString(struct IDProperty *prop) ATTR_NONNULL();
  69. /*-------- ID Type -------*/
  70. typedef void(*IDPWalkFunc)(void *userData, IDProperty *idp);
  71. /*-------- Group Functions -------*/
  72. /** Sync values from one group to another, only where they match */
  73. void IDP_SyncGroupValues(struct IDProperty *dest, const struct IDProperty *src) ATTR_NONNULL();
  74. void IDP_SyncGroupTypes(struct IDProperty *dest, const struct IDProperty *src, const bool do_arraylen) ATTR_NONNULL();
  75. void IDP_ReplaceGroupInGroup(struct IDProperty *dest, const struct IDProperty *src) ATTR_NONNULL();
  76. void IDP_ReplaceInGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL();
  77. void IDP_ReplaceInGroup_ex(struct IDProperty *group, struct IDProperty *prop, struct IDProperty *prop_exist);
  78. void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, const bool do_overwrite) ATTR_NONNULL();
  79. bool IDP_AddToGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL();
  80. bool IDP_InsertToGroup(struct IDProperty *group, struct IDProperty *previous,
  81. struct IDProperty *pnew) ATTR_NONNULL(1 /* group */, 3 /* pnew */);
  82. void IDP_RemoveFromGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL();
  83. void IDP_FreeFromGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL();
  84. IDProperty *IDP_GetPropertyFromGroup(struct IDProperty *prop, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
  85. IDProperty *IDP_GetPropertyTypeFromGroup(struct IDProperty *prop, const char *name, const char type) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
  86. /*-------- Main Functions --------*/
  87. struct IDProperty *IDP_GetProperties(struct ID *id, const bool create_if_needed) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
  88. struct IDProperty *IDP_CopyProperty(const struct IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
  89. bool IDP_EqualsProperties_ex(IDProperty *prop1, IDProperty *prop2, const bool is_strict) ATTR_WARN_UNUSED_RESULT;
  90. bool IDP_EqualsProperties(struct IDProperty *prop1, struct IDProperty *prop2) ATTR_WARN_UNUSED_RESULT;
  91. struct IDProperty *IDP_New(const char type, const IDPropertyTemplate *val, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
  92. void IDP_FreeProperty_ex(struct IDProperty *prop, const bool do_id_user);
  93. void IDP_FreeProperty(struct IDProperty *prop);
  94. void IDP_ClearProperty(IDProperty *prop);
  95. void IDP_RelinkProperty(struct IDProperty *prop);
  96. #define IDP_Int(prop) ((prop)->data.val)
  97. #define IDP_Array(prop) ((prop)->data.pointer)
  98. /* C11 const correctness for casts */
  99. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  100. # define IDP_Float(prop) _Generic((prop), \
  101. IDProperty *: (*(float *)&(prop)->data.val), \
  102. const IDProperty *: (*(const float *)&(prop)->data.val))
  103. # define IDP_Double(prop) _Generic((prop), \
  104. IDProperty *: (*(double *)&(prop)->data.val), \
  105. const IDProperty *: (*(const double *)&(prop)->data.val))
  106. # define IDP_String(prop) _Generic((prop), \
  107. IDProperty *: ((char *) (prop)->data.pointer), \
  108. const IDProperty *: ((const char *) (prop)->data.pointer))
  109. # define IDP_IDPArray(prop) _Generic((prop), \
  110. IDProperty *: ((IDProperty *) (prop)->data.pointer), \
  111. const IDProperty *: ((const IDProperty *) (prop)->data.pointer))
  112. # define IDP_Id(prop) _Generic((prop), \
  113. IDProperty *: ((ID *) (prop)->data.pointer), \
  114. const IDProperty *: ((const ID *) (prop)->data.pointer))
  115. #else
  116. # define IDP_Float(prop) (*(float *)&(prop)->data.val)
  117. # define IDP_Double(prop) (*(double *)&(prop)->data.val)
  118. # define IDP_String(prop) ((char *) (prop)->data.pointer)
  119. # define IDP_IDPArray(prop) ((IDProperty *) (prop)->data.pointer)
  120. # define IDP_Id(prop) ((ID *) (prop)->data.pointer)
  121. #endif
  122. #ifndef NDEBUG
  123. /* for printout only */
  124. void IDP_spit(IDProperty *prop);
  125. #endif
  126. #endif /* __BKE_IDPROP_H__ */