Mercurial > projects > qtd
comparison d2/qtd/meta/Runtime.d @ 364:a084e2df3776
Preparing for non-QObject meta-objects. Now meta-objects for static types can be uniformly accessed using meta!T
author | Max Samukha <maxter@maxter.com> |
---|---|
date | Fri, 11 Jun 2010 12:16:09 +0300 |
parents | 59d847a814e3 |
children | 958e8b9a89bd |
comparison
equal
deleted
inserted
replaced
363:3b0545d4d479 | 364:a084e2df3776 |
---|---|
6 module qtd.meta.Runtime; | 6 module qtd.meta.Runtime; |
7 //TODO: Probably replace switch dispatch with pointer dispatch | 7 //TODO: Probably replace switch dispatch with pointer dispatch |
8 //and leave switch dispatch only in C interface | 8 //and leave switch dispatch only in C interface |
9 | 9 |
10 import | 10 import |
11 qtd.Core, | |
11 qtd.meta.Compiletime, | 12 qtd.meta.Compiletime, |
12 | 13 |
13 std.typetuple, | 14 std.typetuple, |
14 std.conv, | 15 std.conv, |
15 std.variant, | 16 std.variant, |
85 { | 86 { |
86 super(msg); | 87 super(msg); |
87 } | 88 } |
88 } | 89 } |
89 | 90 |
90 abstract class Meta | 91 abstract class MetaBase |
91 { | 92 { |
92 alias typeof(this) This; | 93 alias typeof(this) This; |
93 | 94 |
94 string name; | 95 string name; |
95 MetaAttribute[] attributes; | 96 MetaAttribute[] attributes; |
96 Meta[] members; | 97 MetaBase[] members; |
97 | 98 |
98 template createImpl(M : This) | 99 template createImpl(M : This) |
99 { | 100 { |
100 static M createImpl(alias symbol)() | 101 static M createImpl(alias symbol)() |
101 { | 102 { |
151 this.name = name; | 152 this.name = name; |
152 options = opts; | 153 options = opts; |
153 } | 154 } |
154 } | 155 } |
155 | 156 |
156 abstract class MetaType : Meta | 157 abstract class MetaType : MetaBase |
157 { | 158 { |
158 } | 159 } |
159 | 160 |
160 abstract class MetaAggregate : MetaType | 161 abstract class MetaAggregate : MetaType |
161 { | 162 { |
162 } | 163 } |
163 | 164 |
164 class MetaClass : MetaAggregate | 165 class MetaClass : MetaAggregate |
165 { | 166 { |
166 alias typeof(this) This; | 167 alias typeof(this) This; |
168 | |
169 private: | |
170 This base_; | |
171 This firstDerived_; | |
172 This next_; | |
173 TypeInfo_Class classInfo_; | |
174 | |
175 this() {} | |
176 | |
177 public: | |
178 /** | |
179 Returns the meta-object of the base class. | |
180 */ | |
181 @property | |
182 This base() | |
183 { | |
184 return base_; | |
185 } | |
186 | |
187 /** | |
188 Returns next meta-object on this level of the derivation hierarchy. | |
189 */ | |
190 @property | |
191 This next() | |
192 { | |
193 return next_; | |
194 } | |
195 | |
196 /** | |
197 Returns meta-object of the first derived class. | |
198 */ | |
199 @property | |
200 This firstDerived() | |
201 { | |
202 return firstDerived_; | |
203 } | |
204 | |
205 /** | |
206 D class info. | |
207 */ | |
208 @property | |
209 TypeInfo_Class classInfo() | |
210 { | |
211 return classInfo_; | |
212 } | |
213 | |
214 /* internal */ alias createImpl!This create; | |
215 | |
216 /* internal */ void construct(T : Object)() | |
217 { | |
218 static if (!is(T == Object)) | |
219 { | |
220 alias BaseClassesTuple!(T)[0] Base; | |
221 base_ = meta!Base; | |
222 } | |
223 classInfo_ = T.classinfo; | |
224 } | |
225 } | |
226 | |
227 class MetaStruct : MetaAggregate | |
228 { | |
229 alias typeof(this) This; | |
167 alias createImpl!This create; | 230 alias createImpl!This create; |
168 } | 231 } |
169 | 232 |
170 class MetaStruct : MetaAggregate | |
171 { | |
172 alias typeof(this) This; | |
173 alias createImpl!This create; | |
174 } | |
175 | |
176 @property | 233 @property |
177 auto meta(alias symbol, M : Meta)() | 234 auto meta(alias symbol, M : MetaBase)() |
178 { | 235 { |
179 __gshared static M m; | 236 __gshared static M sharedM; |
180 | 237 static M m; |
181 { | |
182 lock.reader.lock; | |
183 scope(exit) | |
184 lock.reader.unlock; | |
185 if (m) | |
186 return m; | |
187 } | |
188 | |
189 lock.writer.lock; | |
190 scope(exit) | |
191 lock.writer.unlock; | |
192 | 238 |
193 if (!m) | 239 if (!m) |
194 m = M.create!symbol; | 240 { |
241 synchronized(qtdMoLock) | |
242 { | |
243 if (!sharedM) | |
244 sharedM = M.create!symbol; | |
245 } | |
246 m = sharedM; | |
247 } | |
248 | |
195 return m; | 249 return m; |
196 } | 250 } |
197 | 251 |
198 // only classes and structs for now | 252 // only classes and structs for now |
199 @property | 253 @property |
200 auto meta(T)() | 254 auto meta(T)() |
201 { | 255 { |
202 static if (is(typeof(T.staticMetaObject))) | 256 static if (is(T.Meta)) // If the type defines a meta-class - use that. |
203 return T.staticMetaObject; | 257 return meta!(T, T.Meta); |
204 else static if (is(T == class)) | 258 else static if (is(T == class)) |
205 return meta!(T, MetaClass); | 259 return meta!(T, MetaClass); |
206 else static if (is(T == struct)) | 260 else static if (is(T == struct)) |
207 return meta!(T, MetaStruct); | 261 return meta!(T, MetaStruct); |
208 else | 262 else |