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

In part I of this series I discussed Xerces’ UTF16-based string representation, a common deallocation pitfall for such strings, and how to convert to and from such strings correctly by using C++ RAII techniques. For the RAII techniques I presented one solution using boost::shared_ptr, and one more efficient solution using a home-brewed ownership transfer class, cppx::Ownership. And I promised to next (i.e. in this installment) discuss how to do it even more efficiently by using wchar_t strings as the program’s native strings, and detecting the minimal amount of work needed for each conversion.

[… More] Read all of this posting →

Advertisement

[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 →

[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 →

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 →