mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
added string serialization
This commit is contained in:
19
cereal.hpp
19
cereal.hpp
@@ -3,6 +3,7 @@
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
#include "traits.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace cereal
|
||||
{
|
||||
@@ -131,4 +132,22 @@ namespace cereal
|
||||
std::cout << "Serializing NVP: " << t.name << " " << t.value << std::endl;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
28
test.cpp
28
test.cpp
@@ -69,13 +69,14 @@ namespace test4
|
||||
// ######################################################################
|
||||
int main()
|
||||
{
|
||||
std::ostringstream os;
|
||||
//std::ostringstream os;
|
||||
std::ofstream os("out.txt");
|
||||
cereal::BinaryOutputArchive archive(os);
|
||||
|
||||
Test1 t1;
|
||||
Test2 t2;
|
||||
Test3 t3;
|
||||
test4::Test4 t4;
|
||||
Test1 t1 = {1};
|
||||
Test2 t2 = {2};
|
||||
Test3 t3 = {3};
|
||||
test4::Test4 t4 = {4};
|
||||
|
||||
archive & t1;
|
||||
archive & t2;
|
||||
@@ -84,14 +85,19 @@ int main()
|
||||
|
||||
int x = 5;
|
||||
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 & 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user