Fixed const stuff on save/load

This commit is contained in:
Shane Grant
2013-06-12 14:33:00 -07:00
parent f6f0b37c5b
commit 71cd5c3cb5
3 changed files with 106 additions and 84 deletions

View File

@@ -10,57 +10,59 @@ namespace cereal
class BinaryOutputArchive class BinaryOutputArchive
{ {
public: public:
BinaryOutputArchive(std::ostream & stream) : itsStream(stream)
BinaryOutputArchive() {}
BinaryOutputArchive(std::ostream & stream)
{ {
} }
template<class T> //! Member serialization
template <class T>
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_serialize<T, BinaryOutputArchive>(), typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_serialize<T, BinaryOutputArchive>(),
BinaryOutputArchive &>::type BinaryOutputArchive &>::type
operator & (T const & t) operator & (T & t)
{ {
std::cout << "Member serialize" << std::endl; std::cout << "Member serialize" << std::endl;
//t.serialize(*this) t.serialize(*this);
return *this; return *this;
} }
template<class T> //! Non member serialization
template <class T>
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_serialize<T, BinaryOutputArchive>(), typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_serialize<T, BinaryOutputArchive>(),
BinaryOutputArchive &>::type BinaryOutputArchive &>::type
operator & (T const & t) operator & (T & t)
{ {
std::cout << "Non member serialize" << std::endl; std::cout << "Non member serialize" << std::endl;
//serialize(*this, t) serialize(*this, t);
return *this; return *this;
} }
template<class T> //! Member split (save)
template <class T>
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_split<T, BinaryOutputArchive>(), typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_split<T, BinaryOutputArchive>(),
BinaryOutputArchive &>::type BinaryOutputArchive &>::type
operator & (T const & t) operator & (T const & t)
{ {
std::cout << "Member split" << std::endl; std::cout << "Member split" << std::endl;
//t.save(*this); t.save(*this);
return *this; return *this;
} }
template<class T> //! Non member split (save)
template <class T>
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_split<T, BinaryOutputArchive>(), typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_split<T, BinaryOutputArchive>(),
BinaryOutputArchive &>::type BinaryOutputArchive &>::type
operator & (T const & t) operator & (T const & t)
{ {
std::cout << "Non member split" << std::endl; std::cout << "Non member split" << std::endl;
//save(*this, t); save(*this, t);
return *this; return *this;
} }
template<class T> //! No matching serialization
template <class T>
typename std::enable_if<!traits::is_serializable<T, BinaryOutputArchive>(), BinaryOutputArchive &>::type typename std::enable_if<!traits::is_serializable<T, BinaryOutputArchive>(), BinaryOutputArchive &>::type
operator & (T const & t) operator & (T const & t)
{ {
@@ -74,6 +76,28 @@ namespace cereal
" }\n\n" ); " }\n\n" );
return *this; 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;
}
} }

View File

@@ -1,16 +1,17 @@
#include "cereal.hpp" #include "cereal.hpp"
#include <cxxabi.h> #include <cxxabi.h>
#include <sstream>
#include <fstream>
// ################################### // ###################################
struct Test1 struct Test1
{ {
int a; int a;
std::string b;
template<class Archive> template<class Archive>
void serialize(Archive & ar) void serialize(Archive & ar)
{ {
ar & a & b; ar & a;
} }
}; };
@@ -18,18 +19,17 @@ struct Test1
struct Test2 struct Test2
{ {
int a; int a;
std::string b;
template<class Archive> template<class Archive>
void save(Archive & ar) void save(Archive & ar) const
{ {
ar & a & b; ar & a;
} }
template<class Archive> template<class Archive>
void load(Archive & ar) void load(Archive & ar)
{ {
ar & a & b; ar & a;
} }
}; };
@@ -37,14 +37,12 @@ struct Test2
struct Test3 struct Test3
{ {
int a; int a;
std::string b;
}; };
template<class Archive> template<class Archive>
void serialize(Archive & ar, Test3 & t) void serialize(Archive & ar, Test3 & t)
{ {
//ar & t.a; ar & t.a;
//ar & t.b;
} }
namespace test4 namespace test4
@@ -53,26 +51,26 @@ namespace test4
struct Test4 struct Test4
{ {
int a; int a;
std::string b;
}; };
template<class Archive> template<class Archive>
void save(Archive & ar, Test4 & t) void save(Archive & ar, Test4 const & t)
{ {
ar & t.a & t.b; ar & t.a;
} }
template<class Archive> template<class Archive>
void load(Archive & ar, Test4 & t) void load(Archive & ar, Test4 & t)
{ {
ar & t.a & t.b; ar & t.a;
} }
} }
// ###################################################################### // ######################################################################
int main() int main()
{ {
cereal::BinaryOutputArchive archive; std::ostringstream os;
cereal::BinaryOutputArchive archive(os);
Test1 t1; Test1 t1;
Test2 t2; Test2 t2;

View File

@@ -56,7 +56,7 @@ namespace cereal
// ###################################################################### // ######################################################################
// Non Member Save // 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> template<typename T, typename A>
bool constexpr has_non_member_save() bool constexpr has_non_member_save()
{ return std::is_void<decltype(save(std::declval<A&>(), std::declval<T&>()))>::value; }; { return std::is_void<decltype(save(std::declval<A&>(), std::declval<T&>()))>::value; };