mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-02 21:28:06 +02:00
Fixed const stuff on save/load
This commit is contained in:
134
cereal.hpp
134
cereal.hpp
@@ -10,70 +10,94 @@ namespace cereal
|
||||
class BinaryOutputArchive
|
||||
{
|
||||
public:
|
||||
|
||||
BinaryOutputArchive() {}
|
||||
|
||||
BinaryOutputArchive(std::ostream & stream)
|
||||
BinaryOutputArchive(std::ostream & stream) : itsStream(stream)
|
||||
{
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_serialize<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
std::cout << "Member serialize" << std::endl;
|
||||
//! Member serialization
|
||||
template <class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_serialize<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T & t)
|
||||
{
|
||||
std::cout << "Member serialize" << std::endl;
|
||||
|
||||
//t.serialize(*this)
|
||||
return *this;
|
||||
}
|
||||
t.serialize(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_serialize<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
std::cout << "Non member serialize" << std::endl;
|
||||
//! Non member serialization
|
||||
template <class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_serialize<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T & t)
|
||||
{
|
||||
std::cout << "Non member serialize" << std::endl;
|
||||
|
||||
//serialize(*this, t)
|
||||
return *this;
|
||||
}
|
||||
serialize(*this, t);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_split<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
std::cout << "Member split" << std::endl;
|
||||
//! Member split (save)
|
||||
template <class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_split<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
std::cout << "Member split" << std::endl;
|
||||
|
||||
//t.save(*this);
|
||||
return *this;
|
||||
}
|
||||
t.save(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_split<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
std::cout << "Non member split" << std::endl;
|
||||
//save(*this, t);
|
||||
return *this;
|
||||
}
|
||||
//! Non member split (save)
|
||||
template <class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_split<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
std::cout << "Non member split" << std::endl;
|
||||
save(*this, t);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<!traits::is_serializable<T, BinaryOutputArchive>(), BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
static_assert(traits::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)\n"
|
||||
" {\n"
|
||||
" ar & member1 & member2 & member3;\n"
|
||||
" }\n\n" );
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
//! No matching serialization
|
||||
template <class T>
|
||||
typename std::enable_if<!traits::is_serializable<T, BinaryOutputArchive>(), BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
static_assert(traits::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)\n"
|
||||
" {\n"
|
||||
" ar & member1 & member2 & member3;\n"
|
||||
" }\n\n" );
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! Writes size bytes of data to the output stream
|
||||
void save_binary( const void * data, size_t size )
|
||||
{
|
||||
auto const writtenSize = itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size );
|
||||
|
||||
if(writtenSize != size)
|
||||
throw 1; // TODO: something terrible
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostream & itsStream;
|
||||
}; // class BinaryOutputArchive
|
||||
|
||||
//! Serialization for POD types to binary
|
||||
template<class T>
|
||||
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
|
||||
serialize(BinaryOutputArchive & ar, T & t)
|
||||
{
|
||||
//ar.itsStream << t;
|
||||
ar.save_binary(std::addressof(t), sizeof(t));
|
||||
std::cout << "Serializing POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
54
test.cpp
54
test.cpp
@@ -1,50 +1,48 @@
|
||||
#include "cereal.hpp"
|
||||
#include <cxxabi.h>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
// ###################################
|
||||
struct Test1
|
||||
{
|
||||
int a;
|
||||
std::string b;
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar)
|
||||
{
|
||||
ar & a & b;
|
||||
}
|
||||
void serialize(Archive & ar)
|
||||
{
|
||||
ar & a;
|
||||
}
|
||||
};
|
||||
|
||||
// ###################################
|
||||
struct Test2
|
||||
{
|
||||
int a;
|
||||
std::string b;
|
||||
|
||||
template<class Archive>
|
||||
void save(Archive & ar)
|
||||
{
|
||||
ar & a & b;
|
||||
}
|
||||
void save(Archive & ar) const
|
||||
{
|
||||
ar & a;
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void load(Archive & ar)
|
||||
{
|
||||
ar & a & b;
|
||||
}
|
||||
void load(Archive & ar)
|
||||
{
|
||||
ar & a;
|
||||
}
|
||||
};
|
||||
|
||||
// ###################################
|
||||
struct Test3
|
||||
{
|
||||
int a;
|
||||
std::string b;
|
||||
};
|
||||
|
||||
template<class Archive>
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, Test3 & t)
|
||||
{
|
||||
//ar & t.a;
|
||||
//ar & t.b;
|
||||
ar & t.a;
|
||||
}
|
||||
|
||||
namespace test4
|
||||
@@ -53,26 +51,26 @@ namespace test4
|
||||
struct Test4
|
||||
{
|
||||
int a;
|
||||
std::string b;
|
||||
};
|
||||
|
||||
template<class Archive>
|
||||
void save(Archive & ar, Test4 & t)
|
||||
{
|
||||
ar & t.a & t.b;
|
||||
}
|
||||
void save(Archive & ar, Test4 const & t)
|
||||
{
|
||||
ar & t.a;
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void load(Archive & ar, Test4 & t)
|
||||
{
|
||||
ar & t.a & t.b;
|
||||
}
|
||||
void load(Archive & ar, Test4 & t)
|
||||
{
|
||||
ar & t.a;
|
||||
}
|
||||
}
|
||||
|
||||
// ######################################################################
|
||||
int main()
|
||||
{
|
||||
cereal::BinaryOutputArchive archive;
|
||||
std::ostringstream os;
|
||||
cereal::BinaryOutputArchive archive(os);
|
||||
|
||||
Test1 t1;
|
||||
Test2 t2;
|
||||
|
@@ -56,7 +56,7 @@ namespace cereal
|
||||
|
||||
// ######################################################################
|
||||
// Non Member Save
|
||||
template<typename T, typename A> char & save(A&, T&);
|
||||
template<typename T, typename A> char & save(A&, T const &);
|
||||
template<typename T, typename A>
|
||||
bool constexpr has_non_member_save()
|
||||
{ return std::is_void<decltype(save(std::declval<A&>(), std::declval<T&>()))>::value; };
|
||||
|
Reference in New Issue
Block a user