pythonextension.h

00001 /***************************************************************************
00002  * pythonextension.h
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #ifndef KROSS_PYTHON_EXTENSION_H
00021 #define KROSS_PYTHON_EXTENSION_H
00022 
00023 #include "pythonconfig.h"
00024 #include "../api/object.h"
00025 #include "../api/list.h"
00026 #include "../api/dict.h"
00027 #include "../api/class.h"
00028 
00029 #include <qstring.h>
00030 #include <qstringlist.h>
00031 #include <qvaluelist.h>
00032 #include <qvaluevector.h>
00033 #include <qmap.h>
00034 #include <qvariant.h>
00035 
00036 namespace Kross { namespace Python {
00037 
00038     // Forward declaration.
00039     class PythonScript;
00040 
00041     /**
00042      * The PythonExtension is a wrapper-object to let C++ and
00043      * Python interact together.
00044      * Instances of this class are used everytime if we send
00045      * or got something to/from python.
00046      */
00047     class PythonExtension : public Py::PythonExtension<PythonExtension>
00048     {
00049             friend class PythonScript;
00050             friend class PythonObject;
00051             friend class PythonModule;
00052 
00053         public:
00054 
00055             /**
00056              * Constructor.
00057              *
00058              * \param object The \a Kross::Api::Object object
00059              *        this instance is the wrapper for.
00060              */
00061             explicit PythonExtension(Kross::Api::Object::Ptr object);
00062 
00063             /**
00064              * Destructor.
00065              */
00066             virtual ~PythonExtension();
00067 
00068             /**
00069              * Overloaded method to return the string-representation
00070              * of this object.
00071              *
00072              * \return The string representation.
00073              */
00074             virtual Py::Object str();
00075 
00076             /**
00077              * Overloaded method to return the string-representation
00078              * of the value this object has.
00079              *
00080              * \return A string representation of the value.
00081              */
00082             virtual Py::Object repr();
00083 
00084             /**
00085              * Overloaded method to handle attribute calls
00086              * from within python.
00087              *
00088              * \param name The name of the attribute that
00089              *        should be handled.
00090              * \return An \a Py::Object that could be
00091              *         a value or a callable object. Python
00092              *         will decide what to do with the
00093              *         returnvalue.
00094              */
00095             virtual Py::Object getattr(const char* name);
00096 
00097             //virtual Py::Object getattr_methods(const char* name);
00098             //virtual int setattr(const char* name, const Py::Object& value);
00099 
00100         private:
00101 
00102             /**
00103              * Converts a \a Py::Tuple into a \a Kross::Api::List .
00104              *
00105              * \param tuple The Py::Tuple to convert.
00106              * \return The to a Kross::Api::List converted Py::Tuple .
00107              */
00108             static Kross::Api::List::Ptr toObject(const Py::Tuple& tuple);
00109 
00110             /**
00111              * Converts a \a Py::List into a \a Kross::Api::List .
00112              *
00113              * \param list The Py::List to convert.
00114              * \return The to a Kross::Api::List converted Py::List .
00115              */
00116             static Kross::Api::List::Ptr toObject(const Py::List& list);
00117 
00118             /**
00119              * Converts a \a Py::Dict into a \a Kross::Api::Dict .
00120              *
00121              * \param dict The Py::Dict to convert.
00122              * \return The to a Kross::Api::Dict converted Py::Dict .
00123              */
00124             static Kross::Api::Dict::Ptr toObject(const Py::Dict& dict);
00125 
00126             /**
00127              * Converts a \a Py::Object into a \a Kross::Api::Object.
00128              *
00129              * \param object The Py::Object to convert.
00130              * \return The to a Kross::Api::Object converted Py::Object.
00131              */
00132             static Kross::Api::Object::Ptr toObject(const Py::Object& object);
00133 
00134             /**
00135              * Converts a QString to a Py::Object. If
00136              * the QString isNull() then Py::None() will
00137              * be returned.
00138              *
00139              * \param s The QString to convert.
00140              * \return The to a Py::String converted QString.
00141              */
00142             static const Py::Object toPyObject(const QString& s);
00143 
00144             /**
00145              * Converts a QStringList to a Py::List.
00146              *
00147              * \param list The QStringList to convert.
00148              * \return The to a Py::List converted QStringList.
00149              */
00150             static const Py::List toPyObject(const QStringList& list);
00151 
00152             /**
00153              * Converts a QMap to a Py::Dict.
00154              *
00155              * \param map The QMap to convert.
00156              * \return The to a Py::Dict converted QMap.
00157              */
00158             static const Py::Dict toPyObject(const QMap<QString, QVariant>& map);
00159 
00160             /**
00161              * Converts a QValueList to a Py::List.
00162              *
00163              * \param list The QValueList to convert.
00164              * \return The to a Py::List converted QValueList.
00165              */
00166             static const Py::List toPyObject(const QValueList<QVariant>& list);
00167 
00168             /**
00169              * Converts a QVariant to a Py::Object.
00170              *
00171              * \param variant The QVariant to convert.
00172              * \return The to a Py::Object converted QVariant.
00173              */
00174             static const Py::Object toPyObject(const QVariant& variant);
00175 
00176             /**
00177              * Converts a \a Kross::Api::Object to a Py::Object.
00178              *
00179              * \param object The Kross::Api::Object to convert.
00180              * \return The to a Py::Object converted Kross::Api::Object.
00181              */
00182             static const Py::Object toPyObject(Kross::Api::Object::Ptr object);
00183 
00184             /**
00185              * Converts a \a Kross::Api::List into a Py::Tuple.
00186              *
00187              * \param list The Kross::Api::List to convert.
00188              * \return The to a Py::Tuple converted Kross::Api::List.
00189              */
00190             static const Py::Tuple toPyTuple(Kross::Api::List::Ptr list);
00191 
00192             /// The \a Kross::Api::Object this PythonExtension wraps.
00193             Kross::Api::Object::Ptr m_object;
00194 
00195             /**
00196              * The proxymethod which will handle all calls to our
00197              * \a PythonExtension instance.
00198              */
00199             Py::MethodDefExt<PythonExtension>* m_proxymethod;
00200 
00201             /**
00202              * The static proxy-handler which will be used to dispatch
00203              * a call to our \a PythonExtension instance and redirect
00204              * the call to the matching method.
00205              *
00206              * \param _self_and_name_tuple A tuple containing as first
00207              *        argument a reference to our \a PythonExtension
00208              *        instance.
00209              * \param _args The optional passed arguments for the method
00210              *        which should be called.
00211              * \return The returnvalue of the methodcall.
00212              */
00213             static PyObject* proxyhandler(PyObject* _self_and_name_tuple, PyObject* _args);
00214     };
00215 
00216 }}
00217 
00218 #endif

Generated on Thu Feb 9 17:59:10 2006 for Kross by  doxygen 1.4.6