added string serialization

This commit is contained in:
Shane Grant
2013-06-12 15:22:44 -07:00
parent aeeb326477
commit 41e4211748
2 changed files with 36 additions and 11 deletions

View File

@@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <type_traits> #include <type_traits>
#include "traits.hpp" #include "traits.hpp"
#include <string>
namespace cereal namespace cereal
{ {
@@ -131,4 +132,22 @@ namespace cereal
std::cout << "Serializing NVP: " << t.name << " " << t.value << std::endl; std::cout << "Serializing NVP: " << t.name << " " << t.value << std::endl;
ar & t.value; ar & t.value;
} }
//! Serialization for basic_string types to binary
template<class CharT, class Traits, class Alloc>
void save(BinaryOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const & str)
{
// Save number of chars + the data
ar & str.size();
ar.save_binary(str.data(), str.size() * sizeof(CharT));
std::cout << "Saving string: " << str << std::endl;
}
//! Serialization for basic_string types to binary
template<class CharT, class Traits, class Alloc>
void load(BinaryOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> & str)
{
std::cout << "Loading string: " << str << std::endl;
}
} }

View File

@@ -69,13 +69,14 @@ namespace test4
// ###################################################################### // ######################################################################
int main() int main()
{ {
std::ostringstream os; //std::ostringstream os;
std::ofstream os("out.txt");
cereal::BinaryOutputArchive archive(os); cereal::BinaryOutputArchive archive(os);
Test1 t1; Test1 t1 = {1};
Test2 t2; Test2 t2 = {2};
Test3 t3; Test3 t3 = {3};
test4::Test4 t4; test4::Test4 t4 = {4};
archive & t1; archive & t1;
archive & t2; archive & t2;
@@ -84,14 +85,19 @@ int main()
int x = 5; int x = 5;
auto nvp = cereal::make_nvp("hello!", x); auto nvp = cereal::make_nvp("hello!", x);
archive & CEREAL_NVP(x);
std::cout << std::is_base_of<cereal::detail::NameValuePairCore, decltype(nvp)>::value << std::endl;
std::cout << cereal::traits::has_non_member_serialize<decltype(nvp), cereal::BinaryOutputArchive>() << std::endl;
std::cout << cereal::traits::is_serializable<decltype(nvp), cereal::BinaryOutputArchive>() << std::endl;
archive & nvp; archive & nvp;
archive & cereal::make_nvp("another", x);
x = 6;
archive & CEREAL_NVP(x);
std::string bla = "asdf";
//std::cout << cereal::traits::has_non_member_save<std::string, cereal::BinaryOutputArchive>() << std::endl;
//std::cout << cereal::traits::has_non_member_load<std::string, cereal::BinaryOutputArchive>() << std::endl;
//std::cout << cereal::traits::is_serializable<std::string, cereal::BinaryOutputArchive>() << std::endl;
archive & bla;
return 0; return 0;
} }