Mercurial > projects > qtd
comparison qt/d2/qt/Signal.d @ 284:1f6923c8cba0
consistent emit syntax.
author | eldar |
---|---|
date | Fri, 16 Oct 2009 12:23:12 +0000 |
parents | 256ab6cb8e85 |
children | f9559a957be9 |
comparison
equal
deleted
inserted
replaced
283:b61a413fc2f5 | 284:1f6923c8cba0 |
---|---|
779 foreach(ref c; connections) | 779 foreach(ref c; connections) |
780 c.free; | 780 c.free; |
781 } | 781 } |
782 } | 782 } |
783 | 783 |
784 //TODO: this could be avoided if named mixins didn't suck. | |
785 public struct SignalOps(int sigId, A...) | |
786 { | |
787 private SignalHandler sh; | |
788 enum { signalId = sigId } | |
789 | |
790 void emit(A args) | |
791 { | |
792 sh.emit(signalId, args); | |
793 } | |
794 | |
795 debug size_t slotCount() | |
796 { | |
797 return sh.slotCount(signalId); | |
798 } | |
799 } | |
800 | |
801 public template SignalHandlerOps() | 784 public template SignalHandlerOps() |
802 { | 785 { |
803 static assert (is(typeof(this.signalHandler)), | 786 static assert (is(typeof(this.signalHandler)), |
804 "SignalHandlerOps is already instantiated in " ~ typeof(this).stringof ~ " or one of its base classes"); | 787 "SignalHandlerOps is already instantiated in " ~ typeof(this).stringof ~ " or one of its base classes"); |
805 | 788 |
1021 static if (is(result == void)) | 1004 static if (is(result == void)) |
1022 static assert(0, "Signal " ~ name ~ " was not found."); | 1005 static assert(0, "Signal " ~ name ~ " was not found."); |
1023 } | 1006 } |
1024 } | 1007 } |
1025 | 1008 |
1009 string __toString(long v) | |
1010 { | |
1011 if (v == 0) | |
1012 return "0"; | |
1013 | |
1014 string ret; | |
1015 | |
1016 bool neg; | |
1017 if (v < 0) | |
1018 { | |
1019 neg = true; | |
1020 v = -v; | |
1021 } | |
1022 | |
1023 while (v != 0) | |
1024 { | |
1025 ret = cast(char)(v % 10 + '0') ~ ret; | |
1026 v = cast(long)(v / 10); | |
1027 } | |
1028 | |
1029 if (neg) | |
1030 ret = "-" ~ ret; | |
1031 | |
1032 return ret; | |
1033 } | |
1034 | |
1035 public string SignalEmitter(A...)(SignalType signalType, string name, int index) | |
1036 { | |
1037 string fullArgs, args; | |
1038 static if (A.length) | |
1039 { | |
1040 fullArgs = A[0].stringof ~ " a0"; | |
1041 args = ", a0"; | |
1042 foreach(i, _; A[1..$]) | |
1043 { | |
1044 fullArgs ~= ", " ~ A[i+1].stringof ~ " a" ~ __toString(i+1); | |
1045 args ~= ", a" ~ __toString(i+1); | |
1046 } | |
1047 } | |
1048 string attribute; | |
1049 string sigName = name; | |
1050 if (signalType == SignalType.BindQtSignal) | |
1051 name ~= "_emit"; | |
1052 else | |
1053 attribute = "protected "; | |
1054 string str = attribute ~ "void " ~ name ~ "(" ~ fullArgs ~ ")" ~ | |
1055 "{ this.signalHandler.emit(" ~ __toString(index) ~ args ~ "); }"; | |
1056 return str; | |
1057 } | |
1058 | |
1026 /** ---------------- */ | 1059 /** ---------------- */ |
1027 | 1060 |
1028 | 1061 |
1029 /** | 1062 /** |
1030 Examples: | 1063 Examples: |
1056 } | 1089 } |
1057 } | 1090 } |
1058 } | 1091 } |
1059 ---- | 1092 ---- |
1060 */ | 1093 */ |
1094 | |
1095 enum SignalType | |
1096 { | |
1097 BindQtSignal, | |
1098 NewSignal | |
1099 } | |
1100 | |
1101 template BindQtSignal(string name, A...) | |
1102 { | |
1103 mixin SignalImpl!(0, name, SignalType.BindQtSignal, A); | |
1104 } | |
1105 | |
1061 template Signal(string name, A...) | 1106 template Signal(string name, A...) |
1062 { | 1107 { |
1063 mixin SignalImpl!(0, name, A); | 1108 mixin SignalImpl!(0, name, SignalType.NewSignal, A); |
1064 } | 1109 } |
1065 | 1110 |
1066 template SignalImpl(int index, string name, A...) | 1111 template SignalImpl(int index, string name, SignalType signalType, A...) |
1067 { | 1112 { |
1068 static if (is(typeof(mixin(typeof(this).stringof ~ ".__signal" ~ ToString!(index))))) | 1113 static if (is(typeof(mixin(typeof(this).stringof ~ ".__signal" ~ ToString!(index))))) |
1069 mixin SignalImpl!(index + 1, name, A); | 1114 mixin SignalImpl!(index + 1, name, signalType, A); |
1070 else | 1115 else |
1071 { | 1116 { |
1072 // mixed-in once | 1117 // mixed-in once |
1073 static if (!is(typeof(this.signalHandler))) | 1118 static if (!is(typeof(this.signalHandler))) |
1074 { | |
1075 mixin SignalHandlerOps; | 1119 mixin SignalHandlerOps; |
1076 } | 1120 |
1121 mixin (SignalEmitter!(A)(signalType, name, index)); | |
1077 mixin("public alias TypeTuple!(\"" ~ name ~ "\", index, A) __signal" ~ ToString!(index) ~ ";"); | 1122 mixin("public alias TypeTuple!(\"" ~ name ~ "\", index, A) __signal" ~ ToString!(index) ~ ";"); |
1078 mixin("SignalOps!(" ~ ToString!(index) ~ ", A) " ~ name ~ "(){ return SignalOps!(" | |
1079 ~ ToString!(index) ~ ", A)(signalHandler); }"); | |
1080 } | 1123 } |
1081 } | 1124 } |
1082 | 1125 |
1083 extern(C) alias void function(void*) SlotConnector; | 1126 extern(C) alias void function(void*) SlotConnector; |
1084 | 1127 |