From 288f772350fae9d12cb712fc26108c24f6f59bd8 Mon Sep 17 00:00:00 2001 From: Elia Sarti Date: Sat, 11 Apr 2009 12:44:01 +0000 Subject: =?UTF-8?q?2.5=20/=20RNA=20Committing=20quick=20RNA=20function=20c?= =?UTF-8?q?alling=20RNA=5Ffunction=5Fcall=5Fdirect*=20functions=20set=20?= =?UTF-8?q?=C3=A0=20la=20fprintf.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It works like this (with ptr being an RNA pointer to some ID): RNA_function_call_direct_lookup(ptr, "rename", "s", "MyCamera"); the format specifier would not be strictly needed but I prefer to keep this as it gives nice error handling in case some RNA function changes. Format strings are very easy and similar to python ones: "b" for booleans "i" for integers "f" for floats "s" for strings "e" for enums (using int values) "O" for pointers (using O as in py, we can change to P) "N" special NULL parameter, valid to skip optional parameters For bools, ints and floats you can use a special format specifier with [n] where n is the size of an array of that type. For instance "f[4]" to set a location/vector (it expects a pointer to float* holding the array). Return values still have to be implemented. Also I know the name is a bit long maybe we can cut it up at RNA_call_direct or simply RNA_call. --- source/blender/makesrna/RNA_access.h | 3 +- source/blender/makesrna/intern/rna_access.c | 189 ++++++++++++++++++++++++++-- 2 files changed, 180 insertions(+), 12 deletions(-) (limited to 'source') diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 2ffc68a0ba1..f6ec657b36f 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -541,11 +541,10 @@ void RNA_parameter_set_lookup(ParameterList *parms, const char *identifier, void int RNA_function_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *parms); int RNA_function_call_lookup(PointerRNA *ptr, const char *identifier, ParameterList *parms); -/* not implemented yet int RNA_function_call_direct(PointerRNA *ptr, FunctionRNA *func, const char *format, ...); int RNA_function_call_direct_lookup(PointerRNA *ptr, const char *identifier, const char *format, ...); int RNA_function_call_direct_va(PointerRNA *ptr, FunctionRNA *func, const char *format, va_list args); -int RNA_function_call_direct_va_lookup(PointerRNA *ptr, const char *identifier, const char *format, va_list args);*/ +int RNA_function_call_direct_va_lookup(PointerRNA *ptr, const char *identifier, const char *format, va_list args); #ifdef __cplusplus } diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 6b4e4fb5921..a2df0a734c6 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -2316,7 +2316,7 @@ int RNA_function_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *parms) return 0; } - return 1; + return -1; } int RNA_function_call_lookup(PointerRNA *ptr, const char *identifier, ParameterList *parms) @@ -2328,10 +2328,9 @@ int RNA_function_call_lookup(PointerRNA *ptr, const char *identifier, ParameterL if(func) return RNA_function_call(ptr, func, parms); - return 0; + return -1; } -#if 0 int RNA_function_call_direct(PointerRNA *ptr, FunctionRNA *func, const char *format, ...) { va_list args; @@ -2365,7 +2364,7 @@ int RNA_function_call_direct_lookup(PointerRNA *ptr, const char *identifier, con return ret; } - return 0; + return -1; } int RNA_function_call_direct_va(PointerRNA *ptr, FunctionRNA *func, const char *format, va_list args) @@ -2374,10 +2373,10 @@ int RNA_function_call_direct_va(PointerRNA *ptr, FunctionRNA *func, const char * ParameterList *parms; ParameterIterator iter; PropertyRNA *pret, *parm; - PropertyType ptype; - PropertySubType psubtype; - int i, tlen, alen, err= 0; + PropertyType type; + int i, ofs, flen, flag, len, alen, err= 0; const char *tid, *fid, *pid; + char ftype, lenbuf[16]; void **retdata; RNA_pointer_create(NULL, &RNA_Function, func, &funcptr); @@ -2385,19 +2384,190 @@ int RNA_function_call_direct_va(PointerRNA *ptr, FunctionRNA *func, const char * tid= RNA_struct_identifier(ptr); fid= RNA_function_identifier(ptr, func); pret= RNA_function_return(ptr, func); + flen= strlen(format); parms= RNA_parameter_list_create(ptr, func); RNA_parameter_list_begin(parms, &iter); - for(i= 0; iter.valid; RNA_parameter_list_next(&iter), i++) { + for(i= 0, ofs= 0; iter.valid; RNA_parameter_list_next(&iter), i++) { parm= iter.parm; + if(parm==pret) { retdata= iter.data; continue; } - /* XXX todo later, this is not really a priority as of now */ + pid= RNA_property_identifier(&funcptr, parm); + flag= RNA_property_flag(&funcptr, parm); + + if (ofs>=flen || format[ofs]=='N') { + if (flag & PROP_REQUIRED) { + err= -1; + fprintf(stderr, "%s.%s: missing required parameter %s\n", tid, fid, pid); + break; + } + ofs++; + continue; + } + + type= RNA_property_type(&funcptr, parm); + len= RNA_property_array_length(&funcptr, parm); + alen= 0; + ftype= format[ofs++]; + + if (len>0) { + int idx= 0; + + if (format[ofs++]=='[') + for (; ofs=flen or idx over lenbuf capacity */ + lenbuf[idx]= '\0'; + alen= atoi(lenbuf); + } + } + + if (len!=alen) { + err= -1; + fprintf(stderr, "%s.%s: for parameter %s, was expecting an array of %i elements, passed %i elements instead\n", tid, fid, pid, len, alen); + break; + } + + switch (type) { + case PROP_BOOLEAN: + { + if (format[ofs]!='b') { + err= -1; + fprintf(stderr, "%s.%s: wrong type for parameter %s, a boolean was expected\n", tid, fid, pid); + break; + } + + if (len==0) + *((int*)iter.data)= va_arg(args, int); + else + memcpy(iter.data, va_arg(args, int*), len); + + break; + } + case PROP_INT: + { + if (format[ofs]!='i') { + err= -1; + fprintf(stderr, "%s.%s: wrong type for parameter %s, an integer was expected\n", tid, fid, pid); + break; + } + + if (len==0) + *((int*)iter.data)= va_arg(args, int); + else + memcpy(iter.data, va_arg(args, int*), len); + + break; + } + case PROP_FLOAT: + { + if (format[ofs]!='f') { + err= -1; + fprintf(stderr, "%s.%s: wrong type for parameter %s, a float was expected\n", tid, fid, pid); + break; + } + + if (len==0) + *((float*)iter.data)= va_arg(args, float); + else + memcpy(iter.data, va_arg(args, float*), len); + + break; + } + case PROP_STRING: + { + if (format[ofs]!='s') { + err= -1; + fprintf(stderr, "%s.%s: wrong type for parameter %s, a string was expected\n", tid, fid, pid); + break; + } + + *((char**)iter.data)= va_arg(args, char*); + + break; + } + case PROP_ENUM: + { + if (format[ofs]!='e') { + err= -1; + fprintf(stderr, "%s.%s: wrong type for parameter %s, an enum was expected\n", tid, fid, pid); + break; + } + + *((int*)iter.data)= va_arg(args, int); + + break; + } + case PROP_POINTER: + { + StructRNA *ptype, *srna; + void *data; + + if (format[ofs]!='O') { + err= -1; + fprintf(stderr, "%s.%s: wrong type for parameter %s, an object was expected\n", tid, fid, pid); + break; + } + + ptype= RNA_property_pointer_type(&funcptr, parm); + /* for objects we use two parms: the first is the type and the second is the data */ + srna= va_arg(args, StructRNA*); + + if(ptype == &RNA_AnyType) { + *((PointerRNA*)iter.data)= va_arg(args, PointerRNA); + } + else if (ptype!=srna) { + PointerRNA pptr; + + data= va_arg(args, void*); + RNA_pointer_create(NULL, srna, data, &pptr); + + if (!RNA_struct_is_a(&pptr, ptype)) { + PointerRNA tmp; + RNA_pointer_create(NULL, ptype, NULL, &tmp); + + err= -1; + fprintf(stderr, "%s.%s: wrong type for parameter %s, an object of type %s was expected, passed an object of type %s\n", tid, fid, pid, RNA_struct_identifier(&tmp), RNA_struct_identifier(&pptr)); + break; + } + + *((void**)iter.data)= data; + } + + break; + } + case PROP_COLLECTION: + { + /* XXX collections are not supported yet */ + err= -1; + fprintf(stderr, "%s.%s: for parameter %s, collections are not supported yet\n", tid, fid, pid); + break; + } + default: + { + err= -1; + if (len==0) + fprintf(stderr, "%s.%s: unknown type for parameter %s\n", tid, fid, pid); + else + fprintf(stderr, "%s.%s: unknown array type for parameter %s\n", tid, fid, pid); + + break; + } + } + } + + if (err==0 && pret) { + /* XXX return values still not implemented */ } + + return err; } int RNA_function_call_direct_va_lookup(PointerRNA *ptr, const char *identifier, const char *format, va_list args) @@ -2411,5 +2581,4 @@ int RNA_function_call_direct_va_lookup(PointerRNA *ptr, const char *identifier, return 0; } -#endif -- cgit v1.2.3