Dabbling Seriously

Code, Coffee, and other Stuff.

Can you Serialize objects to and from C++?

In short. Yes. The part C++ is lacking is the reflection – so you have to write your own. I’ve been toying with this idea for quite a while and I think that I have an idea of how you would do this. The very first thing you would aim for is brevity – here’s my idea of what a reflection class would look like. Imagine you have a class ‘AClass’ and you want to reflect it.

class AClass
{
public:
   int aValue;
   int anotherValue;
   std::string thirdValue;
};

template
class ClassDescriptor
{
};

template
class ClassDescriptor
{
public:
   template
   void for_each_property(const TCallback& callback)
   {
      callback("aValue", &AClass::aValue);
      callback("anotherValue", &AClass::anotherValue);
      callback("thirdValue", &AClass::thirdValue);
   }
};

A template specialisation allows you to represent any class type that you’re interested in and you can capture the offset for each of the properties of that represented class. Then all you need to do is represent the algorithm:

template
class SamplePropertyAlgorithm
{
    TClass& m_t;
public:
    SamplePropertyAlgorithm(TClass& t):m_t(t)
    {
    }
    
    template
    void operator()(const char* szProperty,
                    TPropertyType TClass::*pPropertyOffset) const
    {
        std::cout << szProperty << ": " << m_t.*pPropertyOffset << std::endl;
        std::cout << "enter new value:"  m_t.*pPropertyOffset;
        
        std::cout << szProperty << ": " << m_t.*pPropertyOffset << std::endl;
    }
};

template
void RunAlgorithm(T& t)
{
    GetClassDescriptor(t).for_each_property(SamplePropertyAlgorithm(t));
}

Wow! that was a mouthful, but you only have to write it once, for each property, and you’ve captured not only the name but the type and value of each property. For illustration here is the usage.

int main(int argc, const char * argv[])
{
    AClass c1;
    c1.aValue = 1;
    c1.anotherValue = 2;
    c1.thirdValue = "this is a test";
    
    AClass2 c2;
    c2.testing = "ABC";
    c2.testing2 = "DEF";
    
    RunAlgorithm(c1);
    RunAlgorithm(c2);
    
    return 0;
}

And here is a sample interaction with the program:

aValue: 1
enter new value:2
aValue: 2
anotherValue: 2
enter new value:3
anotherValue: 3
thirdValue: this is a test
enter new value:test
thirdValue: test
testing: ABC
enter new value:EEE
testing: EEE
testing2: DEF
enter new value:FF
testing2: FF

There you have it. In my next blog I’m going to try serialising and deserialising a C++ object to JSON. I’m sure you can make this briefer using the preprocessor, and abstract it further allowing more kinds of properties – such as getter and setter methods. I’ll think it over.

Thanks for reading,
Phil

Advertisements

2 comments on “Can you Serialize objects to and from C++?

  1. Pingback: Serializing Objects in C++ Part 2 – Writing JSON | Dabbling Seriously

  2. Pingback: Serializing Objects in C++ Part 3 – Parsing JSON | Dabbling Seriously

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on June 12, 2015 by in Uncategorized and tagged , , .
%d bloggers like this: