1

I'm using SFML and cereal to serialize/deserialize data and I want to do that for sf::vector2 and sf::vector3 class:

Data.h

#include <SFML\System.hpp>
#include <fstream>
#include <iostream>
#include "cereal-1.2.2\include\cereal\archives\xml.hpp"
#include "cereal-1.2.2\include\cereal\types\map.hpp"

struct DataInfo {
  map<string, sf::Vector2f> vector2FloatData;
  map<string, sf::Vector3f> vector3FloatData;
  map<string, sf::Vector2i> vector2IntData;
  map<string, sf::Vector3i> vector3IntData;

  template <class Archive>
  void serialize(Archive & ar)
  {
     ar(vector2FloatData, vector3FloatData, vector2IntData, vector3IntData);
  }
};

main.cpp

int Main()
{
   std::ofstream file("Test.xml");
   cereal::XMLOutputArchive archive(file);

   DataInfo data;
   archive(data);

   return 0;
}

But cereal doesn't know what are sf::vectors and i get the following error:

Error C2338 cereal could not find any output serialization functions for the provided type and archive combination.

I know that exist CEREAL_REGISTER_TYPE() but i don't know how to make it work.

Adding to Data.h:

#include "cereal-1.2.2\include\cereal\types\polymorphic.hpp"

CEREAL_REGISTER_TYPE(sf::Vector2f)
CEREAL_REGISTER_TYPE(sf::Vector3f)
CEREAL_REGISTER_TYPE(sf::Vector2i)
CEREAL_REGISTER_TYPE(sf::Vector3i)

I get this error:

Error C2338 Attempting to register non polymorphic type.

Any idea?

Thanks.

nvoigt
  • 75,013
  • 26
  • 93
  • 142

1 Answers1

0

I can't verify if it works, i guess you need to add the serialization for sfml types manually such as:

namespace /*cereal*/ sf
{
    template<class Archive>
    void serialize(Archive & archive, sf::Vector2i & v)
    {
         archive( v.x, v.y);
    }
}

Not sure again, yet it may work with templated types too:

namespace /*cereal*/ sf
{
    template<class Archive, class Type>
    void serialize(Archive & archive, sf::Vector2<Type> & v)
    {
         archive( v.x, v.y);
    }
}

They are not polymorphic types, so you don't need to register anything.

--EDIT--

Add the function under sf namespace.

seleciii44
  • 1,529
  • 3
  • 13
  • 26
  • Yes, you'll need something like that, since Cereal won't know how to treat SFML types out of the box. – Mario Apr 08 '17 at 17:51
  • Tried but still getting error: Error C2338 cereal could not find any output serialization functions for the provided type and archive combination. – Tino Martin Apr 09 '17 at 01:53
  • first try external serialization function with a simple class. then add namespace to your class and try again. finally make it template like sf::vector and try again. i'm sure will find out the problem. Also you need to include cereal before the serialization function. – seleciii44 Apr 09 '17 at 04:39