more progress towards unordered XML - need to look everything over and clean up

This commit is contained in:
Shane Grant
2013-09-01 18:37:19 -07:00
parent 92559772f1
commit 7f857a2f42
2 changed files with 152 additions and 3 deletions

View File

@@ -321,12 +321,17 @@ namespace cereal
auto next = itsNodes.top().child; // By default we would move to the next child node auto next = itsNodes.top().child; // By default we would move to the next child node
auto const expectedName = itsNodes.top().name; // this is the expected name from the NVP, if provided auto const expectedName = itsNodes.top().name; // this is the expected name from the NVP, if provided
//std::cerr << "End of nodes? " << (next == nullptr) << std::endl;
//std::cerr << "Expected name: " << (expectedName ? expectedName : "null") << std::endl;
//std::cerr << "Next name: " << (next && next->name() ? next->name() : "null") << std::endl;
// If we were given an NVP name, look for it in the current level of the document. // If we were given an NVP name, look for it in the current level of the document.
// We only need to do this if we are either at the end of the current level (next is nullptr) or // We only need to do this if we are either at the end of the current level (next is nullptr) or
// the NVP name does not match the name of the node we would normally read next // the NVP name does not match the name of the node we would normally read next
if( expectedName && ( next == nullptr || std::strcmp( next->name(), expectedName ) != 0 ) ) if( expectedName && ( next == nullptr || std::strcmp( next->name(), expectedName ) != 0 ) )
{ {
next = itsNodes.top().node->first_node( expectedName ); //std::cerr << "Loading " << expectedName << std::endl;
next = itsNodes.top().search( expectedName );
if( next == nullptr ) if( next == nullptr )
throw Exception("XML Parsing failed - provided NVP not found"); throw Exception("XML Parsing failed - provided NVP not found");
@@ -338,6 +343,7 @@ namespace cereal
//! Finishes reading the current node //! Finishes reading the current node
void finishNode() void finishNode()
{ {
//std::cerr << "FINISH NODE: " << itsNodes.top().node->name() << std::endl;
// remove current // remove current
itsNodes.pop(); itsNodes.pop();
@@ -486,15 +492,51 @@ namespace cereal
name( nullptr ) name( nullptr )
{ } { }
//! Advances to the next sibling node of the child
void advance() void advance()
{ {
if( size ) //std::cerr << "\tPrevious size was " << size << std::endl;
if( --size )
{ {
--size; //--size;
//std::cerr << "Advancing " << child->name();
child = child->next_sibling(); child = child->next_sibling();
//std::cerr << " to " << (child ? child->name() : "null") << std::endl;
} }
} }
//! Searches for a child with the given name in this node
/*! @param name The name to search for (must be null terminated)
@return The node if found, nullptr otherwise */
rapidxml::xml_node<> * search( const char * name )
{
if( node && name )
{
size_t new_size = XMLInputArchive::getNumChildren( node );
size_t name_size = rapidxml::internal::measure( name );
//std::cerr << "Searching for " << name << " with length " << name_size << std::endl;
//std::cerr << "Resetting to base height of " << new_size << std::endl;
for( auto child = node->first_node(); child != nullptr; child = child->next_sibling() )
{
//std::cerr << "Current child name: " << child->name() << " at height " << new_size << std::endl;
if( rapidxml::internal::compare( child->name(), child->name_size(), name, name_size, true ) )
{
//std::cerr << "Found a match at size " << new_size << std::endl;
size = new_size;
return child;
}
--new_size;
}
}
return nullptr;
}
rapidxml::xml_node<> * node; //!< A pointer to this node rapidxml::xml_node<> * node; //!< A pointer to this node
rapidxml::xml_node<> * child; //!< A pointer to its current child rapidxml::xml_node<> * child; //!< A pointer to its current child
size_t size; size_t size;

View File

@@ -48,6 +48,7 @@
#include <cassert> #include <cassert>
#include <complex> #include <complex>
#include <iostream> #include <iostream>
#include <random>
class Base class Base
{ {
@@ -277,6 +278,109 @@ namespace cereal
}; };
} }
struct unordered_naming
{
int x;
int y;
int z;
template <class Archive>
void save( Archive & ar ) const
{
ar( CEREAL_NVP(x),
CEREAL_NVP(z),
CEREAL_NVP(y) );
}
template <class Archive>
void load( Archive & ar )
{
ar( x,
CEREAL_NVP(y),
CEREAL_NVP(z) );
}
bool operator==( unordered_naming const & other ) const
{
return x == other.x && y == other.y && z == other.z;
}
};
std::ostream& operator<<(std::ostream& os, unordered_naming const & s)
{
os << "[x: " << s.x << " y: " << s.y << " z: " << s.z << "]";
return os;
}
template <class IArchive, class OArchive>
void test_unordered_loads()
{
std::random_device rd;
std::mt19937 gen(rd());
auto rngB = [](){ return true; };
auto rngI = [](){ return 1; };
auto rngF = [](){ return 2.0f; };
auto rngD = [](){ return 3.2; };
for(int i=0; i<1; ++i)
{
auto const name1 = "1";
auto const name2 = "2";
auto const name3 = "3";
auto const name4 = "4";
auto const name5 = "5";
auto const name6 = "6";
auto const name7 = "7";
int o_int1 = rngI();
double o_double2 = rngD();
std::vector<bool> o_vecbool3 = { rngB(), rngB(), rngB(), rngB(), rngB() };
int o_int4 = rngI();
int o_int5 = rngI();
int o_int6 = rngI();
std::pair<float, unordered_naming> o_un7;
o_un7.first = rngF();
o_un7.second.x = rngI();
o_un7.second.y = rngI();
o_un7.second.z = rngI();
{
std::ofstream os("test.xml");
OArchive oar(os);
oar( cereal::make_nvp( name1, o_int1 ),
cereal::make_nvp( name2, o_double2 ),
cereal::make_nvp( name3, o_vecbool3 ),
cereal::make_nvp( name4, o_int4 ),
cereal::make_nvp( name5, o_int5 ),
cereal::make_nvp( name6, o_int6 ),
cereal::make_nvp( name7, o_un7 ) );
}
decltype(o_int1) i_int1;
decltype(o_double2) i_double2;
decltype(o_vecbool3) i_vecbool3;
decltype(o_int4) i_int4;
decltype(o_int5) i_int5;
decltype(o_int6) i_int6;
decltype(o_un7) i_un7;
std::ifstream is("test.xml");
{
IArchive iar(is);
iar( cereal::make_nvp( name7, o_un7 ),
cereal::make_nvp( name2, i_double2 ),
cereal::make_nvp( name4, i_int4 ),
cereal::make_nvp( name3, i_vecbool3 ),
cereal::make_nvp( name1, i_int1 ),
cereal::make_nvp( name5, i_int5 ),
i_int6 );
}
}
}
// ###################################################################### // ######################################################################
int main() int main()
{ {
@@ -546,6 +650,9 @@ int main()
std::cout << std::endl; std::cout << std::endl;
} }
std::cerr << "-------------------------" << std::endl;
test_unordered_loads<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
return 0; return 0;
} }