mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
First commit - basic split/serialize checking works
This commit is contained in:
2
Makefile
Normal file
2
Makefile
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
all: test.cpp cereal.hpp
|
||||||
|
g++ -std=c++0x test.cpp -o test
|
||||||
103
cereal.hpp
Normal file
103
cereal.hpp
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace cereal
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
template<typename> struct Void { typedef void type; };
|
||||||
|
|
||||||
|
template<typename T, class A, typename Sfinae = void>
|
||||||
|
struct has_serialize: std::false_type {};
|
||||||
|
|
||||||
|
template<typename T, class A>
|
||||||
|
struct has_serialize< T, A,
|
||||||
|
typename Void<
|
||||||
|
decltype( std::declval<T&>().serialize( std::declval<A&>(), 0 ) )
|
||||||
|
>::type
|
||||||
|
>: std::true_type {};
|
||||||
|
|
||||||
|
template<typename T, class A, typename Sfinae = void>
|
||||||
|
struct has_load: std::false_type {};
|
||||||
|
|
||||||
|
template<typename T, class A>
|
||||||
|
struct has_load< T, A,
|
||||||
|
typename Void<
|
||||||
|
decltype( std::declval<T&>().load( std::declval<A&>(), 0 ) )
|
||||||
|
>::type
|
||||||
|
>: std::true_type {};
|
||||||
|
|
||||||
|
template<typename T, class A, typename Sfinae = void>
|
||||||
|
struct has_save: std::false_type {};
|
||||||
|
|
||||||
|
template<typename T, class A>
|
||||||
|
struct has_save< T, A,
|
||||||
|
typename Void<
|
||||||
|
decltype( std::declval<T&>().save( std::declval<A&>(), 0 ) )
|
||||||
|
>::type
|
||||||
|
>: std::true_type {};
|
||||||
|
|
||||||
|
template <class T, class A>
|
||||||
|
constexpr bool has_split()
|
||||||
|
{
|
||||||
|
return has_load<T, A>() && has_save<T, A>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, class A>
|
||||||
|
constexpr bool is_serializable()
|
||||||
|
{
|
||||||
|
return has_split<T, A>() ^ has_serialize<T, A>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class BinaryOutputArchive
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
BinaryOutputArchive() {}
|
||||||
|
|
||||||
|
BinaryOutputArchive(std::ostream & stream)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
typename std::enable_if<detail::is_serializable<T, BinaryOutputArchive>() && detail::has_serialize<T, BinaryOutputArchive>(),
|
||||||
|
BinaryOutputArchive &>::type
|
||||||
|
operator & (T const & t)
|
||||||
|
{
|
||||||
|
std::cout << "Saving non-split member" << std::endl;
|
||||||
|
//t.serialize(*this, detail::version<T>::value)
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
typename std::enable_if<detail::is_serializable<T, BinaryOutputArchive>() && detail::has_split<T, BinaryOutputArchive>(),
|
||||||
|
BinaryOutputArchive &>::type
|
||||||
|
operator & (T const & t)
|
||||||
|
{
|
||||||
|
std::cout << "Saving split member" << std::endl;
|
||||||
|
//t.save(*this, detail::version<T>::value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
typename std::enable_if<!detail::is_serializable<T, BinaryOutputArchive>(), BinaryOutputArchive &>::type
|
||||||
|
operator & (T const & t)
|
||||||
|
{
|
||||||
|
static_assert(detail::is_serializable<T, BinaryOutputArchive>(), "Trying to serialize an unserializable type.\n\n"
|
||||||
|
"Types must either have a serialize function, or separate save/load functions (but not both).\n"
|
||||||
|
"Serialize functions generally have the following signature:\n\n"
|
||||||
|
"template<class Archive>\n"
|
||||||
|
" void serialize(int & ar, unsigned int version)\n"
|
||||||
|
" {\n"
|
||||||
|
" ar & member1 & member2 & member3;\n"
|
||||||
|
" }\n\n" );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
92
test.cpp
Normal file
92
test.cpp
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
#include "cereal.hpp"
|
||||||
|
|
||||||
|
// ###################################
|
||||||
|
struct Test1
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
std::string b;
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void serialize(Archive & ar, unsigned int version)
|
||||||
|
{
|
||||||
|
ar & a & b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ###################################
|
||||||
|
struct Test2
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
std::string b;
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void save(Archive & ar, unsigned int version)
|
||||||
|
{
|
||||||
|
ar & a & b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void load(Archive & ar, unsigned int version)
|
||||||
|
{
|
||||||
|
ar & a & b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ###################################
|
||||||
|
struct Test3
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
std::string b;
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void serialize(int & ar, unsigned int version)
|
||||||
|
{
|
||||||
|
ar & a;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ###################################
|
||||||
|
struct Test4
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
std::string b;
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void serialize(Archive & ar, unsigned int version)
|
||||||
|
{
|
||||||
|
ar & a & b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void save(Archive & ar, unsigned int version)
|
||||||
|
{
|
||||||
|
ar & a & b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void load(Archive & ar, unsigned int version)
|
||||||
|
{
|
||||||
|
ar & a & b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ######################################################################
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
cereal::BinaryOutputArchive archive;
|
||||||
|
|
||||||
|
Test1 t1;
|
||||||
|
Test2 t2;
|
||||||
|
Test3 t3;
|
||||||
|
Test4 t4;
|
||||||
|
|
||||||
|
archive & t1;
|
||||||
|
archive & t2;
|
||||||
|
//archive & t3;
|
||||||
|
//archive & t4;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user