Mercurial > projects > qtd
comparison qt/core/QMetaObject.d @ 259:515d6e1c7b10 lifetime
another iteration
author | maxter |
---|---|
date | Thu, 17 Sep 2009 16:28:41 +0000 |
parents | 073b9153ed8a |
children | b5773ccab07d |
comparison
equal
deleted
inserted
replaced
258:1da8870e9a62 | 259:515d6e1c7b10 |
---|---|
2 | 2 |
3 import qt.QGlobal; | 3 import qt.QGlobal; |
4 import qt.core.QObject; | 4 import qt.core.QObject; |
5 import qt.QtdObject; | 5 import qt.QtdObject; |
6 | 6 |
7 final class QMetaObject | 7 /++ |
8 Meta-object for QObject classes. | |
9 +/ | |
10 final class QMetaObject : MetaObject | |
8 { | 11 { |
9 private | 12 alias typeof(this) This; |
10 { | |
11 void* _nativeId; | |
12 QMetaObject _base; // super class | |
13 QMetaObject _firstDerived; // head of the linked list of derived classes | |
14 QMetaObject _next; // next sibling on this derivation level | |
15 ClassInfo _classInfo; | |
16 | |
17 QObject function(void* nativeId) _createWrapper; | |
18 } | |
19 | 13 |
20 private void addDerived(QMetaObject mo) | 14 private void* _nativeId; |
21 { | |
22 mo._next = _firstDerived; | |
23 _firstDerived = mo; | |
24 } | |
25 | 15 |
26 // NOTE: construction is split between this non-templated constructor and 'construct' function below. | |
27 this(void* nativeId, QMetaObject base) | 16 this(void* nativeId, QMetaObject base) |
28 { | 17 { |
29 _nativeId = nativeId; | 18 _nativeId = nativeId; |
30 if (base) | 19 super(base); |
31 { | 20 } |
32 base.addDerived(this); | |
33 _base = base; | |
34 } | |
35 } | |
36 | |
37 // TODO: remove when D acquires templated constructors | |
38 void construct(T : QObject, Concrete = T)() | |
39 { | |
40 _classInfo = T.classinfo; | |
41 | |
42 _createWrapper = function QObject(void* nativeId) { | |
43 // COMPILER BUG: cast is should not be needed | |
44 auto obj = new Concrete(nativeId, cast(QtdObjectFlags)(QtdObjectFlags.nativeOwnership | QtdObjectFlags.dynamicEntity)); | |
45 // TODO: Probably this should be a virtual call from T's constructor | |
46 T.__createEntity(nativeId, cast(void*)obj); | |
47 return obj; | |
48 }; | |
49 } | |
50 | |
51 /++ | |
52 +/ | |
53 QMetaObject base() | |
54 { | |
55 return _base; | |
56 } | |
57 | 21 |
58 /++ | 22 /++ |
59 +/ | 23 +/ |
60 void* nativeId() | 24 void* nativeId() |
61 { | 25 { |
62 return _nativeId; | 26 return _nativeId; |
63 } | 27 } |
64 | |
65 /++ | |
66 +/ | |
67 ClassInfo classInfo() | |
68 { | |
69 return _classInfo; | |
70 } | |
71 | 28 |
72 private QMetaObject lookupDerived(void*[] moIds) | 29 private QMetaObject lookupDerived(void*[] moIds) |
73 { | 30 { |
74 assert (moIds.length >= 1); | 31 assert (moIds.length >= 1); |
75 | 32 |
76 for (auto mo = _firstDerived; mo !is null; mo = mo._next) | 33 for (auto mo = static_cast!(This)(firstDerived); mo !is null; mo = static_cast!(This)(mo.next)) |
77 { | 34 { |
78 if (mo._nativeId == moIds[0]) | 35 if (mo._nativeId == moIds[0]) |
79 { | 36 { |
80 if (moIds.length == 1) // exact match found | 37 if (moIds.length == 1) // exact match found |
81 return mo; | 38 return mo; |
87 // no initialized wrapper that matches the native object. | 44 // no initialized wrapper that matches the native object. |
88 // use the base class wrapper | 45 // use the base class wrapper |
89 return this; | 46 return this; |
90 } | 47 } |
91 | 48 |
92 QObject getObject(void* nativeObjId) | 49 QObject wrap(void* nativeObjId, QtdObjectFlags flags = QtdObjectFlags.none) |
93 { | 50 { |
94 QObject result; | 51 QObject result; |
95 | 52 |
96 if (nativeObjId) | 53 if (nativeObjId) |
97 { | 54 { |
98 result = cast(QObject)qtd_get_d_qobject(nativeObjId); | 55 result = cast(QObject)qtd_get_d_qobject(nativeObjId); |
99 if (!result) | 56 if (!result) |
100 { | 57 { |
101 auto moId = qtd_QObject_metaObject(nativeObjId); | 58 auto moId = qtd_QObject_metaObject(nativeObjId); |
102 if (_nativeId == moId) | 59 if (_nativeId == moId) |
103 result = _createWrapper(nativeObjId); | 60 result = _createWrapper(nativeObjId, flags); |
104 else | 61 else |
105 { | 62 { |
106 // get native metaobjects for the entire derivation lattice | 63 // get native metaobjects for the entire derivation lattice |
107 // up to, but not including, the current metaobject. | 64 // up to, but not including, the current metaobject. |
108 size_t moCount = 1; | 65 size_t moCount = 1; |
109 | 66 |
110 for (void* tmp = moId;;) | 67 for (void* tmp = moId;;) |
111 { | 68 { |
112 tmp = qtd_QMetaObject_superClass(tmp); | 69 tmp = qtd_QMetaObject_superClass(tmp); |
113 assert(tmp); | 70 if (!tmp) |
71 return null; | |
72 | |
114 if (tmp == _nativeId) | 73 if (tmp == _nativeId) |
115 break; | 74 break; |
116 moCount++; | 75 moCount++; |
117 } | 76 } |
118 | 77 |
120 | 79 |
121 moIds[--moCount] = moId; | 80 moIds[--moCount] = moId; |
122 while (moCount > 0) | 81 while (moCount > 0) |
123 moIds[--moCount] = moId = qtd_QMetaObject_superClass(moId); | 82 moIds[--moCount] = moId = qtd_QMetaObject_superClass(moId); |
124 | 83 |
125 result = lookupDerived(moIds)._createWrapper(nativeObjId); | 84 result = lookupDerived(moIds)._createWrapper(nativeObjId, flags); |
126 } | 85 } |
127 } | 86 } |
128 } | 87 } |
129 | 88 |
130 return result; | 89 return result; |