functionWriterPtrToPython.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Filename: functionWriterPtrToPython.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 "functionWriterPtrToPython.h"
  19. #include "typeManager.h"
  20. #include "interrogateBuilder.h"
  21. #include "interrogate.h"
  22. #include "interfaceMakerPythonObj.h"
  23. #include "cppPointerType.h"
  24. ////////////////////////////////////////////////////////////////////
  25. // Function: FunctionWriterPtrToPython::Constructor
  26. // Access: Public
  27. // Description:
  28. ////////////////////////////////////////////////////////////////////
  29. FunctionWriterPtrToPython::
  30. FunctionWriterPtrToPython(CPPType *type) {
  31. _type = TypeManager::unwrap_const(TypeManager::unwrap_pointer(type));
  32. _name =
  33. "to_python_" +
  34. InterrogateBuilder::clean_identifier(_type->get_local_name(&parser));
  35. _pointer_type = new CPPPointerType(_type);
  36. }
  37. ////////////////////////////////////////////////////////////////////
  38. // Function: FunctionWriterPtrToPython::Destructor
  39. // Access: Public
  40. // Description:
  41. ////////////////////////////////////////////////////////////////////
  42. FunctionWriterPtrToPython::
  43. ~FunctionWriterPtrToPython() {
  44. delete _pointer_type;
  45. }
  46. ////////////////////////////////////////////////////////////////////
  47. // Function: FunctionWriterPtrToPython::write_prototype
  48. // Access: Public, Virtual
  49. // Description: Outputs the prototype for the function.
  50. ////////////////////////////////////////////////////////////////////
  51. void FunctionWriterPtrToPython::
  52. write_prototype(ostream &out) {
  53. out << "static PyObject *" << _name << "(";
  54. _pointer_type->output_instance(out, "addr", &parser);
  55. out << ", int caller_manages);\n";
  56. }
  57. ////////////////////////////////////////////////////////////////////
  58. // Function: FunctionWriterPtrToPython::write_code
  59. // Access: Public, Virtual
  60. // Description: Outputs the code for the function.
  61. ////////////////////////////////////////////////////////////////////
  62. void FunctionWriterPtrToPython::
  63. write_code(ostream &out) {
  64. string classobj_func = InterfaceMakerPythonObj::get_builder_name(_type);
  65. out << "static PyObject *\n"
  66. << _name << "(";
  67. _pointer_type->output_instance(out, "addr", &parser);
  68. out << ", int caller_manages) {\n"
  69. << " PyObject *" << classobj_func << "();\n"
  70. << " PyObject *classobj = " << classobj_func << "();\n"
  71. << " PyInstanceObject *instance = (PyInstanceObject *)PyInstance_New(classobj, (PyObject *)NULL, (PyObject *)NULL);\n"
  72. << " if (instance != (PyInstanceObject *)NULL) {\n"
  73. << " PyObject *thisptr = PyInt_FromLong((long)addr);\n"
  74. << " PyDict_SetItemString(instance->in_dict, \"this\", thisptr);\n"
  75. << " }\n"
  76. << " return (PyObject *)instance;\n"
  77. << "}\n\n";
  78. }
  79. ////////////////////////////////////////////////////////////////////
  80. // Function: FunctionWriterPtrToPython::get_pointer_type
  81. // Access: Public
  82. // Description: Returns the type that represents a pointer to the
  83. // data type.
  84. ////////////////////////////////////////////////////////////////////
  85. CPPType *FunctionWriterPtrToPython::
  86. get_pointer_type() const {
  87. return _pointer_type;
  88. }