Main Page | Namespace List | Class Hierarchy | Class List | Directories | 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.2  2005/05/16 06:06:56  n-ando
00020  * - RtcFactoryPy class was DLL exported for Windows port.
00021  *
00022  * Revision 1.1.1.1  2005/05/12 09:06:18  n-ando
00023  * Public release.
00024  *
00025  *
00026  */
00027 
00028 #ifndef RtcFactoryPy_h
00029 #define RtcFactoryPy_h
00030 
00031 #include <list>
00032 #include "Python.h"
00033 #include "rtm/RtcFactory.h"
00034 
00035 namespace RTM 
00036 {
00056   class EXPORTS RtcFactoryPy
00057     : public RtcFactoryBase
00058   {
00059   public:
00060         
00086     RtcFactoryPy(RtcModuleProfile profile, PyObject* callable)
00087       : RtcFactoryBase(profile), m_pyClass(callable)
00088     {
00089       if (!PyCallable_Check(m_pyClass))
00090                 {
00091                   cout << "callable is not callable Python object." << endl;
00092                 }
00093       else
00094                 {
00095                   Py_INCREF(m_pyClass);
00096                 }
00097       //PyEval_InitThreads();
00098       //PyThreadState *tstate = PyEval_SaveThread();
00099       //m_Interp = tstate->interp;
00100       //m_Interp = PyThreadState_Get()->interp;
00101       //PyEval_ReleaseLock();
00102       //PyEval_RestoreThread(tstate);
00103     };
00104         
00112     ~RtcFactoryPy()
00113         {
00114           Py_DECREF(m_pyClass);
00115         }
00116         
00117         
00137     RtcBase* create(RtcManager* mgr)
00138         {
00139           PyObject* pyMgr;
00140           PyObject* pyArg;
00141           PyObject* pyComponent;
00142           //      rtmPyBeginBlockThreads();
00143           if (!PyCallable_Check(m_pyClass))
00144                 {
00145                   cout << "callable is not callable Python object." << endl;
00146                 }
00147           
00148           // Convert RtcManager* to python type RtcManager object
00149           pyMgr = SWIG_NewPointerObj((void *) mgr,
00150                                                                  SWIGTYPE_p_RTM__RtcManager, 1);
00151           if (pyMgr == NULL) cerr << "pyMgr Object is NULL" << endl;
00152           
00153           // Build python type RtcManager as python argument object
00154           pyArg = Py_BuildValue("(O)", pyMgr);
00155           if (pyArg == NULL) cerr << "pyArg Object is NULL" << endl;
00156           
00157           //PyThreadState *tstate;
00158           //      m_Interp is reference to an interpreter object.
00159           //tstate = PyThreadState_New(PyThreadState_Get()->interp);
00160           //PyEval_AcquireThread(tstate);
00161           //      PyEval_InitThreads();
00162           //Py_BEGIN_ALLOW_THREADS
00163           
00164           
00165           // Construct new component from user defined class in python
00166           pyComponent = PyEval_CallObject(m_pyClass, pyArg);
00167           if (pyComponent == NULL)
00168                 {
00169                   cerr << "m_pyComponent Object is NULL" << endl;
00170                   return NULL;
00171                 }
00172           
00173           //      Py_END_ALLOW_THREADS
00174           ++m_Number;
00175           
00176           RtcBase* comp;
00177           if ((SWIG_ConvertPtr(pyComponent, (void **)&comp,
00178                                                    SWIGTYPE_p_RTM__RtcBase,
00179                                                    SWIG_POINTER_EXCEPTION)) == -1)
00180                 {
00181                   cerr << "conversion error" << endl;
00182                   return NULL;
00183                 }
00184           
00185           //      Py_INCREF(pyComponent);
00186           m_pyComponents.push_back(pyComponent);
00187           //      Py_INCREF(pyComponent);
00188           //      Py_DECREF(pyArg);
00189           //      Py_DECREF(pyMgr);
00190           
00191           // Release thepython thread. No Python API allowed beyond this point.
00192           //PyEval_ReleaseThread(tstate);
00193           //PyThreadState_Delete(tstate);
00194           //PyThreadState_Clear(tstate);
00195           //PyThreadState_DeleteCurrent();
00196           //      rtmPyEndBlockThreads();
00197           
00198           return comp;
00199         };
00200         
00220     void destroy(RtcBase* comp)
00221         {
00222           --m_Number;
00223           
00224           delete comp;
00225         };
00226         
00227   protected:
00235     PyObject* m_pyClass;
00236         
00244         std::list<PyObject*> m_pyComponents;
00252         PyInterpreterState *m_Interp;
00253   };
00254   
00255 };
00256 
00257 
00258 
00259 #endif // end of __RtcFactory_h__

Generated on Sat Oct 28 02:28:13 2006 for OpenRTM by  doxygen 1.4.1