Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2021-03-23 13:46:00 +0300
committerJacques Lucke <jacques@blender.org>2021-03-23 13:47:09 +0300
commiteed45b655c9ff27718572da7359749ee615c5191 (patch)
treed176c01b06597de42a4667bc8429c5231cd8940d /source/blender/makesrna
parent4e0fd7fff11b76e52a5f11ba8704028c9b3c3ab0 (diff)
RNA: support automatically mapping string property to char pointer
The goal of this patch is to remove the boilerplate code required to get a string property that maps to an allocated char pointer in dna. Previously, one to to provide three callbacks, all of which are not necessary anymore for a simple string property. Currently, when an empty string is assigned, the `set` function will always allocate it as well, instead of assigning `NULL` to the pointer. Some structs might support `NULL` while others don't, so this is the safer option for now. Differential Revision: https://developer.blender.org/D10773
Diffstat (limited to 'source/blender/makesrna')
-rw-r--r--source/blender/makesrna/intern/makesrna.c82
-rw-r--r--source/blender/makesrna/intern/rna_nodetree.c36
2 files changed, 51 insertions, 67 deletions
diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c
index 6940e475f39..b20d9218316 100644
--- a/source/blender/makesrna/intern/makesrna.c
+++ b/source/blender/makesrna/intern/makesrna.c
@@ -705,26 +705,34 @@ static char *rna_def_property_get_func(
rna_print_data_get(f, dp);
- if (!(prop->flag & PROP_NEVER_NULL)) {
+ if (dp->dnapointerlevel == 1) {
+ /* Handle allocated char pointer properties. */
fprintf(f, " if (data->%s == NULL) {\n", dp->dnaname);
fprintf(f, " *value = '\\0';\n");
fprintf(f, " return;\n");
fprintf(f, " }\n");
- }
-
- if (sprop->maxlength) {
fprintf(f,
- " %s(value, data->%s, %d);\n",
+ " %s(value, data->%s, strlen(data->%s) + 1);\n",
string_copy_func,
dp->dnaname,
- sprop->maxlength);
+ dp->dnaname);
}
else {
- fprintf(f,
- " %s(value, data->%s, sizeof(data->%s));\n",
- string_copy_func,
- dp->dnaname,
- dp->dnaname);
+ /* Handle char array properties. */
+ if (sprop->maxlength) {
+ fprintf(f,
+ " %s(value, data->%s, %d);\n",
+ string_copy_func,
+ dp->dnaname,
+ sprop->maxlength);
+ }
+ else {
+ fprintf(f,
+ " %s(value, data->%s, sizeof(data->%s));\n",
+ string_copy_func,
+ dp->dnaname,
+ dp->dnaname);
+ }
}
}
fprintf(f, "}\n\n");
@@ -1046,25 +1054,30 @@ static char *rna_def_property_set_func(
rna_print_data_get(f, dp);
- if (!(prop->flag & PROP_NEVER_NULL)) {
- fprintf(f, " if (data->%s == NULL) {\n", dp->dnaname);
- fprintf(f, " return;\n");
- fprintf(f, " }\n");
- }
-
- if (sprop->maxlength) {
- fprintf(f,
- " %s(data->%s, value, %d);\n",
- string_copy_func,
- dp->dnaname,
- sprop->maxlength);
+ if (dp->dnapointerlevel == 1) {
+ /* Handle allocated char pointer properties. */
+ fprintf(
+ f, " if (data->%s != NULL) { MEM_freeN(data->%s); }\n", dp->dnaname, dp->dnaname);
+ fprintf(f, " const int length = strlen(value);\n");
+ fprintf(f, " data->%s = MEM_mallocN(length + 1, __func__);\n", dp->dnaname);
+ fprintf(f, " %s(data->%s, value, length + 1);\n", string_copy_func, dp->dnaname);
}
else {
- fprintf(f,
- " %s(data->%s, value, sizeof(data->%s));\n",
- string_copy_func,
- dp->dnaname,
- dp->dnaname);
+ /* Handle char array properties. */
+ if (sprop->maxlength) {
+ fprintf(f,
+ " %s(data->%s, value, %d);\n",
+ string_copy_func,
+ dp->dnaname,
+ sprop->maxlength);
+ }
+ else {
+ fprintf(f,
+ " %s(data->%s, value, sizeof(data->%s));\n",
+ string_copy_func,
+ dp->dnaname,
+ dp->dnaname);
+ }
}
}
fprintf(f, "}\n\n");
@@ -1285,10 +1298,17 @@ static char *rna_def_property_length_func(
}
else {
rna_print_data_get(f, dp);
- if (!(prop->flag & PROP_NEVER_NULL)) {
- fprintf(f, " if (data->%s == NULL) { return 0; }\n", dp->dnaname);
+ if (dp->dnapointerlevel == 1) {
+ /* Handle allocated char pointer properties. */
+ fprintf(f,
+ " return (data->%s == NULL) ? 0 : strlen(data->%s);\n",
+ dp->dnaname,
+ dp->dnaname);
+ }
+ else {
+ /* Handle char array properties. */
+ fprintf(f, " return strlen(data->%s);\n", dp->dnaname);
}
- fprintf(f, " return strlen(data->%s);\n", dp->dnaname);
}
fprintf(f, "}\n\n");
}
diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c
index 3aba3e51217..d69a1e3dd06 100644
--- a/source/blender/makesrna/intern/rna_nodetree.c
+++ b/source/blender/makesrna/intern/rna_nodetree.c
@@ -4290,38 +4290,6 @@ void rna_ShaderNodePointDensity_density_minmax(bNode *self,
RE_point_density_minmax(depsgraph, pd, r_min, r_max);
}
-static void rna_NodeInputString_string_get(PointerRNA *ptr, char *value)
-{
- bNode *node = (bNode *)ptr->data;
- NodeInputString *storage = node->storage;
-
- strcpy(value, (storage->string) ? storage->string : "");
-}
-
-static int rna_NodeInputString_string_length(PointerRNA *ptr)
-{
- bNode *node = (bNode *)ptr->data;
- NodeInputString *storage = node->storage;
-
- return (storage->string) ? strlen(storage->string) : 0;
-}
-
-static void rna_NodeInputString_string_set(PointerRNA *ptr, const char *value)
-{
- bNode *node = (bNode *)ptr->data;
- NodeInputString *storage = node->storage;
-
- if (storage->string) {
- MEM_freeN(storage->string);
- }
-
- if (value && value[0]) {
- storage->string = BLI_strdup(value);
- }
- else {
- storage->string = NULL;
- }
-}
#else
static const EnumPropertyItem prop_image_layer_items[] = {
@@ -4744,10 +4712,6 @@ static void def_fn_input_string(StructRNA *srna)
RNA_def_struct_sdna_from(srna, "NodeInputString", "storage");
prop = RNA_def_property(srna, "string", PROP_STRING, PROP_NONE);
- RNA_def_property_string_funcs(prop,
- "rna_NodeInputString_string_get",
- "rna_NodeInputString_string_length",
- "rna_NodeInputString_string_set");
RNA_def_property_ui_text(prop, "String", "");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
}