Blender V4.3
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/* -------------------------------------------------------------------- */
17std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull 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.c_str(), flags);
24 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
25}
26
27std::unique_ptr<IDProperty, IDPropertyDeleter> create_bool(const StringRefNull 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.c_str(), flags);
34 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
35}
36
37std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull 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.c_str(), flags);
44 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
45}
46
47std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull 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.c_str(), flags);
54 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
55}
56
57std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name,
58 const StringRefNull value,
59 const eIDPropertyFlag flags)
60{
61 IDProperty *property = IDP_NewString(value.c_str(), prop_name.c_str(), flags);
62 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
63}
64
65std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull 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.c_str(), flags);
75 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
76}
77
78static std::unique_ptr<IDProperty, IDPropertyDeleter> array_create(const StringRefNull 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.c_str(), 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(property), values, values_len * value_size);
98}
99
103template<
105 typename PrimitiveType,
107 eIDPropertyType id_property_subtype>
108std::unique_ptr<IDProperty, IDPropertyDeleter> create_array(StringRefNull prop_name,
109 Span<PrimitiveType> values,
110 const eIDPropertyFlag flags)
111{
112 static_assert(std::is_same_v<PrimitiveType, int32_t> || std::is_same_v<PrimitiveType, float> ||
113 std::is_same_v<PrimitiveType, double>,
114 "Allowed values for PrimitiveType are int32_t, float and double.");
115 static_assert(!std::is_same_v<PrimitiveType, int32_t> || id_property_subtype == IDP_INT,
116 "PrimitiveType and id_property_type do not match (int32_t).");
117 static_assert(!std::is_same_v<PrimitiveType, float> || id_property_subtype == IDP_FLOAT,
118 "PrimitiveType and id_property_type do not match (float).");
119 static_assert(!std::is_same_v<PrimitiveType, double> || id_property_subtype == IDP_DOUBLE,
120 "PrimitiveType and id_property_type do not match (double).");
121
122 const int64_t values_len = values.size();
123 BLI_assert(values_len > 0);
124 std::unique_ptr<IDProperty, IDPropertyDeleter> property = array_create(
125 prop_name.c_str(), id_property_subtype, values_len, flags);
127 property.get(), static_cast<const void *>(values.data()), values_len, sizeof(PrimitiveType));
128 return property;
129}
130
131std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name,
132 Span<int32_t> values,
133 const eIDPropertyFlag flags)
134{
135 return create_array<int32_t, IDP_INT>(prop_name, values, flags);
136}
137
138std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name,
139 Span<float> values,
140 const eIDPropertyFlag flags)
141{
142 return create_array<float, IDP_FLOAT>(prop_name, values, flags);
143}
144
145std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name,
146 Span<double> values,
147 const eIDPropertyFlag flags)
148{
149 return create_array<double, IDP_DOUBLE>(prop_name, values, flags);
150}
151
152std::unique_ptr<IDProperty, IDPropertyDeleter> create_group(const StringRefNull prop_name,
153 const eIDPropertyFlag flags)
154{
155 IDPropertyTemplate prop_template{0};
156 IDProperty *property = IDP_New(IDP_GROUP, &prop_template, prop_name.c_str(), flags);
157 return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
158}
159
160/* \} */
161
162} // namespace blender::bke::idprop
IDProperty * IDP_New(char type, const IDPropertyTemplate *val, const char *name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:989
IDProperty * IDP_NewString(const char *st, const char *name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition idprop.cc:393
#define IDP_Array(prop)
#define BLI_assert(a)
Definition BLI_assert.h:50
ID and Library types, which are fundamental for SDNA.
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:725
eIDPropertyType
@ IDP_DOUBLE
@ IDP_FLOAT
@ IDP_BOOLEAN
@ IDP_INT
@ IDP_GROUP
@ IDP_ARRAY
@ IDP_ID
eIDPropertyFlag
constexpr const char * c_str() const
PrimitiveType
std::unique_ptr< IDProperty, IDPropertyDeleter > create_array(StringRefNull prop_name, Span< PrimitiveType > values, const eIDPropertyFlag flags)
static void array_values_set(IDProperty *property, const void *values, size_t values_len, size_t value_size)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_bool(StringRefNull prop_name, bool value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_BOOLEAN, set its name and value.
static std::unique_ptr< IDProperty, IDPropertyDeleter > array_create(const StringRefNull prop_name, eIDPropertyType subtype, size_t array_len, const eIDPropertyFlag flags)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRefNull 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(StringRefNull prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
__int64 int64_t
Definition stdint.h:89
signed int int32_t
Definition stdint.h:77
int len
Definition DNA_ID.h:174
Definition DNA_ID.h:413
struct IDPropertyTemplate::@30 array