Revision: 19367
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19367
Author:   ben2610
Date:     2009-03-22 22:36:48 +0100 (Sun, 22 Mar 2009)

Log Message:
-----------
BGE API cleanup: add support for attribute set/get through functions only.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp
    trunk/blender/source/gameengine/Expressions/PyObjectPlus.h

Modified: trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp        
2009-03-22 21:06:08 UTC (rev 19366)
+++ trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp        
2009-03-22 21:36:48 UTC (rev 19367)
@@ -143,6 +143,13 @@
                                // fake attribute, ignore
                                return NULL;
                        }
+                       if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_FUNCTION)
+                       {
+                               // the attribute has no field correspondance, 
handover processing to function.
+                               if (attrdef->m_getFunction == NULL)
+                                       return NULL;
+                               return (*attrdef->m_getFunction)(self, attrdef);
+                       }
                        char *ptr = 
reinterpret_cast<char*>(self)+attrdef->m_offset;
                        if (attrdef->m_length > 1)
                        {
@@ -270,6 +277,13 @@
                                }
                                switch (attrdef->m_type) 
                                {
+                               case KX_PYATTRIBUTE_TYPE_FUNCTION:
+                                       if (attrdef->m_setFunction == NULL) 
+                                       {
+                                               
PyErr_SetString(PyExc_AttributeError, "function attribute without function, 
report to blender.org");
+                                               return 1;
+                                       }
+                                       return (*attrdef->m_setFunction)(self, 
attrdef, value);
                                case KX_PYATTRIBUTE_TYPE_BOOL:
                                        bufferSize = sizeof(bool);
                                        break;
@@ -419,9 +433,9 @@
                                        }
                                }
                                // no error, call check function if any
-                               if (attrdef->m_function != NULL)
+                               if (attrdef->m_checkFunction != NULL)
                                {
-                                       if ((*attrdef->m_function)(self, 
attrdef) != 0)
+                                       if ((*attrdef->m_checkFunction)(self, 
attrdef) != 0)
                                        {
                                                // post check returned an 
error, restore values
                                        UNDO_AND_ERROR:
@@ -439,9 +453,17 @@
                        }
                        else    // simple attribute value
                        {
-
-                               if (attrdef->m_function != NULL)
+                               if (attrdef->m_type == 
KX_PYATTRIBUTE_TYPE_FUNCTION)
                                {
+                                       if (attrdef->m_setFunction == NULL)
+                                       {
+                                               
PyErr_SetString(PyExc_AttributeError, "function attribute without function, 
report to blender.org");
+                                               return 1;
+                                       }
+                                       return (*attrdef->m_setFunction)(self, 
attrdef, value);
+                               }
+                               if (attrdef->m_checkFunction != NULL)
+                               {
                                        // post check function is provided, 
prepare undo buffer
                                        sourceBuffer = ptr;
                                        switch (attrdef->m_type) 
@@ -628,9 +650,9 @@
                                }
                        }
                        // check if post processing is needed
-                       if (attrdef->m_function != NULL)
+                       if (attrdef->m_checkFunction != NULL)
                        {
-                               if ((*attrdef->m_function)(self, attrdef) != 0)
+                               if ((*attrdef->m_checkFunction)(self, attrdef) 
!= 0)
                                {
                                        // restore value
                                RESTORE_AND_ERROR:

Modified: trunk/blender/source/gameengine/Expressions/PyObjectPlus.h
===================================================================
--- trunk/blender/source/gameengine/Expressions/PyObjectPlus.h  2009-03-22 
21:06:08 UTC (rev 19366)
+++ trunk/blender/source/gameengine/Expressions/PyObjectPlus.h  2009-03-22 
21:36:48 UTC (rev 19367)
@@ -206,6 +206,7 @@
        KX_PYATTRIBUTE_TYPE_FLOAT,
        KX_PYATTRIBUTE_TYPE_STRING,
        KX_PYATTRIBUTE_TYPE_DUMMY,
+       KX_PYATTRIBUTE_TYPE_FUNCTION,
 };
 
 enum KX_PYATTRIBUTE_ACCESS {
@@ -214,7 +215,9 @@
 };
 
 struct KX_PYATTRIBUTE_DEF;
-typedef int (*KX_PYATTRIBUTE_FUNCTION)(void *self, const struct 
KX_PYATTRIBUTE_DEF *attrdef);
+typedef int (*KX_PYATTRIBUTE_CHECK_FUNCTION)(void *self, const struct 
KX_PYATTRIBUTE_DEF *attrdef);
+typedef int (*KX_PYATTRIBUTE_SET_FUNCTION)(void *self, const struct 
KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+typedef PyObject* (*KX_PYATTRIBUTE_GET_FUNCTION)(void *self, const struct 
KX_PYATTRIBUTE_DEF *attrdef);
 
 typedef struct KX_PYATTRIBUTE_DEF {
        const char *m_name;                             // name of the python 
attribute
@@ -228,7 +231,10 @@
        size_t m_offset;                                // position of field in 
structure
        size_t m_size;                                  // size of field for 
runtime verification (enum only)
        size_t m_length;                                // length of array, 
1=simple attribute
-       KX_PYATTRIBUTE_FUNCTION m_function;     // static function to check the 
assignment, returns 0 if no error
+       KX_PYATTRIBUTE_CHECK_FUNCTION m_checkFunction;  // static function to 
check the assignment, returns 0 if no error
+       KX_PYATTRIBUTE_SET_FUNCTION m_setFunction;      // static function to 
check the assignment, returns 0 if no error
+       KX_PYATTRIBUTE_GET_FUNCTION m_getFunction;      // static function to 
check the assignment, returns 0 if no error
+
        // The following pointers are just used to have compile time check for 
attribute type.
        // It would have been good to use a union but that would require C99 
compatibility
        // to initialize specific union fields through designated initializers.
@@ -242,89 +248,95 @@
 } PyAttributeDef;
 
 #define KX_PYATTRIBUTE_DUMMY(name) \
-       { name, KX_PYATTRIBUTE_TYPE_DUMMY, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, 
false, 0, 0, 1, NULL, {NULL, NULL, NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_DUMMY, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, 
false, 0, 0, 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL} }
 
 #define KX_PYATTRIBUTE_BOOL_RW(name,object,field) \
-       { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RW, 0, 1, 0.f, 0.f, 
false, offsetof(object,field), 0, 1, NULL, {&((object *)0)->field, NULL, NULL, 
NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RW, 0, 1, 0.f, 0.f, 
false, offsetof(object,field), 0, 1, NULL, NULL, NULL, {&((object *)0)->field, 
NULL, NULL, NULL, NULL} }
 #define KX_PYATTRIBUTE_BOOL_RW_CHECK(name,object,field,function) \
-       { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RW, 0, 1, 0.f, 0.f, 
false, offsetof(object,field), 0, 1, &object::function, {&((object *)0)->field, 
NULL, NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RW, 0, 1, 0.f, 0.f, 
false, offsetof(object,field), 0, 1, &object::function, NULL, NULL, {&((object 
*)0)->field, NULL, NULL, NULL, NULL} }
 #define KX_PYATTRIBUTE_BOOL_RO(name,object,field) \
-       { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RO, 0, 1, 0.f, 0.f, 
false, offsetof(object,field), 0, 1, NULL, {&((object *)0)->field, NULL, NULL, 
NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RO, 0, 1, 0.f, 0.f, 
false, offsetof(object,field), 0, 1, NULL, NULL, NULL, {&((object *)0)->field, 
NULL, NULL, NULL, NULL} }
 
 // enum field cannot be mapped to pointer (because we would need a pointer for 
each enum)
 // use field size to verify mapping at runtime only, assuming enum size is 
equal to int size.
 #define KX_PYATTRIBUTE_ENUM_RW(name,min,max,clamp,object,field) \
-       { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), sizeof(((object *)0)->field), 1, NULL, 
{NULL, NULL, NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), sizeof(((object *)0)->field), 1, NULL, 
NULL, NULL, {NULL, NULL, NULL, NULL, NULL} }
 #define KX_PYATTRIBUTE_ENUM_RW_CHECK(name,min,max,clamp,object,field,function) 
\
-       { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), sizeof(((object *)0)->field), 1, 
&object::function, {NULL, NULL, NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), sizeof(((object *)0)->field), 1, 
&object::function, NULL, NULL, {NULL, NULL, NULL, NULL, NULL} }
 #define KX_PYATTRIBUTE_ENUM_RO(name,object,field) \
-       { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, 
false, offsetof(object,field), sizeof(((object *)0)->field), 1, NULL, {NULL, 
NULL, NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, 
false, offsetof(object,field), sizeof(((object *)0)->field), 1, NULL, NULL, 
NULL, {NULL, NULL, NULL, NULL, NULL} }
 
 #define KX_PYATTRIBUTE_SHORT_RW(name,min,max,clamp,object,field) \
-       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), 0, 1, NULL, {NULL, &((object *)0)->field, 
NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), 0, 1, NULL, NULL, NULL, {NULL, &((object 
*)0)->field, NULL, NULL, NULL} }
 #define 
KX_PYATTRIBUTE_SHORT_RW_CHECK(name,min,max,clamp,object,field,function) \
-       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), 0, 1, &object::function, {NULL, &((object 
*)0)->field, NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), 0, 1, &object::function, NULL, NULL, {NULL, 
&((object *)0)->field, NULL, NULL, NULL} }
 #define KX_PYATTRIBUTE_SHORT_RO(name,object,field) \
-       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, 
false, offsetof(object,field), 0, 1, NULL, {NULL, &((object *)0)->field, NULL, 
NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, 
false, offsetof(object,field), 0, 1, NULL, NULL, NULL, {NULL, &((object 
*)0)->field, NULL, NULL, NULL} }
 #define KX_PYATTRIBUTE_SHORT_ARRAY_RW(name,min,max,clamp,object,field,length) \
-       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), 0, length, NULL, {NULL, ((object 
*)0)->field, NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), 0, length, NULL, NULL, NULL, {NULL, 
((object *)0)->field, NULL, NULL, NULL} }
 #define 
KX_PYATTRIBUTE_SHORT_ARRAY_RW_CHECK(name,min,max,clamp,object,field,length,function)
 \
-       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), 0, length, &object::function, {NULL, 
((object *)0)->field, NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 
0.f, clamp, offsetof(object,field), 0, length, &object::function, NULL, NULL, 
{NULL, ((object *)0)->field, NULL, NULL, NULL} }
 #define KX_PYATTRIBUTE_SHORT_ARRAY_RO(name,object,field,length) \
-       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, 
false, offsetof(object,field), 0, length, NULL, {NULL, ((object *)0)->field, 
NULL, NULL, NULL} }
+       { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, 
false, offsetof(object,field), 0, length, NULL, NULL, NULL, {NULL, ((object 
*)0)->field, NULL, NULL, NULL} }
 // SHORT_LIST
 #define KX_PYATTRIBUTE_SHORT_LIST_RW(name,min,max,clamp,object,field,length) \

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to