PyConsole_Interp* LightApp_Application::getPyInterp()
{
- static PyConsole_Interp* myInterp = 0;
- if ( !myInterp ) {
+ static PyInterp_Auto<PyConsole_Interp> myInterp;
+ if ( myInterp.isNull() ) {
myInterp = createPyInterp();
myInterp->initialize();
+ myInterp->incrRef();// In Light application the PyConsole_Interp cannot been destroy. Why ?
}
return myInterp;
}
PyConsole_Interp* LightApp_Application::createPyInterp()
{
- return new PyConsole_Interp();
+ return new PyConsole_Interp;
}
#endif // DISABLE_PYCONSOLE
PyConsole_Interp* SalomeApp_Application::createPyInterp()
{
- return new SalomeApp_PyInterp();
+ return new SalomeApp_PyInterp;
}
#endif // DISABLE_PYCONSOLE
\brief Constructor
*/
SalomeApp_PyInterp::SalomeApp_PyInterp()
- : PyConsole_Interp(), myFirstRun( true ), myFirstInitStudy( false )
+ : myFirstRun( true ), myFirstInitStudy( false )
{
}
PyConsole_Editor::PyConsole_Editor( QWidget* parent )
: QTextEdit( parent )
{
- PyConsole_Interp* interp = new PyConsole_Interp();
+ PyConsole_Interp *interp(new PyConsole_Interp);
interp->initialize();
- init( interp );
+ myInterp=interp;
+ init();
}
/*!
PyConsole_Interp* interp )
: QTextEdit( parent )
{
- init( interp );
+ myInterp.takeRef(interp);
+ init();
}
-void PyConsole_Editor::init( PyConsole_Interp* interp )
+void PyConsole_Editor::init()
{
- myInterp = interp;
myCmdInHistory = -1;
myEventLoop = 0;
myShowBanner = true;
*/
PyConsole_Editor::~PyConsole_Editor()
{
- myInterp = 0;
}
/*!
\brief Get Python interpreter
*/
-PyConsole_Interp* PyConsole_Editor::getInterp() const
+PyConsole_Interp *PyConsole_Editor::getInterp() const
{
- return myInterp;
+ return myInterp.iAmATrollConstCast();
}
/*!
#include "PyConsole.h"
+#include "PyInterp_RefCounterObj.h"
+
#include <QTextEdit>
#include <QQueue>
virtual QString getLogFileName();
private:
- void init( PyConsole_Interp* );
+ void init();
void multilinePaste( const QString& );
void multiLineProcessNextLine();
QString banner() const;
inline int promptSize() const { return myPrompt.size(); }
- PyConsole_Interp* myInterp; //!< python interpreter
- QString myCommandBuffer; //!< python command buffer
- QString myCurrentCommand; //!< currently being printed command
- QString myPrompt; //!< current command line prompt
- int myCmdInHistory; //!< current history command index
- QString myLogFile; //!< current output log
- QStringList myHistory; //!< commands history buffer
- QEventLoop* myEventLoop; //!< internal event loop
- bool myShowBanner; //!< 'show banner' flag
- QStringList myQueue; //!< python commands queue
- bool myIsSync; //!< synchronous mode flag
- bool myIsSuppressOutput; //!< suppress output flag
- bool myMultiLinePaste; //!< true when pasting several lines
- QQueue<QString> myMultiLineContent; //!< queue of lines being pasted
- bool myAutoCompletion; //!< auto-completion mode flag
- bool myTabMode; //!< flag that is \c true when editor performs completion
- QString myComplBeforePoint; //!< string on which the dir() command is executed
- QString myComplAfterPoint; //!< string on which the results of the dir() are matched
- int myComplCursorPos; //!< cursor position when <TAB> is hit
+ PyInterp_Auto<PyConsole_Interp> myInterp; //!< python interpreter
+ QString myCommandBuffer; //!< python command buffer
+ QString myCurrentCommand; //!< currently being printed command
+ QString myPrompt; //!< current command line prompt
+ int myCmdInHistory; //!< current history command index
+ QString myLogFile; //!< current output log
+ QStringList myHistory; //!< commands history buffer
+ QEventLoop* myEventLoop; //!< internal event loop
+ bool myShowBanner; //!< 'show banner' flag
+ QStringList myQueue; //!< python commands queue
+ bool myIsSync; //!< synchronous mode flag
+ bool myIsSuppressOutput; //!< suppress output flag
+ bool myMultiLinePaste; //!< true when pasting several lines
+ QQueue<QString> myMultiLineContent; //!< queue of lines being pasted
+ bool myAutoCompletion; //!< auto-completion mode flag
+ bool myTabMode; //!< flag that is \c true when editor performs completion
+ QString myComplBeforePoint; //!< string on which the dir() command is executed
+ QString myComplAfterPoint; //!< string on which the results of the dir() are matched
+ int myComplCursorPos; //!< cursor position when <TAB> is hit
friend void PyConsole_CallbackStdout( void*, char* );
friend void PyConsole_CallbackStderr( void*, char* );
public:
PyConsole_Interp();
- ~PyConsole_Interp();
virtual int afterRun();
virtual int beforeRun();
+protected:
+
+ ~PyConsole_Interp();
+
private:
bool runDirCommand( const QString&, const QString&, QStringList&, QString& );
bool runDirAndExtract( const QString&, const QString&, QStringList&, bool = true ) const;
PyInterp_Interp.h
PyInterp_Request.h
PyInterp_Utils.h
+ PyInterp_RefCounterObj.h
)
# header files / to install
#include "PyInterp.h" // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
#include "PyInterp_Utils.h"
+#include "PyInterp_RefCounterObj.h"
#include <list>
#include <string>
* (only there namespace is made available when importing in another context).
* See also class PyConsole_Interp.
*/
-class PYINTERP_EXPORT PyInterp_Interp
+class PYINTERP_EXPORT PyInterp_Interp : public PyInterp_RefCounterObj
{
public:
static int _argc;
static char* _argv[];
- PyInterp_Interp();
- virtual ~PyInterp_Interp();
-
void initialize();
void destroy();
virtual int run(const char *command);
- virtual void initStudy() {};
+ virtual void initStudy() {}
std::string getBanner() const;
void setverrcb(PyOutChanged*, void*);
const char* getPrevious();
const char* getNext();
+protected:
+
+ PyInterp_Interp();
+ virtual ~PyInterp_Interp();
+
protected:
/** Redirection of stdout and stderr */
PyObject* _vout;
--- /dev/null
+// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+// File : PyInterp_RefCounterObj.h
+// Author : Anthony Geay (EDF R&D)
+
+#ifndef PYINTERP_REFCOUNTEROBJ_H
+#define PYINTERP_REFCOUNTEROBJ_H
+
+#include "PyInterp.h"
+
+template<class T>
+class PYINTERP_EXPORT PyInterp_Auto
+{
+public:
+ PyInterp_Auto(const PyInterp_Auto& other):_ptr(0) { referPtr(other._ptr); }
+ PyInterp_Auto(T *ptr=0):_ptr(ptr) { }
+ ~PyInterp_Auto() { destroyPtr(); }
+ bool isNull() const { return _ptr==0; }
+ bool isNotNull() const { return !isNull(); }
+ void nullify() { destroyPtr(); _ptr=0; }
+ bool operator==(const PyInterp_Auto& other) const { return _ptr==other._ptr; }
+ bool operator==(const T *other) const { return _ptr==other; }
+ PyInterp_Auto &operator=(const PyInterp_Auto<T>& other) { if(_ptr!=other._ptr) { destroyPtr(); referPtr(other._ptr); } return *this; }
+ PyInterp_Auto &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
+ void takeRef(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; if(_ptr) _ptr->incrRef(); } }
+ T *operator->() { return _ptr ; }
+ const T *operator->() const { return _ptr; }
+ T& operator*() { return *_ptr; }
+ const T& operator*() const { return *_ptr; }
+ operator T *() { return _ptr; }
+ operator const T *() const { return _ptr; }
+ T *retn() { if(_ptr) _ptr->incrRef(); return _ptr; }
+ T *iAmATrollConstCast() const { return _ptr; }
+ private:
+ void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); }
+ void destroyPtr() { if(_ptr) _ptr->decrRef(); }
+ private:
+ T *_ptr;
+};
+
+class PYINTERP_EXPORT PyInterp_RefCounterObj
+{
+ protected:
+ PyInterp_RefCounterObj():_cnt(1) { }
+ PyInterp_RefCounterObj(const PyInterp_RefCounterObj& other):_cnt(1) { }
+ public:
+ bool decrRef() const
+ {
+ bool ret=((--_cnt)==0);
+ if(ret)
+ delete this;
+ return ret;
+ }
+ void incrRef() const { _cnt++; }
+ int getRCValue() const { return _cnt; }
+ // copies using operator= should not copy the ref counter of \a other
+ PyInterp_RefCounterObj& operator=(const PyInterp_RefCounterObj& other) { return *this; }
+ protected:
+ virtual ~PyInterp_RefCounterObj() { }
+ private:
+ mutable int _cnt;
+};
+
+#endif // PYINTERP_REFCOUNTEROBJ_H