Kross
metatype.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KROSS_METATYPE_H
00021 #define KROSS_METATYPE_H
00022
00023 #include "krossconfig.h"
00024
00025
00026
00027 #include <QtCore/QStringList>
00028 #include <QtCore/QVariant>
00029 #include <QtCore/QMetaType>
00030
00031 #include <typeinfo>
00032
00033
00034
00035
00036
00037 namespace Kross {
00038
00042 class MetaType
00043 {
00044 public:
00045 virtual ~MetaType() {}
00046
00047 virtual int typeId() = 0;
00048
00049
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
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
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; }
00125
00126 private:
00127 int m_typeId;
00128 void* m_ptr;
00129 bool m_owner;
00130 };
00131
00132 }
00133
00134 #endif