giovedì 26 marzo 2009

The Theory at the End of Time

Qualche novità di poco conto, ma messe tutte assieme possono valere qualche riga.

Ho finalmente il badge dell'ICTP, numero 8000359 che è primo (posso capire che non ve ne importi molto...) e ha un primo gemello. Mi permette di acedere alla mensa e alla biblioteca la sera. Non che voglia provare così presto...

Assieme al badge (che porta a cinque il numero di tessere di identificazione che possiedo) ho ritirato anche la fondamentale chiavetta per la macchina del caffè. Ho dovuto dividere il mio mazzo di chiavi in due, perchè iniziava a diventare troppo ingombrante.

Ancora mi chiedo se ero io ad essere addormentato o abbiamo davvero discusso se inventeranno prima il viaggio interstellare o un metallo auto-pulente, per astronavi sempre splendenti. Insomma, quanto sono credibili le navi dei prequel di Star Wars?

Magari lo sapevate, ma sembra che il cinese abbia un sacco di prestiti linguistici dall'inglese, per cui in Cina si salutano con "Hi" o "Hello". Perlomeno curioso.

Ho le foto del capodanno persiano, eccole

Fahimeh e Zhaleh, del settore SBP.


Ma le provenienze erano piuttosto varie...



Il titolo viene dal talk di Juan Martín Maldacena, che ha ricevuto oggi la medaglia Dirac all'ICTP, assieme a Joseph Polchinski. Mentre si chiedeva che tipo di universo ci aspetti ai confini del tempo (ovvero sul bordo di uno spazio-tempo a curvatura negativa) ha consigliato agli astanti - che erano tantissimi, tanto che mi sono seduto sui gradini della lecture hall - di leggere "Il ristorante alla fine dell'universo" di Douglas Adams!

domenica 22 marzo 2009

Automatic bindings to C++ libraries

As I wrote last time, this post will be about lqt, and first of all about what lqt does.

Qt's and KDE's native language is C++, however some people wants to use them in a different language (in my case Lua) so many bindings have been created for these libraries. There are several methods you can use to do this.

The simplest way in principle is to write the bindings by hand: most languages have some way to communicate with C/C++ so, by writing wrappers for all the functions and methods in the library, you can create the modules you want. For example if you have this

int foo(int);

you can write this (in C)
int call_foo(lua_State *L) {
int ret;
int arg;
arg = lua_tointeger(L, 1); /* take the first argument */
ret = foo(arg);
lua_pushinteger(L, ret); /* give the return value to Lua */
return 1;
}

This function can then be used when a Lua script calls foo().

If you had instead "int bar(int);" only a single line needs to be changed, while the rest is boilerplate code. This fact is not Lua-specific at all and in fact many solutions have been created to make bindings more automated. Java bindings to Qt are created this way, and also Swig and SMOKE just to name a few.

Most of the automated generators require an "API description" file that tells the generator which functions, classes and methods it should wrap, with their arguments and members. This is the case for QtJambi and Swig (and tolua/tolua++). So someone has to write down this description in the first place, but for big libraries it is not so easy. QtSoftware can pay someone to write and mantain this file, but what about KDE?

KDE has SMOKE that takes a better approach because it parses the C++ code, with almost no manual work required. The headers are changed; run it again; you have new bindings; stop.

(SMOKE has many other qualities, such as being language agnostic. Stuff for another post/author)

What lqt does is basically the same as SMOKE, but specialized for Lua, and using a different tool. While SMOKE uses the perl based "kalyptus" to generate the wrappers, I used the parser written by Roberto Raggi. Since a well written C++ parser like this is officially a Big Thing(TM), there are uncountable copies and versions around the marble (the last one I'm aware of is in QtCreator). Thus it's difficult to make it clear which one you are talking about. The one lqt uses was written for KDevelop4 and used in QtJambi. I usually call it RPP for the sake of brevity, until Roberto gives it an official name and versioning system.

My little tool "cpptoxml" just dumps the data structure of RPP into an XML. This simple step has drawn some attention to cpptoxml, while the main work is actually done by RPP. Then a Lua script takes this XML and writes the binding modules. Peter wrote a sane build system, so the bindings can also be used on Windows and OSX. And in the end you can write your Lua program using Qt:
require'qtcore'
require'qtgui'

app = QApplication(1 + select('#', ...), {arg[0], ...})

hello = QPushButton(QString("Hello Planet!"))
hello:resize(140, 40)
hello:show()
app.exec()

I was planning to show some complex example with cool stuff like WebKit but I'd rather leave something for the next post.

sabato 21 marzo 2009

Happy 1388

Abbiamo festeggiato il nuovo anno Persiano ieri, sulla terrazza dell'ICTP, nonostante il vento gelido. I dolci tipici iraniani sono davvero molto dolci: per fortuna non assumo molto zucchero normalmente - nemmeno nel caffè. C'era frutta secca in abbondanza (soprattutto pistacchi), la musica a tema e un piccolo tavolino con le "Sette S": sette oggetti il cui nome inizia con per S e rappresentano altrettanti auguri per il nuovo anno. Le foto non mi sono ancora arrivate.

Ho davvero visto "The Millionaire" e sono convinto che l'Oscar lo avrebbe dovuto vincere "Milk" o "Gran Torino", se non fosse stato completamente snobbato. I premi per il montaggio e la fotografia erano effettivamente meritati.

D'altra parte come è noto gli Oscar non vengono assegnati ai film che vengono considerati troppo "politici". Che è un modo per indicare quei film che mostrano (o suggeriscono) che qualcosa che non va. Oltre che un modo un po' ipocrita di giustificarsi, visto che non parlare di politica è proprio un modo di fare politica - e non sono certo il primo a dirlo. L'Academy funziona così, basta saperlo.

A proposito di "Gran Torino", se non lo avete visto non dovete assolutamente perderlo.



Infine ho visto "Watchmen", che è stato meglio di quanto mi aspettassi. Lo hanno reso leggermente più cupo di quanto fosse il fumetto, ma l'atmosfera è comunque abbastanza fedele. Durante i titoli di testa ci sono molti episodi che non hanno potuto inserire nel film, sarà divertente vederli al rallentatore appena ne avrò una copia. La voce di Manhattan non è veramente all'altezza. Non capisco le polemiche sul finale modificato, dato che il risultato è lo stesso. Il mezzo con cui viene raggiunto è del tutto secondario - un vero e proprio macguffin. Credo lo abbiano ammesso gli stessi autori.

In piscina sono tornato sotto i trenta secondi (in cinquanta metri). Anche se non è strepitoso mi ritengo soddisfatto lo stesso. Comunque, il risultato migliore è esserci andato tutte le volte.

Prima o poi troverò un relatore. A costo di invocare il suo nome per tre volte a mezzanotte in un incrocio deserto.

martedì 17 marzo 2009

Hello Planet

Hello everyone,

I finally added my blog to the PlanetKDE, so here is the obligatory "Hello Planet" post.

First of all something about me: I'm an italian PhD student in Physics. I get to deal routinely with simulations and numerical analysis, which in my field means mostly FORTRAN. If you ever dealt with that you can probably feel my pain. I stick to C++ whenever I can, and in these cases Eigen is a life saver.

But sometimes I have some task which is not performance critical and is better suited for a scripting language. Then my choice is Lua.

My contributions to KDE include three things: Qt bindings for Lua, called "lqt" (with the help of Peter Kümmel), a tool that transforms C++ into XML ("cpptoxml") and a Kross backend for Lua named "kloss". I know, I suck at names...

If you look at the logs (or the fine graphics created by Paul Adams) you will notice that I commited two or three times in a several months period (my account dates from the KDE Bindings meeting in summer 2008). That is because I mostly develop in a separate repository and then commit a lot of changes at once.

You can find the most recent source of lqt here, with the most up to date version of cpptoxml. The Kross backend is kept in playground/bindings/kloss.

I plan to talk about what is lqt, how it works and what are our plans in the next posts.

I will also occasionally talk about how I hate FORTRAN, dislike C/C++ and feel the whole world should get rid of any other language to adopt Lua in every possible situation. Random ideas that somehow manage to evade the sanity filter may be spotted too...

domenica 1 marzo 2009

Back in Pisa. Back from Pisa.

Yuasa-san è venuto in Italia per pochi giorni e non potevo farmi sfuggire l'occasione di incontrarlo. Come al solito è stato molto utile e abbiamo risolto molti problemi. Principalmente lui, devo confessare. Sono abbastanza stanco, sia perchè abbiamo lavorato in continuazione, sia per il viaggio in treno di oggi.

Mi ha fatto piacere rivedere gli amici, e mi dispiace per quelli che, inevitabilmente, non sono riuscito a incrociare. Pisa continua a non piacermi, ma a piccole dosi non è terribile, e per tre giorni il tempo è stato persino bello.

Dall'ultimo aggiornamento ho visto tre film: "Il curioso caso di Benjamin Button", "The Reader" e "Milk". Quest'ultimo sembra aver sofferto una sorta di censura velata, perchè è risultato impossibile vederlo in prima serata, e comunque non è stato proiettato prima che vincesse l'Academy Award. Per cui sono tanto più contento di averlo visto lo stesso. É un film fantastico.



Devo ancora vedere "Millionaire" che era l'alternativa per quella sera. Le aspettative sono buone.

Sempre sugli Oscar: Oktapodi è stato nominato come miglior cortometraggio animato. Il vincitore deve essere veramente spettacolare e cercherò di vederlo presto.

Gli altri due film non mi sono dispiaciuti e valgono la pena di essere visti se non c'è nulla di meglio. Un'affermazione autoevidente, devo ammettere, ma spero che il senso sia chiaro.