functionWriterPtrFromPython.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Filename: functionWriterPtrFromPython.cxx
  2. // Created by: drose (14Sep01)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "functionWriterPtrFromPython.h"
  19. #include "typeManager.h"
  20. #include "interrogateBuilder.h"
  21. #include "interrogate.h"
  22. #include "cppPointerType.h"
  23. ////////////////////////////////////////////////////////////////////
  24. // Function: FunctionWriterPtrFromPython::Constructor
  25. // Access: Public
  26. // Description:
  27. ////////////////////////////////////////////////////////////////////
  28. FunctionWriterPtrFromPython::
  29. FunctionWriterPtrFromPython(CPPType *type) {
  30. _type = TypeManager::unwrap_const(TypeManager::unwrap_pointer(type));
  31. _name =
  32. "from_python_" +
  33. InterrogateBuilder::clean_identifier(_type->get_local_name(&parser));
  34. _pointer_type = new CPPPointerType(_type);
  35. }
  36. ////////////////////////////////////////////////////////////////////
  37. // Function: FunctionWriterPtrFromPython::Destructor
  38. // Access: Public
  39. // Description:
  40. ////////////////////////////////////////////////////////////////////
  41. FunctionWriterPtrFromPython::
  42. ~FunctionWriterPtrFromPython() {
  43. delete _pointer_type;
  44. }
  45. ////////////////////////////////////////////////////////////////////
  46. // Function: FunctionWriterPtrFromPython::write_prototype
  47. // Access: Public, Virtual
  48. // Description: Outputs the prototype for the function.
  49. ////////////////////////////////////////////////////////////////////
  50. void FunctionWriterPtrFromPython::
  51. write_prototype(ostream &out) {
  52. CPPType *ppointer = new CPPPointerType(_pointer_type);
  53. out << "static int " << _name << "(PyObject *obj, ";
  54. ppointer->output_instance(out, "addr", &parser);
  55. out << ");\n";
  56. delete ppointer;
  57. }
  58. ////////////////////////////////////////////////////////////////////
  59. // Function: FunctionWriterPtrFromPython::write_code
  60. // Access: Public, Virtual
  61. // Description: Outputs the code for the function.
  62. ////////////////////////////////////////////////////////////////////
  63. void FunctionWriterPtrFromPython::
  64. write_code(ostream &out) {
  65. CPPType *ppointer = new CPPPointerType(_pointer_type);
  66. out << "static int\n"
  67. << _name << "(PyObject *obj, ";
  68. ppointer->output_instance(out, "addr", &parser);
  69. out << ") {\n"
  70. << " if (obj != (PyObject *)NULL && PyInstance_Check(obj)) {\n"
  71. // << " PyClassObject *in_class = ((PyInstanceObject *)obj)->in_class;\n"
  72. << " PyObject *in_dict = ((PyInstanceObject *)obj)->in_dict;\n"
  73. << " if (in_dict != (PyObject *)NULL && PyDict_Check(in_dict)) {\n"
  74. << " PyObject *thisobj = PyDict_GetItemString(in_dict, \"this\");\n"
  75. << " if (thisobj != (PyObject *)NULL && PyInt_Check(thisobj)) {\n"
  76. << " (*addr) = ("
  77. << _pointer_type->get_local_name(&parser) << ")PyInt_AsLong(thisobj);\n"
  78. << " return 1;\n"
  79. << " }\n"
  80. << " }\n"
  81. << " }\n"
  82. << " return 0;\n"
  83. << "}\n\n";
  84. delete ppointer;
  85. }
  86. ////////////////////////////////////////////////////////////////////
  87. // Function: FunctionWriterPtrFromPython::get_type
  88. // Access: Public
  89. // Description: Returns the type that represents the actual data type.
  90. ////////////////////////////////////////////////////////////////////
  91. CPPType *FunctionWriterPtrFromPython::
  92. get_type() const {
  93. return _type;
  94. }
  95. ////////////////////////////////////////////////////////////////////
  96. // Function: FunctionWriterPtrFromPython::get_pointer_type
  97. // Access: Public
  98. // Description: Returns the type that represents a pointer to the
  99. // data type.
  100. ////////////////////////////////////////////////////////////////////
  101. CPPType *FunctionWriterPtrFromPython::
  102. get_pointer_type() const {
  103. return _pointer_type;
  104. }