File indexing completed on 2024-04-28 11:44:14

0001 /***************************************************************************
0002  * wrapperinterface.h
0003  * This file is part of the KDE project
0004  * copyright (C)2008 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_WRAPPERINTERFACE_H
0021 #define KROSS_WRAPPERINTERFACE_H
0022 
0023 #include "krossconfig.h"
0024 
0025 namespace Kross
0026 {
0027 
0028 /**
0029  * Wrapper-class used to provide handlers for custom types.
0030  *
0031  * Custom types are types other than QObject*, QWidget* or one
0032  * of the base types supported by QVariant. By using the
0033  * Kross::registerMetaTypeHandler() method such custom handlers
0034  * can be registered and used to either translate various
0035  * types to a by QVariant supported type or by providing on
0036  * the fly an own wrapper class that inherits from QObject
0037  * and does provide access to the functionality of the
0038  * wrapped custom type.
0039  *
0040  * Following sample demonstrates the usage by registering
0041  * a handler for the type "TestObject*". Once such a type
0042  * got returned by a C++ class, the handler got called. If
0043  * we return a QObject that implements the WrapperInterface,
0044  * what is not needed, then the wrappedObject() method will
0045  * be used to translate the wrapper back to the wrapped
0046  * object if a C++ function got called and the wrapper is
0047  * passed as argument.
0048  *
0049  * \code
0050  * // This is our wrapper class we are using to e.g. provide
0051  * // additional functionality on the fly or to provide access
0052  * // to a C++ type that does not inherit from QObject.
0053  * class MyWrapper : public QObject, public Kross::WrapperInterface {
0054  *     public:
0055  *         MyWrapper(QObject* obj) : QObject(obj) {}
0056  *         void* wrappedObject() const { return parent(); }
0057  * };
0058  * // This function will be called by Kross if a type named
0059  * // "TestObject*" got returned by a C++ method.
0060  * QVariant TestObjectHandler(void* ptr)
0061  * {
0062  *     TestObject* obj = static_cast<TestObject*>(ptr);
0063  *     MyWrapper* w = new MyWrapper(obj);
0064  *     QVariant r;
0065  *     r.setValue( (QObject*)w );
0066  *     return r;
0067  * }
0068  * // and somewhere else we finally register our function.
0069  * Kross::Manager::self().registerMetaTypeHandler("TestObject*", TestObjectHandler);
0070  * \endcode
0071  *
0072  * \since 4.2
0073  */
0074 class KROSSCORE_EXPORT WrapperInterface
0075 {
0076 public:
0077 
0078     /**
0079      * Destructor.
0080      */
0081     virtual ~WrapperInterface();
0082 
0083     /**
0084      * This method got called by Kross if the wrapper-instance
0085      * got passed to a C++ slot. It is recommend to return here
0086      * the wrapped instance, but you don't need to.
0087      */
0088     virtual void *wrappedObject() const = 0;
0089 
0090     //void wrapperConstructed() {}
0091     //void wrapperDestroyed() {}
0092 };
0093 
0094 }
0095 
0096 #endif