Blender  V3.3
idprop_create.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation. */
3 
4 #include <type_traits>
5 
6 #include "DNA_ID.h"
7 
8 #include "BKE_idprop.hh"
9 
10 namespace blender::bke::idprop {
11 
12 /* -------------------------------------------------------------------- */
16 std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name, int32_t value)
17 {
18  IDPropertyTemplate prop_template{0};
19  prop_template.i = value;
20  IDProperty *property = IDP_New(IDP_INT, &prop_template, prop_name.c_str());
21  return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
22 }
23 
24 std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name, float value)
25 {
26  IDPropertyTemplate prop_template{0};
27  prop_template.f = value;
28  IDProperty *property = IDP_New(IDP_FLOAT, &prop_template, prop_name.c_str());
29  return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
30 }
31 
32 std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name, double value)
33 {
34  IDPropertyTemplate prop_template{0};
35  prop_template.d = value;
36  IDProperty *property = IDP_New(IDP_DOUBLE, &prop_template, prop_name.c_str());
37  return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
38 }
39 
40 std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name,
41  const StringRefNull value)
42 {
43  IDProperty *property = IDP_NewString(value.c_str(), prop_name.c_str(), value.size() + 1);
44  return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
45 }
46 
47 static std::unique_ptr<IDProperty, IDPropertyDeleter> array_create(const StringRefNull prop_name,
48  eIDPropertyType subtype,
49  size_t array_len)
50 {
51  IDPropertyTemplate prop_template{0};
52  prop_template.array.len = array_len;
53  prop_template.array.type = subtype;
54  IDProperty *property = IDP_New(IDP_ARRAY, &prop_template, prop_name.c_str());
55  return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
56 }
57 
58 static void array_values_set(IDProperty *property,
59  const void *values,
60  size_t values_len,
61  size_t value_size)
62 {
63  BLI_assert(values);
64  BLI_assert(property->len == values_len);
65  memcpy(IDP_Array(property), values, values_len * value_size);
66 }
67 
71 template<
73  typename PrimitiveType,
75  eIDPropertyType id_property_subtype>
76 std::unique_ptr<IDProperty, IDPropertyDeleter> create_array(StringRefNull prop_name,
77  Span<PrimitiveType> values)
78 {
79  static_assert(std::is_same_v<PrimitiveType, int32_t> || std::is_same_v<PrimitiveType, float> ||
80  std::is_same_v<PrimitiveType, double>,
81  "Allowed values for PrimitiveType are int32_t, float and double.");
82  static_assert(!std::is_same_v<PrimitiveType, int32_t> || id_property_subtype == IDP_INT,
83  "PrimitiveType and id_property_type do not match (int32_t).");
84  static_assert(!std::is_same_v<PrimitiveType, float> || id_property_subtype == IDP_FLOAT,
85  "PrimitiveType and id_property_type do not match (float).");
86  static_assert(!std::is_same_v<PrimitiveType, double> || id_property_subtype == IDP_DOUBLE,
87  "PrimitiveType and id_property_type do not match (double).");
88 
89  const int64_t values_len = values.size();
90  BLI_assert(values_len > 0);
91  std::unique_ptr<IDProperty, IDPropertyDeleter> property = array_create(
92  prop_name.c_str(), id_property_subtype, values_len);
94  property.get(), static_cast<const void *>(values.data()), values_len, sizeof(PrimitiveType));
95  return property;
96 }
97 
98 std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name,
99  Span<int32_t> values)
100 {
101  return create_array<int32_t, IDP_INT>(prop_name, values);
102 }
103 
104 std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name,
105  Span<float> values)
106 {
107  return create_array<float, IDP_FLOAT>(prop_name, values);
108 }
109 
110 std::unique_ptr<IDProperty, IDPropertyDeleter> create(const StringRefNull prop_name,
111  Span<double> values)
112 {
113  return create_array<double, IDP_DOUBLE>(prop_name, values);
114 }
115 
116 std::unique_ptr<IDProperty, IDPropertyDeleter> create_group(const StringRefNull prop_name)
117 {
118  IDPropertyTemplate prop_template{0};
119  IDProperty *property = IDP_New(IDP_GROUP, &prop_template, prop_name.c_str());
120  return std::unique_ptr<IDProperty, IDPropertyDeleter>(property);
121 }
122 
123 /* \} */
124 
125 } // namespace blender::bke::idprop
struct IDProperty * IDP_New(char type, const IDPropertyTemplate *val, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: idprop.c:887
struct IDProperty * IDP_NewString(const char *st, const char *name, int maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition: idprop.c:339
#define IDP_Array(prop)
Definition: BKE_idprop.h:245
#define BLI_assert(a)
Definition: BLI_assert.h:46
ID and Library types, which are fundamental for sdna.
eIDPropertyType
Definition: DNA_ID.h:135
@ IDP_DOUBLE
Definition: DNA_ID.h:143
@ IDP_FLOAT
Definition: DNA_ID.h:138
@ IDP_INT
Definition: DNA_ID.h:137
@ IDP_GROUP
Definition: DNA_ID.h:141
@ IDP_ARRAY
Definition: DNA_ID.h:140
constexpr const T * data() const
Definition: BLI_span.hh:203
constexpr int64_t size() const
Definition: BLI_span.hh:240
constexpr int64_t size() const
constexpr const char * c_str() const
PrimitiveType
Definition: kernel/types.h:549
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRefNull prop_name)
Allocate a new IDProperty of type IDP_GROUP.
static void array_values_set(IDProperty *property, const void *values, size_t values_len, size_t value_size)
static std::unique_ptr< IDProperty, IDPropertyDeleter > array_create(const StringRefNull prop_name, eIDPropertyType subtype, size_t array_len)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_array(StringRefNull prop_name, Span< PrimitiveType > values)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRefNull prop_name, int32_t value)
Allocate a new IDProperty of type IDP_INT, set its name and value.
__int64 int64_t
Definition: stdint.h:89
signed int int32_t
Definition: stdint.h:77
int len
Definition: DNA_ID.h:121
struct IDPropertyTemplate::@27 array