00001
00022 #ifndef __RtcFactoryPy_h__
00023 #define __RtcFactoryPy_h__
00024
00025 #include <list>
00026 #include "Python.h"
00027 #include "rtm/RtcFactory.h"
00028
00029 namespace RTM
00030 {
00050 class RtcFactoryPy
00051 : public RtcFactoryBase
00052 {
00053 public:
00054
00080 RtcFactoryPy(RtcModuleProfile profile, PyObject* callable)
00081 : RtcFactoryBase(profile), m_pyClass(callable)
00082 {
00083 if (!PyCallable_Check(m_pyClass))
00084 {
00085 cout << "callable is not callable Python object." << endl;
00086 }
00087 else
00088 {
00089 Py_INCREF(m_pyClass);
00090 }
00091
00092
00093
00094
00095
00096
00097 };
00098
00106 ~RtcFactoryPy()
00107 {
00108 Py_DECREF(m_pyClass);
00109 }
00110
00111
00131 RtcBase* create(RtcManager* mgr)
00132 {
00133 PyObject* pyMgr;
00134 PyObject* pyArg;
00135 PyObject* pyComponent;
00136
00137 if (!PyCallable_Check(m_pyClass))
00138 {
00139 cout << "callable is not callable Python object." << endl;
00140 }
00141
00142
00143 pyMgr = SWIG_NewPointerObj((void *) mgr,
00144 SWIGTYPE_p_RTM__RtcManager, 1);
00145 if (pyMgr == NULL) cerr << "pyMgr Object is NULL" << endl;
00146
00147
00148 pyArg = Py_BuildValue("(O)", pyMgr);
00149 if (pyArg == NULL) cerr << "pyArg Object is NULL" << endl;
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 pyComponent = PyEval_CallObject(m_pyClass, pyArg);
00161 if (pyComponent == NULL)
00162 {
00163 cerr << "m_pyComponent Object is NULL" << endl;
00164 return NULL;
00165 }
00166
00167
00168 ++m_Number;
00169
00170 RtcBase* comp;
00171 if ((SWIG_ConvertPtr(pyComponent, (void **)&comp,
00172 SWIGTYPE_p_RTM__RtcBase,
00173 SWIG_POINTER_EXCEPTION)) == -1)
00174 {
00175 cerr << "conversion error" << endl;
00176 return NULL;
00177 }
00178
00179
00180 m_pyComponents.push_back(pyComponent);
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 return comp;
00193 };
00194
00214 void destroy(RtcBase* comp)
00215 {
00216 --m_Number;
00217
00218 delete comp;
00219 };
00220
00221 protected:
00229 PyObject* m_pyClass;
00230
00238 std::list<PyObject*> m_pyComponents;
00246 PyInterpreterState *m_Interp;
00247 };
00248
00249 };
00250
00251
00252
00253 #endif // end of __RtcFactory_h__