Blender  V2.93
rna_test.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 /* Defines a structure with properties used for array manipulation tests in BPY. */
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "RNA_define.h"
27 
28 #include "rna_internal.h"
29 
30 #ifdef RNA_RUNTIME
31 
32 # ifdef ARRAY_SIZE
33 # undef ARRAY_SIZE
34 # endif
35 
36 # define ARRAY_SIZE 3
37 # define DYNAMIC_ARRAY_SIZE 64
38 # define MARRAY_DIM [3][4][5]
39 # define MARRAY_TOTDIM 3
40 # define MARRAY_DIMSIZE 4, 5
41 # define MARRAY_SIZE(type) (sizeof(type MARRAY_DIM) / sizeof(type))
42 # define DYNAMIC_MARRAY_DIM [3][4][5]
43 # define DYNAMIC_MARRAY_SIZE(type) (sizeof(type DYNAMIC_MARRAY_DIM) / sizeof(type))
44 
45 # ifdef UNIT_TEST
46 
47 # define DEF_VARS(type, prefix) \
48  static type prefix##arr[ARRAY_SIZE]; \
49  static type prefix##darr[DYNAMIC_ARRAY_SIZE]; \
50  static int prefix##darr_len = ARRAY_SIZE; \
51  static type prefix##marr MARRAY_DIM; \
52  static type prefix##dmarr DYNAMIC_MARRAY_DIM; \
53  static int prefix##dmarr_len = sizeof(prefix##dmarr); \
54  (void)0
55 
56 # define DEF_GET_SET(type, arr) \
57  void rna_Test_##arr##_get(PointerRNA *ptr, type *values) \
58  { \
59  memcpy(values, arr, sizeof(arr)); \
60  } \
61 \
62  void rna_Test_##arr##_set(PointerRNA *ptr, const type *values) \
63  { \
64  memcpy(arr, values, sizeof(arr)); \
65  } \
66  ((void)0)
67 
68 # define DEF_GET_SET_LEN(arr, max) \
69  static int rna_Test_##arr##_get_length(PointerRNA *ptr) \
70  { \
71  return arr##_len; \
72  } \
73 \
74  static int rna_Test_##arr##_set_length(PointerRNA *ptr, int length) \
75  { \
76  if (length > max) { \
77  return 0; \
78  } \
79  arr##_len = length; \
80 \
81  return 1; \
82  } \
83  ((void)0)
84 
85 DEF_VARS(float, f);
86 DEF_VARS(int, i);
87 DEF_VARS(int, b);
88 
89 DEF_GET_SET(float, farr);
90 DEF_GET_SET(int, iarr);
91 DEF_GET_SET(int, barr);
92 
93 DEF_GET_SET(float, fmarr);
94 DEF_GET_SET(int, imarr);
95 DEF_GET_SET(int, bmarr);
96 
97 DEF_GET_SET(float, fdarr);
98 DEF_GET_SET_LEN(fdarr, DYNAMIC_ARRAY_SIZE);
99 DEF_GET_SET(int, idarr);
100 DEF_GET_SET_LEN(idarr, DYNAMIC_ARRAY_SIZE);
101 DEF_GET_SET(int, bdarr);
102 DEF_GET_SET_LEN(bdarr, DYNAMIC_ARRAY_SIZE);
103 
104 DEF_GET_SET(float, fdmarr);
105 DEF_GET_SET_LEN(fdmarr, DYNAMIC_MARRAY_SIZE(float));
106 DEF_GET_SET(int, idmarr);
107 DEF_GET_SET_LEN(idmarr, DYNAMIC_MARRAY_SIZE(int));
108 DEF_GET_SET(int, bdmarr);
109 DEF_GET_SET_LEN(bdmarr, DYNAMIC_MARRAY_SIZE(int));
110 
111 # endif
112 
113 #else
114 
116 {
117 # ifdef UNIT_TEST
118  StructRNA *srna;
119  PropertyRNA *prop;
120  unsigned short dimsize[] = {MARRAY_DIMSIZE};
121 
122  srna = RNA_def_struct(brna, "Test", NULL);
123  RNA_def_struct_sdna(srna, "Test");
124 
125  prop = RNA_def_float_array(
126  srna, "farr", ARRAY_SIZE, NULL, 0.0f, 0.0f, "farr", "float array", 0.0f, 0.0f);
127  RNA_def_property_float_funcs(prop, "rna_Test_farr_get", "rna_Test_farr_set", NULL);
128 
129  prop = RNA_def_int_array(srna, "iarr", ARRAY_SIZE, NULL, 0, 0, "iarr", "int array", 0, 0);
130  RNA_def_property_int_funcs(prop, "rna_Test_iarr_get", "rna_Test_iarr_set", NULL);
131 
132  prop = RNA_def_boolean_array(srna, "barr", ARRAY_SIZE, NULL, "barr", "boolean array");
133  RNA_def_property_boolean_funcs(prop, "rna_Test_barr_get", "rna_Test_barr_set");
134 
135  /* dynamic arrays */
136 
137  prop = RNA_def_float_array(srna,
138  "fdarr",
139  DYNAMIC_ARRAY_SIZE,
140  NULL,
141  0.0f,
142  0.0f,
143  "fdarr",
144  "dynamic float array",
145  0.0f,
146  0.0f);
149  prop, "rna_Test_fdarr_get_length", "rna_Test_fdarr_set_length");
150  RNA_def_property_float_funcs(prop, "rna_Test_fdarr_get", "rna_Test_fdarr_set", NULL);
151 
152  prop = RNA_def_int_array(
153  srna, "idarr", DYNAMIC_ARRAY_SIZE, NULL, 0, 0, "idarr", "int array", 0, 0);
156  prop, "rna_Test_idarr_get_length", "rna_Test_idarr_set_length");
157  RNA_def_property_int_funcs(prop, "rna_Test_idarr_get", "rna_Test_idarr_set", NULL);
158 
159  prop = RNA_def_boolean_array(srna, "bdarr", DYNAMIC_ARRAY_SIZE, NULL, "bdarr", "boolean array");
162  prop, "rna_Test_bdarr_get_length", "rna_Test_bdarr_set_length");
163  RNA_def_property_boolean_funcs(prop, "rna_Test_bdarr_get", "rna_Test_bdarr_set");
164 
165  /* multidimensional arrays */
166 
167  prop = RNA_def_property(srna, "fmarr", PROP_FLOAT, PROP_NONE);
168  RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(float), MARRAY_TOTDIM, dimsize);
169  RNA_def_property_float_funcs(prop, "rna_Test_fmarr_get", "rna_Test_fmarr_set", NULL);
170 
171  prop = RNA_def_property(srna, "imarr", PROP_INT, PROP_NONE);
172  RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
173  RNA_def_property_int_funcs(prop, "rna_Test_imarr_get", "rna_Test_imarr_set", NULL);
174 
175  prop = RNA_def_property(srna, "bmarr", PROP_BOOLEAN, PROP_NONE);
176  RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
177  RNA_def_property_boolean_funcs(prop, "rna_Test_bmarr_get", "rna_Test_bmarr_set");
178 
179  /* dynamic multidimensional arrays */
180 
181  prop = RNA_def_property(srna, "fdmarr", PROP_FLOAT, PROP_NONE);
182  RNA_def_property_multidimensional_array(
183  prop, DYNAMIC_MARRAY_SIZE(float), MARRAY_TOTDIM, dimsize);
186  prop, "rna_Test_fdmarr_get_length", "rna_Test_fdmarr_set_length");
187  RNA_def_property_float_funcs(prop, "rna_Test_fdmarr_get", "rna_Test_fdmarr_set", NULL);
188 
189  prop = RNA_def_property(srna, "idmarr", PROP_INT, PROP_NONE);
190  RNA_def_property_multidimensional_array(prop, DYNAMIC_MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
193  prop, "rna_Test_idmarr_get_length", "rna_Test_idmarr_set_length");
194  RNA_def_property_int_funcs(prop, "rna_Test_idmarr_get", "rna_Test_idmarr_set", NULL);
195 
196  prop = RNA_def_property(srna, "bdmarr", PROP_BOOLEAN, PROP_NONE);
197  RNA_def_property_multidimensional_array(prop, DYNAMIC_MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
200  prop, "rna_Test_bdmarr_get_length", "rna_Test_bdmarr_set_length");
201  RNA_def_property_boolean_funcs(prop, "rna_Test_bdmarr_get", "rna_Test_bdmarr_set");
202 # else
203  (void)brna;
204 # endif
205 }
206 
207 #endif /* RNA_RUNTIME */
#define ARRAY_SIZE(arr)
@ PROP_FLOAT
Definition: RNA_types.h:75
@ PROP_BOOLEAN
Definition: RNA_types.h:73
@ PROP_INT
Definition: RNA_types.h:74
@ PROP_DYNAMIC
Definition: RNA_types.h:275
@ PROP_NONE
Definition: RNA_types.h:113
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, int len, const int *default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3643
PropertyRNA * RNA_def_float_array(StructOrFunctionRNA *cont_, const char *identifier, int len, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:4065
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3153
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
Definition: rna_define.c:2971
void RNA_def_property_dynamic_array_funcs(PropertyRNA *prop, const char *getlength)
Definition: rna_define.c:2953
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
Definition: rna_define.c:1067
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1279
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
Definition: rna_define.c:1047
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3055
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1512
PropertyRNA * RNA_def_boolean_array(StructOrFunctionRNA *cont_, const char *identifier, int len, bool *default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3497
void RNA_def_test(BlenderRNA *brna)
Definition: rna_test.c:115