Skip to content

Cereal integration

Johan Andruejol requested to merge johan-andruejol/iMSTK:Serialization into master

Hi,

This is a PR to show the integration of iMSTK with Cereal, the serialization library. This is to open the discussion to see if we want to use it or not.

Here are my findings.

Documentation:

Doc is very sparse. Stack overflow is not much help.

Building:

Super easy to build and install. As you can see in this commit, it's super simple.

Using Cereal

Templates

Cereal uses templates which means we cannot really put the implementation of the serialization method in. The alternative would be to have .hpp files included in the header, but that would mean we have to add another file to each file we serialize. That seems like we would be adding a lot of files.

No external headers (No access to private member)

I tried to use the external methods grouped in a header. The idea was that they then could all be included in one header which would make it easy to include for examples.

-> The easiest is to have the serialize method in the header file.

Load_and_construct for non default constructors classes (all of them)

We need to have load_and_construct methods when the default constructor are not accessible. So that's another method that needs to be added to the header file.

NVP

By default, if you don't use NVP, you get this ugly json:

{
    "value0": {
        "polymorphic_id": 1073741824,
        "ptr_wrapper": {
            "id": 2147483649,
            "data": {
                "value0": {
                    "value0": {
                        "value0": 2
                    },
                    "value1": "whiteLight",
                    "value2": 0,
                    "value3": 7.0,
                    "value4": {
                        "value0": 1.0,
                        "value1": 1.0,
                        "value2": 1.0,
                        "value3": 1.0
                    },
                    "value5": true,
                    "value6": {
                        "value0": 3,
                        "value1": 1,
                        "value2": 5.0,
                        "value3": -8.0,
                        "value4": -5.0
                    }
                },
                "value1": true,
                "value2": {
                    "value0": 3,
                    "value1": 1,
                    "value2": 0.0,
                    "value3": 0.0,
                    "value4": 0.0
                },
                "value3": 2.0,
                "value4": -1
            }
        }
    }
}

If you do use the NVP, it's a little better:

{
    "value0": {
        "polymorphic_id": 1073741824,
        "ptr_wrapper": {
            "id": 2147483649,
            "data": {
                "imstk::Light": {
                    "imstk::SceneEntity": {
                        "ID": 2
                    },
                    "name": "whiteLight",
                    "type": 0,
                    "intensity": 7.0,
                    "color": {
                        "value0": 1.0,
                        "value1": 1.0,
                        "value2": 1.0,
                        "value3": 1.0
                    },
                    "switchState": true,
                    "focalPoint": {
                        "value0": 3,
                        "value1": 1,
                        "value2": 5.0,
                        "value3": -8.0,
                        "value4": -5.0
                    }
                },
                "castShadow": true,
                "shadowCenter": {
                    "value0": 3,
                    "value1": 1,
                    "value2": 0.0,
                    "value3": 0.0,
                    "value4": 0.0
                },
                "shadowRange": 2.0,
                "shadowIndex": -1
            }
        }
    }
}

The bad: I get a crash when I try to read the serialization back in. The issue seems that the derived class doesn't seem to be able to access the base class vars.

This seems related to this: https://stackoverflow.com/questions/20940919/c11-cereal-load-and-allocate-not-loading-correctly?

To me that seems like a big showstopper. Let's talk tomorrow.

Edited by Andrew Wilson

Merge request reports