Kross

metatype.h
1 /***************************************************************************
2  * metatype.h
3  * This file is part of the KDE project
4  * copyright (C)2004-2006 by Sebastian Sauer ([email protected])
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  ***************************************************************************/
19 
20 #ifndef KROSS_METATYPE_H
21 #define KROSS_METATYPE_H
22 
23 #include "krossconfig.h"
24 //#include "object.h"
25 
26 #include <QStringList>
27 #include <QVariant>
28 #include <QMetaType>
29 
30 #include <typeinfo>
31 
32 //#include <QDate>
33 //#include <QTime>
34 //#include <QDateTime>
35 
36 namespace Kross
37 {
38 
39 /**
40  * Base class for metatype-implementations.
41  */
42 class MetaType
43 {
44 public:
45  virtual ~MetaType() {}
46 
47  virtual int typeId() = 0;
48  //virtual QObject* toObject() = 0;
49  //virtual QVariant toVariant() = 0;
50  virtual void *toVoidStar() = 0;
51 };
52 
53 /**
54  * Metatypes which are registered in the QMetaType system.
55  */
56 template<typename METATYPE>
57 class MetaTypeImpl : public MetaType
58 {
59 public:
60  MetaTypeImpl(const METATYPE &v) : m_variant(v)
61  {
62 #ifdef KROSS_METATYPE_DEBUG
63  krossdebug(QString("MetaTypeImpl<METATYPE> Ctor typeid=%1 typename=%2").arg(qMetaTypeId<METATYPE>()).arg(typeid(METATYPE).name()));
64 #endif
65  }
66  ~MetaTypeImpl() override
67  {
68 #ifdef KROSS_METATYPE_DEBUG
69  krossdebug(QString("MetaTypeImpl<METATYPE> Dtor typeid=%1 typename=%2").arg(qMetaTypeId<METATYPE>()).arg(typeid(METATYPE).name()));
70 #endif
71  }
72 
73  int typeId() override
74  {
75  return qMetaTypeId<METATYPE>();
76  }
77  //virtual QVariant toVariant() { return QVariant(typeId(), m_variant); }
78  void *toVoidStar() override
79  {
80  return static_cast<void *>(&m_variant);
81  }
82 
83 private:
84  METATYPE m_variant;
85 };
86 
87 /**
88  * Metatypes which are listened in QVariant::Type.
89  */
90 template<typename VARIANTTYPE>
91 class MetaTypeVariant : public MetaType
92 {
93 public:
94  MetaTypeVariant(const VARIANTTYPE &v) : m_value(v)
95  {
96 #ifdef KROSS_METATYPE_DEBUG
97  krossdebug(QString("MetaTypeVariant<VARIANTTYPE> Ctor value=%1 typename=%2").arg(QVariant::fromValue(m_value).toString()).arg(QVariant::fromValue(m_value).typeName()));
98 #endif
99  }
100  ~MetaTypeVariant() override
101  {
102 #ifdef KROSS_METATYPE_DEBUG
103  krossdebug(QString("MetaTypeVariant<VARIANTTYPE> Dtor value=%1 typename=%2").arg(QVariant::fromValue(m_value).toString()).arg(QVariant::fromValue(m_value).typeName()));
104 #endif
105  }
106 
107  int typeId() override
108  {
109  return QVariant::fromValue(m_value).type();
110  }
111  //virtual QVariant toVariant() { return QVariant::fromValue(m_value); }
112  void *toVoidStar() override
113  {
114  return static_cast<void *>(&m_value);
115  }
116 
117 private:
118  VARIANTTYPE m_value;
119 };
120 
121 template<>
122 class MetaTypeVariant<QVariant> : public MetaType
123 {
124 public:
125  MetaTypeVariant(const QVariant &v) : m_value(v)
126  {
127 #ifdef KROSS_METATYPE_DEBUG
128  krossdebug(QString("MetaTypeVariant<QVariant> Ctor value=%1 typename=%2").arg(QVariant::fromValue(m_value).toString()).arg(QVariant::fromValue(m_value).typeName()));
129 #endif
130  }
131 
132  ~MetaTypeVariant() override
133  {
134 #ifdef KROSS_METATYPE_DEBUG
135  krossdebug(QString("MetaTypeVariant<QVariant> Dtor value=%1 typename=%2").arg(QVariant::fromValue(m_value).toString()).arg(QVariant::fromValue(m_value).typeName()));
136 #endif
137  }
138 
139  int typeId() override
140  {
141  return QVariant::fromValue(m_value).type();
142  }
143 
144  void *toVoidStar() override
145  {
146  return m_value.data();
147  }
148 
149 private:
150  QVariant m_value;
151 };
152 
153 /**
154  * Metatype for generic VoidStar pointers.
155  */
157 {
158 public:
159  MetaTypeVoidStar(int typeId, void *ptr, bool owner) : m_typeId(typeId), m_ptr(ptr), m_owner(owner)
160  {
161 #ifdef KROSS_METATYPE_DEBUG
162  krossdebug(QString("MetaTypeVoidStar Ctor typeid=%1 typename=%2 owner=%3").arg(m_typeId).arg(typeid(m_ptr).name()).arg(m_owner));
163 #endif
164  }
165  ~MetaTypeVoidStar() override
166  {
167 #ifdef KROSS_METATYPE_DEBUG
168  krossdebug(QString("MetaTypeVoidStar Ctor typeid=%1 typename=%2 owner=%3").arg(m_typeId).arg(typeid(m_ptr).name()).arg(m_owner));
169 #endif
170  if (m_owner) {
171  QMetaType::destroy(m_typeId, m_ptr);
172  }
173  }
174  int typeId() override
175  {
176  return m_typeId;
177  }
178  void *toVoidStar() override
179  {
180  return static_cast<void *>(&m_ptr);
181  }
182 
183 private:
184  int m_typeId;
185  void *m_ptr;
186  bool m_owner;
187 };
188 
189 /**
190  * Base class for metatype-handlers as used returned by
191  * the Kross::Manager::metaTypeHandler() method.
192  *
193  * \since 4.2
194  */
195 class KROSSCORE_EXPORT MetaTypeHandler
196 {
197 public:
198  typedef QVariant(FunctionPtr)(void *);
199  typedef QVariant(FunctionPtr2)(MetaTypeHandler *handler, void *);
200 
201  explicit MetaTypeHandler() : m_func1(nullptr), m_func2(nullptr) {}
202  explicit MetaTypeHandler(FunctionPtr *func) : m_func1(func), m_func2(nullptr) {}
203  explicit MetaTypeHandler(FunctionPtr2 *func) : m_func1(nullptr), m_func2(func) {}
204  virtual ~MetaTypeHandler() {}
205 
206  /**
207  * This got called by the scripting-backend if the type-handler
208  * is called to translate a void-star pointer to a QVariant.
209  */
210  virtual QVariant callHandler(void *ptr)
211  {
212  return m_func1 ? m_func1(ptr) : m_func2 ? m_func2(this, ptr) : QVariant();
213  }
214 
215 private:
216  FunctionPtr *m_func1;
217  FunctionPtr2 *m_func2;
218 };
219 }
220 
221 #endif
Definition: action.cpp:36
Metatypes which are listened in QVariant::Type.
Definition: metatype.h:91
const QLatin1String name
virtual QVariant callHandler(void *ptr)
This got called by the scripting-backend if the type-handler is called to translate a void-star point...
Definition: metatype.h:210
Metatype for generic VoidStar pointers.
Definition: metatype.h:156
Base class for metatype-implementations.
Definition: metatype.h:42
Metatypes which are registered in the QMetaType system.
Definition: metatype.h:57
QVariant fromValue(const T &value)
void destroy(int type, void *data)
Base class for metatype-handlers as used returned by the Kross::Manager::metaTypeHandler() method...
Definition: metatype.h:195
QVariant::Type type() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 22:51:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.