changeset 284:1f6923c8cba0

consistent emit syntax.
author eldar
date Fri, 16 Oct 2009 12:23:12 +0000
parents b61a413fc2f5
children 04e20937c681
files examples/draganddrop/dropsite/droparea.d examples/opengl/hellogl/glwidget.d examples/opengl/hellogl/glwidget_d1.d generator/dgenerator.cpp qt/d1/qt/Signal.d qt/d2/qt/Signal.d
diffstat 6 files changed, 120 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
--- a/examples/draganddrop/dropsite/droparea.d	Fri Oct 16 05:32:23 2009 +0000
+++ b/examples/draganddrop/dropsite/droparea.d	Fri Oct 16 12:23:12 2009 +0000
@@ -63,7 +63,7 @@
 		setText(tr("<drop content>"));
 		setBackgroundRole(QPalette.Dark);
 
-		changed.emit(null);
+		changed(null);
 	}
 
 	mixin Signal!("changed", QMimeData);
@@ -76,7 +76,7 @@
 		setBackgroundRole(QPalette.Highlight);
 
 		event.acceptProposedAction();
-		changed.emit(event.mimeData);
+		changed(event.mimeData);
 	}
 	
 	void dragMoveEvent(QDragMoveEvent event)
--- a/examples/opengl/hellogl/glwidget.d	Fri Oct 16 05:32:23 2009 +0000
+++ b/examples/opengl/hellogl/glwidget.d	Fri Oct 16 12:23:12 2009 +0000
@@ -86,7 +86,7 @@
             normalizeAngle(&angle);
             if (angle != xRot) {
                 xRot = angle;
-                xRotationChanged.emit(angle);
+                xRotationChanged(angle);
                 updateGL();
             }
         }
@@ -96,7 +96,7 @@
             normalizeAngle(&angle);
             if (angle != yRot) {
                 yRot = angle;
-                yRotationChanged.emit(angle);
+                yRotationChanged(angle);
                 updateGL();
             }
         }
@@ -106,7 +106,7 @@
             normalizeAngle(&angle);
             if (angle != zRot) {
                 zRot = angle;
-                zRotationChanged.emit(angle);
+                zRotationChanged(angle);
                 updateGL();
             }
         }
--- a/examples/opengl/hellogl/glwidget_d1.d	Fri Oct 16 05:32:23 2009 +0000
+++ b/examples/opengl/hellogl/glwidget_d1.d	Fri Oct 16 12:23:12 2009 +0000
@@ -86,7 +86,7 @@
             normalizeAngle(&angle);
             if (angle != xRot) {
                 xRot = angle;
-                xRotationChanged.emit(angle);
+                xRotationChanged(angle);
                 updateGL();
             }
         }
@@ -96,7 +96,7 @@
             normalizeAngle(&angle);
             if (angle != yRot) {
                 yRot = angle;
-                yRotationChanged.emit(angle);
+                yRotationChanged(angle);
                 updateGL();
             }
         }
@@ -106,7 +106,7 @@
             normalizeAngle(&angle);
             if (angle != zRot) {
                 zRot = angle;
-                zRotationChanged.emit(angle);
+                zRotationChanged(angle);
                 updateGL();
             }
         }
--- a/generator/dgenerator.cpp	Fri Oct 16 05:32:23 2009 +0000
+++ b/generator/dgenerator.cpp	Fri Oct 16 12:23:12 2009 +0000
@@ -983,10 +983,10 @@
     if (!(d_function->isEmptyFunction() || d_function->isNormal() || d_function->isSignal()))
         option = Option(option | SkipReturnType);
     writeFunctionAttributes(s, d_function, included_attributes, excluded_attributes, option);
-
+/*
     if(d_function->isSignal())
         functionName += "_emit";
-
+*/
     s << functionName << "(";
     writeFunctionArguments(s, d_function, argument_count, option);
     s << ")";
@@ -1771,7 +1771,7 @@
                 s << endl;
             }
 //            s << INDENT << "Stdout(\"" << d_class->name() << "\", \"" << signal->name() << "\").newline;" << endl;
-            s << INDENT << "d_object." << signal->name() << ".emit(";
+            s << INDENT << "d_object." << signal->name() << "_emit(";
             for (int j = 0; j<sz; ++j) {
                 AbstractMetaArgument *argument = arguments.at(j);
                 QString arg_name = argument->indexedName();
@@ -2817,7 +2817,7 @@
     AbstractMetaArgumentList arguments = d_function->arguments();
     int sz = arguments.count();
 
-    s << INDENT << "mixin Signal!(\"" << d_function->name() << "\"";
+    s << INDENT << "mixin BindQtSignal!(\"" << d_function->name() << "\"";
 
     if (sz > 0) {
         for (int i=0; i<sz; ++i) {
--- a/qt/d1/qt/Signal.d	Fri Oct 16 05:32:23 2009 +0000
+++ b/qt/d1/qt/Signal.d	Fri Oct 16 12:23:12 2009 +0000
@@ -817,47 +817,6 @@
     }
 }
 
-//TODO: this could be avoided if named mixins didn't suck.
-public struct SignalOps(int sigId, A...)
-{
-    private SignalHandler sh;
-    enum { signalId = sigId }
-
-    void connect(R, B...)(R function(B) fn, ConnectionFlags flags = ConnectionFlags.None)
-    {
-        alias CheckSlot!(typeof(fn), A) check;
-        auto invoker = Dg(&sh.invokeSlot!(typeof(fn), Fn, A));
-        sh.connect(signalId, Fn(fn), invoker, flags);
-    }
-
-    void connect(R, B...)(R delegate(B) dg, ConnectionFlags flags = ConnectionFlags.None)
-    {
-        alias CheckSlot!(typeof(dg), A) check;
-        auto invoker = Dg(&sh.invokeSlot!(typeof(dg), Dg, A));
-        sh.connect(signalId, Dg(dg), invoker, flags);
-    }
-
-    void disconnect(R, B...)(R function(B) fn)
-    {
-        sh.disconnect(signalId, Fn(fn));
-    }
-
-    void disconnect(R, B...)(R delegate(B) dg)
-    {
-        sh.disconnect(signalId, Dg(dg));
-    }
-
-    void emit(A args)
-    {
-        sh.emit(signalId, args);
-    }
-
-    debug size_t slotCount()
-    {
-        return sh.slotCount(signalId);
-    }
-}
-
 public template SignalHandlerOps()
 {
     static assert (is(typeof(this.signalHandler)),
@@ -1096,6 +1055,30 @@
 }
 
 
+public string SignalEmitter(A...)(SignalType signalType, string name, int index)
+{
+    string fullArgs, args;
+    static if (A.length)
+    {
+        fullArgs = A[0].stringof ~ " a0";
+        args = ", a0";
+        foreach(i, _; A[1..$])
+        {
+            fullArgs ~= ", " ~ A[i+1].stringof ~ " a" ~ __toString(i+1);
+            args ~= ", a" ~ __toString(i+1);
+        }
+    }
+    string attribute;
+    string sigName = name;
+    if (signalType == SignalType.BindQtSignal)
+        name ~= "_emit";
+    else
+        attribute = "protected ";
+    string str = attribute ~ "void " ~ name ~ "(" ~ fullArgs ~ ")" ~
+                 "{ this.signalHandler.emit(" ~ __toString(index) ~ args ~ "); }";
+    return str;
+}
+
 /**
     Examples:
 ----
@@ -1128,25 +1111,35 @@
 }
 ----
 */
+
+enum SignalType
+{
+    BindQtSignal,
+    NewSignal
+}
+
+template BindQtSignal(string name, A...)
+{
+    mixin SignalImpl!(0, name, SignalType.BindQtSignal, A);
+}
+
 template Signal(string name, A...)
 {
-    mixin SignalImpl!(0, name, A);
+    mixin SignalImpl!(0, name, SignalType.NewSignal, A);
 }
 
-template SignalImpl(int index, string name, A...)
+template SignalImpl(int index, string name, SignalType signalType, A...)
 {
     static if (is(typeof(mixin(typeof(this).stringof ~ ".__signal" ~ ToString!(index)))))
-        mixin SignalImpl!(index + 1, name, A);
+        mixin SignalImpl!(index + 1, name, signalType, A);
     else
     {
         // mixed-in once
         static if (!is(typeof(this.signalHandler)))
-        {
             mixin SignalHandlerOps;
-        }
+
+        mixin (SignalEmitter!(A)(signalType, name, index));
         mixin("public alias Tuple!(\"" ~ name ~ "\", index, A) __signal" ~ ToString!(index) ~ ";");
-        mixin("SignalOps!(" ~ ToString!(index) ~ ", A) " ~ name ~ "(){ return SignalOps!("
-            ~ ToString!(index) ~ ", A)(signalHandler); }");
     }
 }
 
--- a/qt/d2/qt/Signal.d	Fri Oct 16 05:32:23 2009 +0000
+++ b/qt/d2/qt/Signal.d	Fri Oct 16 12:23:12 2009 +0000
@@ -781,23 +781,6 @@
     }
 }
 
-//TODO: this could be avoided if named mixins didn't suck.
-public struct SignalOps(int sigId, A...)
-{
-    private SignalHandler sh;
-    enum { signalId = sigId }
-
-    void emit(A args)
-    {
-        sh.emit(signalId, args);
-    }
-
-    debug size_t slotCount()
-    {
-        return sh.slotCount(signalId);
-    }
-}
-
 public template SignalHandlerOps()
 {
     static assert (is(typeof(this.signalHandler)),
@@ -1023,6 +1006,56 @@
     }
 }
 
+string __toString(long v)
+{
+    if (v == 0)
+        return "0";
+
+    string ret;
+
+    bool neg;
+    if (v < 0)
+    {
+        neg = true;
+        v = -v;
+    }
+
+    while (v != 0)
+    {
+        ret = cast(char)(v % 10 + '0') ~ ret;
+        v = cast(long)(v / 10);
+    }
+
+    if (neg)
+        ret = "-" ~ ret;
+
+    return ret;
+}
+
+public string SignalEmitter(A...)(SignalType signalType, string name, int index)
+{
+    string fullArgs, args;
+    static if (A.length)
+    {
+        fullArgs = A[0].stringof ~ " a0";
+        args = ", a0";
+        foreach(i, _; A[1..$])
+        {
+            fullArgs ~= ", " ~ A[i+1].stringof ~ " a" ~ __toString(i+1);
+            args ~= ", a" ~ __toString(i+1);
+        }
+    }
+    string attribute;
+    string sigName = name;
+    if (signalType == SignalType.BindQtSignal)
+        name ~= "_emit";
+    else
+        attribute = "protected ";
+    string str = attribute ~ "void " ~ name ~ "(" ~ fullArgs ~ ")" ~
+                 "{ this.signalHandler.emit(" ~ __toString(index) ~ args ~ "); }";
+    return str;
+}
+
 /** ---------------- */
 
 
@@ -1058,25 +1091,35 @@
 }
 ----
 */
+
+enum SignalType
+{
+    BindQtSignal,
+    NewSignal
+}
+
+template BindQtSignal(string name, A...)
+{
+    mixin SignalImpl!(0, name, SignalType.BindQtSignal, A);
+}
+
 template Signal(string name, A...)
 {
-    mixin SignalImpl!(0, name, A);
+    mixin SignalImpl!(0, name, SignalType.NewSignal, A);
 }
 
-template SignalImpl(int index, string name, A...)
+template SignalImpl(int index, string name, SignalType signalType, A...)
 {
     static if (is(typeof(mixin(typeof(this).stringof ~ ".__signal" ~ ToString!(index)))))
-        mixin SignalImpl!(index + 1, name, A);
+        mixin SignalImpl!(index + 1, name, signalType, A);
     else
     {
         // mixed-in once
         static if (!is(typeof(this.signalHandler)))
-        {
             mixin SignalHandlerOps;
-        }
+
+        mixin (SignalEmitter!(A)(signalType, name, index));
         mixin("public alias TypeTuple!(\"" ~ name ~ "\", index, A) __signal" ~ ToString!(index) ~ ";");
-        mixin("SignalOps!(" ~ ToString!(index) ~ ", A) " ~ name ~ "(){ return SignalOps!("
-            ~ ToString!(index) ~ ", A)(signalHandler); }");
     }
 }