[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/object.doc:610 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Why doesn't Qt use templates for signals and slots?</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Why doesn't Qt use templates for signals and slots?</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | <p> A simple answer is that when Qt was designed, it was not possible to
|
---|
| 37 | fully exploit the template mechanism in multi-platform applications due
|
---|
| 38 | to the inadequacies of various compilers. Even today, many widely used
|
---|
| 39 | C++ compilers have problems with advanced templates. For example, you
|
---|
| 40 | cannot safely rely on partial template instantiation, which is essential
|
---|
| 41 | for some non-trivial problem domains. Thus Qt's usage of templates has
|
---|
| 42 | to be rather conservative. Keep in mind that Qt is a multi-platform
|
---|
| 43 | toolkit, and progress on the Linux/g++ platform does not necessarily
|
---|
| 44 | improve the situation elsewhere.
|
---|
| 45 | <p> Eventually those compilers with weak template implementations will
|
---|
| 46 | improve. But even if all our users had access to a fully standards
|
---|
| 47 | compliant modern C++ compiler with excellent template support, we would
|
---|
| 48 | not abandon the string-based approach used by our <a href="metaobjects.html#meta-object">meta object</a> compiler.
|
---|
| 49 | Here are five reasons why:
|
---|
| 50 | <p> <h3>1. Syntax matters</h3>
|
---|
| 51 | <p> Syntax isn't just sugar: the syntax we use to express our algorithms can
|
---|
| 52 | significantly affect the readability and maintainability of our code.
|
---|
| 53 | The syntax used for Qt's signals and slots has proved very successful in
|
---|
| 54 | practice. The syntax is intuitive, simple to use and easy to read.
|
---|
| 55 | People learning Qt find the syntax helps them understand and utilize the
|
---|
| 56 | signals and slots concept -- despite its highly abstract and generic
|
---|
| 57 | nature. Furthermore, declaring signals in class definitions ensures that
|
---|
| 58 | the signals are protected in the sense of protected C++ member
|
---|
| 59 | functions. This helps programmers get their design right from the very
|
---|
| 60 | beginning, without even having to think about design patterns.
|
---|
| 61 | <p> <h3>2. Precompilers are good</h3>
|
---|
| 62 | <p> Qt's <tt><a href="moc.html#moc">moc</a></tt> (Meta Object Compiler) provides a clean way to go
|
---|
| 63 | beyond the compiled language's facilities. It does so by generating
|
---|
| 64 | additional C++ code which can be compiled by any standard C++ compiler.
|
---|
| 65 | The <tt>moc</tt> reads C++ source files. If it finds one or more class
|
---|
| 66 | declarations that contain the "Q_OBJECT" macro, it produces another C++
|
---|
| 67 | source file which contains the meta object code for those classes. The
|
---|
| 68 | C++ source file generated by the <tt>moc</tt> must be compiled and
|
---|
| 69 | linked with the implementation of the class (or it can be
|
---|
| 70 | <tt>#included</tt> into the class's source file). Typically <tt>moc</tt>
|
---|
| 71 | is not called manually, but automatically by the build system, so it
|
---|
| 72 | requires no additional effort by the programmer.
|
---|
| 73 | <p> There are other precompilers, for example, <tt>rpc</tt> and
|
---|
| 74 | <tt>idl</tt>, that enable programs or objects to communicate over
|
---|
| 75 | process or machine boundaries. The alternatives to precompilers are
|
---|
| 76 | hacked compilers, proprietary languages or graphical programming tools
|
---|
| 77 | with dialogs or wizards that generate obscure code. Rather than locking
|
---|
| 78 | our customers into a proprietary C++ compiler or into a particular
|
---|
| 79 | Integrated Development Environment, we enable them to use whatever tools
|
---|
| 80 | they prefer. Instead of forcing programmers to add generated code into
|
---|
| 81 | source repositories, we encourage them to add our tools to their build
|
---|
| 82 | system: cleaner, safer and more in the spirit of UNIX.
|
---|
| 83 | <p> <h3>3. Flexibility is king</h3>
|
---|
| 84 | <p> C++ is a standarized, powerful and elaborate general-purpose language.
|
---|
| 85 | It's the only language that is exploited on such a wide range of
|
---|
| 86 | software projects, spanning every kind of application from entire
|
---|
| 87 | operating systems, database servers and high end graphics
|
---|
| 88 | applications to common desktop applications. One of the keys to C++'s
|
---|
| 89 | success is its scalable language design that focuses on maximum
|
---|
| 90 | performance and minimal memory consumption whilst still maintaining
|
---|
| 91 | ANSI-C compatibility.
|
---|
| 92 | <p> For all these advantages, there are some downsides. For C++, the static
|
---|
| 93 | object model is a clear disadvantage over the dynamic messaging approach
|
---|
| 94 | of Objective C when it comes to component-based graphical user interface
|
---|
| 95 | programming. What's good for a high end database server or an operating
|
---|
| 96 | system isn't necessarily the right design choice for a GUI frontend.
|
---|
| 97 | With <tt>moc</tt>, we have turned this disadvantage into an advantage,
|
---|
| 98 | and added the flexibility required to meet the challenge of safe and
|
---|
| 99 | efficient graphical user interface programming.
|
---|
| 100 | <p> Our approach goes far beyond anything you can do with templates. For
|
---|
| 101 | example, we can have object properties. And we can have overloaded
|
---|
| 102 | signals and slots, which feels natural when programming in a language
|
---|
| 103 | where overloads are a key concept. Our signals add zero bytes to the
|
---|
| 104 | size of a class instance, which means we can add new signals without
|
---|
| 105 | breaking binary compatibility. Because we do not rely on excessive
|
---|
| 106 | inlining as done with templates, we can keep the code size smaller.
|
---|
| 107 | Adding new connections just expands to a simple function call rather
|
---|
| 108 | than a complex template function.
|
---|
| 109 | <p> Another benefit is that we can explore an object's signals and slots at
|
---|
| 110 | runtime. We can establish connections using type-safe call-by-name,
|
---|
| 111 | without having to know the exact types of the objects we are connecting.
|
---|
| 112 | This is impossible with a template based solution. This kind of runtime
|
---|
| 113 | introspection opens up new possibilities, for example GUIs that are
|
---|
| 114 | generated and connected from Qt Designer's XML <tt>ui</tt> files.
|
---|
| 115 | <p> <h3>4. Calling performance is not everything</h3>
|
---|
| 116 | <p> Qt's signals and slots implementation is not as fast as a template-based
|
---|
| 117 | solution. While emitting a signal is approximately the cost of four
|
---|
| 118 | ordinary function calls with common template implementations, Qt
|
---|
| 119 | requires effort comparable to about ten function calls. This is not
|
---|
| 120 | surprising since the Qt mechanism includes a generic marshaller,
|
---|
| 121 | introspection and ultimately scriptability. It does not rely on
|
---|
| 122 | excessive inlining and code expansion and it provides unmatched runtime
|
---|
| 123 | safety. Qt's iterators are safe while those of faster template-based
|
---|
| 124 | systems are not. Even during the process of emitting a signal to several
|
---|
| 125 | receivers, those receivers can be deleted safely without your program
|
---|
| 126 | crashing. Without this safety, your application would eventually crash
|
---|
| 127 | with a difficult to debug free'd memory read or write error.
|
---|
| 128 | <p> Nonetheless, couldn't a template-based solution improve the performance
|
---|
| 129 | of an application using signals and slots? While it is true that Qt adds
|
---|
| 130 | a small overhead to the cost of calling a slot through a signal, the
|
---|
| 131 | cost of the call is only a small proportion of the entire cost of a
|
---|
| 132 | slot. Benchmarking against Qt's signals and slots system is typically
|
---|
| 133 | done with empty slots. As soon as you do anything useful in your slots,
|
---|
| 134 | for example a few simple string operations, the calling overhead becomes
|
---|
| 135 | negligible. Qt's system is so optimized that anything that requires
|
---|
| 136 | operator new or delete (for example, string operations or
|
---|
| 137 | inserting/removing something from a template container) is significantly
|
---|
| 138 | more expensive than emitting a signal.
|
---|
| 139 | <p> Aside: If you have a signals and slots connection in a tight inner loop
|
---|
| 140 | of a performance critical task and you identify this connection as the
|
---|
| 141 | bottleneck, think about using the standard listener-interface pattern
|
---|
| 142 | rather than signals and slots. In cases where this occurs, you probably
|
---|
| 143 | only require a 1:1 connection anyway. For example, if you have an object
|
---|
| 144 | that downloads data from the network, it's a perfectly sensible design
|
---|
| 145 | to use a signal to indicate that the requested data arrived. But if you
|
---|
| 146 | need to send out every single byte one by one to a consumer, use a
|
---|
| 147 | listener interface rather than signals and slots.
|
---|
| 148 | <p> <h3>5. No limits</h3>
|
---|
| 149 | <p> Because we had the <tt>moc</tt> for signals and slots, we could add
|
---|
| 150 | other useful things to it that could not not be done with templates.
|
---|
| 151 | Among these are scoped translations via a generated <tt>tr()</tt>
|
---|
| 152 | function, and an advanced property system with introspection and
|
---|
| 153 | extended runtime type information. The property system alone is a great
|
---|
| 154 | advantage: a powerful and generic user interface design tool like Qt
|
---|
| 155 | Designer would be a lot harder to write - if not impossible - without a
|
---|
| 156 | powerful and introspective property system.
|
---|
| 157 | <p> C++ with the <tt>moc</tt> preprocessor essentially gives us the
|
---|
| 158 | flexibility of Objective-C or of a Java Runtime Environment, while
|
---|
| 159 | maintaining C++'s unique performance and scalability advantages. It is
|
---|
| 160 | what makes Qt the flexible and comfortable tool we have today.
|
---|
| 161 | <p>
|
---|
| 162 | <!-- eof -->
|
---|
| 163 | <p><address><hr><div align=center>
|
---|
| 164 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 165 | <td>Copyright © 2007
|
---|
| 166 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 167 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 168 | </table></div></address></body>
|
---|
| 169 | </html>
|
---|