comparison qt/d2/qt/Signal.d @ 320:5c6455c4889b signals

remove rubbish
author eldar_ins@eldar-laptop
date Fri, 25 Dec 2009 22:08:25 +0500
parents 894d40eb89b6
children d458ed66e871
comparison
equal deleted inserted replaced
319:894d40eb89b6 320:5c6455c4889b
28 std.conv, 28 std.conv,
29 std.string, 29 std.string,
30 std.metastrings; 30 std.metastrings;
31 31
32 32
33 /* returns name, arguments or tuple of the function depending on type parameter
34 foo(int, float)
35 _Name: "foo"
36 _Tuple: "(int, float)"
37 _Args: "int, float"
38 */
39 enum {_Name, _Tuple, _Args}
40 string getFunc(int type)(string fullName)
41 {
42 int pos = 0;
43 foreach(i, c; fullName)
44 if (c == '(')
45 static if (type == _Tuple)
46 return fullName[i..$];
47 else static if (type == _Name)
48 return fullName[0..i];
49 else static if (type == _Args)
50 for(int j = fullName.length-1; ; j--)
51 if(fullName[j] == ')')
52 return fullName[i+1 .. j];
53 return null;
54 }
55
56 /** The beast that takes string representation of function arguments 33 /** The beast that takes string representation of function arguments
57 * and returns an array of default values it doesn't check if arguments 34 * and returns an array of default values it doesn't check if arguments
58 * without default values follow the arguments with default values for 35 * without default values follow the arguments with default values for
59 * simplicity. It is done by mixing in an delegate alias. 36 * simplicity. It is done by mixing in an delegate alias.
60 */ 37 */
119 int defaultValuesLength(string[] defVals) 96 int defaultValuesLength(string[] defVals)
120 { 97 {
121 return defVals.length; 98 return defVals.length;
122 } 99 }
123 100
124 /**
125 New implementation.
126 */
127
128
129 101
130 // templates for extracting data from static meta-information of signals, slots or properties 102 // templates for extracting data from static meta-information of signals, slots or properties
131 // public alias TypeTuple!("name", index, OwnerClass, ArgTypes) __signal 103 // public alias TypeTuple!("name", index, OwnerClass, ArgTypes) __signal
132 template MetaEntryName(source...) 104 template MetaEntryName(source...)
133 { 105 {
144 { 116 {
145 alias ParameterTypeTuple!(source[1]) MetaEntryArgs; // arguments-tuple starts from the fourth position 117 alias ParameterTypeTuple!(source[1]) MetaEntryArgs; // arguments-tuple starts from the fourth position
146 } 118 }
147 119
148 template TupleWrapper(A...) { alias A at; } 120 template TupleWrapper(A...) { alias A at; }
149
150 template isDg(Dg)
151 {
152 enum isDg = is(Dg == delegate);
153 }
154
155 template isFn(Fn)
156 {
157 enum isFn = is(typeof(*Fn.init) == function);
158 }
159
160 template isFnOrDg(Dg)
161 {
162 enum isFnOrDg = isFn!(Dg) || isDg!(Dg);
163 }
164
165 string joinArgs(A...)()
166 {
167 string res = "";
168 static if(A.length)
169 {
170 res = A[0].stringof;
171 foreach(k; A[1..$])
172 res ~= "," ~ k.stringof;
173 }
174 return res;
175 }
176
177 template SlotPred(T1, T2)
178 {
179 enum SlotPred = is(T1 : T2);
180 }
181
182 template CheckSlot(alias Needle, alias Source)
183 {
184 static if(Needle.at.length <= Source.at.length)
185 enum CheckSlot = CheckArgs!(Needle, Source, SlotPred, 0).value;
186 else
187 enum CheckSlot = false;
188 }
189
190 template SignalPred(T1, T2)
191 {
192 enum SignalPred = is(T1 == T2);
193 }
194
195 template CheckSignal(alias Needle, alias Source)
196 {
197 static if(Needle.at.length == Source.at.length)
198 enum CheckSignal = CheckArgs!(Needle, Source, SignalPred, 0).value;
199 else
200 enum CheckSignal = false;
201 }
202
203 template CheckArgs(alias Needle, alias Source, alias pred, int i)
204 {
205 static if (i < Needle.at.length)
206 {
207 static if (pred!(Needle.at[i], Source.at[i]))
208 enum value = CheckArgs!(Needle, Source, pred, i + 1).value;
209 else
210 enum value = false;
211 }
212 else
213 {
214 enum value = true;
215 }
216 }
217
218 template SigByNamePred(string name, SlotArgs...)
219 {
220 template SigByNamePred(source...)
221 {
222 static if (source[0] == name) // only instantiate CheckSlot if names match
223 enum SigByNamePred = CheckSlot!(TupleWrapper!(SlotArgs), TupleWrapper!(source[2 .. $]));
224 else
225 enum SigByNamePred = false;
226 }
227 }
228
229 template SigBySignPred(string name, SigArgs...)
230 {
231 template SigBySignPred(source...)
232 {
233 static if (source[0] == name) // only instantiate CheckSignal if names match
234 enum SigBySignPred = CheckSignal!(TupleWrapper!(SigArgs), TupleWrapper!(source[2 .. $]));
235 else
236 enum SigBySignPred = false;
237 }
238 }
239
240 template ByOwner(Owner)
241 {
242 template ByOwner(source...)
243 {
244 enum ByOwner = is(MetaEntryOwner!source == Owner);
245 }
246 }
247
248 template staticSymbolName(string prefix, int id)
249 {
250 const string staticSymbolName = prefix ~ ToString!(id);
251 }
252
253 template signatureString(string name, A...)
254 {
255 const string signatureString = name ~ "(" ~ joinArgs!(A) ~ ")";
256 }
257
258 // recursive search in the static meta-information
259 template findSymbolImpl(string prefix, C, int id, alias pred)
260 {
261 static if ( is(typeof(mixin("C." ~ staticSymbolName!(prefix, id)))) )
262 {
263 mixin ("alias C." ~ staticSymbolName!(prefix, id) ~ " current;");
264 static if (pred!current)
265 alias current result;
266 else
267 alias findSymbolImpl!(prefix, C, id + 1, pred).result result;
268 }
269 else
270 {
271 alias void result;
272 }
273 }
274
275 template findSymbol(string prefix, C, alias pred)
276 {
277 alias findSymbolImpl!(prefix, C, 0, pred).result findSymbol;
278 }
279
280 template findSignal(C, string name, Receiver, SigArgs...)
281 {
282 alias TupleWrapper!(ParameterTypeTuple!Receiver) SlotArgsWr;
283 static if (SigArgs.length > 0)
284 {
285 alias findSymbol!(signalPrefix, C, SigBySignPred!(name, SigArgs)) result;
286 static if (is(result == void))
287 static assert(0, "Signal " ~ name ~ "(" ~ joinArgs!SigArgs() ~ ") was not found.");
288 else
289 static if (!CheckSlot!(SlotArgsWr, TupleWrapper!(result[2 .. $])))
290 static assert(0, "Signature of slot is incompatible with signal " ~ name ~ ".");
291 }
292 else
293 {
294 alias findSymbol!(signalPrefix, C, SigByNamePred!(name, SlotArgsWr.at)) result;
295 static if (is(result == void))
296 static assert(0, "Signal " ~ name ~ " was not found.");
297 }
298 }
299
300 // recursive search in the static meta-information
301 template findSymbolsImpl(string prefix, C, int id, alias pred)
302 {
303 static if ( is(typeof(mixin("C." ~ staticSymbolName!(prefix, id)))) )
304 {
305 mixin ("alias C." ~ staticSymbolName!(prefix, id) ~ " current;");
306 static if (pred!current) {
307 alias TupleWrapper!current subres;
308 // pragma(msg, toStringNow!id ~ " " ~ subres.stringof);
309 } else
310 alias TypeTuple!() subres;
311 alias TypeTuple!(subres, findSymbolsImpl!(prefix, C, id + 1, pred).result) result;
312 }
313 else
314 {
315 alias TypeTuple!() result;
316 }
317 }
318
319 template findSymbols(string prefix, C, alias pred)
320 {
321 alias findSymbolsImpl!(prefix, C, 0, pred).result findSymbols;
322 }
323 121
324 string convertSignalArguments(Args...)() 122 string convertSignalArguments(Args...)()
325 { 123 {
326 // void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) }; 124 // void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
327 // at least for string argument need to construct a QString value 125 // at least for string argument need to construct a QString value
386 BindQtSignal, 184 BindQtSignal,
387 NewSignal, 185 NewSignal,
388 NewSlot 186 NewSlot
389 } 187 }
390 188
391 template BindQtSignal(string fullName)
392 {
393 mixin MetaMethodImpl!(signalPrefix, 0, fullName, SignalType.BindQtSignal);
394 }
395
396 template Signal(string fullName)
397 {
398 mixin MetaMethodImpl!(signalPrefix, 0, fullName, SignalType.NewSignal);
399 }
400
401 template Slot(string fullName)
402 {
403 mixin MetaMethodImpl!(slotPrefix, 0, fullName, SignalType.NewSlot);
404 }
405
406 template SignalImpl(int index, string fullName, SignalType signalType)
407 {
408 static if (is(typeof(mixin(typeof(this).stringof ~ "." ~ signalPrefix ~ ToString!(index)))))
409 mixin SignalImpl!(index + 1, fullName, signalType);
410 else
411 {
412 // pragma(msg, "alias void delegate" ~ getFunc!_Tuple(fullName) ~ " Dg;");
413 mixin("alias void delegate" ~ getFunc!_Tuple(fullName) ~ " Dg;");
414 alias ParameterTypeTuple!(Dg) ArgTypes;
415 enum args = getFunc!_Args(fullName);
416 enum defVals = defaultValues(args);
417 enum defValsLength = defaultValuesLength(defVals);
418
419 // pragma (msg, SignalEmitter!(ArgTypes)(SignalType.NewSignal, getFunc!_Name(fullName), defVals, index));
420 mixin InsertMetaSignal!(fullName, index, defValsLength, ArgTypes);
421 // pragma (msg, ctfe_meta_signal!(ArgTypes)(fullName, index, defValsLength));
422 }
423 }
424 template MetaMethodImpl(string metaPrefix, int index, string fullName, SignalType signalType)
425 {
426 static if (is(typeof(mixin(typeof(this).stringof ~ "." ~ metaPrefix ~ toStringNow!(index)))))
427 {
428 mixin MetaMethodImpl!(metaPrefix, index + 1, fullName, signalType);
429 }
430 else
431 {
432 mixin("alias void delegate" ~ getFunc!_Tuple(fullName) ~ " Dg;");
433 alias ParameterTypeTuple!(Dg) ArgTypes;
434 enum args = getFunc!_Args(fullName);
435 enum defVals = defaultValues(args);
436 enum defValsLength = defaultValuesLength(defVals);
437
438 static if (metaPrefix == signalPrefix)
439 {
440 // calculating local index of the signal
441 static if (typeof(this).stringof == "QObject")
442 enum localIndex = index;
443 else
444 mixin ("enum localIndex = index - 1 - lastSignalIndex_" ~ (typeof(super)).stringof ~ ";");
445
446 static if (signalType == SignalType.NewSignal)
447 {
448 pragma (msg, SignalEmitter!(ArgTypes)(SignalType.NewSignal, getFunc!_Name(fullName), defVals, localIndex));
449 mixin (SignalEmitter!(ArgTypes)(SignalType.NewSignal, getFunc!_Name(fullName), defVals, localIndex));
450 }
451 }
452 mixin InsertMetaMethod!(fullName, metaPrefix, index, defValsLength, ArgTypes);
453 // pragma (msg, ctfe_meta_signal!(ArgTypes)(fullName, index, defValsLength));
454 }
455 }
456 template InsertMetaMethod(string fullName, string metaPrefix, int index, int defValsCount, ArgTypes...)
457 {
458 static if(defValsCount >= 0)
459 mixin("public alias TypeTuple!(\"" ~ getFunc!_Name(fullName) ~ "\", index, typeof(this), ArgTypes) " ~ metaPrefix ~ toStringNow!(index) ~ ";");
460 static if(defValsCount > 0)
461 mixin InsertMetaMethod!(fullName, metaPrefix, index+1, defValsCount-1, ArgTypes[0..$-1]);
462 }
463
464
465 string signature_impl(T...)(string name) 189 string signature_impl(T...)(string name)
466 { 190 {
467 string res = name ~ "("; 191 string res = name ~ "(";
468 foreach(i, _; T) 192 foreach(i, _; T)
469 { 193 {
551 template findSlots(C) 275 template findSlots(C)
552 { 276 {
553 alias findSymbols2!(C, "slot_").result findSlots; 277 alias findSymbols2!(C, "slot_").result findSlots;
554 } 278 }
555 279
556 280 /* commented out for future when we will implement default arguments
557 template metaMethods(alias func, int index, int defValsCount) 281 template metaMethods(alias func, int index, int defValsCount)
558 { 282 {
559 static if(defValsCount >= 0) { 283 static if(defValsCount >= 0) {
560 alias TupleWrapper!(func, index) current; 284 alias TupleWrapper!(func, index) current;
561 // pragma(msg, __traits(identifier, (current.at)[0]) ~ " " ~ typeof(&(current.at)[0]).stringof); 285 // pragma(msg, __traits(identifier, (current.at)[0]) ~ " " ~ typeof(&(current.at)[0]).stringof);
565 else 289 else
566 { 290 {
567 alias TypeTuple!() result; 291 alias TypeTuple!() result;
568 } 292 }
569 } 293 }
294 */
570 295
571 template toMetaEntriesImpl(int id, Methods...) 296 template toMetaEntriesImpl(int id, Methods...)
572 { 297 {
573 static if (Methods.length > id) 298 static if (Methods.length > id)
574 { 299 {
575 alias typeof(&Methods[id]) Fn; 300 alias typeof(&Methods[id]) Fn;
301 // commented out for future when we will implement default arguments
576 // enum defValsLength = 0; //ParameterTypeTuple!(Fn).length - requiredArgCount!(Methods[id])(); 302 // enum defValsLength = 0; //ParameterTypeTuple!(Fn).length - requiredArgCount!(Methods[id])();
577 // pragma(msg, __traits(identifier, Methods[id]) ~ " " ~ typeof(&Methods[id]).stringof); 303 // pragma(msg, __traits(identifier, Methods[id]) ~ " " ~ typeof(&Methods[id]).stringof);
578 // alias metaMethods!(Methods[id], 0, defValsLength).result subres; 304 // alias metaMethods!(Methods[id], 0, defValsLength).result subres;
579 alias TupleWrapper!(removePrefix(__traits(identifier, Methods[id])), typeof(&Methods[id])) subres; 305 alias TupleWrapper!(removePrefix(__traits(identifier, Methods[id])), typeof(&Methods[id])) subres;
580 alias TypeTuple!(subres, toMetaEntriesImpl!(id+1, Methods).result) result; 306 alias TypeTuple!(subres, toMetaEntriesImpl!(id+1, Methods).result) result;
587 313
588 template toMetaEntries(Methods...) 314 template toMetaEntries(Methods...)
589 { 315 {
590 alias TupleWrapper!(toMetaEntriesImpl!(0, Methods).result) toMetaEntries; 316 alias TupleWrapper!(toMetaEntriesImpl!(0, Methods).result) toMetaEntries;
591 } 317 }
592
593
594 bool printRawFuncs(T...)()
595 {
596 pragma(msg, "---Raw---");
597 foreach(i, _; T)
598 pragma(msg, __traits(identifier, T[i]) ~ " " ~ typeof(&T[i]).stringof);
599 return true;
600 }
601
602
603 bool printFuncs(alias T)()
604 {
605 pragma(msg, "---MetaEntries---");
606 alias T.at tuple;
607 enum num = tuple.length;
608 foreach(i, _; Repeat!(void, num))
609 pragma(msg, tuple[i].at[0] ~ " " ~ tuple[i].at[1].stringof);
610 // pragma(msg, typeof(&tuple[i].at[0]).stringof ~ " " ~ __toString(tuple[i].at[1]));
611 return true;
612 }