Adonthell  0.4
py_object.cc
Go to the documentation of this file.
1 /*
2  $Id: py_object.cc,v 1.17 2003/05/18 21:54:20 ksterker Exp $
3 
4  Copyright (C) 1999/2000/2001/2003 Kai Sterker
5  Copyright (C) 2001 Alexandre Courbot
6  Part of the Adonthell Project http://adonthell.linuxgames.com
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 
17 /**
18  * @file py_object.cc
19  * @author Kai Sterker <kaisterker@linuxgames.com>
20  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
21  *
22  * @brief Defines the py_object class.
23  *
24  *
25  */
26 
27 #include "py_object.h"
28 
30 {
31  Instance = NULL;
32  Filename = "";
33  Classname = "";
34 }
35 
37 {
38  clear ();
39 }
40 
41 // Cleanup (and re-initialisation)
43 {
44  // Delete our Instance
45  Py_XDECREF (Instance);
46  Instance = NULL;
47 
48  Filename = "";
49  Classname = "";
50 }
51 
52 // Pass a (new) Python module to be used
53 bool py_object::create_instance (string file, string classname, PyObject * args)
54 {
55  // Try to import the given script
56  PyObject *module = python::import_module (file);
57  if (!module) return false;
58 
59  // Instanciate!
60  return instanciate (module, file, classname, args);
61 }
62 
63 // Reload a python module in case it has changed on disk
64 bool py_object::reload_instance (string file, string classname, PyObject * args)
65 {
66  // Try to import the given script
67  PyObject *module = python::import_module (file);
68  if (!module) return false;
69 
70  // Now Reload
71  PyObject *reload = PyImport_ReloadModule (module);
72  Py_DECREF (module);
73  if (!reload) return false;
74 
75  return instanciate (reload, file, classname, args);
76 }
77 
78 // Instanciate the given class from the module
79 bool py_object::instanciate (PyObject *module, string file, string classname, PyObject * args)
80 {
81  // Cleanup
82  clear ();
83 
84  PyObject * classobj = PyObject_GetAttrString (module, (char *) classname.c_str ());
85  Py_DECREF (module);
86  if (!classobj)
87  {
89  return false;
90  }
91 
92  // Create the Instance
93  Instance = PyObject_CallObject (classobj, args);
94  Py_DECREF (classobj);
95  if (!Instance)
96  {
98  return false;
99  }
100 
101  Filename = file;
102  Classname = classname;
103 
104  return true;
105 }
106 
107 // Execute a method of the script
108 PyObject* py_object::call_method_ret (const string &name, PyObject *args) const
109 {
110  PyObject *result = NULL;
111 
112  if (Instance)
113  {
114  PyObject *tocall = PyObject_GetAttrString (Instance, (char *) name.c_str ());
115 
116  if (PyCallable_Check (tocall) == 1)
117  {
118  result = PyObject_CallObject (tocall, args);
119  Py_DECREF (tocall);
120  }
121 #ifdef PY_DEBUG
123 #endif
124  }
125 
126  return result;
127 }
128 
129 // check for a certain attribute
130 bool py_object::has_attribute (const std::string & name)
131 {
132  if (Instance)
133  return PyObject_HasAttrString (Instance, (char *) name.c_str ());
134  else
135  return false;
136 }
137 
138 // Get an attribute of the instance
139 PyObject *py_object::get_attribute (const string &name) const
140 {
141  if (Instance)
142  return PyObject_GetAttrString (Instance, (char *) name.c_str ());
143  else
144  return NULL;
145 }
146 
147 // Get an int attribute of the instance
149 {
150  if (Instance)
151  {
152  PyObject *attribute = PyObject_GetAttrString (Instance, (char *) name.c_str ());
153  if (!attribute) return 0;
154 
155  s_int32 value = PyInt_AsLong (attribute);
156  Py_DECREF (attribute);
157 
158  return value;
159  }
160  else
161  return 0;
162 }
163 
164  // Get a string attribute of the instance
165 string py_object::get_attribute_string (const string &name)
166 {
167  if (Instance)
168  {
169  PyObject *attribute = PyObject_GetAttrString (Instance, (char *) name.c_str ());
170  if (!attribute) return 0;
171 
172  string value = PyString_AsString (attribute);
173  Py_DECREF (attribute);
174 
175  return value;
176  }
177  else
178  return string ("");
179 }
180 
181 // Set an attribute of the instance
182 void py_object::set_attribute (const string &name, PyObject *value)
183 {
184  if (Instance)
185  if (PyObject_SetAttrString (Instance, (char *) name.c_str (), value) == -1)
187  else return;
188 }
189 
190 // Set an int attribute of the instance
191 void py_object::set_attribute_int (const string &name, int value)
192 {
193  if (Instance)
194  {
195  PyObject *val = PyInt_FromLong (value);
196 
197  if (PyObject_SetAttrString (Instance, (char *) name.c_str (), val) == -1)
199 
200  Py_DECREF (val);
201  }
202  else return;
203 }
204 
205 // Set a string attribute of the instance
206 void py_object::set_attribute_string (const string &name, const string & value)
207 {
208  if (Instance)
209  {
210  PyObject *val = PyString_FromString (value.c_str ());
211 
212  if (PyObject_SetAttrString (Instance, (char *) name.c_str (), val) == -1)
214 
215  Py_DECREF (val);
216  }
217  else return;
218 }