title: QObject generic signal handler
body: <p>(With "signal handler" I mean slots, not handlers for POSIX signals.)</p>
  
  <p>I need to "connect" (<em>probably not</em> using <code>QObject::connect</code> directly) <strong>all signals</strong> from an instance of a (not yet known) subclass of QObject to <strong>one single slot</strong> of another QObject. I need this in order to send the signal (with arguments) over network (for an own RPC system with support for signals).</p>
  
  <p>(With "not yet known" I mean that my code should be as generic as possible. So it souldn't contain a <code>connect</code> statement for each signal in each class I'm using with my RPC system, but provide something like <code>RPC::connectAllSignals(QObject*);</code>, which then scanns all signals during runtime and connects them.)</p>
  
  <p>What I'd like to achieve is: Handle all signals and serialise them (signal name + arguments). I already can serialise the arguments, but I don't know how to get the signal name. After googling, it seems to be impossible to use something similar like there is <code>sender()</code> for the QObject instance. So I need to do something far more complicated.</p>
  
  <p>My current type system for passing the arguments to a target function on the remote end is restricted to some types anyway. (That's because I need <code>qt_metacall</code>, which excepts the arguments to be of type <code>void*</code> with the "correct types" behind them. My RPC system uses QVariants with only a couple of types internally and I convert them to <code>void*</code> of the correct types using custom methods. I heard about <code>QVariant::constData</code> too late to use it, and it probably won't fit anyway; so I will stick to my type conversion if there is no drawback.)</p>
  
  <p>The target slot, where all signals should be mapped to, should look similar to this:</p>
  
  <pre><code>void handleSignal(QByteArray signalName, QVariantList arguments);
  </code></pre>
  
  <p>It would be best if the solution is supported by C++03, so I only want to use variadic templates if it is a <em>big</em> drawback to not use them. In this case C++11 is OK, so I'm also happy about answers using C++11.</p>
  
  <hr>
  
  <p>Now my <strong>possible solution</strong> to the question I'm thinking about:</p>
  
  <p>I could scan all signals of the object using its <code>QMetaObject</code> and then creating a <code>QSignalMapper</code> (or something similar which passes all arguments) for <em>each</em> signal. This is easy, and I need no help on this part. As mentioned before, I'm already restricted to some types for arguments, and I can also live with a restriction on the argument count.</p>
  
  <p>It sounds like a dirty hack, but I could use some sort of custom, template-based signal mappers like this (in this example for three arguments):</p>
  
  <pre><code>template&lt;class T1, class T2, class T3&gt;
  class MySignalMapper : public QObject
  {
      Q_OBJECT
  public:
      void setSignalName(QByteArray signalName)
      {
          this-&gt;signalName = signalName;
      }
  signals:
      void mapped(QByteArray signalName, QVariantList arguments);
  public slots:
      void map(T1 arg1, T2 arg2, T3 arg3)
      {
          QVariantList args;
          // QVariant myTypeConverter&lt;T&gt;(T) already implemented:
          args &lt;&lt; myTypeConverter(arg1);
          args &lt;&lt; myTypeConverter(arg2);
          args &lt;&lt; myTypeConverter(arg3);
          emit mapped(signalName, args);
      }
  private:
      QByteArray signalName;
  };
  </code></pre>
  
  <p>Then I could connect a QMetaMethod called <code>method</code> (which is known to be a signal) of a QObject called <code>obj</code> like this (which might be generated using some sort of script for all supported types and argument counts... yeah... <em>it's getting dirty!</em>):</p>
  
  <pre><code>    // ...
  }
  else if(type1 == "int" &amp;&amp; type2 == "char" &amp;&amp; type3 == "bool")
  {
      MySignalMapper&lt;int,char,bool&gt; *sm = new MySignalMapper&lt;int,char,bool&gt;(this);
      QByteArray signalName = method.signature();
      signalName = signalName.left(signalName.indexOf('(')); // remove parameters
      sm-&gt;setMember(signalName);
  
      // prepend "2", like Qt's SIGNAL() macro does:
      QByteArray signalName = QByteArray("2") + method.signature();
  
      // connect the mapper:
      connect(obj, signalName.constData(),
              sm, SLOT(map(int,char,bool)));
      connect(sm, SIGNAL(mapped(int,char,bool)),
              this, SLOT(handleSignal(const char*,QVariantList)));
  }
  else if(type1 == ...)
  {
      // ...
  </code></pre>
  
  <p>As this <em>may work</em>, it really is a dirty solution. I'd need either a lot of macros to cover all combinations of types for at most <code>N</code> arguments (where <code>N</code> is about 3 to 5, not yet known), or a simple script generating the code for all cases. The problem is that this will be a <em>lot</em> of cases, as I'm supporting about 70 different types per argument (10 primitive types + nested lists and maps with depth 2 for <em>every</em> type of them). So for an argument count limit of <code>N</code> there are <code>N</code> ^ 70 cases to cover!</p>
  
  <p><strong>Is there a completely different approach for this objective, which I'm overlooking?</strong></p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I solved the problem on my own (see answer). If you are interested in the full source code, see my repository on bitbucket of my RPC system, which I have just published: bitbucket.org/leemes/qtsimplerpc</p>