File indexing completed on 2024-04-28 11:44:13
0001 /*************************************************************************** 0002 * metatype.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_METATYPE_H 0021 #define KROSS_METATYPE_H 0022 0023 #include "krossconfig.h" 0024 //#include "object.h" 0025 0026 #include <QStringList> 0027 #include <QVariant> 0028 #include <QMetaType> 0029 0030 #include <typeinfo> 0031 0032 //#include <QDate> 0033 //#include <QTime> 0034 //#include <QDateTime> 0035 0036 namespace Kross 0037 { 0038 0039 /** 0040 * Base class for metatype-implementations. 0041 */ 0042 class MetaType 0043 { 0044 public: 0045 virtual ~MetaType() {} 0046 0047 virtual int typeId() = 0; 0048 //virtual QObject* toObject() = 0; 0049 //virtual QVariant toVariant() = 0; 0050 virtual void *toVoidStar() = 0; 0051 }; 0052 0053 /** 0054 * Metatypes which are registered in the QMetaType system. 0055 */ 0056 template<typename METATYPE> 0057 class MetaTypeImpl : public MetaType 0058 { 0059 public: 0060 MetaTypeImpl(const METATYPE &v) : m_variant(v) 0061 { 0062 #ifdef KROSS_METATYPE_DEBUG 0063 krossdebug(QString("MetaTypeImpl<METATYPE> Ctor typeid=%1 typename=%2").arg(qMetaTypeId<METATYPE>()).arg(typeid(METATYPE).name())); 0064 #endif 0065 } 0066 ~MetaTypeImpl() override 0067 { 0068 #ifdef KROSS_METATYPE_DEBUG 0069 krossdebug(QString("MetaTypeImpl<METATYPE> Dtor typeid=%1 typename=%2").arg(qMetaTypeId<METATYPE>()).arg(typeid(METATYPE).name())); 0070 #endif 0071 } 0072 0073 int typeId() override 0074 { 0075 return qMetaTypeId<METATYPE>(); 0076 } 0077 //virtual QVariant toVariant() { return QVariant(typeId(), m_variant); } 0078 void *toVoidStar() override 0079 { 0080 return static_cast<void *>(&m_variant); 0081 } 0082 0083 private: 0084 METATYPE m_variant; 0085 }; 0086 0087 /** 0088 * Metatypes which are listened in QVariant::Type. 0089 */ 0090 template<typename VARIANTTYPE> 0091 class MetaTypeVariant : public MetaType 0092 { 0093 public: 0094 MetaTypeVariant(const VARIANTTYPE &v) : m_value(v) 0095 { 0096 #ifdef KROSS_METATYPE_DEBUG 0097 krossdebug(QString("MetaTypeVariant<VARIANTTYPE> Ctor value=%1 typename=%2").arg(QVariant::fromValue(m_value).toString()).arg(QVariant::fromValue(m_value).typeName())); 0098 #endif 0099 } 0100 ~MetaTypeVariant() override 0101 { 0102 #ifdef KROSS_METATYPE_DEBUG 0103 krossdebug(QString("MetaTypeVariant<VARIANTTYPE> Dtor value=%1 typename=%2").arg(QVariant::fromValue(m_value).toString()).arg(QVariant::fromValue(m_value).typeName())); 0104 #endif 0105 } 0106 0107 int typeId() override 0108 { 0109 return QVariant::fromValue(m_value).type(); 0110 } 0111 //virtual QVariant toVariant() { return QVariant::fromValue(m_value); } 0112 void *toVoidStar() override 0113 { 0114 return static_cast<void *>(&m_value); 0115 } 0116 0117 private: 0118 VARIANTTYPE m_value; 0119 }; 0120 0121 template<> 0122 class MetaTypeVariant<QVariant> : public MetaType 0123 { 0124 public: 0125 MetaTypeVariant(const QVariant &v) : m_value(v) 0126 { 0127 #ifdef KROSS_METATYPE_DEBUG 0128 krossdebug(QString("MetaTypeVariant<QVariant> Ctor value=%1 typename=%2").arg(QVariant::fromValue(m_value).toString()).arg(QVariant::fromValue(m_value).typeName())); 0129 #endif 0130 } 0131 0132 ~MetaTypeVariant() override 0133 { 0134 #ifdef KROSS_METATYPE_DEBUG 0135 krossdebug(QString("MetaTypeVariant<QVariant> Dtor value=%1 typename=%2").arg(QVariant::fromValue(m_value).toString()).arg(QVariant::fromValue(m_value).typeName())); 0136 #endif 0137 } 0138 0139 int typeId() override 0140 { 0141 return QVariant::fromValue(m_value).type(); 0142 } 0143 0144 void *toVoidStar() override 0145 { 0146 return m_value.data(); 0147 } 0148 0149 private: 0150 QVariant m_value; 0151 }; 0152 0153 /** 0154 * Metatype for generic VoidStar pointers. 0155 */ 0156 class MetaTypeVoidStar : public MetaType 0157 { 0158 public: 0159 MetaTypeVoidStar(int typeId, void *ptr, bool owner) : m_typeId(typeId), m_ptr(ptr), m_owner(owner) 0160 { 0161 #ifdef KROSS_METATYPE_DEBUG 0162 krossdebug(QString("MetaTypeVoidStar Ctor typeid=%1 typename=%2 owner=%3").arg(m_typeId).arg(typeid(m_ptr).name()).arg(m_owner)); 0163 #endif 0164 } 0165 ~MetaTypeVoidStar() override 0166 { 0167 #ifdef KROSS_METATYPE_DEBUG 0168 krossdebug(QString("MetaTypeVoidStar Ctor typeid=%1 typename=%2 owner=%3").arg(m_typeId).arg(typeid(m_ptr).name()).arg(m_owner)); 0169 #endif 0170 if (m_owner) { 0171 QMetaType::destroy(m_typeId, m_ptr); 0172 } 0173 } 0174 int typeId() override 0175 { 0176 return m_typeId; 0177 } 0178 void *toVoidStar() override 0179 { 0180 return static_cast<void *>(&m_ptr); 0181 } 0182 0183 private: 0184 int m_typeId; 0185 void *m_ptr; 0186 bool m_owner; 0187 }; 0188 0189 /** 0190 * Base class for metatype-handlers as used returned by 0191 * the Kross::Manager::metaTypeHandler() method. 0192 * 0193 * \since 4.2 0194 */ 0195 class KROSSCORE_EXPORT MetaTypeHandler 0196 { 0197 public: 0198 typedef QVariant(FunctionPtr)(void *); 0199 typedef QVariant(FunctionPtr2)(MetaTypeHandler *handler, void *); 0200 0201 explicit MetaTypeHandler() : m_func1(nullptr), m_func2(nullptr) {} 0202 explicit MetaTypeHandler(FunctionPtr *func) : m_func1(func), m_func2(nullptr) {} 0203 explicit MetaTypeHandler(FunctionPtr2 *func) : m_func1(nullptr), m_func2(func) {} 0204 virtual ~MetaTypeHandler() {} 0205 0206 /** 0207 * This got called by the scripting-backend if the type-handler 0208 * is called to translate a void-star pointer to a QVariant. 0209 */ 0210 virtual QVariant callHandler(void *ptr) 0211 { 0212 return m_func1 ? m_func1(ptr) : m_func2 ? m_func2(this, ptr) : QVariant(); 0213 } 0214 0215 private: 0216 FunctionPtr *m_func1; 0217 FunctionPtr2 *m_func2; 0218 }; 0219 } 0220 0221 #endif