Interpreter.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   (c) Jürgen Riegel (juergen.riegel@web.de) 2002                        *   
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU Library General Public License (LGPL)   *
00008  *   as published by the Free Software Foundation; either version 2 of     *
00009  *   the License, or (at your option) any later version.                   *
00010  *   for detail see the LICENCE text file.                                 *
00011  *                                                                         *
00012  *   FreeCAD is distributed in the hope that it will be useful,            *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * 
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with FreeCAD; if not, write to the Free Software        * 
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020  *   USA                                                                   *
00021  *                                                                         *
00022  *   Juergen Riegel 2002                                                   *
00023  ***************************************************************************/
00024 
00025 #ifndef BASE_INTERPRETER_H
00026 #define BASE_INTERPRETER_H
00027 
00028 #if defined (_POSIX_C_SOURCE)
00029 #   undef  _POSIX_C_SOURCE
00030 #endif // (re-)defined in pyconfig.h
00031 #if defined (_XOPEN_SOURCE)
00032 #   undef _XOPEN_SOURCE
00033 #endif // (re-)defined in pyconfig.h
00034 
00035 
00036 #include <Python.h>
00037 
00038 // Std. configurations
00039 #include <string>
00040 #include <map>
00041 
00042 #include "Exception.h"
00043 
00044 
00045 namespace Base {
00046 
00047     using std::string;
00048     using std::vector;
00049 
00050 
00051 
00052 class BaseExport PyException : public Exception
00053 {
00054 public:
00056     PyException(void);
00057     ~PyException() throw() {}
00058 
00060     const std::string &getStackTrace(void) const {return _stackTrace;}
00061     const std::string &getErrorType(void) const {return _errorType;}
00062 
00063 protected:
00064     std::string _stackTrace;
00065     std::string _errorType;
00066 };
00067 
00073 class BaseExport SystemExitException : public Exception
00074 {
00075 public:
00076     SystemExitException(void);
00077     SystemExitException(const SystemExitException &inst);
00078     virtual ~SystemExitException() throw() {}
00079 };
00080 
00088 class BaseExport PyGILStateLocker
00089 {
00090 public:
00091     PyGILStateLocker()
00092     {
00093         gstate = PyGILState_Ensure();
00094     }
00095     ~PyGILStateLocker()
00096     {
00097         PyGILState_Release(gstate);
00098     }
00099 
00100 private:
00101     PyGILState_STATE gstate;
00102 };
00103 
00113 class BaseExport PyGILStateRelease
00114 {
00115 public:
00116     PyGILStateRelease()
00117     {
00118         // release the global interpreter lock
00119         state = PyEval_SaveThread();
00120     }
00121     ~PyGILStateRelease()
00122     {
00123         // grab the global interpreter lock again
00124         PyEval_RestoreThread(state);
00125     }
00126 
00127 private:
00128     PyThreadState* state;
00129 };
00130 
00131 
00136 class BaseExport InterpreterSingleton
00137 {
00138 public:
00139     InterpreterSingleton();
00140     ~InterpreterSingleton();
00141 
00145 
00146     std::string runString(const char *psCmd);
00148     void runInteractiveString(const char *psCmd);
00150     void runFile(const char*pxFileName);
00152     void runStringArg(const char * psCom,...);
00154     void runMethodVoid(PyObject *pobject, const char *method);
00156     PyObject* runMethodObject(PyObject *pobject, const char *method);
00158     void runMethod(PyObject *pobject, const char *method,
00159                    const char *resfmt=0,   void *cresult=0,   
00160                    const char *argfmt="()",   ...  );
00162 
00166     /* Loads a module
00167      */
00168     bool loadModule(const char* psModName);
00170     void addPythonPath(const char* Path);
00171     static void addType(PyTypeObject* Type,PyObject* Module, const char * Name);
00173 
00183     int cleanup(void (*func)(void));
00185     void finalize();
00187     void systemExit();
00189 
00193 
00194     const char* init(int argc,char *argv[]);
00195     int  runCommandLine(const char *prompt);
00196     static InterpreterSingleton &Instance(void);
00197     static void Destruct(void);
00199 
00205 
00206     PyObject* createSWIGPointerObj(const char* Modole, const char* TypeName, void* Pointer, int own);
00207     bool convertSWIGPointerObj(const char* Module, const char* TypeName, PyObject* obj, void** ptr, int flags);
00208     void cleanupSWIG(const char* TypeName);
00210 
00214 
00215     void dbgObserveFile(const char* sFileName="");
00217     void dbgSetBreakPoint(unsigned int uiLineNumber);
00219     void dbgUnsetBreakPoint(unsigned int uiLineNumber);
00221     void dbgStep(void);
00223 
00224 
00228 
00229     static const std::string strToPython(const char* Str);
00230     static const std::string strToPython(const std::string &Str){return strToPython(Str.c_str());}
00232 
00233 protected:
00234     // singleton
00235     static InterpreterSingleton *_pcSingelton;
00236 
00237 private:
00238     std::string _cDebugFileName;
00239     PyThreadState* _global;
00240 };
00241 
00242 
00247 inline InterpreterSingleton &Interpreter(void)
00248 {
00249     return InterpreterSingleton::Instance();
00250 }
00251 
00252 } //namespace Base 
00253 
00254 #endif // BASE_INTERPRETER_H

Generated on Wed Nov 23 19:00:19 2011 for FreeCAD by  doxygen 1.6.1