functionWriterPtrFromPython.cxx 4.0 KB

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