diff options
author | Jacques Lucke <jacques@blender.org> | 2021-03-23 13:46:00 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-03-23 13:47:09 +0300 |
commit | eed45b655c9ff27718572da7359749ee615c5191 (patch) | |
tree | d176c01b06597de42a4667bc8429c5231cd8940d /source/blender/makesrna/intern/makesrna.c | |
parent | 4e0fd7fff11b76e52a5f11ba8704028c9b3c3ab0 (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/intern/makesrna.c')
-rw-r--r-- | source/blender/makesrna/intern/makesrna.c | 82 |
1 files changed, 51 insertions, 31 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"); } |