mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
Class versioning working
This commit is contained in:
14
cereal.hpp
14
cereal.hpp
@@ -22,7 +22,8 @@ namespace cereal
|
|||||||
BinaryOutputArchive &>::type
|
BinaryOutputArchive &>::type
|
||||||
operator & (T const & t)
|
operator & (T const & t)
|
||||||
{
|
{
|
||||||
std::cout << "Member serialize" << std::endl;
|
std::cout << "Member serialize. Version " << cereal_class_version(t) << std::endl;
|
||||||
|
|
||||||
//t.serialize(*this, traits::version<T>::value)
|
//t.serialize(*this, traits::version<T>::value)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -32,7 +33,8 @@ namespace cereal
|
|||||||
BinaryOutputArchive &>::type
|
BinaryOutputArchive &>::type
|
||||||
operator & (T const & t)
|
operator & (T const & t)
|
||||||
{
|
{
|
||||||
std::cout << "Non member serialize" << std::endl;
|
std::cout << "Non member serialize. Version " << cereal_class_version(t) << std::endl;
|
||||||
|
|
||||||
//serialize(*this, t, traits::version<T>::value)
|
//serialize(*this, t, traits::version<T>::value)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -42,7 +44,8 @@ namespace cereal
|
|||||||
BinaryOutputArchive &>::type
|
BinaryOutputArchive &>::type
|
||||||
operator & (T const & t)
|
operator & (T const & t)
|
||||||
{
|
{
|
||||||
std::cout << "Member split" << std::endl;
|
std::cout << "Member split. Version " << cereal_class_version(t) << std::endl;
|
||||||
|
|
||||||
//t.save(*this, traits::version<T>::value);
|
//t.save(*this, traits::version<T>::value);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -52,7 +55,7 @@ namespace cereal
|
|||||||
BinaryOutputArchive &>::type
|
BinaryOutputArchive &>::type
|
||||||
operator & (T const & t)
|
operator & (T const & t)
|
||||||
{
|
{
|
||||||
std::cout << "Non member split" << std::endl;
|
std::cout << "Non member split. Version " << cereal_class_version(t) << std::endl;
|
||||||
//save(*this, t, traits::version<T>::value);
|
//save(*this, t, traits::version<T>::value);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -73,4 +76,7 @@ namespace cereal
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define CEREAL_CLASS_VERSION(classname, version) \
|
||||||
|
unsigned int constexpr cereal_class_version(classname const &) { return version; };
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
34
test.cpp
34
test.cpp
@@ -13,6 +13,7 @@ struct Test1
|
|||||||
ar & a & b;
|
ar & a & b;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
CEREAL_CLASS_VERSION(Test1, 1);
|
||||||
|
|
||||||
// ###################################
|
// ###################################
|
||||||
struct Test2
|
struct Test2
|
||||||
@@ -32,6 +33,7 @@ struct Test2
|
|||||||
ar & a & b;
|
ar & a & b;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
CEREAL_CLASS_VERSION(Test2, 2);
|
||||||
|
|
||||||
// ###################################
|
// ###################################
|
||||||
struct Test3
|
struct Test3
|
||||||
@@ -46,25 +48,29 @@ void serialize(Archive & ar, Test3 & t, unsigned int version)
|
|||||||
//ar & t.a;
|
//ar & t.a;
|
||||||
//ar & t.b;
|
//ar & t.b;
|
||||||
}
|
}
|
||||||
|
CEREAL_CLASS_VERSION(Test3, 3);
|
||||||
|
|
||||||
// ###################################
|
namespace test4
|
||||||
struct Test4
|
|
||||||
{
|
{
|
||||||
int a;
|
// ###################################
|
||||||
std::string b;
|
struct Test4
|
||||||
|
{
|
||||||
};
|
int a;
|
||||||
|
std::string b;
|
||||||
|
};
|
||||||
|
|
||||||
template<class Archive>
|
template<class Archive>
|
||||||
void save(Archive & ar, Test4 & t, unsigned int version)
|
void save(Archive & ar, Test4 & t, unsigned int version)
|
||||||
{
|
{
|
||||||
ar & t.a & t.b;
|
ar & t.a & t.b;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Archive>
|
template<class Archive>
|
||||||
void load(Archive & ar, Test4 & t, unsigned int version)
|
void load(Archive & ar, Test4 & t, unsigned int version)
|
||||||
{
|
{
|
||||||
ar & t.a & t.b;
|
ar & t.a & t.b;
|
||||||
|
}
|
||||||
|
CEREAL_CLASS_VERSION(Test4, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ######################################################################
|
// ######################################################################
|
||||||
@@ -75,7 +81,7 @@ int main()
|
|||||||
Test1 t1;
|
Test1 t1;
|
||||||
Test2 t2;
|
Test2 t2;
|
||||||
Test3 t3;
|
Test3 t3;
|
||||||
Test4 t4;
|
test4::Test4 t4;
|
||||||
|
|
||||||
archive & t1;
|
archive & t1;
|
||||||
archive & t2;
|
archive & t2;
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
namespace cereal
|
namespace cereal
|
||||||
{
|
{
|
||||||
|
|
||||||
|
template<typename T> unsigned int constexpr cereal_class_version(T const &) { return 0; };
|
||||||
|
|
||||||
namespace traits
|
namespace traits
|
||||||
{
|
{
|
||||||
template<typename> struct Void { typedef void type; };
|
template<typename> struct Void { typedef void type; };
|
||||||
|
|
||||||
|
|
||||||
// ######################################################################
|
// ######################################################################
|
||||||
// Member Serialize
|
// Member Serialize
|
||||||
template<typename T, class A, typename Sfinae = void>
|
template<typename T, class A, typename Sfinae = void>
|
||||||
|
|||||||
Reference in New Issue
Block a user