ElementInstancer.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef ROCKETCOREPYTHONELEMENTINSTANCER_H
00029 #define ROCKETCOREPYTHONELEMENTINSTANCER_H
00030
00031 #include <Rocket/Core/String.h>
00032 #include <Rocket/Core/XMLParser.h>
00033 #include <Rocket/Core/Python/Python.h>
00034 #include <Rocket/Core/Element.h>
00035 #include <Rocket/Core/ElementInstancer.h>
00036 #include <Rocket/Core/Python/Header.h>
00037
00038 namespace Rocket {
00039 namespace Core {
00040 namespace Python {
00041
00053 class ElementInstancer : public Rocket::Core::ElementInstancer
00054 {
00055 public:
00056 ElementInstancer(PyObject* _class_definition)
00057 {
00058 class_definition = _class_definition;
00059 Py_INCREF(class_definition);
00060 }
00061 virtual ~ElementInstancer()
00062 {
00063 Py_DECREF(class_definition);
00064 }
00065
00069 virtual Element* InstanceElement(Element* ROCKET_UNUSED(parent), const Rocket::Core::String& tag, const Rocket::Core::XMLAttributes& ROCKET_UNUSED(attributes))
00070 {
00071
00072 PyObject* args = PyTuple_New(1);
00073 PyTuple_SetItem(args, 0, PyString_FromString(tag.CString()));
00074
00075 PyObject* instance = PyObject_CallObject(class_definition, args);
00076 Py_DECREF(args);
00077
00078
00079 if (instance == NULL)
00080 {
00081 PyErr_Print();
00082 return NULL;
00083 }
00084
00085 Element* element = python::extract<Element*>(python::object(python::handle<>(python::borrowed(instance))));
00086
00087
00088 element->AddReference();
00089 Py_DECREF( instance );
00090
00091 return element;
00092 }
00093
00096 virtual void ReleaseElement(Element* ROCKET_UNUSED(element))
00097 {
00098
00099 }
00100
00102 virtual void Release()
00103 {
00104 delete this;
00105 }
00106
00107 private:
00108 PyObject* class_definition;
00109 };
00110
00111 }
00112 }
00113 }
00114
00115 #endif