[cppx] Xerces strings simplified by ownership, part I.

For Windows, where wchar_t corresponds in size and encoding to XercesXMLCh, all that’s needed to convert from a wchar_t literal to Xerces XMLCh string is, at most, a little reinterpret_cast… But that won’t work on a system with 4 byte wchar_t or a system where wchar_t doesn’t imply Unicode. And although the non-Unicode systems are perhaps not your top porting priority, 4 byte wchar_t systems are common.

The wrapping that I start discussing in this posting, based on the cppx Ownership class, does a simple reinterpret_cast when it can (e.g. for a literal wchar_t string argument in Windows), and a dynamic allocation plus transcoding in other cases.

The Ownership wrapping of the result abstracts away the details of how the string is converted, and whether deallocation is needed. Thus, the calling code does not have to care. It gets raw null-operation efficiency where possible, and otherwise at least the same convenient notation + C++ RAII based safety. :-)

[… More] Read all of this posting →

[cppx] Ownership with custom deleter.

The cppx Ownership class is like std::auto_ptr with custom deleter and without implicit pointer conversions. In my next posting I’ll show how to use it to simplify dealing with Xerces strings, in an efficient way. It’s also convenient for many other things. :-)

[… More] Read all of this posting →

[wordpress] Broken stats?

It’s official: all modern software is broken. You thought perhaps that it was only on your machine that Thunderbird messes up quoting and gets confused about the number of unread messages, or that Firefox, if left to itself, will gobble up memory like mad, or that Internet Explorer will invite all kinds of malware (I don’t use IE), or that Microsoft Update will bring your machine to its knees, or, the Windows volume control taking one thousand times longer to respond to a click than it should? No, it’s like that everywhere, for all software, it’s just that most people don’t notice, and those who do notice it probably mostly think that it’s something local.

And I think that the WordPress stats, visually impressive as they are :-), are just as broken as most modern software.

Yesterday it reported some 64 views of my blog. This morning, 11 or 12 views or what it was, and it hasn’t changed through the day, for ~12 hours! Similarly, the other dips downward you see in the graph above also corresponded to about 12-hour periods with no traffic at all, which I find hard to believe (I also find the peak at 180-something views on the second day of my blog very hard to believe!). And, for example, according to the stats the steady stream of views yesterday of a certain posting came from nowhere, no referral. And, for that matter, the “featured blog” status in tag “programming” (and how did I get that? I have no idea) apparently generates no traffic at all.

But, again, if it is the case that the stats are broken, then that’s no different from most modern software. I can’t think offhand of any program or programming language implementation that I use that isn’t full of bugs (sometimes I report some particularly annoying bug, but most times I don’t bother: it’s just too much). It boggles the mind.

[cppx] How to do typed optional arguments in C++98

How can you have strictly typed optional arguments in C++98? Well, for a free-standing routine or just a single class you can use the Named Parameters Idiom, essentially just stuffing all those options into an object that has chainable setter methods, as discussed in the C++ FAQ. But when you get tired of implementing the Named Parameters Idiom ad-hoc in each and every case, or where you need to e.g. extend the set of options in a derived class, you may consider a reusable solution such as the one I’m presenting here.

[… More] Read all of this posting →

[Thunderbird] Adding a quoting options GUI to Thunderbird

Actually I didn’t set out on Sunday to add quoting option choices to Thunderbird’s preferences dialog. My goal was a loftier one, to fix the broken quoting in Thunderbird 3.x – Thunderbird 2 had its small quirks, but Thunderbird 3 collapses any sequence of spaces to a single space in quoted text, when you’re replying to a flowed format message. It rather restricts which Usenet messages one can respond to when using Thunderbird 3, and some other similar bugs (or moron’s fancy functionality, MFFs) also make life difficult for those who try to use it to submit patches. Hence the name of this extension, TBQuoteFix.

Paraphrasing former US vice president Dan Quayle: in a word, this extension may or may not live up to its name, later.

For now, as far as I got in the weekend it just reveals some “hidden” or “advanced” Thunderbird options, letting the user configure those via the ordinary options dialog:

I implemented this GUI side of things first because I already had nearly-finished code for it from my old NewsWorthy Thunderbird extension.

And I stopped there because I realized that I’d forgotten how excruciatingly slow it is to develop Thunderbird or Firefox extension code… It’s not for the impatient, or for anyone who has any pressing matters to attend to. Mostly, because so little is documented, it’s experimentation, including experiments to try to identify erratic behavior and devise more reliable work-arounds.

[… More] Read all of this posting →

Do it this way? Or that way? What’s best?

Long, heated debates, and long, painful agonizing over the choice, often result from “this, or that?”. E.g., return by value or take an in/out argument? The practical answer is often just “yes, both, thank you”, like Winnie the Pooh, and in some cases the practical answer is “a different way”, but for some mysterious reason the practical answer can be very hard to see.

[… More] Read all of this posting →

[cppx] C++98 constructor arguments forwarding (v2 posting).

OK, this is a version 2 of my last posting about constructor arguments forwarding in C++98… Anders Dalvander kindly pointed out (without using these words) that my initial motivating example was pretty silly since Boost already offers the functionality that I fronted as desirable, namely, since 2008, the boost::make_shared function. The original posting is still technically correct, and it’s still about something Practically Very Important™ that Boost apparently does not yet offer, namely reusable C++98-compatible constructor arguments forwarding. But my original posting (1) was sort of lying by omission, by not mentioning boost::make_shared, and due to that, (2) at least the casual reader would not care to read beyond the introduction! So herewith an updated version, with hopefully no lying by omission, and (cheers!) more examples & discussion! :-)

[… More] Read all of this posting →

[cppx] C++98 constructor arguments forwarding

Every time you dynamically allocate a new T object and give it to a boost::shared_ptr<T> for safekeeping, the boost::shared_ptr<T> will dynamically allocate a corresponding reference counter object. But dynamic allocations are usually Very Costly. So wouldn’t it be great if those two allocations could be reduced to a single allocation, a sort of cost-free boost::shared_ptr?

Uhm, well, yes? And one way is to specially design your type T to support reference counting and use e.g. boost::intrusive_ptr (unfortunately not adopted into C++0x). But this is an intrusive solution. And so it is not always applicable, and where it is applicable it may just transfer a runtime efficiency cost to a programmer’s time cost. So on reflection, while for the cases where it can be applied it is perhaps better than shared_ptr’s extra allocation, it’ not all that great a solution, really.

But wouldn’t it be even greater to have some non-intrusive shared pointer type like boost::shared_ptr that could manage this for you, a single allocation, without any special support from or requirements on your type T?

YES! :-)

That means that you specify your type T constructor arguments to the smart pointer constructor, and let the smart pointer do the allocation. I.e. the smart pointer takes responsibility not only for destruction but also for construction. However, this requires constructor arguments forwarding, which is not supported by C++98.

[… More] Read all of this posting →

I got an article in Dr. Dobbs Journal! :-)

Huh, I got an article in Dr. Dobbs! :-) About my cppx library’s optional parameter support. But I can’t see the code anywhere. OK, I’ll probably be blogging about the optional parameters here too, after first writing up about the cppx constructor argument forwarding (question is, should I discuss smart pointers before optional parameters or after? there are so many branches off the subjects I want to blog about!).

By the way, what’s normal view rate for a blog? First day, Thursday last week, I had just 1 view. Second day, 182. Third day, some 40-50, and it seems it’s stabilized there. Is that normal?

How to avoid post-construction by using Parts Factories.

I did my first GUI programming on an Atari 1040ST, using Modula-2 and MC68000 assembly language, focusing on such immensely interesting things as implementing general windowing, a resonable blitter interface, raw mouse and non-buffered keyboard access, smooth text cursor and scrolling, and so on, basics that (except for the latter two, or three or four) nowadays e.g. Windows provides, and that in most cases nowadays is down at abstraction levels that you seldom if ever visit. But on the Atari e.g. the quality of printed output depended on the program; each program did it It’s Own Way™. This was around 1986/1987, I was a student and I could not afford a PC, not even a harddisk, much less a Mac (my plan was to convert the Atari to a Mac on-the-cheap, through some half legal means, but I never got that far).

Oh, joys of the PC in the 1990s! Except for lacking coroutines Borland’s Turbo Pascal was much more practical and appealing than Modula-2, and Windows could do so much more – like general windowing, and like printing! I almost replaced Niklaus Wirth with Anders Hejlsberg as my programmer idol (Anders Hejlsberg later defected from Borland to Microsoft, creating the C# language; it’s as if he made a second try at expressing one general idea, the first time with Wirth’s Pascal as his starting point and the second time with Gosling’s Java as his starting point, managing to create much the same “feel” in two languages that at first glance appear to be very different, but which successively filled the same niche).

And, joys of the long-winded rambling introduction! :-) Anyways, with Borland’s OWL framework, as well as with Microsoft’ MFC framework, constructing a window object was a two-step procedure. First, you constructed your “blank” Pascal or C++ object (OWL was available for both languages, MFC only for C++), like building a house without electrical wires and plumbing. And then you called some init method to create a corresponding API-level window, managed by your object, like opening the walls and floors of your new house to stuff in the missing electrical wiring and plumbing. We may laugh today at this totally impractical and error-prone create-unusuble-and-then-fix-it-up Rube Goldberg like idiocy. Or, we could have laughed at it, except that that’s still how it’s most often done!

[… More] Read all of this posting →