Kross
metatype.h
Go to the documentation of this file.
00001 /*************************************************************************** 00002 * metatype.h 00003 * This file is part of the KDE project 00004 * copyright (C)2004-2006 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_METATYPE_H 00021 #define KROSS_METATYPE_H 00022 00023 #include "krossconfig.h" 00024 //#include "object.h" 00025 00026 00027 #include <QtCore/QStringList> 00028 #include <QtCore/QVariant> 00029 #include <QtCore/QMetaType> 00030 00031 #include <typeinfo> 00032 00033 //#include <QDate> 00034 //#include <QTime> 00035 //#include <QDateTime> 00036 00037 namespace Kross { 00038 00042 class MetaType 00043 { 00044 public: 00045 virtual ~MetaType() {} 00046 00047 virtual int typeId() = 0; 00048 //virtual QObject* toObject() = 0; 00049 //virtual QVariant toVariant() = 0; 00050 virtual void* toVoidStar() = 0; 00051 }; 00052 00056 template<typename METATYPE> 00057 class MetaTypeImpl : public MetaType 00058 { 00059 public: 00060 MetaTypeImpl(const METATYPE& v) : m_variant(v) { 00061 #ifdef KROSS_METATYPE_DEBUG 00062 krossdebug( QString("MetaTypeImpl<METATYPE> Ctor typeid=%1 typename=%2").arg(qMetaTypeId<METATYPE>()).arg(typeid(METATYPE).name()) ); 00063 #endif 00064 } 00065 virtual ~MetaTypeImpl() { 00066 #ifdef KROSS_METATYPE_DEBUG 00067 krossdebug( QString("MetaTypeImpl<METATYPE> Dtor typeid=%1 typename=%2").arg(qMetaTypeId<METATYPE>()).arg(typeid(METATYPE).name()) ); 00068 #endif 00069 } 00070 00071 virtual int typeId() { return qMetaTypeId<METATYPE>(); } 00072 //virtual QVariant toVariant() { return QVariant(typeId(), m_variant); } 00073 virtual void* toVoidStar() { return (void*) &m_variant; } 00074 00075 private: 00076 METATYPE m_variant; 00077 }; 00078 00082 template<typename VARIANTTYPE> 00083 class MetaTypeVariant : public MetaType 00084 { 00085 public: 00086 MetaTypeVariant(const VARIANTTYPE& v) : m_value(v) { 00087 #ifdef KROSS_METATYPE_DEBUG 00088 krossdebug( QString("MetaTypeVariant<VARIANTTYPE> Ctor value=%1 typename=%2").arg(qVariantFromValue(m_value).toString()).arg(qVariantFromValue(m_value).typeName()) ); 00089 #endif 00090 } 00091 virtual ~MetaTypeVariant() { 00092 #ifdef KROSS_METATYPE_DEBUG 00093 krossdebug( QString("MetaTypeVariant<VARIANTTYPE> Dtor value=%1 typename=%2").arg(qVariantFromValue(m_value).toString()).arg(qVariantFromValue(m_value).typeName()) ); 00094 #endif 00095 } 00096 00097 virtual int typeId() { return qVariantFromValue(m_value).type(); } 00098 //virtual QVariant toVariant() { return qVariantFromValue(m_value); } 00099 virtual void* toVoidStar() { return (void*) &m_value; } 00100 00101 private: 00102 VARIANTTYPE m_value; 00103 }; 00104 00108 class MetaTypeVoidStar : public MetaType 00109 { 00110 public: 00111 MetaTypeVoidStar(int typeId, void* ptr, bool owner) : m_typeId(typeId), m_ptr(ptr), m_owner(owner) { 00112 #ifdef KROSS_METATYPE_DEBUG 00113 krossdebug( QString("MetaTypeVoidStar Ctor typeid=%1 typename=%2 owner=%3").arg(m_typeId).arg(typeid(m_ptr).name()).arg(m_owner) ); 00114 #endif 00115 } 00116 virtual ~MetaTypeVoidStar() { 00117 #ifdef KROSS_METATYPE_DEBUG 00118 krossdebug( QString("MetaTypeVoidStar Ctor typeid=%1 typename=%2 owner=%3").arg(m_typeId).arg(typeid(m_ptr).name()).arg(m_owner) ); 00119 #endif 00120 if( m_owner ) 00121 QMetaType::destroy(m_typeId, m_ptr); 00122 } 00123 virtual int typeId() { return m_typeId; } 00124 virtual void* toVoidStar() { return (void*) &m_ptr; /*return m_ptr;*/ } 00125 00126 private: 00127 int m_typeId; 00128 void* m_ptr; 00129 bool m_owner; 00130 }; 00131 00138 class KROSSCORE_EXPORT MetaTypeHandler 00139 { 00140 public: 00141 typedef QVariant (FunctionPtr) (void*); 00142 typedef QVariant (FunctionPtr2) (MetaTypeHandler* handler, void*); 00143 00144 explicit MetaTypeHandler() : m_func1(0), m_func2(0) {} 00145 explicit MetaTypeHandler(FunctionPtr *func) : m_func1(func), m_func2(0) {} 00146 explicit MetaTypeHandler(FunctionPtr2 *func) : m_func1(0), m_func2(func) {} 00147 virtual ~MetaTypeHandler() {} 00148 00153 virtual QVariant callHandler(void* ptr) { 00154 return m_func1 ? m_func1(ptr) : m_func2 ? m_func2(this, ptr) : QVariant(); 00155 } 00156 00157 private: 00158 FunctionPtr *m_func1; 00159 FunctionPtr2 *m_func2; 00160 }; 00161 } 00162 00163 #endif
KDE 4.6 API Reference