00001
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef RtcFactoryPy_h
00026 #define RtcFactoryPy_h
00027
00028 #include <list>
00029 #include "Python.h"
00030 #include "rtm/RtcFactory.h"
00031
00032 namespace RTM
00033 {
00053 class RtcFactoryPy
00054 : public RtcFactoryBase
00055 {
00056 public:
00057
00083 RtcFactoryPy(RtcModuleProfile profile, PyObject* callable)
00084 : RtcFactoryBase(profile), m_pyClass(callable)
00085 {
00086 if (!PyCallable_Check(m_pyClass))
00087 {
00088 cout << "callable is not callable Python object." << endl;
00089 }
00090 else
00091 {
00092 Py_INCREF(m_pyClass);
00093 }
00094
00095
00096
00097
00098
00099
00100 };
00101
00109 ~RtcFactoryPy()
00110 {
00111 Py_DECREF(m_pyClass);
00112 }
00113
00114
00134 RtcBase* create(RtcManager* mgr)
00135 {
00136 PyObject* pyMgr;
00137 PyObject* pyArg;
00138 PyObject* pyComponent;
00139
00140 if (!PyCallable_Check(m_pyClass))
00141 {
00142 cout << "callable is not callable Python object." << endl;
00143 }
00144
00145
00146 pyMgr = SWIG_NewPointerObj((void *) mgr,
00147 SWIGTYPE_p_RTM__RtcManager, 1);
00148 if (pyMgr == NULL) cerr << "pyMgr Object is NULL" << endl;
00149
00150
00151 pyArg = Py_BuildValue("(O)", pyMgr);
00152 if (pyArg == NULL) cerr << "pyArg Object is NULL" << endl;
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 pyComponent = PyEval_CallObject(m_pyClass, pyArg);
00164 if (pyComponent == NULL)
00165 {
00166 cerr << "m_pyComponent Object is NULL" << endl;
00167 return NULL;
00168 }
00169
00170
00171 ++m_Number;
00172
00173 RtcBase* comp;
00174 if ((SWIG_ConvertPtr(pyComponent, (void **)&comp,
00175 SWIGTYPE_p_RTM__RtcBase,
00176 SWIG_POINTER_EXCEPTION)) == -1)
00177 {
00178 cerr << "conversion error" << endl;
00179 return NULL;
00180 }
00181
00182
00183 m_pyComponents.push_back(pyComponent);
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 return comp;
00196 };
00197
00217 void destroy(RtcBase* comp)
00218 {
00219 --m_Number;
00220
00221 delete comp;
00222 };
00223
00224 protected:
00232 PyObject* m_pyClass;
00233
00241 std::list<PyObject*> m_pyComponents;
00249 PyInterpreterState *m_Interp;
00250 };
00251
00252 };
00253
00254
00255
00256 #endif // end of __RtcFactory_h__