Blender V4.3
rna_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9/* Defines a structure with properties used for array manipulation tests in BPY. */
10
11#include <cstdlib>
12#include <cstring>
13
14#include "RNA_define.hh"
15
16#include "rna_internal.hh"
17
18#ifdef RNA_RUNTIME
19
20# ifdef ARRAY_SIZE
21# undef ARRAY_SIZE
22# endif
23
24# define ARRAY_SIZE 3
25# define DYNAMIC_ARRAY_SIZE 64
26# define MARRAY_DIM [3][4][5]
27# define MARRAY_TOTDIM 3
28# define MARRAY_DIMSIZE 4, 5
29# define MARRAY_SIZE(type) (sizeof(type MARRAY_DIM) / sizeof(type))
30# define DYNAMIC_MARRAY_DIM [3][4][5]
31# define DYNAMIC_MARRAY_SIZE(type) (sizeof(type DYNAMIC_MARRAY_DIM) / sizeof(type))
32
33# ifdef UNIT_TEST
34
35# define DEF_VARS(type, prefix) \
36 static type prefix##arr[ARRAY_SIZE]; \
37 static type prefix##darr[DYNAMIC_ARRAY_SIZE]; \
38 static int prefix##darr_len = ARRAY_SIZE; \
39 static type prefix##marr MARRAY_DIM; \
40 static type prefix##dmarr DYNAMIC_MARRAY_DIM; \
41 static int prefix##dmarr_len = sizeof(prefix##dmarr); \
42 (void)0
43
44# define DEF_GET_SET(type, arr) \
45 void rna_Test_##arr##_get(PointerRNA *ptr, type *values) \
46 { \
47 memcpy(values, arr, sizeof(arr)); \
48 } \
49\
50 void rna_Test_##arr##_set(PointerRNA *ptr, const type *values) \
51 { \
52 memcpy(arr, values, sizeof(arr)); \
53 } \
54 ((void)0)
55
56# define DEF_GET_SET_LEN(arr, max) \
57 static int rna_Test_##arr##_get_length(PointerRNA *ptr) \
58 { \
59 return arr##_len; \
60 } \
61\
62 static int rna_Test_##arr##_set_length(PointerRNA *ptr, int length) \
63 { \
64 if (length > max) { \
65 return 0; \
66 } \
67 arr##_len = length; \
68\
69 return 1; \
70 } \
71 ((void)0)
72
73DEF_VARS(float, f);
74DEF_VARS(int, i);
75DEF_VARS(int, b);
76
77DEF_GET_SET(float, farr);
78DEF_GET_SET(int, iarr);
79DEF_GET_SET(int, barr);
80
81DEF_GET_SET(float, fmarr);
82DEF_GET_SET(int, imarr);
83DEF_GET_SET(int, bmarr);
84
85DEF_GET_SET(float, fdarr);
86DEF_GET_SET_LEN(fdarr, DYNAMIC_ARRAY_SIZE);
87DEF_GET_SET(int, idarr);
88DEF_GET_SET_LEN(idarr, DYNAMIC_ARRAY_SIZE);
89DEF_GET_SET(int, bdarr);
90DEF_GET_SET_LEN(bdarr, DYNAMIC_ARRAY_SIZE);
91
92DEF_GET_SET(float, fdmarr);
93DEF_GET_SET_LEN(fdmarr, DYNAMIC_MARRAY_SIZE(float));
94DEF_GET_SET(int, idmarr);
95DEF_GET_SET_LEN(idmarr, DYNAMIC_MARRAY_SIZE(int));
96DEF_GET_SET(int, bdmarr);
97DEF_GET_SET_LEN(bdmarr, DYNAMIC_MARRAY_SIZE(int));
98
99# endif
100
101#else
102
104{
105# ifdef UNIT_TEST
106 StructRNA *srna;
107 PropertyRNA *prop;
108 ushort dimsize[] = {MARRAY_DIMSIZE};
109
110 srna = RNA_def_struct(brna, "Test", nullptr);
111 RNA_def_struct_sdna(srna, "Test");
112
113 prop = RNA_def_float_array(
114 srna, "farr", ARRAY_SIZE, nullptr, 0.0f, 0.0f, "farr", "float array", 0.0f, 0.0f);
115 RNA_def_property_float_funcs(prop, "rna_Test_farr_get", "rna_Test_farr_set", nullptr);
116
117 prop = RNA_def_int_array(srna, "iarr", ARRAY_SIZE, nullptr, 0, 0, "iarr", "int array", 0, 0);
118 RNA_def_property_int_funcs(prop, "rna_Test_iarr_get", "rna_Test_iarr_set", nullptr);
119
120 prop = RNA_def_boolean_array(srna, "barr", ARRAY_SIZE, nullptr, "barr", "boolean array");
121 RNA_def_property_boolean_funcs(prop, "rna_Test_barr_get", "rna_Test_barr_set");
122
123 /* dynamic arrays */
124
125 prop = RNA_def_float_array(srna,
126 "fdarr",
127 DYNAMIC_ARRAY_SIZE,
128 nullptr,
129 0.0f,
130 0.0f,
131 "fdarr",
132 "dynamic float array",
133 0.0f,
134 0.0f);
137 prop, "rna_Test_fdarr_get_length", "rna_Test_fdarr_set_length");
138 RNA_def_property_float_funcs(prop, "rna_Test_fdarr_get", "rna_Test_fdarr_set", nullptr);
139
140 prop = RNA_def_int_array(
141 srna, "idarr", DYNAMIC_ARRAY_SIZE, nullptr, 0, 0, "idarr", "int array", 0, 0);
144 prop, "rna_Test_idarr_get_length", "rna_Test_idarr_set_length");
145 RNA_def_property_int_funcs(prop, "rna_Test_idarr_get", "rna_Test_idarr_set", nullptr);
146
148 srna, "bdarr", DYNAMIC_ARRAY_SIZE, nullptr, "bdarr", "boolean array");
151 prop, "rna_Test_bdarr_get_length", "rna_Test_bdarr_set_length");
152 RNA_def_property_boolean_funcs(prop, "rna_Test_bdarr_get", "rna_Test_bdarr_set");
153
154 /* multidimensional arrays */
155
156 prop = RNA_def_property(srna, "fmarr", PROP_FLOAT, PROP_NONE);
157 RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(float), MARRAY_TOTDIM, dimsize);
158 RNA_def_property_float_funcs(prop, "rna_Test_fmarr_get", "rna_Test_fmarr_set", nullptr);
159
160 prop = RNA_def_property(srna, "imarr", PROP_INT, PROP_NONE);
161 RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
162 RNA_def_property_int_funcs(prop, "rna_Test_imarr_get", "rna_Test_imarr_set", nullptr);
163
164 prop = RNA_def_property(srna, "bmarr", PROP_BOOLEAN, PROP_NONE);
165 RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
166 RNA_def_property_boolean_funcs(prop, "rna_Test_bmarr_get", "rna_Test_bmarr_set");
167
168 /* dynamic multidimensional arrays */
169
170 prop = RNA_def_property(srna, "fdmarr", PROP_FLOAT, PROP_NONE);
171 RNA_def_property_multidimensional_array(
172 prop, DYNAMIC_MARRAY_SIZE(float), MARRAY_TOTDIM, dimsize);
175 prop, "rna_Test_fdmarr_get_length", "rna_Test_fdmarr_set_length");
176 RNA_def_property_float_funcs(prop, "rna_Test_fdmarr_get", "rna_Test_fdmarr_set", nullptr);
177
178 prop = RNA_def_property(srna, "idmarr", PROP_INT, PROP_NONE);
179 RNA_def_property_multidimensional_array(prop, DYNAMIC_MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
182 prop, "rna_Test_idmarr_get_length", "rna_Test_idmarr_set_length");
183 RNA_def_property_int_funcs(prop, "rna_Test_idmarr_get", "rna_Test_idmarr_set", nullptr);
184
185 prop = RNA_def_property(srna, "bdmarr", PROP_BOOLEAN, PROP_NONE);
186 RNA_def_property_multidimensional_array(prop, DYNAMIC_MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
189 prop, "rna_Test_bdmarr_get_length", "rna_Test_bdmarr_set_length");
190 RNA_def_property_boolean_funcs(prop, "rna_Test_bdmarr_get", "rna_Test_bdmarr_set");
191# else
192 (void)brna;
193# endif
194}
195
196#endif /* RNA_RUNTIME */
unsigned short ushort
#define ARRAY_SIZE(arr)
@ PROP_FLOAT
Definition RNA_types.hh:67
@ PROP_BOOLEAN
Definition RNA_types.hh:65
@ PROP_INT
Definition RNA_types.hh:66
@ PROP_DYNAMIC
Definition RNA_types.hh:317
@ PROP_NONE
Definition RNA_types.hh:136
local_group_size(16, 16) .push_constant(Type b
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
PropertyRNA * RNA_def_boolean_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const bool *default_value, const char *ui_name, const char *ui_description)
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
void RNA_def_property_dynamic_array_funcs(PropertyRNA *prop, const char *getlength)
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
PropertyRNA * RNA_def_float_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_test(BlenderRNA *brna)
Definition rna_test.cc:103