mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
renamed CEREAL_ARCHIVE_RESTRICT_SERIALIZE to CEREAL_ARCHIVE_RESTRICT, more progress on xml serializtion
This commit is contained in:
@@ -105,7 +105,7 @@ namespace cereal
|
|||||||
|
|
||||||
//! Serializing NVP types to binary
|
//! Serializing NVP types to binary
|
||||||
template <class Archive, class T> inline
|
template <class Archive, class T> inline
|
||||||
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
|
CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
|
||||||
serialize( Archive & ar, NameValuePair<T> & t )
|
serialize( Archive & ar, NameValuePair<T> & t )
|
||||||
{
|
{
|
||||||
ar( t.value );
|
ar( t.value );
|
||||||
@@ -113,7 +113,7 @@ namespace cereal
|
|||||||
|
|
||||||
//! Serializing SizeTags to binary
|
//! Serializing SizeTags to binary
|
||||||
template <class Archive, class T> inline
|
template <class Archive, class T> inline
|
||||||
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
|
CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
|
||||||
serialize( Archive & ar, SizeTag<T> & t )
|
serialize( Archive & ar, SizeTag<T> & t )
|
||||||
{
|
{
|
||||||
ar( t.size );
|
ar( t.size );
|
||||||
|
|||||||
@@ -43,6 +43,11 @@
|
|||||||
|
|
||||||
namespace cereal
|
namespace cereal
|
||||||
{
|
{
|
||||||
|
namespace xml_detail
|
||||||
|
{
|
||||||
|
static const char * CEREAL_XML_STRING = "cereal";
|
||||||
|
}
|
||||||
|
|
||||||
// ######################################################################
|
// ######################################################################
|
||||||
//! An output archive designed to save data to XML
|
//! An output archive designed to save data to XML
|
||||||
class XMLOutputArchive : public OutputArchive<XMLOutputArchive>
|
class XMLOutputArchive : public OutputArchive<XMLOutputArchive>
|
||||||
@@ -65,7 +70,7 @@ namespace cereal
|
|||||||
itsXML.append_node( node );
|
itsXML.append_node( node );
|
||||||
|
|
||||||
// allocate root node
|
// allocate root node
|
||||||
auto root = itsXML.allocate_node( rapidxml::node_element, "cereal" );
|
auto root = itsXML.allocate_node( rapidxml::node_element, xml_detail::CEREAL_XML_STRING );
|
||||||
itsXML.append_node( root );
|
itsXML.append_node( root );
|
||||||
itsNodes.emplace( root );
|
itsNodes.emplace( root );
|
||||||
|
|
||||||
@@ -165,6 +170,7 @@ namespace cereal
|
|||||||
finishNode();
|
finishNode();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
protected:
|
||||||
//! A struct that contains metadata about a node
|
//! A struct that contains metadata about a node
|
||||||
struct NodeInfo
|
struct NodeInfo
|
||||||
{
|
{
|
||||||
@@ -216,7 +222,6 @@ namespace cereal
|
|||||||
@param stream The stream to read from. Can be a stringstream or a file. */
|
@param stream The stream to read from. Can be a stringstream or a file. */
|
||||||
XMLInputArchive( std::istream & stream ) :
|
XMLInputArchive( std::istream & stream ) :
|
||||||
InputArchive<XMLInputArchive>( this ),
|
InputArchive<XMLInputArchive>( this ),
|
||||||
itsStream( stream ),
|
|
||||||
itsData( std::istreambuf_iterator<char>( stream ), std::istreambuf_iterator<char>() )
|
itsData( std::istreambuf_iterator<char>( stream ), std::istreambuf_iterator<char>() )
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -229,12 +234,29 @@ namespace cereal
|
|||||||
//std::cout << e.what() << std::endl;
|
//std::cout << e.what() << std::endl;
|
||||||
//std::cout << e.where<char>() << std::endl;
|
//std::cout << e.where<char>() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse the root
|
||||||
|
auto root = itsXML.first_node( xml_detail::CEREAL_XML_STRING );
|
||||||
|
itsNodes.emplace( root );
|
||||||
}
|
}
|
||||||
|
|
||||||
//private:
|
protected:
|
||||||
std::istream & itsStream;
|
//! A struct that contains metadata about a node
|
||||||
|
struct NodeInfo
|
||||||
|
{
|
||||||
|
NodeInfo( rapidxml::xml_node<> * n = nullptr ) :
|
||||||
|
node( n ),
|
||||||
|
counter( 0 )
|
||||||
|
{ }
|
||||||
|
|
||||||
|
rapidxml::xml_node<> * node; //!< A pointer to this node
|
||||||
|
size_t counter; //!< The counter for naming child nodes
|
||||||
|
}; // NodeInfo
|
||||||
|
|
||||||
|
private:
|
||||||
std::vector<uint8_t> itsData; //!< The raw data loaded
|
std::vector<uint8_t> itsData; //!< The raw data loaded
|
||||||
rapidxml::xml_document<> itsXML; //!< The XML document
|
rapidxml::xml_document<> itsXML; //!< The XML document
|
||||||
|
std::stack<NodeInfo> itsNodes; //!< A stack of nodes read from the document
|
||||||
};
|
};
|
||||||
|
|
||||||
// ######################################################################
|
// ######################################################################
|
||||||
@@ -287,7 +309,7 @@ namespace cereal
|
|||||||
|
|
||||||
//! Serializing NVP types to XML
|
//! Serializing NVP types to XML
|
||||||
template <class Archive, class T> inline
|
template <class Archive, class T> inline
|
||||||
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(XMLInputArchive, XMLOutputArchive)
|
CEREAL_ARCHIVE_RESTRICT(XMLInputArchive, XMLOutputArchive)
|
||||||
serialize( Archive & ar, NameValuePair<T> & t )
|
serialize( Archive & ar, NameValuePair<T> & t )
|
||||||
{
|
{
|
||||||
ar.setNextName( t.name );
|
ar.setNextName( t.name );
|
||||||
@@ -296,7 +318,7 @@ namespace cereal
|
|||||||
|
|
||||||
//! Serializing SizeTags to XML
|
//! Serializing SizeTags to XML
|
||||||
template <class Archive, class T> inline
|
template <class Archive, class T> inline
|
||||||
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(XMLInputArchive, XMLOutputArchive)
|
CEREAL_ARCHIVE_RESTRICT(XMLInputArchive, XMLOutputArchive)
|
||||||
serialize( Archive & ar, SizeTag<T> & )
|
serialize( Archive & ar, SizeTag<T> & )
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
|||||||
@@ -260,14 +260,14 @@ namespace cereal
|
|||||||
|
|
||||||
@code{.cpp}
|
@code{.cpp}
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
|
CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
|
||||||
serialize( Archive & ar, MyCoolType & m )
|
serialize( Archive & ar, MyCoolType & m )
|
||||||
{
|
{
|
||||||
ar & m;
|
ar & m;
|
||||||
}
|
}
|
||||||
@endcode
|
@endcode
|
||||||
*/
|
*/
|
||||||
#define CEREAL_ARCHIVE_RESTRICT_SERIALIZE(INTYPE, OUTTYPE) \
|
#define CEREAL_ARCHIVE_RESTRICT(INTYPE, OUTTYPE) \
|
||||||
typename std::enable_if<std::is_same<Archive, INTYPE>::value || std::is_same<Archive, OUTTYPE>::value, void>::type
|
typename std::enable_if<std::is_same<Archive, INTYPE>::value || std::is_same<Archive, OUTTYPE>::value, void>::type
|
||||||
} // namespace traits
|
} // namespace traits
|
||||||
|
|
||||||
|
|||||||
@@ -333,8 +333,10 @@ int main()
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::ifstream is("out.xml");
|
std::ifstream is("out.xml");
|
||||||
cereal::XMLInputArchive oar( is );
|
cereal::XMLInputArchive iar( is );
|
||||||
std::cout << oar.itsData.size() << std::endl;
|
|
||||||
|
//int z;
|
||||||
|
//iar( cereal::make_nvp("hello", z) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user