rss

Anonymous methods make elegant proxies

2

Category : C#, Design

Just stumbled on that fact some times ago.

Well at first some background, with C# 2.0 appeared a new usage of the keyword delegate in form of anonymous functions or closure (although they aren’t really new in themselves).

One of the most useful feature of anonymous method is the concept of ‘variable capture’ but even though it proves useful here it’s not the main part of the pattern.

Together with this C# 2.0′s anonymous method feature, the compiler also get enhanced to allow delegate type inference for variable and parameter. Indeed before C# 2.0 you had to explicitly initialize a delegate in order to use it :

private delegate void Func(int someInt);
Func func = new Func(MyMethod);

With C# 2.0 this dummy syntax is removed and one can write the more concise :

private delegate void Func(int someInt);
Func func = MyMethod;

But what this had to do with our subject ? Well you may know it or not but when you create anonymous method using only the delegate keyword (i.e. without any definition of parameters) the compiler automatically adapt the signature of the anonymous method to the underlying delegate. In other word it’s a cheap way to make Proxy (where Proxy refers to the design pattern that might be described as : « Providing an interface to a class which doesn’t implement it natively »).

How can we use this feature then ? Say you want to run a method on a dedicated thread (like the target toolkit’s mainloop in Circ) then this method (that we will call StartPresentation) might want some information to initialize the presentation like, for example, a reference to the main controller, the title for the Window etc… (which used to be the case in previous version of Circ).

Problem : the Thread class allow only a method which signature correspond to the ThreadStart delegate (defined as public delegate void ThreadStart();) in its constructor. Unfortunately, in our case, it doesn’t correspond the signature of our StartPresentation method. Nonetheless, thanks to some delegate magic, you can circumvent this limitation with this pattern :

public void InitializePresentation()
{
    // Well usually the MainControl isn't defined
    // there but it shows closure's ability to
    // capture outer variables
    MainControl ctrl = new MainControl();
    Thread thread = new Thread(delegate {
            StartPresentation(ctrl, "Window's Title);
    });
    thread.Start();
}

And here you go ! The anonymous method signature is automatically mapped to the one of the ThreadStart’s delegate by the compiler without any developer intervention.

Hourraa !

Category : Linux, Ubuntu

Yay, I finally got the latest git version of the Avivo driver to run on my Dell Inspiron 6400 (Ati X1400 Mobility card). /me and my computer’s battery/fans are happy :D (because now I can really enjoy the advantage of the latest Linux’s tickless feature).

PS : Just for the record, here is why avivo used to do with my desktop in earlier version (funny but not really usable :’) ) :

Avivo funny screenie

Big kudos to Jérome Glisse for his hard work on this !

Important message : "You have to update your blog"

1

Category : C#, Circ, General

Heya long time no post indeed which doesn’t mean that nothing had happened since July ;) .

First of all I have to talk about my job which is taking most of my time lately and which, unfortunately, isn’t computer-related at all :D . It’s more an ingrat-summer-job-for-yet-undiplomed-student type of job (cleaning, tiding and sorting clothes and stuff in a warehouse) but to make it more enjoyable I still managed to snake my laptop in to do some hacking while I’m bored :P . Basically it means that Circ has well progressed and, in my opinion, progressed enough to issue a first beta.

The list of changes for this beta are :

  • Modified some parts of Circ to work in a APM (Asynchronous Programming Model) way. Now the GUI shouldn’t freeze anymore.
  • Added and updated plugins. New interesting plugins are the Autoconnect and Autojoin ones which, as their names imply, allow you to connect automatically to some network & channel. There is a basic configuration in ~/.Circ/Circ.conf that you can modify at your wish (I won’t explain here because most of the options are straighforward). There is also a Banshee plugin which let you spam your friend with what your Banshee is currently playing thanks to some D-Bus goodness, just type /np and see for yourself ;) .
  • LOT of cleanup and refractoring of the code base (mainly in the PAC glue pattern and in the Gtk# frontend) which is resulting in more abstracted relation between Presentation and Controller (in fact now it shouldn’t be necessary to rewrite a set of controllers for a particulate frontend).
  • Some UI work like an improved notebook label widget (alert you when you receive message, has a close button…)
  • Switched VCS from Git to Mercurial which I found for the moment more simpler and direct than Git. The new webinterface and pull url is : http://hg.sharesource.org/circ/ . Use this one if you want to peek at the source code as the beta package contains only binaries (Monodevelop is still doing strange thing with me for the packaging part :( ).
  • A fair number of bug fixes which had been around for some times.

And finally the link to get these new features : http://netherilshade.free.fr/circ/Circ-0.1-beta1_bin.tar.bz2 .

Please delete your ~/.Circ folder before you run the new version.