Blender V5.3
BKE_idprop.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include <memory>
12#include <optional>
13
14#include "DNA_ID.h"
15#include "DNA_ID_enums.h"
16
17#include "BLI_compiler_attrs.hh"
18#include "BLI_function_ref.hh"
19#include "BLI_span.hh"
20#include "BLI_string_ref.hh"
21#include "BLI_sys_types.hh"
22#include "BLI_vector_set.hh"
23
24namespace blender {
25
26struct BlendDataReader;
27struct BlendWriter;
28struct ID;
29struct IDProperty;
30struct IDPropertyUIData;
32namespace io::serialize {
33class ArrayValue;
34class Value;
35} // namespace io::serialize
36
38 int i;
39 float f;
40 double d;
41 struct {
42 const char *str;
44 int len;
46 char subtype;
49 struct {
50 int len;
52 char type;
54};
55
56/* ----------- Property Array Type ---------- */
57
62IDProperty *IDP_NewIDPArray(StringRef name) ATTR_WARN_UNUSED_RESULT;
67IDProperty *IDP_CopyIDPArray(const IDProperty *array, int flag) ATTR_WARN_UNUSED_RESULT
69
73void IDP_SetIndexArray(IDProperty *prop, int index, IDProperty *item) ATTR_NONNULL();
74IDProperty *IDP_GetIndexArray(IDProperty *prop, int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
75void IDP_AppendArray(IDProperty *prop, IDProperty *item);
76void IDP_ResizeIDPArray(IDProperty *prop, int newlen);
77
78/* ----------- Numeric Array Type ----------- */
79
83void IDP_ResizeArray(IDProperty *prop, int newlen);
84
85/* ---------- String Type ------------ */
94IDProperty *IDP_NewStringMaxSize(const char *st,
95 size_t st_maxncpy,
96 StringRef name,
98IDProperty *IDP_NewString(const char *st,
99 StringRef name,
110void IDP_AssignStringMaxSize(IDProperty *prop, const char *st, size_t st_maxncpy) ATTR_NONNULL();
111void IDP_AssignString(IDProperty *prop, const char *st) ATTR_NONNULL();
112void IDP_FreeString(IDProperty *prop) ATTR_NONNULL();
113
114/*-------- Enum Type -------*/
115
116const IDPropertyUIDataEnumItem *IDP_EnumItemFind(const IDProperty *prop);
117
118bool IDP_EnumItemsValidate(const IDPropertyUIDataEnumItem *items,
119 int items_num,
120 void (*error_fn)(const char *));
121
123void IDP_EnumItemsFree(IDPropertyUIDataEnumItem *items, int items_num);
124
125/*-------- ID Type -------*/
126
127using IDPWalkFunc = void (*)(void *user_data, IDProperty *idp);
128
133void IDP_AssignID(IDProperty *prop, ID *id, int flag);
134
135/*-------- Group Functions -------*/
136
143void IDP_SyncGroupValues(IDProperty *dest, const IDProperty *src) ATTR_NONNULL();
144void IDP_SyncGroupTypes(IDProperty *dest, const IDProperty *src, bool do_arraylen) ATTR_NONNULL();
157void IDP_ReplaceInGroup_ex(IDProperty *group, IDProperty *prop, IDProperty *prop_exist, int flag);
162void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, bool do_overwrite) ATTR_NONNULL();
170void IDP_MergeGroup_ex(IDProperty *dest, const IDProperty *src, bool do_overwrite, int flag)
171 ATTR_NONNULL();
194
200
205 StringRef name,
207
208/*-------- Main Functions --------*/
227
234 ATTR_NONNULL();
240
244bool IDP_EqualsProperties_ex(const IDProperty *prop1,
245 const IDProperty *prop2,
246 bool is_strict) ATTR_WARN_UNUSED_RESULT;
247
248bool IDP_EqualsProperties(const IDProperty *prop1,
250
278IDProperty *IDP_New(char type,
279 const IDPropertyTemplate *val,
280 StringRef name,
282
283/* ----------- Allocators for simple types ----------- */
284
285[[nodiscard]] IDProperty *IDP_NewInt(int value,
287 eIDPropertyFlag flags = {});
288
293void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user);
294void IDP_FreeProperty(IDProperty *prop);
295
296void IDP_ClearProperty(IDProperty *prop);
297
298void IDP_Reset(IDProperty *prop, const IDProperty *reference);
299
300#ifndef NDEBUG
301const IDProperty *_IDP_assert_type(const IDProperty *prop, char ty);
302const IDProperty *_IDP_assert_type_and_subtype(const IDProperty *prop, char ty, char sub_ty);
303const IDProperty *_IDP_assert_type_mask(const IDProperty *prop, int ty_mask);
304
305#else
306# define _IDP_assert_type(prop, ty) (prop)
307# define _IDP_assert_type_and_subtype(prop, ty, sub_ty) (prop)
308# define _IDP_assert_type_mask(prop, ty_mask) (prop)
309#endif
310
311#define IDP_int_get(prop) (_IDP_assert_type(prop, IDP_INT)->data.val)
312#define IDP_int_set(prop, value) \
313 { \
314 IDProperty *prop_ = (prop); \
315 BLI_assert(prop_->type == IDP_INT); \
316 prop_->data.val = value; \
317 } \
318 ((void)0)
319
320#define IDP_bool_get(prop) ((_IDP_assert_type(prop, IDP_BOOLEAN))->data.val)
321#define IDP_bool_set(prop, value) \
322 { \
323 IDProperty *prop_ = (prop); \
324 BLI_assert(prop_->type == IDP_BOOLEAN); \
325 prop_->data.val = value; \
326 } \
327 ((void)0)
328
329#define IDP_int_or_bool_get(prop) \
330 (_IDP_assert_type_mask(prop, (1 << IDP_INT) | (1 << IDP_BOOLEAN))->data.val)
331#define IDP_int_or_bool_set(prop, value) \
332 { \
333 IDProperty *prop_ = (prop); \
334 BLI_assert(ELEM(prop_->type, IDP_INT, IDP_BOOLEAN)); \
335 prop_->data.val = value; \
336 } \
337 ((void)0)
338
339#define IDP_float_get(prop) (*(const float *)&(_IDP_assert_type(prop, IDP_FLOAT)->data.val))
340#define IDP_float_set(prop, value) \
341 { \
342 IDProperty *prop_ = (prop); \
343 BLI_assert(prop_->type == IDP_FLOAT); \
344 (*(float *)&(prop_)->data.val) = value; \
345 } \
346 ((void)0)
347
348#define IDP_double_get(prop) (*(const double *)&(_IDP_assert_type(prop, IDP_DOUBLE)->data.val))
349#define IDP_double_set(prop, value) \
350 { \
351 IDProperty *prop_ = (prop); \
352 BLI_assert(prop_->type == IDP_DOUBLE); \
353 (*(double *)&(prop_)->data.val) = value; \
354 } \
355 ((void)0)
356
362#define IDP_array_voidp_get(prop) (_IDP_assert_type(prop, IDP_ARRAY)->data.pointer)
363
364#define IDP_array_int_get(prop) \
365 static_cast<int *>(_IDP_assert_type_and_subtype(prop, IDP_ARRAY, IDP_INT)->data.pointer)
366#define IDP_array_bool_get(prop) \
367 static_cast<int8_t *>(_IDP_assert_type_and_subtype(prop, IDP_ARRAY, IDP_BOOLEAN)->data.pointer)
368#define IDP_array_float_get(prop) \
369 static_cast<float *>(_IDP_assert_type_and_subtype(prop, IDP_ARRAY, IDP_FLOAT)->data.pointer)
370#define IDP_array_double_get(prop) \
371 static_cast<double *>(_IDP_assert_type_and_subtype(prop, IDP_ARRAY, IDP_DOUBLE)->data.pointer)
372#define IDP_property_array_get(prop) \
373 static_cast<IDProperty *>(_IDP_assert_type(prop, IDP_IDPARRAY)->data.pointer)
374
375#define IDP_string_get(prop) ((char *)_IDP_assert_type(prop, IDP_STRING)->data.pointer)
376/* No `IDP_string_set` needed. */
377#define IDP_ID_get(prop) ((void)0, ((ID *)_IDP_assert_type(prop, IDP_ID)->data.pointer))
378/* No `IDP_ID_set` needed. */
379
380std::optional<StringRefNull> IDP_group_lookup_string(const IDProperty &group, StringRef name);
381std::optional<float> IDP_group_lookup_float(const IDProperty &group, StringRef name);
382std::optional<int> IDP_group_lookup_int(const IDProperty &group, StringRef name);
383std::optional<bool> IDP_group_lookup_bool(const IDProperty &group, StringRef name);
384std::optional<Span<float>> IDP_group_lookup_float_array(const IDProperty &group,
385 StringRef name,
386 int required_size);
387
392int IDP_coerce_to_int_or_zero(const IDProperty *prop);
397float IDP_coerce_to_float_or_zero(const IDProperty *prop);
402double IDP_coerce_to_double_or_zero(const IDProperty *prop);
403
412void IDP_foreach_property(IDProperty *id_property_root,
413 int type_filter,
414 FunctionRef<void(IDProperty *id_property)> callback);
415
416/* Format IDProperty as strings */
417char *IDP_reprN(const IDProperty *prop, uint *r_len);
418void IDP_repr_fn(const IDProperty *prop,
419 void (*str_append_fn)(void *user_data, const char *str, uint str_len),
420 void *user_data);
421void IDP_print(const IDProperty *prop);
422
423const char *IDP_type_str(eIDPropertyType type, short sub_type);
424const char *IDP_type_str(const IDProperty *prop);
425
426void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop);
428 IDProperty **prop,
429 const char *caller_func_id);
430#define IDP_BlendDataRead(reader, prop) IDP_BlendReadData_impl(reader, prop, __func__)
431
446
447bool IDP_ui_data_supported(const IDProperty *prop);
448eIDPropertyUIDataType IDP_ui_data_type(const IDProperty *prop);
449void IDP_ui_data_free(IDProperty *prop);
455void IDP_ui_data_free_unique_contents(IDPropertyUIData *ui_data,
457 const IDPropertyUIData *other);
458IDPropertyUIData *IDP_ui_data_ensure(IDProperty *prop);
459IDPropertyUIData *IDP_ui_data_copy(const IDProperty *prop);
464IDPropertyUIData *IDP_TryConvertUIData(IDPropertyUIData *src,
465 eIDPropertyUIDataType src_type,
466 eIDPropertyUIDataType dst_type);
467
468namespace bke::idprop {
469
477std::unique_ptr<io::serialize::ArrayValue> convert_to_serialize_values(
478 const IDProperty *properties);
479
486
488 public:
489 void operator()(IDProperty *id_prop)
490 {
491 IDP_FreeProperty(id_prop);
492 }
493};
494
505
507std::unique_ptr<IDProperty, IDPropertyDeleter> create_bool(StringRef prop_name,
508 bool value,
509 eIDPropertyFlag flags = {});
510
512std::unique_ptr<IDProperty, IDPropertyDeleter> create(StringRef prop_name,
513 int32_t value,
514 eIDPropertyFlag flags = {});
515
517std::unique_ptr<IDProperty, IDPropertyDeleter> create(StringRef prop_name,
518 float value,
519 eIDPropertyFlag flags = {});
520
522std::unique_ptr<IDProperty, IDPropertyDeleter> create(StringRef prop_name,
523 double value,
524 eIDPropertyFlag flags = {});
525
527std::unique_ptr<IDProperty, IDPropertyDeleter> create(StringRef prop_name,
528 StringRefNull value,
529 eIDPropertyFlag flags = {});
530
532std::unique_ptr<IDProperty, IDPropertyDeleter> create(StringRef prop_name,
533 ID *value,
534 eIDPropertyFlag flags = {});
535
541std::unique_ptr<IDProperty, IDPropertyDeleter> create(StringRef prop_name,
542 Span<int32_t> values,
543 eIDPropertyFlag flags = {});
544
550std::unique_ptr<IDProperty, IDPropertyDeleter> create(StringRef prop_name,
551 Span<float> values,
552 eIDPropertyFlag flags = {});
553
559std::unique_ptr<IDProperty, IDPropertyDeleter> create(StringRef prop_name,
560 Span<double> values,
561 eIDPropertyFlag flags = {});
562
568
569std::unique_ptr<IDProperty, IDPropertyDeleter> create_group(StringRef prop_name,
570 eIDPropertyFlag flags = {});
571
572} // namespace bke::idprop
573} // namespace blender
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_NONNULL(...)
ID and Library types, which are fundamental for SDNA.
Enumerations for DNA_ID.h.
constexpr StringRef()
void operator()(IDProperty *id_prop)
#define str(s)
#define index(i)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_bool(StringRef prop_name, bool value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_BOOLEAN, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
IDProperty * convert_from_serialize_value(const io::serialize::Value &value)
Convert the given value (which must be an array) to an IDProperty.
std::unique_ptr< io::serialize::ArrayValue > convert_to_serialize_values(const IDProperty *properties)
Convert the given properties to Value objects for serialization.
BMesh Iterators.
bool IDP_EnumItemsValidate(const IDPropertyUIDataEnumItem *items, int items_num, void(*error_fn)(const char *))
Definition idprop.cc:558
void IDP_FreeFromGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:812
float IDP_coerce_to_float_or_zero(const IDProperty *prop)
Definition idprop.cc:952
void IDP_print(const IDProperty *prop)
void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user)
Definition idprop.cc:1398
void IDP_ReplaceGroupInGroup(IDProperty *dest, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:705
void IDP_ReplaceInGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:737
IDProperty * IDP_GetPropertyTypeFromGroup(const IDProperty *prop, StringRef name, char type) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:839
unsigned int uint
Definition BKE_text.h:55
IDProperty * IDP_ID_system_properties_ensure(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1023
double IDP_coerce_to_double_or_zero(const IDProperty *prop)
Definition idprop.cc:936
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop)
Definition idprop.cc:1696
void IDP_foreach_property(IDProperty *id_property_root, int type_filter, FunctionRef< void(IDProperty *id_property)> callback)
void IDP_FreeString(IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:512
int IDP_coerce_to_int_or_zero(const IDProperty *prop)
Definition idprop.cc:920
const IDProperty * _IDP_assert_type_mask(const IDProperty *prop, int ty_mask)
Definition idprop.cc:2322
std::optional< int > IDP_group_lookup_int(const IDProperty &group, StringRef name)
Definition idprop.cc:880
std::optional< float > IDP_group_lookup_float(const IDProperty &group, StringRef name)
Definition idprop.cc:871
eIDPropertyUIDataType IDP_ui_data_type(const IDProperty *prop)
Definition idprop.cc:1964
IDProperty * IDP_NewString(const char *st, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:443
std::optional< StringRefNull > IDP_group_lookup_string(const IDProperty &group, StringRef name)
Definition idprop.cc:862
void IDP_AssignID(IDProperty *prop, ID *id, int flag)
Definition idprop.cc:616
IDProperty * IDP_ID_system_properties_get(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1018
void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, bool do_overwrite) ATTR_NONNULL()
Definition idprop.cc:783
void IDP_ReplaceInGroup_ex(IDProperty *group, IDProperty *prop, IDProperty *prop_exist, int flag)
Definition idprop.cc:716
std::optional< bool > IDP_group_lookup_bool(const IDProperty &group, StringRef name)
Definition idprop.cc:889
void IDP_CopyPropertyContent(IDProperty *dst, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:991
void IDP_ResizeIDPArray(IDProperty *prop, int newlen)
Definition idprop.cc:203
void IDP_ClearProperty(IDProperty *prop)
Definition idprop.cc:1408
IDPropertyUIData * IDP_TryConvertUIData(IDPropertyUIData *src, eIDPropertyUIDataType src_type, eIDPropertyUIDataType dst_type)
Definition idprop.cc:2052
void IDP_RemoveFromGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:801
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet
void IDP_ui_data_free(IDProperty *prop)
Definition idprop.cc:1340
IDProperty * IDP_GetProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1000
void IDP_SetIndexArray(IDProperty *prop, int index, IDProperty *item) ATTR_NONNULL()
Definition idprop.cc:149
void IDP_AssignString(IDProperty *prop, const char *st) ATTR_NONNULL()
Definition idprop.cc:505
const IDProperty * _IDP_assert_type(const IDProperty *prop, char ty)
Definition idprop.cc:2309
char * IDP_reprN(const IDProperty *prop, uint *r_len)
void IDP_ResizeArray(IDProperty *prop, int newlen)
Definition idprop.cc:263
const char * IDP_type_str(eIDPropertyType type, short sub_type)
IDProperty * IDP_GetIndexArray(IDProperty *prop, int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:165
void IDP_SyncGroupValues(IDProperty *dest, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:655
void IDP_BlendReadData_impl(BlendDataReader *reader, IDProperty **prop, const char *caller_func_id)
Definition idprop.cc:1948
void IDP_AppendArray(IDProperty *prop, IDProperty *item)
Definition idprop.cc:172
void IDP_SyncGroupTypes(IDProperty *dest, const IDProperty *src, bool do_arraylen) ATTR_NONNULL()
Definition idprop.cc:682
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
void IDP_ui_data_free_unique_contents(IDPropertyUIData *ui_data, eIDPropertyUIDataType type, const IDPropertyUIData *other)
Definition idprop.cc:1247
void IDP_repr_fn(const IDProperty *prop, void(*str_append_fn)(void *user_data, const char *str, uint str_len), void *user_data)
IDProperty * IDP_NewInt(int value, blender::StringRef name, eIDPropertyFlag flags={})
Definition idprop.cc:1240
IDProperty * IDP_CopyIDPArray(const IDProperty *array, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:114
void IDP_MergeGroup_ex(IDProperty *dest, const IDProperty *src, bool do_overwrite, int flag) ATTR_NONNULL()
Definition idprop.cc:744
signed int int32_t
Definition BKE_text.h:78
std::optional< Span< float > > IDP_group_lookup_float_array(const IDProperty &group, StringRef name, int required_size)
Definition idprop.cc:898
IDProperty * IDP_EnsureProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1005
const IDPropertyUIDataEnumItem * IDP_EnumItemFind(const IDProperty *prop)
Definition idprop.cc:543
IDProperty * IDP_New(char type, const IDPropertyTemplate *val, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:1132
IDProperty * IDP_NewStringMaxSize(const char *st, size_t st_maxncpy, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:400
void IDP_EnumItemsFree(IDPropertyUIDataEnumItem *items, int items_num)
Definition idprop.cc:526
void(*)(void *user_data, IDProperty *idp) IDPWalkFunc
bool ATTR_WARN_UNUSED_RESULT
IDPropertyUIData * IDP_ui_data_copy(const IDProperty *prop)
Definition idprop.cc:298
void ATTR_NONNULL(1, 2)
IDProperty * IDP_CopyProperty_ex(const IDProperty *prop, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:968
IDPropertyUIData * IDP_ui_data_ensure(IDProperty *prop)
Definition idprop.cc:2034
IDProperty * IDP_GetPropertyFromGroup_null(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:831
eIDPropertyUIDataType
@ IDP_UI_DATA_TYPE_ID
@ IDP_UI_DATA_TYPE_STRING
@ IDP_UI_DATA_TYPE_FLOAT
@ IDP_UI_DATA_TYPE_INT
@ IDP_UI_DATA_TYPE_UNSUPPORTED
@ IDP_UI_DATA_TYPE_BOOLEAN
bool IDP_ui_data_supported(const IDProperty *prop)
Definition idprop.cc:1986
bool IDP_EqualsProperties(const IDProperty *prop1, const IDProperty *prop2) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:1127
void IDP_Reset(IDProperty *prop, const IDProperty *reference)
Definition idprop.cc:1415
IDProperty * IDP_NewIDPArray(StringRef name) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:104
void IDP_AssignStringMaxSize(IDProperty *prop, const char *st, size_t st_maxncpy) ATTR_NONNULL()
Definition idprop.cc:470
const IDProperty * _IDP_assert_type_and_subtype(const IDProperty *prop, char ty, char sub_ty)
Definition idprop.cc:2314
bool IDP_EqualsProperties_ex(const IDProperty *prop1, const IDProperty *prop2, bool is_strict) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:1036
CustomIDVectorSet< IDProperty *, IDPropNameGetter, 8 > children
struct blender::IDPropertyTemplate::@116303142222164257205355242364302075230051143265 string