xml.hpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*! \file xml.hpp
  2. \brief XML input and output archives */
  3. /*
  4. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the copyright holder nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef CEREAL_ARCHIVES_XML_HPP_
  28. #define CEREAL_ARCHIVES_XML_HPP_
  29. #include "cereal/cereal.hpp"
  30. #include "cereal/details/util.hpp"
  31. #include "cereal/external/rapidxml/rapidxml.hpp"
  32. #include "cereal/external/rapidxml/rapidxml_print.hpp"
  33. #include "cereal/external/base64.hpp"
  34. #include <sstream>
  35. #include <stack>
  36. #include <vector>
  37. #include <limits>
  38. #include <string>
  39. #include <cstring>
  40. #include <cmath>
  41. namespace cereal
  42. {
  43. namespace xml_detail
  44. {
  45. #ifndef CEREAL_XML_STRING_VALUE
  46. //! The default name for the root node in a cereal xml archive.
  47. /*! You can define CEREAL_XML_STRING_VALUE to be different assuming you do so
  48. before this file is included. */
  49. #define CEREAL_XML_STRING_VALUE "cereal"
  50. #endif // CEREAL_XML_STRING_VALUE
  51. //! The name given to the root node in a cereal xml archive
  52. static const char * CEREAL_XML_STRING = CEREAL_XML_STRING_VALUE;
  53. //! Returns true if the character is whitespace
  54. inline bool isWhitespace( char c )
  55. {
  56. return c == ' ' || c == '\t' || c == '\n' || c == '\r';
  57. }
  58. }
  59. // ######################################################################
  60. //! An output archive designed to save data to XML
  61. /*! This archive uses RapidXML to build an in memory XML tree of the
  62. data it serializes before outputting it to its stream upon destruction.
  63. This archive should be used in an RAII fashion, letting
  64. the automatic destruction of the object cause the flush to its stream.
  65. XML archives provides a human readable output but at decreased
  66. performance (both in time and space) compared to binary archives.
  67. XML benefits greatly from name-value pairs, which if present, will
  68. name the nodes in the output. If these are not present, each level
  69. of the output tree will be given an automatically generated delimited name.
  70. The precision of the output archive controls the number of decimals output
  71. for floating point numbers and should be sufficiently large (i.e. at least 20)
  72. if there is a desire to have binary equality between the numbers output and
  73. those read in. In general you should expect a loss of precision when going
  74. from floating point to text and back.
  75. XML archives can optionally print the type of everything they serialize, which
  76. adds an attribute to each node.
  77. XML archives do not output the size information for any dynamically sized structure
  78. and instead infer it from the number of children for a node. This means that data
  79. can be hand edited for dynamic sized structures and will still be readable. This
  80. is accomplished through the cereal::SizeTag object, which will also add an attribute
  81. to its parent field.
  82. \ingroup Archives */
  83. class XMLOutputArchive : public OutputArchive<XMLOutputArchive>, public traits::TextArchive
  84. {
  85. public:
  86. /*! @name Common Functionality
  87. Common use cases for directly interacting with an XMLOutputArchive */
  88. //! @{
  89. //! A class containing various advanced options for the XML archive
  90. /*! Options can either be directly passed to the constructor, or chained using the
  91. modifier functions for an interface analogous to named parameters */
  92. class Options
  93. {
  94. public:
  95. //! Default options
  96. static Options Default(){ return Options(); }
  97. //! Specify specific options for the XMLOutputArchive
  98. /*! @param precision_ The precision used for floating point numbers
  99. @param indent_ Whether to indent each line of XML
  100. @param outputType_ Whether to output the type of each serialized object as an attribute
  101. @param sizeAttributes_ Whether dynamically sized containers output the size=dynamic attribute */
  102. explicit Options( int precision_ = std::numeric_limits<double>::max_digits10,
  103. bool indent_ = true,
  104. bool outputType_ = false,
  105. bool sizeAttributes_ = true ) :
  106. itsPrecision( precision_ ),
  107. itsIndent( indent_ ),
  108. itsOutputType( outputType_ ),
  109. itsSizeAttributes( sizeAttributes_ )
  110. { }
  111. /*! @name Option Modifiers
  112. An interface for setting option settings analogous to named parameters.
  113. @code{cpp}
  114. cereal::XMLOutputArchive ar( myStream,
  115. cereal::XMLOutputArchive::Options()
  116. .indent(true)
  117. .sizeAttributes(false) );
  118. @endcode
  119. */
  120. //! @{
  121. //! Sets the precision used for floaing point numbers
  122. Options & precision( int value ){ itsPrecision = value; return * this; }
  123. //! Whether to indent each line of XML
  124. Options & indent( bool enable ){ itsIndent = enable; return *this; }
  125. //! Whether to output the type of each serialized object as an attribute
  126. Options & outputType( bool enable ){ itsOutputType = enable; return *this; }
  127. //! Whether dynamically sized containers (e.g. vector) output the size=dynamic attribute
  128. Options & sizeAttributes( bool enable ){ itsSizeAttributes = enable; return *this; }
  129. //! @}
  130. private:
  131. friend class XMLOutputArchive;
  132. int itsPrecision;
  133. bool itsIndent;
  134. bool itsOutputType;
  135. bool itsSizeAttributes;
  136. };
  137. //! Construct, outputting to the provided stream upon destruction
  138. /*! @param stream The stream to output to. Note that XML is only guaranteed to flush
  139. its output to the stream upon destruction.
  140. @param options The XML specific options to use. See the Options struct
  141. for the values of default parameters */
  142. XMLOutputArchive( std::ostream & stream, Options const & options = Options::Default() ) :
  143. OutputArchive<XMLOutputArchive>(this),
  144. itsStream(stream),
  145. itsOutputType( options.itsOutputType ),
  146. itsIndent( options.itsIndent ),
  147. itsSizeAttributes(options.itsSizeAttributes)
  148. {
  149. // rapidxml will delete all allocations when xml_document is cleared
  150. auto node = itsXML.allocate_node( rapidxml::node_declaration );
  151. node->append_attribute( itsXML.allocate_attribute( "version", "1.0" ) );
  152. node->append_attribute( itsXML.allocate_attribute( "encoding", "utf-8" ) );
  153. itsXML.append_node( node );
  154. // allocate root node
  155. auto root = itsXML.allocate_node( rapidxml::node_element, xml_detail::CEREAL_XML_STRING );
  156. itsXML.append_node( root );
  157. itsNodes.emplace( root );
  158. // set attributes on the streams
  159. itsStream << std::boolalpha;
  160. itsStream.precision( options.itsPrecision );
  161. itsOS << std::boolalpha;
  162. itsOS.precision( options.itsPrecision );
  163. }
  164. //! Destructor, flushes the XML
  165. ~XMLOutputArchive() CEREAL_NOEXCEPT
  166. {
  167. const int flags = itsIndent ? 0x0 : rapidxml::print_no_indenting;
  168. rapidxml::print( itsStream, itsXML, flags );
  169. itsXML.clear();
  170. }
  171. //! Saves some binary data, encoded as a base64 string, with an optional name
  172. /*! This can be called directly by users and it will automatically create a child node for
  173. the current XML node, populate it with a base64 encoded string, and optionally name
  174. it. The node will be finished after it has been populated. */
  175. void saveBinaryValue( const void * data, size_t size, const char * name = nullptr )
  176. {
  177. itsNodes.top().name = name;
  178. startNode();
  179. auto base64string = base64::encode( reinterpret_cast<const unsigned char *>( data ), size );
  180. saveValue( base64string );
  181. if( itsOutputType )
  182. itsNodes.top().node->append_attribute( itsXML.allocate_attribute( "type", "cereal binary data" ) );
  183. finishNode();
  184. }
  185. //! @}
  186. /*! @name Internal Functionality
  187. Functionality designed for use by those requiring control over the inner mechanisms of
  188. the XMLOutputArchive */
  189. //! @{
  190. //! Creates a new node that is a child of the node at the top of the stack
  191. /*! Nodes will be given a name that has either been pre-set by a name value pair,
  192. or generated based upon a counter unique to the parent node. If you want to
  193. give a node a specific name, use setNextName prior to calling startNode.
  194. The node will then be pushed onto the node stack. */
  195. void startNode()
  196. {
  197. // generate a name for this new node
  198. const auto nameString = itsNodes.top().getValueName();
  199. // allocate strings for all of the data in the XML object
  200. auto namePtr = itsXML.allocate_string( nameString.data(), nameString.length() + 1 );
  201. // insert into the XML
  202. auto node = itsXML.allocate_node( rapidxml::node_element, namePtr, nullptr, nameString.size() );
  203. itsNodes.top().node->append_node( node );
  204. itsNodes.emplace( node );
  205. }
  206. //! Designates the most recently added node as finished
  207. void finishNode()
  208. {
  209. itsNodes.pop();
  210. }
  211. //! Sets the name for the next node created with startNode
  212. void setNextName( const char * name )
  213. {
  214. itsNodes.top().name = name;
  215. }
  216. //! Saves some data, encoded as a string, into the current top level node
  217. /*! The data will be be named with the most recent name if one exists,
  218. otherwise it will be given some default delimited value that depends upon
  219. the parent node */
  220. template <class T> inline
  221. void saveValue( T const & value )
  222. {
  223. itsOS.clear(); itsOS.seekp( 0, std::ios::beg );
  224. itsOS << value << std::ends;
  225. auto strValue = itsOS.str();
  226. // itsOS.str() may contain data from previous calls after the first '\0' that was just inserted
  227. // and this data is counted in the length call. We make sure to remove that section so that the
  228. // whitespace validation is done properly
  229. strValue.resize(std::strlen(strValue.c_str()));
  230. // If the first or last character is a whitespace, add xml:space attribute
  231. const auto len = strValue.length();
  232. if ( len > 0 && ( xml_detail::isWhitespace( strValue[0] ) || xml_detail::isWhitespace( strValue[len - 1] ) ) )
  233. {
  234. itsNodes.top().node->append_attribute( itsXML.allocate_attribute( "xml:space", "preserve" ) );
  235. }
  236. // allocate strings for all of the data in the XML object
  237. auto dataPtr = itsXML.allocate_string(strValue.c_str(), strValue.length() + 1 );
  238. // insert into the XML
  239. itsNodes.top().node->append_node( itsXML.allocate_node( rapidxml::node_data, nullptr, dataPtr ) );
  240. }
  241. //! Overload for uint8_t prevents them from being serialized as characters
  242. void saveValue( uint8_t const & value )
  243. {
  244. saveValue( static_cast<uint32_t>( value ) );
  245. }
  246. //! Overload for int8_t prevents them from being serialized as characters
  247. void saveValue( int8_t const & value )
  248. {
  249. saveValue( static_cast<int32_t>( value ) );
  250. }
  251. //! Causes the type to be appended as an attribute to the most recently made node if output type is set to true
  252. template <class T> inline
  253. void insertType()
  254. {
  255. if( !itsOutputType )
  256. return;
  257. // generate a name for this new node
  258. const auto nameString = util::demangledName<T>();
  259. // allocate strings for all of the data in the XML object
  260. auto namePtr = itsXML.allocate_string( nameString.data(), nameString.length() + 1 );
  261. itsNodes.top().node->append_attribute( itsXML.allocate_attribute( "type", namePtr ) );
  262. }
  263. //! Appends an attribute to the current top level node
  264. void appendAttribute( const char * name, const char * value )
  265. {
  266. auto namePtr = itsXML.allocate_string( name );
  267. auto valuePtr = itsXML.allocate_string( value );
  268. itsNodes.top().node->append_attribute( itsXML.allocate_attribute( namePtr, valuePtr ) );
  269. }
  270. bool hasSizeAttributes() const { return itsSizeAttributes; }
  271. protected:
  272. //! A struct that contains metadata about a node
  273. struct NodeInfo
  274. {
  275. NodeInfo( rapidxml::xml_node<> * n = nullptr,
  276. const char * nm = nullptr ) :
  277. node( n ),
  278. counter( 0 ),
  279. name( nm )
  280. { }
  281. rapidxml::xml_node<> * node; //!< A pointer to this node
  282. size_t counter; //!< The counter for naming child nodes
  283. const char * name; //!< The name for the next child node
  284. //! Gets the name for the next child node created from this node
  285. /*! The name will be automatically generated using the counter if
  286. a name has not been previously set. If a name has been previously
  287. set, that name will be returned only once */
  288. std::string getValueName()
  289. {
  290. if( name )
  291. {
  292. auto n = name;
  293. name = nullptr;
  294. return {n};
  295. }
  296. else
  297. return "value" + std::to_string( counter++ ) + "\0";
  298. }
  299. }; // NodeInfo
  300. //! @}
  301. private:
  302. std::ostream & itsStream; //!< The output stream
  303. rapidxml::xml_document<> itsXML; //!< The XML document
  304. std::stack<NodeInfo> itsNodes; //!< A stack of nodes added to the document
  305. std::ostringstream itsOS; //!< Used to format strings internally
  306. bool itsOutputType; //!< Controls whether type information is printed
  307. bool itsIndent; //!< Controls whether indenting is used
  308. bool itsSizeAttributes; //!< Controls whether lists have a size attribute
  309. }; // XMLOutputArchive
  310. // ######################################################################
  311. //! An output archive designed to load data from XML
  312. /*! This archive uses RapidXML to build an in memory XML tree of the
  313. data in the stream it is given before loading any types serialized.
  314. As with the output XML archive, the preferred way to use this archive is in
  315. an RAII fashion, ensuring its destruction after all data has been read.
  316. Input XML should have been produced by the XMLOutputArchive. Data can
  317. only be added to dynamically sized containers - the input archive will
  318. determine their size by looking at the number of child nodes. Data that
  319. did not originate from an XMLOutputArchive is not officially supported,
  320. but may be possible to use if properly formatted.
  321. The XMLInputArchive does not require that nodes are loaded in the same
  322. order they were saved by XMLOutputArchive. Using name value pairs (NVPs),
  323. it is possible to load in an out of order fashion or otherwise skip/select
  324. specific nodes to load.
  325. The default behavior of the input archive is to read sequentially starting
  326. with the first node and exploring its children. When a given NVP does
  327. not match the read in name for a node, the archive will search for that
  328. node at the current level and load it if it exists. After loading an out of
  329. order node, the archive will then proceed back to loading sequentially from
  330. its new position.
  331. Consider this simple example where loading of some data is skipped:
  332. @code{cpp}
  333. // imagine the input file has someData(1-9) saved in order at the top level node
  334. ar( someData1, someData2, someData3 ); // XML loads in the order it sees in the file
  335. ar( cereal::make_nvp( "hello", someData6 ) ); // NVP given does not
  336. // match expected NVP name, so we search
  337. // for the given NVP and load that value
  338. ar( someData7, someData8, someData9 ); // with no NVP given, loading resumes at its
  339. // current location, proceeding sequentially
  340. @endcode
  341. \ingroup Archives */
  342. class XMLInputArchive : public InputArchive<XMLInputArchive>, public traits::TextArchive
  343. {
  344. public:
  345. /*! @name Common Functionality
  346. Common use cases for directly interacting with an XMLInputArchive */
  347. //! @{
  348. //! Construct, reading in from the provided stream
  349. /*! Reads in an entire XML document from some stream and parses it as soon
  350. as serialization starts
  351. @param stream The stream to read from. Can be a stringstream or a file. */
  352. XMLInputArchive( std::istream & stream ) :
  353. InputArchive<XMLInputArchive>( this ),
  354. itsData( std::istreambuf_iterator<char>( stream ), std::istreambuf_iterator<char>() )
  355. {
  356. try
  357. {
  358. itsData.push_back('\0'); // rapidxml will do terrible things without the data being null terminated
  359. itsXML.parse<rapidxml::parse_trim_whitespace | rapidxml::parse_no_data_nodes | rapidxml::parse_declaration_node>( reinterpret_cast<char *>( itsData.data() ) );
  360. }
  361. catch( rapidxml::parse_error const & )
  362. {
  363. //std::cerr << "-----Original-----" << std::endl;
  364. //stream.seekg(0);
  365. //std::cout << std::string( std::istreambuf_iterator<char>( stream ), std::istreambuf_iterator<char>() ) << std::endl;
  366. //std::cerr << "-----Error-----" << std::endl;
  367. //std::cerr << e.what() << std::endl;
  368. //std::cerr << e.where<char>() << std::endl;
  369. throw Exception("XML Parsing failed - likely due to invalid characters or invalid naming");
  370. }
  371. // Parse the root
  372. auto root = itsXML.first_node( xml_detail::CEREAL_XML_STRING );
  373. if( root == nullptr )
  374. throw Exception("Could not detect cereal root node - likely due to empty or invalid input");
  375. else
  376. itsNodes.emplace( root );
  377. }
  378. ~XMLInputArchive() CEREAL_NOEXCEPT = default;
  379. //! Loads some binary data, encoded as a base64 string, optionally specified by some name
  380. /*! This will automatically start and finish a node to load the data, and can be called directly by
  381. users.
  382. Note that this follows the same ordering rules specified in the class description in regards
  383. to loading in/out of order */
  384. void loadBinaryValue( void * data, size_t size, const char * name = nullptr )
  385. {
  386. setNextName( name );
  387. startNode();
  388. std::string encoded;
  389. loadValue( encoded );
  390. auto decoded = base64::decode( encoded );
  391. if( size != decoded.size() )
  392. throw Exception("Decoded binary data size does not match specified size");
  393. std::memcpy( data, decoded.data(), decoded.size() );
  394. finishNode();
  395. }
  396. //! @}
  397. /*! @name Internal Functionality
  398. Functionality designed for use by those requiring control over the inner mechanisms of
  399. the XMLInputArchive */
  400. //! @{
  401. //! Prepares to start reading the next node
  402. /*! This places the next node to be parsed onto the nodes stack.
  403. By default our strategy is to start with the document root node and then
  404. recursively iterate through all children in the order they show up in the document.
  405. We don't need to know NVPs do to this; we'll just blindly load in the order things appear in.
  406. We check to see if the specified NVP matches what the next automatically loaded node is. If they
  407. match, we just continue as normal, going in order. If they don't match, we attempt to find a node
  408. named after the NVP that is being loaded. If that NVP does not exist, we throw an exception. */
  409. void startNode()
  410. {
  411. auto next = itsNodes.top().child; // By default we would move to the next child node
  412. auto const expectedName = itsNodes.top().name; // this is the expected name from the NVP, if provided
  413. // If we were given an NVP name, look for it in the current level of the document.
  414. // We only need to do this if either we have exhausted the siblings of the current level or
  415. // the NVP name does not match the name of the node we would normally read next
  416. if( expectedName && ( next == nullptr || std::strcmp( next->name(), expectedName ) != 0 ) )
  417. {
  418. next = itsNodes.top().search( expectedName );
  419. if( next == nullptr )
  420. throw Exception("XML Parsing failed - provided NVP (" + std::string(expectedName) + ") not found");
  421. }
  422. itsNodes.emplace( next );
  423. }
  424. //! Finishes reading the current node
  425. void finishNode()
  426. {
  427. // remove current
  428. itsNodes.pop();
  429. // advance parent
  430. itsNodes.top().advance();
  431. // Reset name
  432. itsNodes.top().name = nullptr;
  433. }
  434. //! Retrieves the current node name
  435. //! will return @c nullptr if the node does not have a name
  436. const char * getNodeName() const
  437. {
  438. return itsNodes.top().getChildName();
  439. }
  440. //! Sets the name for the next node created with startNode
  441. void setNextName( const char * name )
  442. {
  443. itsNodes.top().name = name;
  444. }
  445. //! Loads a bool from the current top node
  446. template <class T, traits::EnableIf<std::is_unsigned<T>::value,
  447. std::is_same<T, bool>::value> = traits::sfinae> inline
  448. void loadValue( T & value )
  449. {
  450. std::istringstream is( itsNodes.top().node->value() );
  451. is.setf( std::ios::boolalpha );
  452. is >> value;
  453. }
  454. //! Loads a char (signed or unsigned) from the current top node
  455. template <class T, traits::EnableIf<std::is_integral<T>::value,
  456. !std::is_same<T, bool>::value,
  457. sizeof(T) == sizeof(char)> = traits::sfinae> inline
  458. void loadValue( T & value )
  459. {
  460. value = *reinterpret_cast<T*>( itsNodes.top().node->value() );
  461. }
  462. //! Load an int8_t from the current top node (ensures we parse entire number)
  463. void loadValue( int8_t & value )
  464. {
  465. int32_t val; loadValue( val ); value = static_cast<int8_t>( val );
  466. }
  467. //! Load a uint8_t from the current top node (ensures we parse entire number)
  468. void loadValue( uint8_t & value )
  469. {
  470. uint32_t val; loadValue( val ); value = static_cast<uint8_t>( val );
  471. }
  472. //! Loads a type best represented as an unsigned long from the current top node
  473. template <class T, traits::EnableIf<std::is_unsigned<T>::value,
  474. !std::is_same<T, bool>::value,
  475. !std::is_same<T, char>::value,
  476. !std::is_same<T, unsigned char>::value,
  477. sizeof(T) < sizeof(long long)> = traits::sfinae> inline
  478. void loadValue( T & value )
  479. {
  480. value = static_cast<T>( std::stoul( itsNodes.top().node->value() ) );
  481. }
  482. //! Loads a type best represented as an unsigned long long from the current top node
  483. template <class T, traits::EnableIf<std::is_unsigned<T>::value,
  484. !std::is_same<T, bool>::value,
  485. sizeof(T) >= sizeof(long long)> = traits::sfinae> inline
  486. void loadValue( T & value )
  487. {
  488. value = static_cast<T>( std::stoull( itsNodes.top().node->value() ) );
  489. }
  490. //! Loads a type best represented as an int from the current top node
  491. template <class T, traits::EnableIf<std::is_signed<T>::value,
  492. !std::is_same<T, char>::value,
  493. sizeof(T) <= sizeof(int)> = traits::sfinae> inline
  494. void loadValue( T & value )
  495. {
  496. value = static_cast<T>( std::stoi( itsNodes.top().node->value() ) );
  497. }
  498. //! Loads a type best represented as a long from the current top node
  499. template <class T, traits::EnableIf<std::is_signed<T>::value,
  500. (sizeof(T) > sizeof(int)),
  501. sizeof(T) <= sizeof(long)> = traits::sfinae> inline
  502. void loadValue( T & value )
  503. {
  504. value = static_cast<T>( std::stol( itsNodes.top().node->value() ) );
  505. }
  506. //! Loads a type best represented as a long long from the current top node
  507. template <class T, traits::EnableIf<std::is_signed<T>::value,
  508. (sizeof(T) > sizeof(long)),
  509. sizeof(T) <= sizeof(long long)> = traits::sfinae> inline
  510. void loadValue( T & value )
  511. {
  512. value = static_cast<T>( std::stoll( itsNodes.top().node->value() ) );
  513. }
  514. //! Loads a type best represented as a float from the current top node
  515. void loadValue( float & value )
  516. {
  517. try
  518. {
  519. value = std::stof( itsNodes.top().node->value() );
  520. }
  521. catch( std::out_of_range const & )
  522. {
  523. // special case for denormalized values
  524. std::istringstream is( itsNodes.top().node->value() );
  525. is >> value;
  526. if( std::fpclassify( value ) != FP_SUBNORMAL )
  527. throw;
  528. }
  529. }
  530. //! Loads a type best represented as a double from the current top node
  531. void loadValue( double & value )
  532. {
  533. try
  534. {
  535. value = std::stod( itsNodes.top().node->value() );
  536. }
  537. catch( std::out_of_range const & )
  538. {
  539. // special case for denormalized values
  540. std::istringstream is( itsNodes.top().node->value() );
  541. is >> value;
  542. if( std::fpclassify( value ) != FP_SUBNORMAL )
  543. throw;
  544. }
  545. }
  546. //! Loads a type best represented as a long double from the current top node
  547. void loadValue( long double & value )
  548. {
  549. try
  550. {
  551. value = std::stold( itsNodes.top().node->value() );
  552. }
  553. catch( std::out_of_range const & )
  554. {
  555. // special case for denormalized values
  556. std::istringstream is( itsNodes.top().node->value() );
  557. is >> value;
  558. if( std::fpclassify( value ) != FP_SUBNORMAL )
  559. throw;
  560. }
  561. }
  562. //! Loads a string from the current node from the current top node
  563. template<class CharT, class Traits, class Alloc> inline
  564. void loadValue( std::basic_string<CharT, Traits, Alloc> & str )
  565. {
  566. std::basic_istringstream<CharT, Traits> is( itsNodes.top().node->value() );
  567. str.assign( std::istreambuf_iterator<CharT, Traits>( is ),
  568. std::istreambuf_iterator<CharT, Traits>() );
  569. }
  570. //! Loads the size of the current top node
  571. template <class T> inline
  572. void loadSize( T & value )
  573. {
  574. value = getNumChildren( itsNodes.top().node );
  575. }
  576. protected:
  577. //! Gets the number of children (usually interpreted as size) for the specified node
  578. static size_t getNumChildren( rapidxml::xml_node<> * node )
  579. {
  580. size_t size = 0;
  581. node = node->first_node(); // get first child
  582. while( node != nullptr )
  583. {
  584. ++size;
  585. node = node->next_sibling();
  586. }
  587. return size;
  588. }
  589. //! A struct that contains metadata about a node
  590. /*! Keeps track of some top level node, its number of
  591. remaining children, and the current active child node */
  592. struct NodeInfo
  593. {
  594. NodeInfo( rapidxml::xml_node<> * n = nullptr ) :
  595. node( n ),
  596. child( n->first_node() ),
  597. size( XMLInputArchive::getNumChildren( n ) ),
  598. name( nullptr )
  599. { }
  600. //! Advances to the next sibling node of the child
  601. /*! If this is the last sibling child will be null after calling */
  602. void advance()
  603. {
  604. if( size > 0 )
  605. {
  606. --size;
  607. child = child->next_sibling();
  608. }
  609. }
  610. //! Searches for a child with the given name in this node
  611. /*! @param searchName The name to search for (must be null terminated)
  612. @return The node if found, nullptr otherwise */
  613. rapidxml::xml_node<> * search( const char * searchName )
  614. {
  615. if( searchName )
  616. {
  617. size_t new_size = XMLInputArchive::getNumChildren( node );
  618. const size_t name_size = rapidxml::internal::measure( searchName );
  619. for( auto new_child = node->first_node(); new_child != nullptr; new_child = new_child->next_sibling() )
  620. {
  621. if( rapidxml::internal::compare( new_child->name(), new_child->name_size(), searchName, name_size, true ) )
  622. {
  623. size = new_size;
  624. child = new_child;
  625. return new_child;
  626. }
  627. --new_size;
  628. }
  629. }
  630. return nullptr;
  631. }
  632. //! Returns the actual name of the next child node, if it exists
  633. const char * getChildName() const
  634. {
  635. return child ? child->name() : nullptr;
  636. }
  637. rapidxml::xml_node<> * node; //!< A pointer to this node
  638. rapidxml::xml_node<> * child; //!< A pointer to its current child
  639. size_t size; //!< The remaining number of children for this node
  640. const char * name; //!< The NVP name for next child node
  641. }; // NodeInfo
  642. //! @}
  643. private:
  644. std::vector<char> itsData; //!< The raw data loaded
  645. rapidxml::xml_document<> itsXML; //!< The XML document
  646. std::stack<NodeInfo> itsNodes; //!< A stack of nodes read from the document
  647. };
  648. // ######################################################################
  649. // XMLArchive prologue and epilogue functions
  650. // ######################################################################
  651. // ######################################################################
  652. //! Prologue for NVPs for XML output archives
  653. /*! NVPs do not start or finish nodes - they just set up the names */
  654. template <class T> inline
  655. void prologue( XMLOutputArchive &, NameValuePair<T> const & )
  656. { }
  657. //! Prologue for NVPs for XML input archives
  658. template <class T> inline
  659. void prologue( XMLInputArchive &, NameValuePair<T> const & )
  660. { }
  661. // ######################################################################
  662. //! Epilogue for NVPs for XML output archives
  663. /*! NVPs do not start or finish nodes - they just set up the names */
  664. template <class T> inline
  665. void epilogue( XMLOutputArchive &, NameValuePair<T> const & )
  666. { }
  667. //! Epilogue for NVPs for XML input archives
  668. template <class T> inline
  669. void epilogue( XMLInputArchive &, NameValuePair<T> const & )
  670. { }
  671. // ######################################################################
  672. //! Prologue for deferred data for XML archives
  673. /*! Do nothing for the defer wrapper */
  674. template <class T> inline
  675. void prologue( XMLOutputArchive &, DeferredData<T> const & )
  676. { }
  677. //! Prologue for deferred data for XML archives
  678. template <class T> inline
  679. void prologue( XMLInputArchive &, DeferredData<T> const & )
  680. { }
  681. // ######################################################################
  682. //! Epilogue for deferred for XML archives
  683. /*! NVPs do not start or finish nodes - they just set up the names */
  684. template <class T> inline
  685. void epilogue( XMLOutputArchive &, DeferredData<T> const & )
  686. { }
  687. //! Epilogue for deferred for XML archives
  688. /*! Do nothing for the defer wrapper */
  689. template <class T> inline
  690. void epilogue( XMLInputArchive &, DeferredData<T> const & )
  691. { }
  692. // ######################################################################
  693. //! Prologue for SizeTags for XML output archives
  694. /*! SizeTags do not start or finish nodes */
  695. template <class T> inline
  696. void prologue( XMLOutputArchive & ar, SizeTag<T> const & )
  697. {
  698. if (ar.hasSizeAttributes())
  699. {
  700. ar.appendAttribute("size", "dynamic");
  701. }
  702. }
  703. template <class T> inline
  704. void prologue( XMLInputArchive &, SizeTag<T> const & )
  705. { }
  706. //! Epilogue for SizeTags for XML output archives
  707. /*! SizeTags do not start or finish nodes */
  708. template <class T> inline
  709. void epilogue( XMLOutputArchive &, SizeTag<T> const & )
  710. { }
  711. template <class T> inline
  712. void epilogue( XMLInputArchive &, SizeTag<T> const & )
  713. { }
  714. // ######################################################################
  715. //! Prologue for all other types for XML output archives (except minimal types)
  716. /*! Starts a new node, named either automatically or by some NVP,
  717. that may be given data by the type about to be archived
  718. Minimal types do not start or end nodes */
  719. template <class T, traits::DisableIf<traits::has_minimal_base_class_serialization<T, traits::has_minimal_output_serialization, XMLOutputArchive>::value ||
  720. traits::has_minimal_output_serialization<T, XMLOutputArchive>::value> = traits::sfinae> inline
  721. void prologue( XMLOutputArchive & ar, T const & )
  722. {
  723. ar.startNode();
  724. ar.insertType<T>();
  725. }
  726. //! Prologue for all other types for XML input archives (except minimal types)
  727. template <class T, traits::DisableIf<traits::has_minimal_base_class_serialization<T, traits::has_minimal_input_serialization, XMLInputArchive>::value ||
  728. traits::has_minimal_input_serialization<T, XMLInputArchive>::value> = traits::sfinae> inline
  729. void prologue( XMLInputArchive & ar, T const & )
  730. {
  731. ar.startNode();
  732. }
  733. // ######################################################################
  734. //! Epilogue for all other types other for XML output archives (except minimal types)
  735. /*! Finishes the node created in the prologue
  736. Minimal types do not start or end nodes */
  737. template <class T, traits::DisableIf<traits::has_minimal_base_class_serialization<T, traits::has_minimal_output_serialization, XMLOutputArchive>::value ||
  738. traits::has_minimal_output_serialization<T, XMLOutputArchive>::value> = traits::sfinae> inline
  739. void epilogue( XMLOutputArchive & ar, T const & )
  740. {
  741. ar.finishNode();
  742. }
  743. //! Epilogue for all other types other for XML output archives (except minimal types)
  744. template <class T, traits::DisableIf<traits::has_minimal_base_class_serialization<T, traits::has_minimal_input_serialization, XMLInputArchive>::value ||
  745. traits::has_minimal_input_serialization<T, XMLInputArchive>::value> = traits::sfinae> inline
  746. void epilogue( XMLInputArchive & ar, T const & )
  747. {
  748. ar.finishNode();
  749. }
  750. // ######################################################################
  751. // Common XMLArchive serialization functions
  752. // ######################################################################
  753. //! Saving NVP types to XML
  754. template <class T> inline
  755. void CEREAL_SAVE_FUNCTION_NAME( XMLOutputArchive & ar, NameValuePair<T> const & t )
  756. {
  757. ar.setNextName( t.name );
  758. ar( t.value );
  759. }
  760. //! Loading NVP types from XML
  761. template <class T> inline
  762. void CEREAL_LOAD_FUNCTION_NAME( XMLInputArchive & ar, NameValuePair<T> & t )
  763. {
  764. ar.setNextName( t.name );
  765. ar( t.value );
  766. }
  767. // ######################################################################
  768. //! Saving SizeTags to XML
  769. template <class T> inline
  770. void CEREAL_SAVE_FUNCTION_NAME( XMLOutputArchive &, SizeTag<T> const & )
  771. { }
  772. //! Loading SizeTags from XML
  773. template <class T> inline
  774. void CEREAL_LOAD_FUNCTION_NAME( XMLInputArchive & ar, SizeTag<T> & st )
  775. {
  776. ar.loadSize( st.size );
  777. }
  778. // ######################################################################
  779. //! Saving for POD types to xml
  780. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  781. void CEREAL_SAVE_FUNCTION_NAME(XMLOutputArchive & ar, T const & t)
  782. {
  783. ar.saveValue( t );
  784. }
  785. //! Loading for POD types from xml
  786. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  787. void CEREAL_LOAD_FUNCTION_NAME(XMLInputArchive & ar, T & t)
  788. {
  789. ar.loadValue( t );
  790. }
  791. // ######################################################################
  792. //! saving string to xml
  793. template<class CharT, class Traits, class Alloc> inline
  794. void CEREAL_SAVE_FUNCTION_NAME(XMLOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const & str)
  795. {
  796. ar.saveValue( str );
  797. }
  798. //! loading string from xml
  799. template<class CharT, class Traits, class Alloc> inline
  800. void CEREAL_LOAD_FUNCTION_NAME(XMLInputArchive & ar, std::basic_string<CharT, Traits, Alloc> & str)
  801. {
  802. ar.loadValue( str );
  803. }
  804. } // namespace cereal
  805. // register archives for polymorphic support
  806. CEREAL_REGISTER_ARCHIVE(cereal::XMLOutputArchive)
  807. CEREAL_REGISTER_ARCHIVE(cereal::XMLInputArchive)
  808. // tie input and output archives together
  809. CEREAL_SETUP_ARCHIVE_TRAITS(cereal::XMLInputArchive, cereal::XMLOutputArchive)
  810. #endif // CEREAL_ARCHIVES_XML_HPP_