<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>El Tramo | Programming</title>
  <subtitle>Remko Tronçon's Homepage</subtitle>
  <link href="http://el-tramo.be/blog/category/programming/feed/" rel="self" type="application/rss+xml"/>
  <link href="http://el-tramo.be/"/>
  <updated>2011-12-30T17:00:29+01:00</updated>
  <id>http://el-tramo.be/</id>
  <author>
    <name>Remko Tronçon</name>
    <uri>http://el-tramo.be/about/</uri>
  </author>
  
  <entry>
    <title>XMPP Scripting with Sluift</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/sluift"/>
    <updated>2011-03-05T00:00:00+01:00</updated>
    <id>http://el-tramo.be/blog/sluift</id>
    <content type="html">Did you ever want to find out what XMPP clients people in your contact list are using? Do you want to migrate your contact list from one server to another, but don’t want to provide your password to some on-line service to do that? Do you have some XMPP-related task you quickly want to write a script for, but don’t want to deal with complex asynchronous APIs? Well, &lt;em&gt;Sluift&lt;/em&gt; may be just the thing you are looking for! 

Sluift is a &lt;a href=&quot;http://lua.org&quot;&gt;Lua&lt;/a&gt;-based script layer on top of the &lt;a href=&quot;http://swift.im/swiften&quot;&gt;Swiften&lt;/a&gt; XMPP library. It provides a simple API to do common XMPP tasks, either interactively (through an XMPP console), or by running a script in batch mode. In this post, we’ll go through some examples of what you can already do with Sluift today.

&lt;!--more--&gt;

Let’s start with a very simple task: logging into a server, and sending a message to one of our contacts. We fire up Sluift, and start by typing the commands to log in:
&lt;pre&gt;== Sluift XMPP Console (20110304) == 
Press Ctrl-D to exit  
&gt; &lt;b&gt;c = sluift.new_client(&quot;alice@wonderland.lit&quot;, &quot;MySecret&quot;)&lt;/b&gt;
&gt; &lt;b&gt;c:connect()&lt;/b&gt;
&gt;&lt;/pre&gt;
Now that we're connected to the server, let's send out presence (so people can see we’re online), and send the message to one of our contacts:
&lt;pre&gt;&gt; &lt;b&gt;c:send_presence(&quot;I'm here&quot;)&lt;/b&gt;
&gt; &lt;b&gt;c:send_message(&quot;sister@realworld.lit&quot;, &quot;Hi there!&quot;)&lt;/b&gt;&lt;/pre&gt;
Now, let's wait 3 seconds for her reply, by printing out all the incoming events, and finish off by disconnecting:
&lt;pre&gt;&gt; &lt;b&gt;c:for_event(function(e) tprint(e) end, 3000)&lt;/b&gt;
([type] =&gt; presence, [from] =&gt; sister@realworld.lit/Home, 
 [status] =&gt; At home)
([type] =&gt; presence, [from] =&gt; rabbit@wonderland.lit/Party, 
 [status] =&gt; Tea party!)
([type] =&gt; message, [from] =&gt; sister@realworld.lit/Home, 
 [body] =&gt; Hi Alice!)
&gt; &lt;b&gt;c:disconnect()&lt;/b&gt;
&gt;&lt;/pre&gt;

Now for a slightly more complex example. Suppose that you want to switch XMPP servers, but you want to take all your contacts with you to your new account. To do this, you start by logging into your current account, and fetch your contact list:
&lt;pre&gt;&gt; &lt;b&gt;c = sluift.new_client(&quot;alice@wonderland.lit&quot;, &quot;MySecret&quot;)&lt;/b&gt;
&gt; &lt;b&gt;c:connect()&lt;/b&gt;
&gt; &lt;b&gt;contacts = c:get_contacts()&lt;/b&gt;
&gt; &lt;b&gt;tprint(contacts)&lt;/b&gt;
[sister@wonderland.lit] =&gt; table
    ( [jid] =&gt; sister@wonderland.lit
      [subscription] =&gt; both
      [groups] =&gt; table ( [1] =&gt; Family )
      [name] =&gt; Sister )
...
&lt;/pre&gt;
Now that we have our contact list, let’s disconnect, log into our second account, and add all the contacts from the list to the second account:
&lt;pre&gt;&gt; &lt;b&gt;c:disconnect()&lt;/b&gt;
&gt; &lt;b&gt;c = sluift.new_client(&quot;alice@teaparty.lit&quot;, &quot;MyOtherSecret&quot;)&lt;/b&gt;
&gt; &lt;b&gt;c:connect()&lt;/b&gt;
&gt; &lt;b&gt;for _, contact in pairs(contacts) do c:add_contact(contact) end&lt;/b&gt;
&lt;/pre&gt;
That’s it, all your contacts have now been migrated! 

So far, we have used Sluift as an interactive XMPP console. However, Sluift’s synchronous/blocking nature (it only executes the next command when you have the results of the previous) also makes it very suitable for writing small batch scripts. For example, you can write a short script to collect statistics about which sever software is popular amongst your contacts:
&lt;pre&gt;c = sluift.new_client(&quot;alice@wonderland.lit&quot;, &quot;MySecret&quot;)
c:connect()
versions = {}
for jid, _ in pairs(c:get_contacts()) do
  v = c:get_version(sluift.jid_domain(jid))
  if v then versions[v[&quot;name&quot;]] = (versions[v[&quot;name&quot;]] or 0) + 1 end
end
for name, cnt in pairs(versions) do print(name .. &quot;: &quot; .. cnt) end
c:disconnect()&lt;/pre&gt;
Executing the script gives us the following result:
&lt;pre&gt;$ ./sluift CollectVersions.lua
jabberd2: 1
Prosody: 25
Isode M-Link: 31
yabberd: 1
SoapBox Server 2007: 1
jabberd: 5
Tigase: 2
ejabberd: 19
Openfire: 4&lt;/pre&gt;

And finally, we can’t show script examples without including our beloved &lt;a href=&quot;https://github.com/remko/xmpp-tdg/tree/master/code/EchoBot&quot;&gt;EchoBot&lt;/a&gt; from &lt;a href=&quot;http://oreilly.com/catalog/9780596521271&quot;&gt;XMPP: The Definitive Guide&lt;/a&gt;:
&lt;pre&gt;c = sluift.new_client(&quot;echobot@wonderland.lit&quot;, &quot;mypass&quot;)
c:connect():send_presence(&quot;Send me a message&quot;)
c:for_event(function(e)
    if e[&quot;type&quot;] == &quot;message&quot; then 
      c:send_message(e[&quot;from&quot;], e[&quot;body&quot;])
    end
  end)&lt;/pre&gt;
&lt;/pre&gt;

Besides interactive and scripted tasks, another great use for Sluift is for XMPP testing (which is the reason we started Sluift in the first place). However, we will go into details about this use case in a later blog post. For now, if you want to play around with Sluift yourself, you can build a development version from the &lt;a href=&quot;http://swift.im/git/swift&quot;&gt;Swift Git repository&lt;/a&gt; (together with some &lt;a href=&quot;http://swift.im/git/swift/tree/Sluift/Examples&quot;&gt;examples&lt;/a&gt;), or you can get a snapshot for Windows or Mac OS X &lt;a href=&quot;http://swift.im/sluift/&quot;&gt;here&lt;/a&gt; (currently only works on very specific setups).

Finally, note that, although you can already do quite some tasks with Sluift today, the API is still quite limited (and undocumented). We are adding new functionality to Sluift as we need it or as people ask for it, so if you’re interested in Sluift, speak up!

Stay tuned for more on the topic of XMPP scripting!
</content>
  </entry>
  
  <entry>
    <title>Swiften on Lambdas</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/swiften-lambdas"/>
    <updated>2011-02-04T00:00:00+01:00</updated>
    <id>http://el-tramo.be/blog/swiften-lambdas</id>
    <content type="html">One of the cool new features of the upcoming C++ (0x) standard is support for
&lt;a
href=&quot;http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions&quot;&gt;lambda
expressions&lt;/a&gt;, providing functional-style inline function declarations. After
seeing &lt;a href=&quot;http://herbsutter.com/&quot;&gt;Herb Sutter&lt;/a&gt;’s &lt;a
href=&quot;http://blogs.msdn.com/b/vcblog/archive/2011/02/01/10123374.aspx&quot;&gt;PDC 2010
webcast on lambdas&lt;/a&gt;, I wanted to try this out on Swiften, the XMPP library
behind &lt;a href=&quot;http://swift.im&quot;&gt;Swift&lt;/a&gt;. I adapted the introductory &lt;a
href=&quot;https://github.com/remko/xmpp-tdg/tree/master/code/EchoBot&quot;&gt;EchoBot
example&lt;/a&gt; from &lt;a href=&quot;http://oreilly.com/catalog/9780596521271&quot;&gt;XMPP: The
Definitive Guide&lt;/a&gt;, and ported it from Python to a C++ application using
Swiften. The result is surprisingly clean.

&lt;!--more--&gt;

The purpose of EchoBot is simple: connect to an XMPP server, log in, send out a
status message to announce you're available, and then wait and respond to all
incoming messages with the exact same message. Translating this into C++ code,
with lambdas, gives us the following: 

<div class="highlight"><pre><code class="c"><span class="cp">#include &amp;lt;Swiften/Swiften.h&amp;gt;</span>

<span class="n">using</span> <span class="n">namespace</span> <span class="n">Swift</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span><span class="p">)</span> <span class="p">{</span>
	<span class="c1">// Set up the event loop and network classes</span>
	<span class="n">SimpleEventLoop</span> <span class="n">eventLoop</span><span class="p">;</span>
	<span class="n">BoostNetworkFactories</span> <span class="n">networkFactories</span><span class="p">(</span><span class="o">&amp;</span><span class="n">amp</span><span class="p">;</span><span class="n">eventLoop</span><span class="p">);</span>

	<span class="c1">// Initialize the client with the JID and password</span>
	<span class="n">Client</span> <span class="n">client</span><span class="p">(</span>
		<span class="o">&amp;</span><span class="n">quot</span><span class="p">;</span><span class="n">echobot</span><span class="err">@</span><span class="n">wonderland</span><span class="p">.</span><span class="n">lit</span><span class="o">&amp;</span><span class="n">quot</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">quot</span><span class="p">;</span><span class="n">mypass</span><span class="o">&amp;</span><span class="n">quot</span><span class="p">;,</span> <span class="o">&amp;</span><span class="n">amp</span><span class="p">;</span><span class="n">networkFactories</span><span class="p">);</span>

	<span class="c1">// When the client is connected, send out initial presence</span>
	<span class="n">client</span><span class="p">.</span><span class="n">onConnected</span><span class="p">.</span><span class="n">connect</span><span class="p">([</span><span class="o">&amp;</span><span class="n">amp</span><span class="p">;]</span> <span class="p">{</span>
		<span class="n">client</span><span class="p">.</span><span class="n">sendPresence</span><span class="p">(</span><span class="n">Presence</span><span class="o">::</span><span class="n">create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">quot</span><span class="p">;</span><span class="n">Send</span> <span class="n">me</span> <span class="n">a</span> <span class="n">message</span><span class="o">&amp;</span><span class="n">quot</span><span class="p">;));</span>
	<span class="p">});</span>

	<span class="c1">// When the client receives an incoming message, echo it back</span>
	<span class="n">client</span><span class="p">.</span><span class="n">onMessageReceived</span><span class="p">.</span><span class="n">connect</span><span class="p">([</span><span class="o">&amp;</span><span class="n">amp</span><span class="p">;]</span> <span class="p">(</span><span class="n">Message</span><span class="o">::</span><span class="n">ref</span> <span class="n">message</span><span class="p">)</span> <span class="p">{</span>
		<span class="n">message</span><span class="o">-&amp;</span><span class="n">gt</span><span class="p">;</span><span class="n">setTo</span><span class="p">(</span><span class="n">message</span><span class="o">-&amp;</span><span class="n">gt</span><span class="p">;</span><span class="n">getFrom</span><span class="p">());</span>
		<span class="n">message</span><span class="o">-&amp;</span><span class="n">gt</span><span class="p">;</span><span class="n">setFrom</span><span class="p">(</span><span class="n">JID</span><span class="p">());</span>
		<span class="n">client</span><span class="p">.</span><span class="n">sendMessage</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
	<span class="p">});</span>

	<span class="c1">// Start the client</span>
	<span class="n">client</span><span class="p">.</span><span class="n">connect</span><span class="p">();</span>

	<span class="c1">// Run the event loop to start processing incoming network events</span>
	<span class="n">eventLoop</span><span class="p">.</span><span class="n">run</span><span class="p">();</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>


The flow of the code follows the description of the bot quite closely. Before
connecting  the client to the network, we declare the actions to be taken on
different events: when connected, send out presence, and when a message was
received, send it back. Swiften uses signals to notify events, and instead of
having to declare slot methods and using &lt;code&gt;boost::bind&lt;/code&gt; to connect
signals to slots, we can now use lambdas to directly specify what should
happen. (for the curious: &lt;code&gt;[&amp;amp;]&lt;/code&gt; means that all variables outside
of the lambda, i.e. &lt;code&gt;client&lt;/code&gt;, are passed by reference) 

The great thing about this is that this works out of the box with recent
compilers: I tried this code with Microsoft Visual Studio 2010 and GCC 4.5.0.
(unfortunately, CLang doesn’t support this feature yet)

If you want to try building your own XMPP applications using Swiften (with or
without C++0x), grab a development version from &lt;a
href=&quot;http://gitorious.com/swift&quot;&gt;the Git repository&lt;/a&gt;, and consult the &lt;a
href=&quot;http://swift.im/swiften/guide&quot;&gt;Swiften Developers Guide&lt;/a&gt; and &lt;a
href=&quot;http://swift.im/swiften/api&quot;&gt;Swiften API documentation&lt;/a&gt; to get
started.
</content>
  </entry>
  
  <entry>
    <title>Eclipse CPPUnit Error Parser</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/ecppunit"/>
    <updated>2010-08-24T00:00:00+02:00</updated>
    <id>http://el-tramo.be/blog/ecppunit</id>
    <content type="html">I've recently been experimenting with using &lt;a href=&quot;http://eclipse.org/cdt&quot;&gt;Eclipse CDT&lt;/a&gt; as IDE for &lt;a href=&quot;http://swift.im&quot;&gt;Swift&lt;/a&gt; development. One of the handy things is that Eclipse CDT has support for parsing compiler error messages, allowing you to quickly navigate to the failing source code line by simply clicking on the error message. Although Eclipse CDT supports all the compilers we use for Swift out of the box, I was still missing the easy navigation for fixing failing &lt;a href=&quot;http://cppunit.sourceforge.net&quot;&gt;CPPUnit&lt;/a&gt; tests. Since the error parser (just like almost everything else from Eclipse) is extensible, I wrote a small plugin for parsing CPPUnit error messages.

&lt;!--more--&gt;

You can get the plugin by opening Eclipse's &lt;em&gt;Install new software&lt;/em&gt; menu, adding &lt;code&gt;&lt;a href=&quot;http://el-tramo.be/eclipse&quot;&gt;http://el-tramo.be/eclipse&lt;/a&gt;&lt;/code&gt; as a plugin repository, and selecting the ECPPUnit plugin from the list of available software.
</content>
  </entry>
  
  <entry>
    <title>“XMPP: The Definitive Guide” Code Examples</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/xmpp-tdg-code"/>
    <updated>2009-07-13T00:00:00+02:00</updated>
    <id>http://el-tramo.be/blog/xmpp-tdg-code</id>
    <content type="html">Although the primary focus of &lt;a href=&quot;http://oreilly.com/catalog/9780596521264/&quot;&gt;&lt;em&gt;XMPP: The Definitive Guide&lt;/em&gt;&lt;/a&gt; is explaning the XMPP protocol and all its extensions through text and illustrations, we also included a few Python code examples to help people get started with implementing their own ideas. In fact, we devoted a whole chapter to building an XMPP application, starting out with a simple bot implementation, but gradually extending the application into a full server component. For people who want to try this out for themselves, we’re releasing the &lt;a href=&quot;/git/xmpp-tdg/snapshot/xmpp-tdg-master.zip&quot;&gt;source code of all code examples&lt;/a&gt;, including a &lt;a href=&quot;/git/xmpp-tdg/tree/code/EchoBot&quot;&gt;simple echo bot&lt;/a&gt;, and different variants of the &lt;a href=&quot;/git/xmpp-tdg/tree/code/CheshiR&quot;&gt;CheshiR microblogging platform XMPP interface&lt;/a&gt;.

&lt;!--more--&gt;

All examples are built using the lightweight &lt;a href=&quot;http://code.google.com/p/sleekxmpp/&quot;&gt;SleekXMPP&lt;/a&gt; Python XMPP library. In fact, SleekXMPP is so lightweight that we included a version in the source bundle, making it even easier to get started implementing your own bots and components.

Do bear in mind that these examples only serve illustrative purposes for the book, so don’t expect very robust code. Although making this code fail-safe is not really our primary goal (since that would involve a lot of code that would only distract the reader), we &lt;em&gt;do&lt;/em&gt; welcome bug reports or fixes.

The code examples are available as a &lt;a href=&quot;/git/xmpp-tdg/snapshot/xmpp-tdg-master.zip&quot;&gt;source package&lt;/a&gt;, or directly from the &lt;a href=&quot;/git/xmpp-tdg&quot;&gt;Git repository&lt;/a&gt; (mirrored on &lt;a href=&quot;http://github.com/remko/xmpp-tdg&quot;&gt;GitHub&lt;/a&gt;). We will soon put a link to the package on &lt;a href=&quot;http://oreilly.com/catalog/9780596521264/&quot;&gt;the book’s webpage&lt;/a&gt;.
</content>
  </entry>
  
  <entry>
    <title>Mixing Cocoa and Qt</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/mixing-cocoa-and-qt"/>
    <updated>2008-08-16T00:00:00+02:00</updated>
    <id>http://el-tramo.be/blog/mixing-cocoa-and-qt</id>
    <content type="html">&lt;a href=&quot;http://trolltech.com/products/qt/index&quot;&gt;Qt&lt;/a&gt; does a great job at abstracting out platform-specific features into platform-independent C++ APIs. However, sometimes you still need to write platform-specific code for features that are not in Qt (e.g. to access the platform's address book),  or to access platform-specific applications (e.g. iTunes) or libraries (e.g. &lt;a href=&quot;http://sparkle.andymatuschak.org/&quot;&gt;Sparkle&lt;/a&gt;). On Mac OS X, almost all interfaces are offered through the &lt;a href=&quot;http://developer.apple.com/cocoa/&quot;&gt;Cocoa&lt;/a&gt; Objective-C interface, and the interfaces that are written in C++ have been deprecated and will disappear soon in favor of Cocoa. Although the language of Cocoa is different from Qt's, Qt and &lt;a href=&quot;http://gcc.gnu.org&quot;&gt;GCC&lt;/a&gt; make it very easy to call these interfaces from within your application. In this post, I will show how this can be done by making an auto-updating application using Sparkle.

&lt;!--more--&gt;

To create our auto-updating application, we first lay down the auto-updater interface in a (pure) virtual class:
&lt;blockquote&gt;
&lt;pre&gt;class AutoUpdater {
  public:
    virtual void checkForUpdates() = 0;
};&lt;/pre&gt;
&lt;/blockquote&gt;
The Sparkle implementation of this interface has the following C++ header:
&lt;blockquote&gt;
&lt;pre&gt;class SparkleAutoUpdater : public AutoUpdater {
  public:
    SparkleAutoUpdater(const QString&amp;amp; url);
    ~SparkleAutoUpdater();
    virtual void checkForUpdates();
  private:
    class Private;
    Private* d;
};&lt;/pre&gt;
&lt;/blockquote&gt;
Note that I am using the &lt;a href=&quot;http://c2.com/cgi/wiki?PimplIdiom&quot;&gt;Pimpl&lt;/a&gt; idiom to hide Cocoa-specific internals from the API into a private internal class.

The actual impementation of the &lt;code&gt;SparkleUpdater&lt;/code&gt; needs to call Sparkle's Objective-C API. However, we cannot write the implementation in Objective-C, since the header for our updater (which will be included from within our application) is written in C++. Luckily, GCC's Objective-C++ extension allows you to mix Objective-C with C++ without any problem. So, we create an Objective-C++ implementation of our updater, and put it in a &lt;code&gt;.mm&lt;/code&gt; file:
&lt;blockquote&gt;
&lt;pre&gt;class SparkleAutoUpdater::Private {
  public:
    SUUpdater* updater;
};

SparkleAutoUpdater::SparkleAutoUpdater(const QString&amp;amp; aUrl) {
  d = new Private;
  d-&amp;gt;updater = [[SUUpdater sharedUpdater] retain];
  NSURL* url = [NSURL URLWithString:
      [NSString stringWithUTF8String: aUrl.toUtf8().data()]];
  [d-&amp;gt;updater setFeedURL: url];
}

SparkleAutoUpdater::~SparkleAutoUpdater() {
  [d-&amp;gt;updater release];
  delete d;
}

void SparkleAutoUpdater::checkForUpdates() {
  [d-&amp;gt;updater checkForUpdatesInBackground];
}&lt;/pre&gt;
&lt;/blockquote&gt;
All we do here is create an &lt;code&gt;SUUpdater&lt;/code&gt; (Sparkle's updater class), set it up, and activate it in our &lt;code&gt;checkForUpdates()&lt;/code&gt; method.

To integrate Objective-C++ classes into a Qt project, &lt;code&gt;qmake&lt;/code&gt; provides the &lt;code&gt;OBJECTIVE_SOURCES&lt;/code&gt; variable. Knowing this, we add our new classes to our &lt;code&gt;qmake&lt;/code&gt; project file:
&lt;blockquote&gt;
&lt;pre&gt;HEADERS += AutoUpdater.h
SOURCES += AutoUpdater.cpp
mac {
  HEADERS += SparkleAutoUpdater.h
  OBJECTIVE_SOURCES += SparkleAutoUpdater.mm
  LIBS += -framework Sparkle
}&lt;/pre&gt;
&lt;/blockquote&gt;
Before we integrate the updater into our application, there is still one detail we have to take care of: if you write Objective-C code that creates objects, you need to instantiate an &lt;code&gt;NSAutoReleasePool&lt;/code&gt; to enable Cocoa's memory management. Although we could do this in &lt;code&gt;SparkleAutoUpdater&lt;/code&gt;, we're going to put this in a separate initializer class, so we can share this with other Objective-C++ implementations in our application. The body of this class looks as follows:
&lt;blockquote&gt;
&lt;pre&gt;class CocoaInitializer::Private {
  public:
    NSAutoreleasePool* autoReleasePool;
};

CocoaInitializer::CocoaInitializer() {
  d = new CocoaInitializer::Private();
  NSApplicationLoad();
  d-&amp;gt;autoReleasePool = [[NSAutoreleasePool alloc] init];
}

CocoaInitializer::~CocoaInitializer() {
  [d-&amp;gt;autoReleasePool release];
  delete d;
}&lt;/pre&gt;
&lt;/blockquote&gt;
Now all that's left to do is integrate all this into our application:
&lt;blockquote&gt;&lt;code&gt; &lt;/code&gt;
&lt;pre&gt;int main(int argc, char* argv[]) {
  ...
  AutoUpdater* updater = 0;
#ifdef Q_WS_MAC
  CocoaInitializer cocoaInitiarizer;
  updater = new SparkleUpdater(&quot;http://el-tramo.be/myapp/appcast.xml&quot;);
#endif
  ...
  if (updater) {
    updater-&amp;gt;checkForUpdates();
  }
  ...
}&lt;/pre&gt;
&lt;/blockquote&gt;
That's it! If you want to try this out yourself, just download the &lt;a href=&quot;http://el-tramo.be/files/blog/mixing-cocoa-and-qt/mixing-cocoa-and-qt.zip&quot;&gt;sources&lt;/a&gt; of this mini-project, build them, and off you go. Don't forget to remove &lt;code&gt;~/Library/Preferences/be.el-tramo.mixing-cocoa-and-qt.plist&lt;/code&gt; to clear Sparkle's state cache.
</content>
  </entry>
  
  <entry>
    <title>Basic Music Theory in Haskell</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/basic-music-theory-in-haskell"/>
    <updated>2008-06-19T00:00:00+02:00</updated>
    <id>http://el-tramo.be/blog/basic-music-theory-in-haskell</id>
    <content type="html">While doing some spring cleaning around my hard disk, I found a &lt;a href=&quot;/git/haskell/tree/MusicTheoryBasics.hs&quot;&gt;little Haskell program&lt;/a&gt; I wrote several years ago in an attempt to learn the basics of music theory. Now, I'm not a pro at writing Haskell, and I know even less about music theory, but I'm hoping that what I wrote down back then is a bit accurate. The program seems to summarize the basics quite consisely: by just having a glance at the program, I'm rediscovering some things I totally forgot about scales and chords.

&lt;!--more--&gt;

For example, this is what it says about the &lt;em&gt;sus4&lt;/em&gt; chord:
&lt;blockquote&gt;chordNotes Five = [(ScaleNote Major 1), (ScaleNote Major 5)]
chordNotes Sus4 = (chordNotes Five) ++ [(ScaleNote Major 4)]&lt;/blockquote&gt;
So, &lt;em&gt;sus4&lt;/em&gt; is a power (&lt;em&gt;5&lt;/em&gt;) chord (consisting of the first and the fifth of the major scale), added with the 4th note of the major scale. So, for &lt;em&gt;Esus4&lt;/em&gt;, the program tells me:
&lt;blockquote&gt;Main&amp;gt; scale2notes $ Scale (read &quot;E&quot;) Major
[E,F#,G#,A,B,C#,D#]
&lt;div&gt;Main&amp;gt; chord2notes $ Chord (read &quot;E&quot;) Sus4&lt;/div&gt;
&lt;div&gt;[E,A,B]&lt;/div&gt;&lt;/blockquote&gt;
Something else I forgot is:
&lt;blockquote&gt;intervals Ionian = [2,2,1,2,2,2,1]
intervals Major = Ionian
intervals scale = shift (intervals Ionian) (rank scale)
where rank ...&lt;/blockquote&gt;
So, every scale is really a shift of the major (well, any) scale, which is actually called the Ionian scale.

This program might come in handy as a summary of music theory in case I forget these things again :)
</content>
  </entry>
  
  <entry>
    <title>:set noexpandtab</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/set-noexpandtab"/>
    <updated>2008-06-18T00:00:00+02:00</updated>
    <id>http://el-tramo.be/blog/set-noexpandtab</id>
    <content type="html">Google recently published a &lt;a href=&quot;http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml&quot;&gt;C++ style guide&lt;/a&gt;, containing all the rules that Google code adheres to. Many of the style tips are quite sensible, and well accepted by many developers out there. However, I was surprised to find the following rule:
&lt;blockquote&gt;&lt;span&gt;&lt;strong&gt;Spaces vs. Tabs:&lt;/strong&gt;&lt;em&gt; Use only spaces, and indent 2 spaces at a time.
&lt;span style=&quot;font-style: normal;&quot;&gt;We use spaces for indentation. Do not use tabs in your code. You should set your editor to emit spaces when you hit the tab key.&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/blockquote&gt;
I never really understood why so many people have such a hatred towards tabs. Is it just because they have seen code where some editor has mixed tabs with spaces (which of course results in a horrible mess)? Or do they have valid counter-arguments, even when tabs are used consistently?

&lt;!--more--&gt;Why would you force presentation of your code upon someone if you don't need to? If you use tabs, you don't need rules like ‘indent 2 spaces at a time’: I can choose whether I want 2 spaces, 4, or whatever indentation I feel most comfortable with when reading code. 

After some discussion, the pro-space people typically come up with the following piece of code, where they have their parameter names aligned:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;void SomeClass::someMethodName(int parameterA,
                               unsigned int parameterB,
                               std::string parameterC)
{
    ...
}&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
No, you can't do that with tabs, that's true. But why would you do that in the first place (unless you're using an editor which does such annoying auto-formating automatically)? When your method name gets too long, it won't fit on your screen anyway. Why don't you indent 2 tabs for your parameter names instead:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;void SomeClassWithALongName::someMethodName(
        unsigned int parameterA,
        int parameterB,
        std::string parameterC)
{
    ...
}&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
Another popular argument is aligning member declarations:
&lt;blockquote&gt;&lt;code&gt;
&lt;pre&gt;FooClass foo_;
Bar      bar_;&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
Same remark: why would you do this? Does it really make your code more readable? Besides, it doesn't scale: whenever someone comes along and has to add a &lt;code&gt;VeryLongClassName&lt;/code&gt; member, your alignment  will be messed up, and you will have to change every member again.

My opinion: let the person who reads your code decide how he/she wants to see it, and lay out your code in a way it is flexible to be seen with different view settings. This avoids lengthy discussions about whether tabs should be 2, 3, 4, or 13 spaces wide. Besides, people have been preaching the separation of presentation and content for ages for HTML and CSS, why would this be any different for code?
</content>
  </entry>
  
  <entry>
    <title>Unit testing method overrides</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/unit-testing-overrides"/>
    <updated>2008-03-07T00:00:00+01:00</updated>
    <id>http://el-tramo.be/blog/unit-testing-overrides</id>
    <content type="html">It probably happened to most of us developers before: while refactoring, you change the name of a virtual method, but forget to change the name of the overriding method in one of your derived classes. Compilation works fine, all unit tests pass, but your program doesn't work: the overriding method is never called. Java (and C#) programmers can avoid this problem by putting &lt;code&gt;@Override&lt;/code&gt; (and &lt;code&gt;override&lt;/code&gt;) in front of their methods, which causes the compiler to print out an error message if the method is not overriding anything. However, most other languages leave you hanging with this problem. Luckily, with statically typed languages like C++, you can avoid these bugs by slightly adapting your unit tests.

&lt;!--more--&gt;

Suppose you have a base class &lt;code&gt;Oracle&lt;/code&gt;, and a derived class &lt;code&gt;GeekyOracle&lt;/code&gt;, defined as follows:
&lt;code&gt;
&lt;pre&gt;  class Oracle {
    virtual int getLckyNmbr() {
      return 13;
    }
  };

  class GeekyOracle : public Oracle {
    virtual int getLckyNmbr() {
      return 42;
    }
  };&lt;/pre&gt;
&lt;/code&gt;

 

Being a good developer, you unit test &lt;code&gt;GeekyOracle&lt;/code&gt;'s &lt;code&gt;getLckyNmbr()&lt;/code&gt;:
&lt;code&gt;
&lt;pre&gt;  void GeekyOracleTest::testGetLckyNmbr() {
    GeekyOracle* oracle = createGeekyOracle();
    assertEquals(42, oracle-&amp;gt;getLckyNmbr());
  }&lt;/pre&gt;
&lt;/code&gt;

 

After a while, you realize that using abbreviations in method names is actually not very good practice, so you change your method name to something more readable:
&lt;code&gt;
&lt;pre&gt;  class Oracle {
    virtual int getLuckyNumber() {
      return 13;
    }
  };&lt;/pre&gt;
&lt;/code&gt;

 

However, when you compile your program now, it is behaving completely different than before, although all unit tests passed. It requires manual debugging to find out that you forgot to rename &lt;code&gt;GeekyOracle&lt;/code&gt;'s method as well, causing &lt;code&gt;Oracle&lt;/code&gt;'s &lt;code&gt;getLuckyNumber()&lt;/code&gt; to be executed, no matter what oracle you instantiate. Your unit tests didn't catch this, because they test every method of a class separately, but don't test the class being used in your application.

A way to check for this in your unit tests is to make sure that, when testing a virtual method, you always call it on an object that is &lt;strong&gt;statically typed with its base class&lt;/strong&gt;. In our example, our unit test would call &lt;code&gt;getLckyNmbr()&lt;/code&gt; on an &lt;code&gt;Oracle&lt;/code&gt; instead of on a &lt;code&gt;GeekyOracle&lt;/code&gt;:
&lt;code&gt;
&lt;pre&gt;  void GeekyOracleTest::testGetLckyNmbr() {
    Oracle* oracle = createGeekyOracle();
    assertEquals(42, oracle-&amp;gt;getLckyNmbr());
  }&lt;/pre&gt;
&lt;/code&gt;

 

This unit test will fail to compile when you change the method name of the base class, which avoids you end up with the silly bug in your program.

If your test allocates your object under test on the stack (e.g. for simplicity), you could use the following alternative:
&lt;code&gt;
&lt;pre&gt;  void GeekyOracleTest::testGetLckyNmbr() {
    GeekyOracle oracle;
    assertEquals(42, ((Oracle*) &amp;amp;oracle)-&amp;gt;getLckyNmbr());
  }&lt;/pre&gt;
&lt;/code&gt;

 

However, since this degrades the readability of the test, I prefer to add a static helper method to my unit test, and write the test as follows:
&lt;code&gt;
&lt;pre&gt;  static inline Oracle* p(Oracle&amp;amp; foo) {
    return &amp;amp;foo;
  }

  void GeekyOracleTest::testGetLckyNmbr() {
    GeekyOracle oracle;
    assertEquals(42, p(oracle)-&amp;gt;getLckyNmbr());
  }&lt;/pre&gt;
&lt;/code&gt;

 

 

Although I used C++ in my example, this technique should work for all statically typed languages. Advocates of dynamically typed languages claim they can compensate the lack of types by unit testing their classes properly; I wonder how they avoid this kind of problems, though.
</content>
  </entry>
  
  <entry>
    <title>Qtopia Greenphone Grant</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/greenphone-grant"/>
    <updated>2007-08-15T00:00:00+02:00</updated>
    <id>http://el-tramo.be/blog/greenphone-grant</id>
    <content type="html">A month or 2 ago, I applied for the Qtopia Greenphone Innovation Grant Program, an initiative from TrollTech to promote the development of applications for their Linux-based Qtopia Greenphone. I probably won't surprise anyone by saying that I sent in a proposal about writing a good, cross-platform, mobile Jabber/XMPP client. Anyway, I was very excited to receive a mail from TrollTech yesterday, stating that my proposal was accepted by their review panel! As an applicant, I will be receiving a shiny new Greenphone, together with a Qtopia SDK to develop against. Deadline for submitting my application: October 31st. Let the coding begin.
</content>
  </entry>
  
  <entry>
    <title>'The First 10 Prolog Programming Contests' available for downloading</title>
    <author>
      <name>Remko Tronçon</name>
      <uri>http://el-tramo.be/about/</uri>
    </author>
    <link href="http://el-tramo.be/blog/ppcbook"/>
    <updated>2006-07-01T00:00:00+02:00</updated>
    <id>http://el-tramo.be/blog/ppcbook</id>
    <content type="html">Exactly one year after we finished it, our book &lt;em&gt;`The First 10 Prolog Programming Contests'&lt;/em&gt; is now &lt;a href=&quot;http://www.cs.kuleuven.be/~dtai/ppcbook&quot;&gt;freely downloadable&lt;/a&gt;. On the home page of the book, you will also find the source code of all solutions presented in the book. Below are some pictures of the 'deluxe' edition of the book, hand-made by &lt;a href=&quot;http://mp.lanemetonne.be&quot;&gt;my mom&lt;/a&gt;.

&lt;!--more--&gt;
&lt;div style=&quot;text-align: center&quot;&gt;&lt;a href=&quot;http://el-tramo.be/files/ppcbook/ppcbook_1.jpg&quot;&gt;&lt;img src=&quot;http://el-tramo.be/files/ppcbook/ppcbook_1_thumb.jpg&quot; alt=&quot;ppcbook 1&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://el-tramo.be/files/ppcbook/ppcbook_2.jpg&quot;&gt;&lt;img src=&quot;http://el-tramo.be/files/ppcbook/ppcbook_2_thumb.jpg&quot; alt=&quot;ppcbook 2&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://el-tramo.be/files/ppcbook/ppcbook_3.jpg&quot;&gt;&lt;img src=&quot;http://el-tramo.be/files/ppcbook/ppcbook_3_thumb.jpg&quot; alt=&quot;ppcbook 3&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://el-tramo.be/files/ppcbook/ppcbook_4.jpg&quot;&gt;&lt;img src=&quot;http://el-tramo.be/files/ppcbook/ppcbook_4_thumb.jpg&quot; alt=&quot;ppcbook 4&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://el-tramo.be/files/ppcbook/ppcbook_5.jpg&quot;&gt;&lt;img src=&quot;http://el-tramo.be/files/ppcbook/ppcbook_5_thumb.jpg&quot; alt=&quot;ppcbook 5&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
</content>
  </entry>
  
</feed>

