diff --git a/include/cereal/archives/xml.hpp b/include/cereal/archives/xml.hpp index b73797db..fadc1c1c 100644 --- a/include/cereal/archives/xml.hpp +++ b/include/cereal/archives/xml.hpp @@ -238,6 +238,7 @@ namespace cereal { try { + itsData.push_back('\0'); // rapidxml will do terrible things without this itsXML.parse( reinterpret_cast( itsData.data() ) ); } catch( rapidxml::parse_error const & e ) @@ -273,22 +274,25 @@ namespace cereal itsNodes.top().advance(); } - // int - // long - // long long - // ulong - // ulong long - // + //! Loads a bool + template inline + typename std::enable_if::value && std::is_same::value, void>::type + loadValue( T & value ) + { + std::istringstream is( itsNodes.top().node->value() ); + is.setf( std::ios::boolalpha ); + is >> value; + } template inline - typename std::enable_if::value && sizeof(T) < sizeof(long long), void>::type + typename std::enable_if::value && !std::is_same::value && sizeof(T) < sizeof(long long), void>::type loadValue( T & value ) { value = std::stoul( itsNodes.top().node->value() ); } template inline - typename std::enable_if::value && sizeof(T) >= sizeof(long long), void>::type + typename std::enable_if::value && !std::is_same::value && sizeof(T) >= sizeof(long long), void>::type loadValue( T & value ) { value = std::stoull( itsNodes.top().node->value() ); @@ -401,7 +405,7 @@ namespace cereal }; // NodeInfo private: - std::vector itsData; //!< The raw data loaded + std::vector itsData; //!< The raw data loaded rapidxml::xml_document<> itsXML; //!< The XML document std::stack itsNodes; //!< A stack of nodes read from the document }; diff --git a/sandbox.cpp b/sandbox.cpp index 2248aa77..23591c12 100644 --- a/sandbox.cpp +++ b/sandbox.cpp @@ -279,12 +279,6 @@ int main() { std::cout << std::boolalpha << std::endl; - std::stringstream os; - cereal::BinaryOutputArchive archive(os); - - Derived d; - archive( d ); - Everything e_out; e_out.x = 99; e_out.y = 100; @@ -294,175 +288,177 @@ int main() e_out.t4 = {4}; e_out.s = "Hello, World!"; - Test2 t2 = {22}; + //Test2 t2 = {22}; + //{ + // std::ofstream os("out.txt"); + // cereal::BinaryOutputArchive archive(os); + // archive(CEREAL_NVP(e_out)); + // archive(t2); + //} + + //Everything e_in; + + //{ + // std::ifstream is("out.txt"); + // cereal::BinaryInputArchive archive(is); + // archive(CEREAL_NVP(e_in)); + // archive(t2); + // std::remove("out.txt"); + //} + + //assert(e_in == e_out); + + //{ + // cereal::BinaryOutputArchive archive(std::cout); + // int xxx[] = {-1, 95, 3}; + // archive( xxx ); + + // cereal::XMLOutputArchive archive2(std::cout); + // archive2( xxx ); + //} + + for( int i = 0; i < 10; ++i ) { - std::ofstream os("out.txt"); - cereal::BinaryOutputArchive archive(os); - archive(CEREAL_NVP(e_out)); - archive(t2); - } - - Everything e_in; - - { - std::ifstream is("out.txt"); - cereal::BinaryInputArchive archive(is); - archive(CEREAL_NVP(e_in)); - archive(t2); - std::remove("out.txt"); - } - - assert(e_in == e_out); - - { - cereal::BinaryOutputArchive archive(std::cout); - int xxx[] = {-1, 95, 3}; - archive( xxx ); - - cereal::XMLOutputArchive archive2(std::cout); - archive2( xxx ); - } - - { - std::ofstream os("out.xml"); - cereal::XMLOutputArchive oar( os ); - //cereal::XMLOutputArchive oar( std::cout ); - - oar( cereal::make_nvp("hello", 5 ) ); - - std::string bla("bla"); - oar( bla ); - - auto intptr = std::make_shared(99); - oar( CEREAL_NVP(intptr) ); - - std::map map1 = { - {"one", 1}, - {"two", 2}, - {"three", 3} - }; + std::ofstream os("out.xml"); + cereal::XMLOutputArchive oar( os ); + //cereal::XMLOutputArchive oar( std::cout ); - oar( CEREAL_NVP(map1) ); + oar( cereal::make_nvp("hello", 5 ) ); - int x = 3; - oar( CEREAL_NVP(x) ); - oar( 5 ); - oar( 3.3 ); - oar( 3.2f ); - oar( true ); + std::string bla("bla"); + oar( bla ); - std::array arr = {{1, 2, 3, 4, 5}}; - oar( arr ); + auto intptr = std::make_shared(99); + oar( CEREAL_NVP(intptr) ); - std::vector vec = {"hey", - "there", - "buddy"}; + std::map map1 = + { + {"one", 1}, + {"two", 2}, + {"three", 3} + }; - std::vector> vec2 = {vec, vec, vec}; + oar( CEREAL_NVP(map1) ); - oar( cereal::make_nvp("EVERYTHING", e_out) ); - oar( vec ); - oar( vec2 ); + int x = 3; + oar( CEREAL_NVP(x) ); + oar( 5 ); + oar( 3.3 ); + oar( 3.2f ); + oar( true ); - int xxx[] = {-1, 95, 3}; - oar.saveBinaryValue( xxx, sizeof(int)*3, "xxxbinary" ); - //oar.saveBinaryValue( xxx, sizeof(int)*3 ); + std::array arr = {{1, 2, 3, 4, 5}}; + oar( arr ); - std::unique_ptr d1( new Derived(3, 4) ); - std::unique_ptr d2( new Derived(4, 5) ); - std::shared_ptr d3( new Derived(5, 6) ); - oar( d1 ); - oar( d2 ); - oar( d3 ); - } + std::vector vec = {"hey", + "there", + "buddy"}; - if(false) - { - std::ifstream is("out.xml"); - cereal::XMLInputArchive iar( is ); + std::vector> vec2 = {vec, vec, vec}; - int hello; - iar( cereal::make_nvp("hello", hello) ); - assert( hello == 5 ); + oar( cereal::make_nvp("EVERYTHING", e_out) ); + oar( vec ); + oar( vec2 ); - std::string bla; - iar( bla ); - assert( bla == "bla" ); + int xxx[] = {-1, 95, 3}; + oar.saveBinaryValue( xxx, sizeof(int)*3, "xxxbinary" ); + //oar.saveBinaryValue( xxx, sizeof(int)*3 ); - std::shared_ptr intptr; - iar( CEREAL_NVP(intptr) ); - assert( *intptr == 99 ); - - std::map map1; - - iar( CEREAL_NVP(map1) ); - assert( map1["one"] == 1 ); - assert( map1["two"] == 2 ); - assert( map1["three"] == 3 ); - - - int x; - iar( CEREAL_NVP(x) ); - assert( x == 3 ); - - int x5; - iar( x5 ); - assert( x5 == 5 ); - - double x33; - iar( x33 ); - assert( x33 == 3.3 ); - - float x32; - iar( x32 ); - assert( x32 == 3.2f ); - - bool xtrue; - iar( xtrue ); - assert( xtrue == true ); - - std::array arr; - iar( arr ); - for( size_t i = 0; i < 5; ++i ) - assert( arr[i] == (i+1) ); - - Everything e; - iar( cereal::make_nvp("EVERYTHING", e) ); - assert( e == e_out ); - - std::vector vec; - iar( vec ); - assert( vec[0] == "hey" ); - assert( vec[1] == "there" ); - assert( vec[2] == "buddy" ); - - std::vector> vec2; - iar( vec2 ); - for( auto & v : vec2 ) - { - assert( v[0] == "hey" ); - assert( v[1] == "there" ); - assert( v[2] == "buddy" ); + std::unique_ptr d1( new Derived(3, 4) ); + std::unique_ptr d2( new Derived(4, 5) ); + std::shared_ptr d3( new Derived(5, 6) ); + oar( d1 ); + oar( d2 ); + oar( d3 ); } - int xxx[3]; - iar.loadBinaryValue( xxx, sizeof(int)*3 ); - assert( xxx[0] == -1 ); - assert( xxx[1] == 95 ); - assert( xxx[2] == 3 ); + { + std::ifstream is("out.xml"); + cereal::XMLInputArchive iar( is ); - std::unique_ptr d1; - std::unique_ptr d2; - std::shared_ptr d3; + int hello; + iar( cereal::make_nvp("hello", hello) ); + assert( hello == 5 ); - iar( d1 ); - assert( d1->x == 4 && d1->y == 3 ); - iar( d2 ); - assert( ((Derived*)d2.get())->x == 5 && ((Derived*)d2.get())->y == 4 ); - iar( d3 ); - assert( ((Derived*)d3.get())->x == 6 && ((Derived*)d3.get())->y == 5 ); + std::string bla; + iar( bla ); + assert( bla == "bla" ); + + std::shared_ptr intptr; + iar( CEREAL_NVP(intptr) ); + assert( *intptr == 99 ); + + std::map map1; + + iar( CEREAL_NVP(map1) ); + assert( map1["one"] == 1 ); + assert( map1["two"] == 2 ); + assert( map1["three"] == 3 ); + + + int x; + iar( CEREAL_NVP(x) ); + assert( x == 3 ); + + int x5; + iar( x5 ); + assert( x5 == 5 ); + + double x33; + iar( x33 ); + assert( x33 == 3.3 ); + + float x32; + iar( x32 ); + assert( x32 == 3.2f ); + + bool xtrue; + iar( xtrue ); + assert( xtrue == true ); + + std::array arr; + iar( arr ); + for( size_t i = 0; i < 5; ++i ) + assert( arr[i] == (i+1) ); + + Everything e; + iar( cereal::make_nvp("EVERYTHING", e) ); + assert( e == e_out ); + + std::vector vec; + iar( vec ); + assert( vec[0] == "hey" ); + assert( vec[1] == "there" ); + assert( vec[2] == "buddy" ); + + std::vector> vec2; + iar( vec2 ); + for( auto & v : vec2 ) + { + assert( v[0] == "hey" ); + assert( v[1] == "there" ); + assert( v[2] == "buddy" ); + } + + int xxx[3]; + iar.loadBinaryValue( xxx, sizeof(int)*3 ); + assert( xxx[0] == -1 ); + assert( xxx[1] == 95 ); + assert( xxx[2] == 3 ); + + std::unique_ptr d1; + std::unique_ptr d2; + std::shared_ptr d3; + + iar( d1 ); + assert( d1->x == 4 && d1->y == 3 ); + iar( d2 ); + assert( ((Derived*)d2.get())->x == 5 && ((Derived*)d2.get())->y == 4 ); + iar( d3 ); + assert( ((Derived*)d3.get())->x == 6 && ((Derived*)d3.get())->y == 5 ); + } } diff --git a/unittests.cpp b/unittests.cpp index 72197277..a9f7571f 100644 --- a/unittests.cpp +++ b/unittests.cpp @@ -214,6 +214,8 @@ void test_pod() float const o_float = random_value(gen); double const o_double = random_value(gen); + std::cerr << i << std::endl; + std::ostringstream os; { OArchive oar(os);