Thursday, March 24, 2016

Reflection in C++

Topics: C++, Serialization, Reflection, Templates, JSON, Visitor pattern

I've posted a new article here titled "Automating Object Serialization/De-serialization in C++":

 http://inspired-code.blogspot.com.au/p/automating-object-serializationdeserial.html

Overview


Some languages support reflection better than others. C++ has its runtime type information system which is reasonably limited. It won't help you much if you are doing serialization and de-serialization, so you end up writing a lot of boiler-plate code which feels like you are repeating yourself a lot.

Serialization though is just one example. In the article I give a bit of a run down on some of the alternatives and present a solution which reduces how much you have to repeat yourself.
In the end, this is an example of all you should end up needing to write to define and declare a struct with members and the code needed for being able to serialize/de-serialize it:

    DECLARE_CLASS(Color)
       DECLARE_MEMBER(int, red)
       DECLARE_MEMBER(int, green)
       DECLARE_MEMBER(int, blue)
    END_DECLARE_CLASS

That is it. No duplicated info anywhere. I show how this isn't locked in to just serializing/de serializing too, and give examples of hashing and other uses. In follow up articles, I can elaborate on some of those other uses and the details of those.

Full article here:

  http://inspired-code.blogspot.com.au/p/automating-object-serializationdeserial.html


No comments:

Post a Comment