Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtObjectPtr.h
3206 views
#ifndef _PYTHONQTOBJECTPTR_H1#define _PYTHONQTOBJECTPTR_H23/*4*5* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.6*7* This library is free software; you can redistribute it and/or8* modify it under the terms of the GNU Lesser General Public9* License as published by the Free Software Foundation; either10* version 2.1 of the License, or (at your option) any later version.11*12* This library is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU15* Lesser General Public License for more details.16*17* Further, this software is distributed without any warranty that it is18* free of the rightful claim of any third person regarding infringement19* or the like. Any license provided herein, whether implied or20* otherwise, applies only to this software file. Patent licenses, if21* any, provided herein do not apply to combinations of this program with22* other software, or any other product whatsoever.23*24* You should have received a copy of the GNU Lesser General Public25* License along with this library; if not, write to the Free Software26* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA27*28* Contact information: MeVis Research GmbH, Universitaetsallee 29,29* 28359 Bremen, Germany or:30*31* http://www.mevis.de32*33*/3435//----------------------------------------------------------------------------------36/*!37// \file PythonQtObjectPtr.h38// \author Florian Link39// \author Last changed by $Author: florian $40// \date 2006-0541*/42//----------------------------------------------------------------------------------4344#include <Python.h>45#include "PythonQtSystem.h"4647//! a smart pointer that stores a PyObject pointer and that handles reference counting automatically48class PYTHONQT_EXPORT PythonQtObjectPtr49{50public:51PythonQtObjectPtr():_object(NULL) {}5253PythonQtObjectPtr(const PythonQtObjectPtr &p):_object(NULL) {54setObject(p.object());55}5657PythonQtObjectPtr(PyObject* o) {58_object = o;59if (o) Py_INCREF(_object);60}6162~PythonQtObjectPtr() { if (_object) Py_DECREF(_object); }6364PythonQtObjectPtr &operator=(const PythonQtObjectPtr &p) {65setObject(p.object());66return *this;67}6869PythonQtObjectPtr &operator=(PyObject* o) {70setObject(o);71return *this;72}7374bool operator==( const PythonQtObjectPtr &p ) const {75return object() == p.object();76}7778bool operator!= ( const PythonQtObjectPtr& p ) const {79return !( *this == p );80}8182bool operator==( PyObject* p ) const {83return object() == p;84}8586bool operator!= ( PyObject* p ) const {87return object() != p;88}8990bool isNull() const { return !object(); }9192PyObject* operator->() const { return object(); }9394PyObject& operator*() const { return *( object() ); }9596operator PyObject*() const { return object(); }9798//! sets the object and passes the ownership (stealing the reference, in Python slang)99void setNewRef(PyObject* o) {100if (o != _object) {101if (_object) Py_DECREF(_object);102_object = o;103}104}105106PyObject* object() const {107return _object;108}109110//! evaluates the given script code in the context of this object and returns the result value111QVariant evalScript(const QString& script, int start = Py_file_input);112113//! evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string)114//! If pycode is NULL, a python error is printed.115QVariant evalCode(PyObject* pycode);116117//! evaluates the given code in the context118void evalFile(const QString& filename);119120//! add the given \c object to the \c module as a variable with \c name (it can be removed via clearVariable)121void addObject(const QString& name, QObject* object);122123//! add the given variable to the module124void addVariable(const QString& name, const QVariant& v);125126//! remove the given variable127void removeVariable(const QString& name);128129//! get the variable with the \c name of the \c module, returns an invalid QVariant on error130QVariant getVariable(const QString& name);131132//! call the given python object (in the scope of the current object), returns the result converted to a QVariant133QVariant call(const QString& callable, const QVariantList& args);134135136protected:137138void setObject(PyObject* o) {139if (o != _object) {140if (_object) Py_DECREF(_object);141_object = o;142if (_object) Py_INCREF(_object);143}144}145146private:147PyObject* _object;148};149150151// register it to the meta type system152Q_DECLARE_METATYPE(PythonQtObjectPtr)153154#endif155156157