Cereal vectors of structs


#1

I recently started using Cinder’s Cereal block, instead of the Xml block for saving and loading data. I find it way easier to maintain. Love it. But I’m having a hard time figuring out how to save and load custom vector types.
For example, the cereal block shows a basic archive template to save and load a camera.

template
void save( Archive & archive, const ci::Camera &cam )
{
archive( cereal::make_nvp( “eye_point”, cam.getEyePoint() ), cereal::make_nvp( “orientation”, cam.getOrientation() ), cereal::make_nvp( “fov”, cam.getFov() ), cereal::make_nvp( “aspect_ratio”, cam.getAspectRatio() ), cereal::make_nvp( “near_clip”, cam.getNearClip() ), cereal::make_nvp( “far_clip”, cam.getFarClip() ), cereal::make_nvp( “pivot_distance”, cam.getPivotDistance() ) );
}
template
void load( Archive & archive, ci::Camera &cam )
{
ci::vec3 eye;
ci::quat orientation;
float verticalFovDegrees, aspectRatio, nearPlane, farPlane, pivotDist;
archive( eye, orientation, verticalFovDegrees, aspectRatio, nearPlane, farPlane, pivotDist );
cam.setEyePoint( eye );
cam.setOrientation( orientation );
cam.setFov( verticalFovDegrees );
cam.setAspectRatio( aspectRatio );
cam.setNearClip( nearPlane );
cam.setFarClip( farPlane );
cam.setPivotDistance( pivotDist );
}

Simple enough. And the cereal documentation shows how to use a vector of int.

int main()
{
  cereal::XMLOutputArchive archive( std::cout );
  bool arr[] = {true, false};
  std::vector<int> vec = {1, 2, 3, 4, 5};
  archive( CEREAL_NVP(vec),
           arr );
}

But how would one go about vector of cameras?

something like?

template<class Archive>
void save(Archive & archive, vector<camera>  &camVector)
{
	archive(CEREAL_NVP( camVector));
}

All i could figure is it could have something to do with typedefs and a proper archive template?


#2

Cinder-Cereal sets Camera serialization up as you have mentioned. Cereal takes care of serializing vectors for you.

#include "cereal/types/vector.hpp"
#include "CinderCereal.h"

...

cereal::XMLOutputArchive archive( std::cout );
vector< CameraPersp > vecCamera = {
    CameraPersp( getWindowWidth(), getWindowHeight(), 50.0f, 0.1f, 100.0f ),
    CameraPersp( getWindowWidth(), getWindowHeight(), 45.0f, 0.1f, 100.0f ) };

archive( CEREAL_NVP(vecCamera) );

-Gabor


#3

Ha! Well isn’t that just dandy… thanks Gabor!