Blender V5.0
idprop_create.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <type_traits>
6
7#include "DNA_ID.h"
8
9#include "BKE_idprop.hh"
10
11namespace blender::bke::idprop {
12
13/* -------------------------------------------------------------------- */
16
17std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRef prop_name,
18 int32_t value,
19 const eIDPropertyFlag flags)
20{
21 IDPropertyTemplate prop_template{0};
22 prop_template.i = value;
23 IDProperty *property = IDP_New(IDP_INT, &prop_template, prop_name, flags);
24 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
25}
26
27std::unique_ptr<IDProperty, IDPropertyDeleter> create_bool(const StringRef prop_name,
28 bool value,
29 const eIDPropertyFlag flags)
30{
31 IDPropertyTemplate prop_template{0};
32 prop_template.i = value;
33 IDProperty *property = IDP_New(IDP_BOOLEAN, &prop_template, prop_name, flags);
34 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
35}
36
37std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRef prop_name,
38 float value,
39 const eIDPropertyFlag flags)
40{
41 IDPropertyTemplate prop_template{0};
42 prop_template.f = value;
43 IDProperty *property = IDP_New(IDP_FLOAT, &prop_template, prop_name, flags);
44 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
45}
46
47std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRef prop_name,
48 double value,
49 const eIDPropertyFlag flags)
50{
51 IDPropertyTemplate prop_template{0};
52 prop_template.d = value;
53 IDProperty *property = IDP_New(IDP_DOUBLE, &prop_template, prop_name, flags);
54 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
55}
56
57std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRef prop_name,
58 const StringRefNull value,
59 const eIDPropertyFlag flags)
60{
61 IDProperty *property = IDP_NewString(value.c_str(), prop_name, flags);
62 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
63}
64
65std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRef prop_name,
66 ID *value,
67 const eIDPropertyFlag flags)
68{
69 /* Do not assign embedded IDs to IDProperties. */
70 BLI_assert(!value || (value->flag & ID_FLAG_EMBEDDED_DATA) == 0);
71
72 IDPropertyTemplate prop_template{0};
73 prop_template.id = value;
74 IDProperty *property = IDP_New(IDP_ID, &prop_template, prop_name, flags);
75 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
76}
77
78static std::unique_ptr<IDProperty, IDPropertyDeleter> array_create(const StringRef prop_name,
79 eIDPropertyType subtype,
80 size_t array_len,
81 const eIDPropertyFlag flags)
82{
83 IDPropertyTemplate prop_template{0};
84 prop_template.array.len = array_len;
85 prop_template.array.type = subtype;
86 IDProperty *property = IDP_New(IDP_ARRAY, &prop_template, prop_name, flags);
87 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
88}
89
90static void array_values_set(IDProperty *property,
91 const void *values,
92 size_t values_len,
93 size_t value_size)
94{
95 BLI_assert(values);
96 BLI_assert(property->len == values_len);
97 memcpy(IDP_array_voidp_get(property), values, values_len * value_size);
98}
99
103template<
105 typename PrimitiveType,
107 eIDPropertyType id_property_subtype>
108std::unique_ptr<IDProperty, IDPropertyDeleter> create_array(StringRef prop_name,
109 Span<PrimitiveType> values,
110 const eIDPropertyFlag flags)
111{
113 "Allowed values for PrimitiveType are int32_t, float and double.");
114 static_assert(!std::is_same_v<PrimitiveType, int32_t> || id_property_subtype == IDP_INT,
115 "PrimitiveType and id_property_type do not match (int32_t).");
116 static_assert(!std::is_same_v<PrimitiveType, float> || id_property_subtype == IDP_FLOAT,
117 "PrimitiveType and id_property_type do not match (float).");
118 static_assert(!std::is_same_v<PrimitiveType, double> || id_property_subtype == IDP_DOUBLE,
119 "PrimitiveType and id_property_type do not match (double).");
120
121 const int64_t values_len = values.size();
122 BLI_assert(values_len > 0);
123 std::unique_ptr<IDProperty, IDPropertyDeleter> property = array_create(
124 prop_name, id_property_subtype, values_len, flags);
126 property.get(), static_cast<const void *>(values.data()), values_len, sizeof(PrimitiveType));
127 return property;
128}
129
130std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRef prop_name,
131 Span<int32_t> values,
132 const eIDPropertyFlag flags)
133{
134 return create_array<int32_t, IDP_INT>(prop_name, values, flags);
135}
136
137std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRef prop_name,
138 Span<float> values,
139 const eIDPropertyFlag flags)
140{
141 return create_array<float, IDP_FLOAT>(prop_name, values, flags);
142}
143
144std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRef prop_name,
145 Span<double> values,
146 const eIDPropertyFlag flags)
147{
148 return create_array<double, IDP_DOUBLE>(prop_name, values, flags);
149}
150
151std::unique_ptr<IDProperty, IDPropertyDeleter> create_group(const StringRef prop_name,
152 const eIDPropertyFlag flags)
153{
154 IDPropertyTemplate prop_template{0};
155 IDProperty *property = IDP_New(IDP_GROUP, &prop_template, prop_name, flags);
156 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
157}
158
160
161} // namespace blender::bke::idprop
IDProperty * IDP_NewString(const char *st, blender::StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:399
#define IDP_array_voidp_get(prop)
IDProperty * IDP_New(char type, const IDPropertyTemplate *val, blender::StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:1009
#define BLI_assert(a)
Definition BLI_assert.h:46
ID and Library types, which are fundamental for SDNA.
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:774
eIDPropertyType
@ IDP_DOUBLE
@ IDP_FLOAT
@ IDP_BOOLEAN
@ IDP_INT
@ IDP_GROUP
@ IDP_ARRAY
@ IDP_ID
eIDPropertyFlag
long long int int64_t
constexpr const T * data() const
Definition BLI_span.hh:215
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr const char * c_str() const
PrimitiveType
std::unique_ptr< IDProperty, IDPropertyDeleter > create_array(StringRef prop_name, Span< PrimitiveType > values, const eIDPropertyFlag flags)
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.
static void array_values_set(IDProperty *property, const void *values, size_t values_len, size_t value_size)
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.
static std::unique_ptr< IDProperty, IDPropertyDeleter > array_create(const StringRef prop_name, eIDPropertyType subtype, size_t array_len, const eIDPropertyFlag flags)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
constexpr bool is_same_any_v
int len
Definition DNA_ID.h:175
Definition DNA_ID.h:414
short flag
Definition DNA_ID.h:438
struct IDPropertyTemplate::@032057005265002020267344110225167212360002125060 array