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++ -*-
00017 /*
00018  * $Log: RtcFactoryPy.h,v $
00019  * Revision 1.1.1.1  2005/05/12 09:06:18  n-ando
00020  * Public release.
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       //PyEval_InitThreads();
00095       //PyThreadState *tstate = PyEval_SaveThread();
00096       //m_Interp = tstate->interp;
00097       //m_Interp = PyThreadState_Get()->interp;
00098       //PyEval_ReleaseLock();
00099       //PyEval_RestoreThread(tstate);
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           //      rtmPyBeginBlockThreads();
00140           if (!PyCallable_Check(m_pyClass))
00141                 {
00142                   cout << "callable is not callable Python object." << endl;
00143                 }
00144           
00145           // Convert RtcManager* to python type RtcManager object
00146           pyMgr = SWIG_NewPointerObj((void *) mgr,
00147                                                                  SWIGTYPE_p_RTM__RtcManager, 1);
00148           if (pyMgr == NULL) cerr << "pyMgr Object is NULL" << endl;
00149           
00150           // Build python type RtcManager as python argument object
00151           pyArg = Py_BuildValue("(O)", pyMgr);
00152           if (pyArg == NULL) cerr << "pyArg Object is NULL" << endl;
00153           
00154           //PyThreadState *tstate;
00155           //      m_Interp is reference to an interpreter object.
00156           //tstate = PyThreadState_New(PyThreadState_Get()->interp);
00157           //PyEval_AcquireThread(tstate);
00158           //      PyEval_InitThreads();
00159           //Py_BEGIN_ALLOW_THREADS
00160           
00161           
00162           // Construct new component from user defined class in python
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           //      Py_END_ALLOW_THREADS
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           //      Py_INCREF(pyComponent);
00183           m_pyComponents.push_back(pyComponent);
00184           //      Py_INCREF(pyComponent);
00185           //      Py_DECREF(pyArg);
00186           //      Py_DECREF(pyMgr);
00187           
00188           // Release thepython thread. No Python API allowed beyond this point.
00189           //PyEval_ReleaseThread(tstate);
00190           //PyThreadState_Delete(tstate);
00191           //PyThreadState_Clear(tstate);
00192           //PyThreadState_DeleteCurrent();
00193           //      rtmPyEndBlockThreads();
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__

Generated on Mon May 23 03:13:56 2005 for OpenRTM by doxygen 1.3.6