Sunday, June 7, 2009

Pythonizing C++

Yep, I'm still working with that possible GUI designer. It's been fascinating. To do a designer properly, you need some way of dealing with all of the different controls in an abstract way of dealing with properties and methods. The main problem for this in BeOS is that C++ doesn't like dynamic interfaces. At all. Luckily, I've managed to take some cues from Java and Python and the result seems to be both pretty flexible and reasonably elegant. Check this out:

PObject *o = NULL;
o = NewObject("PWindow");

StringValue sv("FooWindowTest");
o->SetProperty("Title", &sv);
RectValue rv(BRect(100,100,500,400));
o->SetProperty("Frame",&rv);

BFile file("/boot/home/Desktop/test.plg",B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
BMessage msg;
o->Archive(&msg);
msg.Flatten(&file);

In these reasonably few lines of test code, I not only manage to create a window in the upper left corner of the screen in a thread-safe fashion, but I can even save it to disk and recreate it in the exact same state as I left it, and the code is refreshingly short:

BFile file("/boot/home/Desktop/test.plg",B_READ_ONLY);
BMessage msg;
msg.Unflatten(&file);
PObject *o = (PObject*)UnflattenObject(&msg);

The backend code is only 5 or 6 files of reasonable length, but even so, I'd be hard-pressed to do the same kind of trick using regular BWindow and BMessage code in anywhere near the same amount of code. Then again, maybe I'm just overly excited. *shrug*

What's the significance of this technical rambling? The designer, assuming no other major roadblocks from here, will be very flexible, relatively easy to code, and pretty simple to extend with classes not bundled with the operating system. Code generation may not be all that difficult, either, but only time will tell on that one.

No comments:

Post a Comment