Probably Dance

I can program and like games

Tag: Reflection

Quickly Loading Things From Disk

Loading things from disk is a surprisingly unsolved problem in C++. If you have a class that you want to save to JSON or binary and load back later, you’ll probably end up writing custum parsing code. Solutions like Protobuf or Flatbuffers generate custom classes that are… how should I put it… bad code. They force you to work in a parallel type system that’s not quite as powerful as C++ and that doesn’t allow you to write custom member functions. You pretty much have to wrap anything that they generate. Boost serialization is a better solution in that it allows you to load and save existing classes, which allows you to keep your code quality higher, but it’s unable to write human readable files. The default implementation that it provides is also surprisingly slow. I blame it on the fact that it loads using std::istream.

Based on frustrations with existing solutions I decided to write something that can

  1. read and write existing, non-POD classes. No custom build step should be required
  2. write human readable formats like JSON
  3. read data very quickly (I care less about how fast it writes stuff. I work in an environment where there is a separate compile step for all data)

I came up with something that is faster than any of the libraries mentioned above.

Read the rest of this entry »

Template Metaprogramming, Reflection in C++0x

So I’ve been working on the serialization code for our engine. And I’m finally understanding the more complex aspects of template metaprogramming, and it turns out that there are some neat things you can do with it that aren’t directly related to serialization.

OK let’s say you have an editor in your game, and in this editor you have a button “Add Component” which adds a new component to an entity. That button would open a pop-up menu, where you can select from all your component types and after selecting one, a new instance of that component-class is created and added to the entity. How do you populate that list? My solution using template metaprogramming and reflection is after the jump:

Read the rest of this entry »