1 #ifndef __PYTHON_CLASS_H__
2 #define __PYTHON_CLASS_H__
20 #include <boost/version.hpp>
21 #if BOOST_VERSION != 102800
22 #if defined (_MSC_VER) && _MSC_VER<=1200
25 #include "boost/python/object.hpp"
26 #include "boost/python/class.hpp"
27 #include "boost/python/call_method.hpp"
29 #if defined (_MSC_VER) && _MSC_VER<=1200
32 #define class_builder class_
34 #include "boost/python/objects.hpp"
35 #include "boost/python/class_builder.hpp"
36 #include "boost/python/detail/extension_class.hpp"
47 #if BOOST_VERSION == 102800
48 #define PYTHONCALLBACK(rtype, ptr, str) \
49 boost::python::callback<rtype>::call_method(ptr, str)
50 #define PYTHONCALLBACK2(rtype, ptr, str, str2) \
51 boost::python::callback<rtype>::call_method(ptr, str, str2)
53 #define PYTHONCALLBACK(rtype, ptr, str) \
54 boost::python::call_method<rtype>(ptr, str)
55 #define PYTHONCALLBACK2(rtype, ptr, str, str2) \
56 boost::python::call_method<rtype>(ptr, str, str2)
92 #define TO_PYTHON_SMART_POINTER(Pointer) \
93 BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE \
94 inline PyObject* to_python(class Pointer* x) {return boost::python::python_extension_class_converters<Pointer>::smart_ptr_to_python(x);} \
95 inline PyObject* to_python(const class Pointer* p) {return to_python(const_cast<class Pointer*>(p));} \
96 BOOST_PYTHON_END_CONVERSION_NAMESPACE
101 #define ADD_FROM_PYTHON_FUNCTION(SuperClass) \
102 BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE \
103 SuperClass & from_python(PyObject *obj,boost::python::type<SuperClass &>) { \
104 boost::python::detail::extension_instance* self = boost::python::detail::get_extension_instance(obj); \
105 typedef std::vector<boost::python::detail::instance_holder_base*>::const_iterator iterator; \
106 for (iterator p = self->wrapped_objects().begin(); \
107 p != self->wrapped_objects().end(); ++p) \
109 boost::python::detail::instance_holder<SuperClass>* held = dynamic_cast<boost::python::detail::instance_holder<SuperClass>*>(*p); \
111 return *held->target(); \
112 void* target = boost::python::detail::class_registry<SuperClass>::class_object()->try_class_conversions(*p); \
114 return *boost::python::detail::check_non_null(static_cast<SuperClass*>(target)); \
116 boost::python::detail::report_missing_instance_data(self, boost::python::detail::class_registry<SuperClass>::class_object(), typeid(SuperClass)); \
117 boost::python::throw_argument_error(); \
118 return *((SuperClass*)0); \
119 }BOOST_PYTHON_END_CONVERSION_NAMESPACE
122 #ifdef FROM_PYTHON_ERRORS
123 #define PYTHON_INIT_INHERIT_GLOBALS(name,SuperClass) template <> PythonClass <SuperClass> *PythonClass< SuperClass >::last_instance = NULL; \
124 ADD_FROM_PYTHON_FUNCTION(SuperClass)
125 #define PYTHON_INIT_GLOBALS(name,Class) ADD_FROM_PYTHON_FUNCTION(Class)
127 #define PYTHON_INIT_INHERIT_GLOBALS(name,SuperClass) template <> PythonClass <SuperClass> *PythonClass< SuperClass >::last_instance = NULL;
128 #define PYTHON_INIT_GLOBALS(name,Class)
131 #if BOOST_VERSION != 102800
132 #define PYTHON_BEGIN_MODULE(name) BOOST_PYTHON_MODULE(name) {
133 #define PYTHON_DEFINE_GLOBAL(modul,fun,funname) boost::python::def (funname,fun)
134 #define VS_BOOST_MAKE_TUPLE(a,b,c) boost::python::make_tuple(a,b,c)
135 #define VS_BOOST_MAKE_TUPLE_2(a,b) boost::python::make_tuple(a,b)
136 #define VS_BOOST_MAKE_TUPLE_4(a,b,c,d) boost::python::make_tuple(a,b,c,d)
138 #define PYTHON_BEGIN_MODULE(name) BOOST_PYTHON_MODULE_INIT(name) {boost::python::module_builder name(#name);
139 #define PYTHON_DEFINE_GLOBAL(modul,fun,funname) modul.def (fun,funname)
140 #define VS_BOOST_MAKE_TUPLE(a,b,c) boost::python::tuple(a,b,c)
141 #define VS_BOOST_MAKE_TUPLE_2(a,b) boost::python::tuple(a,b)
142 #define VS_BOOST_MAKE_TUPLE_4(a,b,c,d) boost::python::tuple(a,b,c,d)
144 #define PYTHON_END_MODULE(name) }
145 #define PYTHON_INIT_MODULE(name) init##name()
146 #if BOOST_VERSION != 102800
148 #define PYTHON_BASE_BEGIN_INHERIT_CLASS(name,NewClass,SuperClass,myclass) { \
149 boost::python::class_builder <SuperClass, NewClass, boost::noncopyable > Class (myclass
150 #define PYTHON_BEGIN_INHERIT_CLASS(name,NewClass,SuperClass,myclass) PYTHON_BASE_BEGIN_INHERIT_CLASS(name,NewClass,SuperClass,myclass) \
153 #define PYTHON_BASE_BEGIN_CLASS(name,CLASS,myclass) { \
154 boost::python::class_builder <CLASS> Class (myclass
155 #define PYTHON_BEGIN_CLASS(name,CLASS,myclass) PYTHON_BASE_BEGIN_CLASS(name,CLASS,myclass) \
157 #define PYTHON_DEFINE_METHOD(modul,fun,funname) modul.def (funname,fun)
158 #define PYTHON_DEFINE_METHOD_DEFAULT(modul,fun,funname,deflt) modul.def (funname,deflt)
161 #define PYTHON_BASE_BEGIN_INHERIT_CLASS(name,NewClass,SuperClass,myclass) { \
162 boost::python::class_builder <SuperClass ,NewClass> Class (name,myclass);
163 #define PYTHON_BEGIN_INHERIT_CLASS(name,NewClass,SuperClass,myclass) PYTHON_BASE_BEGIN_INHERIT_CLASS(name,NewClass,SuperClass,myclass) \
164 Class.def (boost::python::constructor<>());
166 #define PYTHON_BASE_BEGIN_CLASS(name,CLASS,myclass) { \
167 boost::python::class_builder <CLASS> Class (name,myclass);
168 #define PYTHON_BEGIN_CLASS(name,CLASS,myclass) PYTHON_BASE_BEGIN_CLASS(name,CLASS,myclass) \
169 Class.def (boost::python::constructor<>());
170 #define PYTHON_DEFINE_METHOD(modul,fun,funname) modul.def (fun,funname)
171 #define PYTHON_DEFINE_METHOD_DEFAULT(modul,fun,funname,defaultfun) modul.def (fun,funname,defaultfun)
173 #define PYTHON_END_CLASS(name,SuperClass) }
178 template <
class SuperClass>
212 Python::reseterrors();
213 PyRun_SimpleString (code);
214 Python::reseterrors();
219 fprintf (stderr,
"Destruct called. If called from C++ this is death %ld (0x%lx)",(
unsigned long)
this,(
unsigned long)
this);
223 template <
class SuperClass>
240 SuperClass::SetParent (parent);
245 (self_).SuperClass::Execute();
249 (self_).SuperClass::ChooseTarget();
277 Python::reseterrors();
281 Python::reseterrors();
283 Python::reseterrors();
288 Python::reseterrors();
290 Python::reseterrors();
298 return (self_).PythonMissionBaseClass::Pickle();