added some comments to xml for loadvalue, everything running fine through valgrind now

This commit is contained in:
Shane Grant
2013-07-06 15:58:55 -07:00
parent 283db1b09d
commit 1f64755397
3 changed files with 163 additions and 155 deletions

View File

@@ -238,7 +238,7 @@ namespace cereal
{ {
try try
{ {
itsData.push_back('\0'); // rapidxml will do terrible things without this itsData.push_back('\0'); // rapidxml will do terrible things without the data being null terminated
itsXML.parse<rapidxml::parse_no_data_nodes | rapidxml::parse_declaration_node>( reinterpret_cast<char *>( itsData.data() ) ); itsXML.parse<rapidxml::parse_no_data_nodes | rapidxml::parse_declaration_node>( reinterpret_cast<char *>( itsData.data() ) );
} }
catch( rapidxml::parse_error const & e ) catch( rapidxml::parse_error const & e )
@@ -284,6 +284,7 @@ namespace cereal
is >> value; is >> value;
} }
//! Loads a type best represented as an unsigned long
template <class T> inline template <class T> inline
typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value && sizeof(T) < sizeof(long long), void>::type typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value && sizeof(T) < sizeof(long long), void>::type
loadValue( T & value ) loadValue( T & value )
@@ -291,6 +292,7 @@ namespace cereal
value = std::stoul( itsNodes.top().node->value() ); value = std::stoul( itsNodes.top().node->value() );
} }
//! Loads a type best represented as an unsigned long long
template <class T> inline template <class T> inline
typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value && sizeof(T) >= sizeof(long long), void>::type typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value && sizeof(T) >= sizeof(long long), void>::type
loadValue( T & value ) loadValue( T & value )
@@ -298,6 +300,7 @@ namespace cereal
value = std::stoull( itsNodes.top().node->value() ); value = std::stoull( itsNodes.top().node->value() );
} }
//! Loads a type best represented as an int
template <class T> inline template <class T> inline
typename std::enable_if<std::is_signed<T>::value && sizeof(T) <= sizeof(int), void>::type typename std::enable_if<std::is_signed<T>::value && sizeof(T) <= sizeof(int), void>::type
loadValue( T & value ) loadValue( T & value )
@@ -305,6 +308,7 @@ namespace cereal
value = std::stoi( itsNodes.top().node->value() ); value = std::stoi( itsNodes.top().node->value() );
} }
//! Loads a type best represented as a long
template <class T> inline template <class T> inline
typename std::enable_if<std::is_signed<T>::value && (sizeof(T) > sizeof(int)) && (sizeof(T) <= sizeof(long)), void>::type typename std::enable_if<std::is_signed<T>::value && (sizeof(T) > sizeof(int)) && (sizeof(T) <= sizeof(long)), void>::type
loadValue( T & value ) loadValue( T & value )
@@ -312,6 +316,7 @@ namespace cereal
value = std::stol( itsNodes.top().node->value() ); value = std::stol( itsNodes.top().node->value() );
} }
//! Loads a type best represented as a long long
template <class T> inline template <class T> inline
typename std::enable_if<std::is_signed<T>::value && (sizeof(T) > sizeof(long)) && (sizeof(T) <= sizeof(long long)), void>::type typename std::enable_if<std::is_signed<T>::value && (sizeof(T) > sizeof(long)) && (sizeof(T) <= sizeof(long long)), void>::type
loadValue( T & value ) loadValue( T & value )
@@ -319,16 +324,24 @@ namespace cereal
value = std::stoll( itsNodes.top().node->value() ); value = std::stoll( itsNodes.top().node->value() );
} }
//! Loads a type best represented as a float
void loadValue( float & value ) void loadValue( float & value )
{ {
value = std::stof( itsNodes.top().node->value() ); value = std::stof( itsNodes.top().node->value() );
} }
//! Loads a type best represented as a double
void loadValue( double & value ) void loadValue( double & value )
{ {
value = std::stod( itsNodes.top().node->value() ); value = std::stod( itsNodes.top().node->value() );
} }
//! Loads a type best represented as a long double
void loadValue( long double & value )
{
value = std::stold( itsNodes.top().node->value() );
}
//! Loads a string from the current node //! Loads a string from the current node
template<class CharT, class Traits, class Alloc> inline template<class CharT, class Traits, class Alloc> inline
void loadValue( std::basic_string<CharT, Traits, Alloc> & str ) void loadValue( std::basic_string<CharT, Traits, Alloc> & str )

View File

@@ -288,177 +288,174 @@ int main()
e_out.t4 = {4}; e_out.t4 = {4};
e_out.s = "Hello, World!"; 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<int>(99);
oar( CEREAL_NVP(intptr) );
std::map<std::string, int> map1 =
{ {
std::ofstream os("out.xml"); {"one", 1},
cereal::XMLOutputArchive oar( os ); {"two", 2},
//cereal::XMLOutputArchive oar( std::cout ); {"three", 3}
};
oar( cereal::make_nvp("hello", 5 ) ); oar( CEREAL_NVP(map1) );
std::string bla("bla"); int x = 3;
oar( bla ); oar( CEREAL_NVP(x) );
oar( 5 );
oar( 3.3 );
oar( 3.2f );
oar( true );
auto intptr = std::make_shared<int>(99); std::array<int,5> arr = {{1, 2, 3, 4, 5}};
oar( CEREAL_NVP(intptr) ); oar( arr );
std::map<std::string, int> map1 = std::vector<std::string> vec = {"hey",
{ "there",
{"one", 1}, "buddy"};
{"two", 2},
{"three", 3}
};
oar( CEREAL_NVP(map1) ); std::vector<std::vector<std::string>> vec2 = {vec, vec, vec};
int x = 3; oar( cereal::make_nvp("EVERYTHING", e_out) );
oar( CEREAL_NVP(x) ); oar( vec );
oar( 5 ); oar( vec2 );
oar( 3.3 );
oar( 3.2f );
oar( true );
std::array<int,5> arr = {{1, 2, 3, 4, 5}}; int xxx[] = {-1, 95, 3};
oar( arr ); oar.saveBinaryValue( xxx, sizeof(int)*3, "xxxbinary" );
//oar.saveBinaryValue( xxx, sizeof(int)*3 );
std::vector<std::string> vec = {"hey", std::unique_ptr<Derived> d1( new Derived(3, 4) );
"there", std::unique_ptr<Base> d2( new Derived(4, 5) );
"buddy"}; std::shared_ptr<Base> d3( new Derived(5, 6) );
oar( d1 );
oar( d2 );
oar( d3 );
}
std::vector<std::vector<std::string>> vec2 = {vec, vec, vec}; {
std::ifstream is("out.xml");
cereal::XMLInputArchive iar( is );
oar( cereal::make_nvp("EVERYTHING", e_out) ); int hello;
oar( vec ); iar( cereal::make_nvp("hello", hello) );
oar( vec2 ); assert( hello == 5 );
int xxx[] = {-1, 95, 3}; std::string bla;
oar.saveBinaryValue( xxx, sizeof(int)*3, "xxxbinary" ); iar( bla );
//oar.saveBinaryValue( xxx, sizeof(int)*3 ); assert( bla == "bla" );
std::unique_ptr<Derived> d1( new Derived(3, 4) ); std::shared_ptr<int> intptr;
std::unique_ptr<Base> d2( new Derived(4, 5) ); iar( CEREAL_NVP(intptr) );
std::shared_ptr<Base> d3( new Derived(5, 6) ); assert( *intptr == 99 );
oar( d1 );
oar( d2 ); std::map<std::string, int> map1;
oar( d3 );
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<int,5> 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<std::string> vec;
iar( vec );
assert( vec[0] == "hey" );
assert( vec[1] == "there" );
assert( vec[2] == "buddy" );
std::vector<std::vector<std::string>> vec2;
iar( vec2 );
for( auto & v : vec2 )
{
assert( v[0] == "hey" );
assert( v[1] == "there" );
assert( v[2] == "buddy" );
} }
{ int xxx[3];
std::ifstream is("out.xml"); iar.loadBinaryValue( xxx, sizeof(int)*3 );
cereal::XMLInputArchive iar( is ); assert( xxx[0] == -1 );
assert( xxx[1] == 95 );
assert( xxx[2] == 3 );
int hello; std::unique_ptr<Derived> d1;
iar( cereal::make_nvp("hello", hello) ); std::unique_ptr<Base> d2;
assert( hello == 5 ); std::shared_ptr<Base> d3;
std::string bla; iar( d1 );
iar( bla ); assert( d1->x == 4 && d1->y == 3 );
assert( bla == "bla" ); iar( d2 );
assert( ((Derived*)d2.get())->x == 5 && ((Derived*)d2.get())->y == 4 );
std::shared_ptr<int> intptr; iar( d3 );
iar( CEREAL_NVP(intptr) ); assert( ((Derived*)d3.get())->x == 6 && ((Derived*)d3.get())->y == 5 );
assert( *intptr == 99 );
std::map<std::string, int> 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<int,5> 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<std::string> vec;
iar( vec );
assert( vec[0] == "hey" );
assert( vec[1] == "there" );
assert( vec[2] == "buddy" );
std::vector<std::vector<std::string>> 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<Derived> d1;
std::unique_ptr<Base> d2;
std::shared_ptr<Base> 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 );
}
} }

View File

@@ -214,8 +214,6 @@ void test_pod()
float const o_float = random_value<float>(gen); float const o_float = random_value<float>(gen);
double const o_double = random_value<double>(gen); double const o_double = random_value<double>(gen);
std::cerr << i << std::endl;
std::ostringstream os; std::ostringstream os;
{ {
OArchive oar(os); OArchive oar(os);