Feb 192017

I just recently joined stackoverflow after having passively used it for years. I usually found my questions already answered or could figure stuff out by myself.

So I thought that I could give something back by answering questions. It turns out that by answering questions you may actually learn something new. Awesome Zen wisdom gained! đŸ˜€

Some answers of mine that fall into this category:


Feb 152017


During C++ development for the Windows OS one often has to convert a stream from UTF-8 (e. g. an XML-File) to UTF-16 (standard encoding for almost all OS APIs).

Usually I do this by creating a std::wistream and imbue() it with a conversion facet like this:

Pretty standard, but what if you are just given an std::istream (e. g. as parameter of a function you can’t or don’t want to change to std::wistream)?

I found two solutions that are almost as straightforward to use as imbue(). The first one requires only standard C++11, the second one utilizes Boost.IoStreams.

Standard C++11 Solution

I just provide a code example with explanations in the comments. You may look up std::wbuffer_convert for further details.

Boost.IoStreams Solution

What is Boost.IoStreams? It is basically a library to easily create custom streams that are compatible with standard C++ streams.

Using Boost.IoStreams the code goes like this:

Some explanations:

  • Line 9 creates a “device” which is a concept that exists solely in the boost::iostreams library. This type is just used in the following line to create the actual standard-compatible stream type.
  • Line 18 creates an instance of that stream which just wraps the std::istream parameter. The boost::ref is required because Boost.IoStreams requires devices to be copyable (a standard stream is just a model of device in terms of Boost.IoStreams). Standard streams are by definition non-copyable, so we must work around that by using boost::ref which is a copyable reference-wrapper (see Design Rationale of Boost.IoStreams).


Both the standard C++11 and the Boost.IoStreams solution are pretty straightforward to use. There are basically just one or two additional code lines to wrap a wide stream around the narrow stream (not counting the includes and the using-statements)!

I prefer the standard C++11 solution because it has less dependencies. The Boost.IoStreams solution might be useful in a context where one already works with Boost.IoStreams though.

Feb 172015

Short answer: we don’t.

But it comes in handy.

Consider this code:

So, in case of an error this code might show the following message to the user:

“Could not connect to game server. Error: 19970829”

Wouldn’t it be better if the user got the following message instead?

“Could not connect to game server. Error: skynetTookOver”

No it wouldn’t !!! Everyone would panic!!!

You get my point. It is often useful to translate internal program data into something that the outside world can understand (or the other way around). And it would be nice if we could do so in an automated fashion, instead of having to hand-write all the “translation” code. This is actually what we call “reflection”.

Enum Reflection

In this article I’ll focus on enum reflection, because enums are simple and they are widely used. So you get an immediate benefit from reflection. In the next article I’ll write about general reflection of game objects.

To translate an enum value into a string we could write a function with a switch/case statement:

Easy enough, but this quickly becomes unmaintainable when you add new enum values as you have to do so in two places. It is also a source of errors if you forget to extend the function.

With the help of some boost preprocessor magic we can write a little macro that allows us to write this instead:

This declares the enum and makes it reflectable at the same time. Cool eh? When you get used to the syntax đŸ˜‰
We can now use our enum like this:

It will print what you expect:

“Could not connect to game server. Error: skynetTookOver”

You can also use enumToStr() instead of boost::lexical_cast if a const char* is all you need.
To convert from string back to enum there is a strToEnum() function.


The macro also provides overloads of the stream operators << and >> so we can write an enum directly into a stream or read it back from there in string form. This is in itself very useful for writing human-readable logs.

Where it really shines though, is when you use enums with other libraries, like boost::property_tree. The property_tree class is very handy for reading and writing program configuration. When you declare an enum via the DECLARE_ENUM macro, you can put enum values directly into the property_tree and they will automagically be converted to string (or the other way around). A complete example:

This program simply writes an enum value into a JSON file and reads it back to verify it. Note that there isn’t any explicit conversion to string! It all works automagically because of the operator << and >> overloads provided by the DECLARE_ENUM macro. This example wouldn’t even compile if Fruit was just a “normal” enum declaration.

All of them

Also handy at times is the ability to iterate over the values of an enum. For instance, if you have a level editor, you may want to add all possible values of a game option to a combobox.

This would add the names of all 5 beater shapes to the combobox. If I later add more beater shapes, there is no need to modify the editor code at all!

There isn’t much more to it. To use this macro you only need to include a single 250 lines header, which you can download below. The only required dependency is the boost.preprocessor library (tested with version 1.58.0). For the enumValues template you also need boost.range and boost.iterator.

I leave it as an excercise for the reader to provide wchar_t support ;-).

Have fun and keep coding!

I want it!

Download EnumReflection.zip

Apr 042013

Recently I developed an HTML+Javascript based application that embedded the Internet Explorer browser control on the Windows platform.

MFC (in my case VC++ 2005) makes this task quite easy – so it seems. Just create a dialog-based application using the wizard, add the web browser ActiveX control to your dialog, call Navigate() on your browser object and you are done. This is described in many tutorials on the web, so I won’t go further into this.

At first everything looked fine, but when I opened the HTML in standalone IE, my Javascript (jQuery) menu was animated much more fluid compared to the embedded IE.

Continue reading »

Aug 082012

I was astonished when I first saw the possibilities of what is called optical feedback in a presentation. I mean that you’re actually be able to create fractals without a computer, even in realtime! You just need a video camera, a monitor and a mirror.

Since I’m a software guy, I immediately thought of reproducing these effects in software. It should be possible to capture the current image of the screen and render it smaller, possibly rotated and mirrored to the same screen, thus creating a feedback loop.

Continue reading »