00001 // Filename: functionWriterPtrFromPython.cxx 00002 // Created by: drose (14Sep01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "functionWriterPtrFromPython.h" 00020 #include "typeManager.h" 00021 #include "interrogateBuilder.h" 00022 #include "interrogate.h" 00023 00024 #include "cppPointerType.h" 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: FunctionWriterPtrFromPython::Constructor 00028 // Access: Public 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 FunctionWriterPtrFromPython:: 00032 FunctionWriterPtrFromPython(CPPType *type) { 00033 _type = TypeManager::unwrap_const(TypeManager::unwrap_pointer(type)); 00034 _name = 00035 "from_python_" + 00036 InterrogateBuilder::clean_identifier(_type->get_local_name(&parser)); 00037 00038 _pointer_type = new CPPPointerType(_type); 00039 } 00040 00041 //////////////////////////////////////////////////////////////////// 00042 // Function: FunctionWriterPtrFromPython::Destructor 00043 // Access: Public 00044 // Description: 00045 //////////////////////////////////////////////////////////////////// 00046 FunctionWriterPtrFromPython:: 00047 ~FunctionWriterPtrFromPython() { 00048 delete _pointer_type; 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: FunctionWriterPtrFromPython::write_prototype 00053 // Access: Public, Virtual 00054 // Description: Outputs the prototype for the function. 00055 //////////////////////////////////////////////////////////////////// 00056 void FunctionWriterPtrFromPython:: 00057 write_prototype(ostream &out) { 00058 CPPType *ppointer = new CPPPointerType(_pointer_type); 00059 00060 out << "static int " << _name << "(PyObject *obj, "; 00061 ppointer->output_instance(out, "addr", &parser); 00062 out << ");\n"; 00063 00064 delete ppointer; 00065 } 00066 00067 //////////////////////////////////////////////////////////////////// 00068 // Function: FunctionWriterPtrFromPython::write_code 00069 // Access: Public, Virtual 00070 // Description: Outputs the code for the function. 00071 //////////////////////////////////////////////////////////////////// 00072 void FunctionWriterPtrFromPython:: 00073 write_code(ostream &out) { 00074 CPPType *ppointer = new CPPPointerType(_pointer_type); 00075 00076 out << "static int\n" 00077 << _name << "(PyObject *obj, "; 00078 ppointer->output_instance(out, "addr", &parser); 00079 out << ") {\n" 00080 << " if (obj != (PyObject *)NULL && PyInstance_Check(obj)) {\n" 00081 // << " PyClassObject *in_class = ((PyInstanceObject *)obj)->in_class;\n" 00082 << " PyObject *in_dict = ((PyInstanceObject *)obj)->in_dict;\n" 00083 << " if (in_dict != (PyObject *)NULL && PyDict_Check(in_dict)) {\n" 00084 << " PyObject *thisobj = PyDict_GetItemString(in_dict, \"this\");\n" 00085 << " if (thisobj != (PyObject *)NULL && PyInt_Check(thisobj)) {\n" 00086 << " (*addr) = (" 00087 << _pointer_type->get_local_name(&parser) << ")PyInt_AsLong(thisobj);\n" 00088 << " return 1;\n" 00089 << " }\n" 00090 << " }\n" 00091 << " }\n" 00092 << " return 0;\n" 00093 << "}\n\n"; 00094 00095 delete ppointer; 00096 } 00097 00098 //////////////////////////////////////////////////////////////////// 00099 // Function: FunctionWriterPtrFromPython::get_type 00100 // Access: Public 00101 // Description: Returns the type that represents the actual data type. 00102 //////////////////////////////////////////////////////////////////// 00103 CPPType *FunctionWriterPtrFromPython:: 00104 get_type() const { 00105 return _type; 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: FunctionWriterPtrFromPython::get_pointer_type 00110 // Access: Public 00111 // Description: Returns the type that represents a pointer to the 00112 // data type. 00113 //////////////////////////////////////////////////////////////////// 00114 CPPType *FunctionWriterPtrFromPython:: 00115 get_pointer_type() const { 00116 return _pointer_type; 00117 }