Callback: Add callback interfaces for python.
[gemrb.git] / gemrb / plugins / GUIScript / PythonHelpers.h
blob2526013712a52737bb4affd2d17d06f76d7a0550
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #ifndef PYTHON_HELPERS_H
20 #define PYTHON_HELPERS_H
22 // Python.h needs to be included first.
23 #include "GUIScript.h"
25 #include "win32def.h" // For Logging
27 #include "Callback.h"
28 #include "Holder.h"
29 #include "Interface.h"
31 class StringCallback : public Callback {
32 public:
33 StringCallback(const char*);
34 StringCallback(PyObject*);
35 ~StringCallback();
36 bool call();
37 private:
38 PyObject* Name;
41 struct PythonCallback : public Callback {
42 public:
43 PythonCallback(PyObject *Function);
44 ~PythonCallback();
45 bool call();
46 private:
47 PyObject *Function;
51 template <typename T>
52 class CObject : public Holder<T> {
53 private:
54 public:
55 operator PyObject* () const
57 if (Holder<T>::ptr) {
58 Holder<T>::ptr->acquire();
59 GUIScript *gs = (GUIScript *) core->GetGUIScriptEngine();
60 PyObject *obj = PyCObject_FromVoidPtrAndDesc(Holder<T>::ptr,const_cast<TypeID*>(&T::ID),PyRelease);
61 PyObject *tuple = PyTuple_New(1);
62 PyTuple_SET_ITEM(tuple, 0, obj);
63 return gs->ConstructObject(T::ID.description, tuple);
64 } else {
65 Py_INCREF( Py_None );
66 return Py_None;
69 CObject(PyObject *obj)
71 if (obj == Py_None)
72 return;
73 PyObject *id = PyObject_GetAttrString(obj, "ID");
74 if (id)
75 obj = id;
76 else
77 PyErr_Clear();
78 if (!PyCObject_Check(obj) || PyCObject_GetDesc(obj) != const_cast<TypeID*>(&T::ID)) {
79 printMessage("GUIScript","Bad CObject extracted.\n",LIGHT_RED);
80 return;
82 Holder<T>::ptr = static_cast<T*>(PyCObject_AsVoidPtr(obj));
83 Holder<T>::ptr->acquire();
84 if (id) {
85 Py_DECREF(id);
88 CObject(const Holder<T>& ptr)
89 : Holder<T>(ptr)
92 // This is here because of lookup order issues.
93 operator bool () const
95 return Holder<T>::ptr;
97 private:
98 static void PyRelease(void *obj, void *desc)
100 if (desc != const_cast<TypeID*>(&T::ID)) {
101 printMessage("GUIScript","Bad CObject deleted.\n",LIGHT_RED);
102 return;
104 static_cast<T*>(obj)->release();
108 template <typename T, class Container>
109 PyObject* MakePyList(const Container &source)
111 size_t size = source.size();
112 PyObject *list = PyList_New(size);
113 for (size_t i = 0; i < size; ++i) {
114 // SET_ITEM might be preferable to SetItem here, but MSVC6 doesn't like it.
115 PyList_SetItem(list, i, CObject<T>(source[i]));
117 return list;
120 #endif