From 41e4211748e8e9d06f7da249ccef4078f1551ed5 Mon Sep 17 00:00:00 2001 From: Shane Grant Date: Wed, 12 Jun 2013 15:22:44 -0700 Subject: [PATCH] added string serialization --- cereal.hpp | 19 +++++++++++++++++++ test.cpp | 28 +++++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/cereal.hpp b/cereal.hpp index 8aff621c..f1d3d3c8 100644 --- a/cereal.hpp +++ b/cereal.hpp @@ -3,6 +3,7 @@ #include #include #include "traits.hpp" +#include 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 + void save(BinaryOutputArchive & ar, std::basic_string 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 + void load(BinaryOutputArchive & ar, std::basic_string & str) + { + std::cout << "Loading string: " << str << std::endl; + } } diff --git a/test.cpp b/test.cpp index 5cd4699f..2855ee98 100644 --- a/test.cpp +++ b/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::value << std::endl; - std::cout << cereal::traits::has_non_member_serialize() << std::endl; - std::cout << cereal::traits::is_serializable() << 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::endl; + //std::cout << cereal::traits::has_non_member_load() << std::endl; + //std::cout << cereal::traits::is_serializable() << std::endl; + + archive & bla; return 0; }