Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add vector child name #705

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions include/cereal/archives/xml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ namespace cereal
// insert into the XML
auto node = itsXML.allocate_node( rapidxml::node_element, namePtr, nullptr, nameString.size() );
itsNodes.top().node->append_node( node );
itsNodes.emplace( node );
const auto vectorChildNameString = itsNodes.top().vectorChildName;
itsNodes.emplace(node);
itsNodes.top().vectorChildName = vectorChildNameString;
}

//! Designates the most recently added node as finished
Expand All @@ -245,9 +247,10 @@ namespace cereal
}

//! Sets the name for the next node created with startNode
void setNextName( const char * name )
void setNextName(const char * name, const char * vectorChildName = nullptr)
{
itsNodes.top().name = name;
itsNodes.top().name = name;
itsNodes.top().vectorChildName = vectorChildName;
}

//! Saves some data, encoded as a string, into the current top level node
Expand Down Expand Up @@ -327,12 +330,14 @@ namespace cereal
const char * nm = nullptr ) :
node( n ),
counter( 0 ),
name( nm )
name( nm ),
vectorChildName(nullptr)
{ }

rapidxml::xml_node<> * node; //!< A pointer to this node
size_t counter; //!< The counter for naming child nodes
const char * name; //!< The name for the next child node
const char * vectorChildName; //!< The name for the inner child node

//! Gets the name for the next child node created from this node
/*! The name will be automatically generated using the counter if
Expand All @@ -343,9 +348,13 @@ namespace cereal
if( name )
{
auto n = name;
name = nullptr;
name = vectorChildName;
return {n};
}
else if (vectorChildName)
{
return {vectorChildName};
}
else
return "value" + std::to_string( counter++ ) + "\0";
}
Expand Down Expand Up @@ -890,7 +899,7 @@ namespace cereal
template <class T> inline
void CEREAL_SAVE_FUNCTION_NAME( XMLOutputArchive & ar, NameValuePair<T> const & t )
{
ar.setNextName( t.name );
ar.setNextName( t.name, t.vectorChildName );
ar( t.value );
}

Expand Down
4 changes: 3 additions & 1 deletion include/cereal/details/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ namespace cereal
only pass r-values in cases where this makes sense, such as the result of some
size() call.
@internal */
NameValuePair( char const * n, T && v ) : name(n), value(std::forward<T>(v)) {}
NameValuePair( char const * n, T && v ) : name(n), value(std::forward<T>(v)), vectorChildName(nullptr) {}
NameValuePair<T>& withVectorChildName(char const* n) { vectorChildName = n; return *this; }

char const * name;
Type value;
char const * vectorChildName;
};

//! A specialization of make_nvp<> that simply forwards the value for binary archives
Expand Down