File indexing completed on 2024-04-21 15:02:58

0001 /***************************************************************************
0002  * object.h
0003  * This file is part of the KDE project
0004  * copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 #ifndef KROSS_OBJECT_H
0021 #define KROSS_OBJECT_H
0022 
0023 #include <QString>
0024 #include <QStringList>
0025 #include <QMap>
0026 #include <QVariant>
0027 #include <QSharedData>
0028 #include <QExplicitlySharedDataPointer>
0029 
0030 #include "krossconfig.h"
0031 #include "errorinterface.h"
0032 
0033 namespace Kross
0034 {
0035 
0036 /**
0037  * The class Object does provide us scripting objects like
0038  * class instances to the C++ world.
0039  *
0040  * This class implements reference counting for shared
0041  * objects. So, no need to take care of freeing objects.
0042  *
0043  * Sample that does use the \a Object functionality to
0044  * pass a Javascript classinstance to C++ code which then
0045  * calls a method the classinstance provides.
0046  * \code
0047  * class MyObject : public QObject
0048  * {
0049  *     public Q_SLOTS:
0050  *         QVariant myFunction(Kross::Object::Ptr obj) {
0051  *             QVariantList args;
0052  *             return obj->callMethod("myMethod", args);
0053  *         }
0054  * };
0055  * \endcode
0056  * \code
0057  * function MyClass(result) {
0058  *     this.myMethod = function() {
0059  *         return result;
0060  *     }
0061  * }
0062  * var myclass = new MyClass("my string");
0063  * var r = MyObject.myFunction(myclass);
0064  * print(r); // prints "my string"
0065  * \endcode
0066  *
0067  * \since 4.1
0068  */
0069 class KROSSCORE_EXPORT Object : public QSharedData, public ErrorInterface
0070 {
0071 public:
0072 
0073     /**
0074      * Shared pointer to implement reference-counting.
0075      */
0076     typedef QExplicitlySharedDataPointer<Object> Ptr;
0077 
0078     /**
0079      * Default constructor.
0080      */
0081     explicit Object();
0082 
0083     /**
0084      * Copy constructor.
0085      */
0086     Object(const Object &other);
0087 
0088     /**
0089      * Destructor.
0090      */
0091     virtual ~Object();
0092 
0093     /**
0094      * Pass a call to the object and evaluated it.
0095      *
0096      * \param name Each call has a name that says what
0097      * should be called.
0098      * \param args The optional list of arguments
0099      * passed to the call.
0100      * \return The call-result as QVariant.
0101      */
0102     virtual QVariant callMethod(const QString &name,
0103                                 const QVariantList &args = QVariantList());
0104 
0105     /**
0106      * Return a list of supported callable objects.
0107      *
0108      * \return List of supported calls.
0109      */
0110     virtual QStringList methodNames();
0111 
0112     /**
0113      * \internal used virtual hook to easier the job to keep
0114      * binary compatibility.
0115      */
0116     virtual void virtual_hook(int id, void *ptr);
0117 
0118 private:
0119     /// \internal d-pointer class.
0120     class Private;
0121     /// \internal d-pointer instance.
0122     Private *const d;
0123 };
0124 }
0125 
0126 Q_DECLARE_METATYPE(Kross::Object::Ptr)
0127 
0128 #endif
0129