/[adonthell]/adonthell-0.3/src/python_class.cc
ViewVC logotype

Diff of /adonthell-0.3/src/python_class.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.10 by ksterker, Thu Feb 20 21:03:11 2003 UTC revision 1.11 by ksterker, Sat Apr 16 17:56:32 2005 UTC
# Line 26  Line 26 
26  #include "game.h"  #include "game.h"
27  #include <iostream>  #include <iostream>
28    
29  PyObject * data::globals;  PyObject *data::globals;
30  PyObject * python::module;  PyObject *python::module;
31    
32    // defined in py_adonthell_wrap.cc
33    PyObject *pass_instance (void *instance, const char* class_name);
34    
35  using namespace std;  using namespace std;
36    
# Line 39  void python::init () Line 42  void python::init ()
42      Py_Initialize ();      Py_Initialize ();
43  }  }
44    
45    /**
46     * Stop Python
47     */
48  void python::cleanup ()  void python::cleanup ()
49  {  {
50      // Cleanup the global namespace of python interpreter      // Cleanup the global namespace of python interpreter
# Line 117  PyObject *python::import_module (string Line 123  PyObject *python::import_module (string
123  // Make a C++ instance available to Python  // Make a C++ instance available to Python
124  PyObject *python::pass_instance (void *instance, const char *class_name)  PyObject *python::pass_instance (void *instance, const char *class_name)
125  {  {
126      char class_ptr[256];      return pass_instance (instance, class_name);
     char class_addr[256];  
     char *buffer = class_addr;  
   
     // Construct the python shadow class matching the "instance" class  
     strcat (strcpy (class_ptr, class_name), "Ptr");  
       
     // Construct SWIG's representation of the "instance" pointer  
     *(buffer++) = '_';  
     buffer = ptr_to_string (buffer, &instance, sizeof (void *));  
     strcpy (buffer, "_p_");  
     strcpy (buffer+3, class_name);  
   
     // Now create the Python object corresponding to "instance"  
     PyObject *cls = PyDict_GetItemString (data::globals, class_ptr);  
     PyObject *arg = Py_BuildValue ("(s)", class_addr);  
     PyObject *res = PyObject_CallObject (cls, arg);  
       
 #ifdef PY_DEBUG  
     show_traceback ();  
 #endif  
   
     // Clean up  
     Py_DECREF (arg);  
       
     // Voila: "res" is 'identical' to "instance" :)  
     return res;  
 }  
   
 // Convert a pointer to a string (like SWIG 1.3.7 does)  
 char *python::ptr_to_string (char *c, void *ptr, int sz)  
 {  
     static char hex[17] = "0123456789abcdef";  
     int i;  
     unsigned char *u = (unsigned char *) ptr;  
     register unsigned char uu;  
   
     for (i = 0; i < sz; i++,u++)  
     {  
         uu = *u;  
         *(c++) = hex[(uu & 0xf0) >> 4];  
         *(c++) = hex[uu & 0xf];  
     }  
   
     return c;  
127  }  }
128    
129  PyObject * python::get_tuple (igzstream & file)  PyObject * python::get_tuple (igzstream & file)

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26