Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

RtcFactoryPy.h

Go to the documentation of this file.
00001 // -*- C++ -*-
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       //PyEval_InitThreads();
00092       //PyThreadState *tstate = PyEval_SaveThread();
00093       //m_Interp = tstate->interp;
00094       //m_Interp = PyThreadState_Get()->interp;
00095       //PyEval_ReleaseLock();
00096       //PyEval_RestoreThread(tstate);
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           //      rtmPyBeginBlockThreads();
00137           if (!PyCallable_Check(m_pyClass))
00138                 {
00139                   cout << "callable is not callable Python object." << endl;
00140                 }
00141           
00142           // Convert RtcManager* to python type RtcManager object
00143           pyMgr = SWIG_NewPointerObj((void *) mgr,
00144                                                                  SWIGTYPE_p_RTM__RtcManager, 1);
00145           if (pyMgr == NULL) cerr << "pyMgr Object is NULL" << endl;
00146           
00147           // Build python type RtcManager as python argument object
00148           pyArg = Py_BuildValue("(O)", pyMgr);
00149           if (pyArg == NULL) cerr << "pyArg Object is NULL" << endl;
00150           
00151           //PyThreadState *tstate;
00152           //      m_Interp is reference to an interpreter object.
00153           //tstate = PyThreadState_New(PyThreadState_Get()->interp);
00154           //PyEval_AcquireThread(tstate);
00155           //      PyEval_InitThreads();
00156           //Py_BEGIN_ALLOW_THREADS
00157           
00158           
00159           // Construct new component from user defined class in python
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           //      Py_END_ALLOW_THREADS
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           //      Py_INCREF(pyComponent);
00180           m_pyComponents.push_back(pyComponent);
00181           //      Py_INCREF(pyComponent);
00182           //      Py_DECREF(pyArg);
00183           //      Py_DECREF(pyMgr);
00184           
00185           // Release thepython thread. No Python API allowed beyond this point.
00186           //PyEval_ReleaseThread(tstate);
00187           //PyThreadState_Delete(tstate);
00188           //PyThreadState_Clear(tstate);
00189           //PyThreadState_DeleteCurrent();
00190           //      rtmPyEndBlockThreads();
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__

Generated on Thu Dec 16 15:29:18 2004 for OpenRTM by doxygen 1.3.6