diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:30:42 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:30:42 +0200 |
commit | 767bb3951c65e38627cb0bbad9a3756e1cda2520 (patch) | |
tree | 460b18606d2c3472d5aa08444db4db62c6410248 /zenxml/doc/index.html | |
parent | 6.0 (diff) | |
download | FreeFileSync-767bb3951c65e38627cb0bbad9a3756e1cda2520.tar.gz FreeFileSync-767bb3951c65e38627cb0bbad9a3756e1cda2520.tar.bz2 FreeFileSync-767bb3951c65e38627cb0bbad9a3756e1cda2520.zip |
6.1
Diffstat (limited to 'zenxml/doc/index.html')
-rw-r--r-- | zenxml/doc/index.html | 739 |
1 files changed, 357 insertions, 382 deletions
diff --git a/zenxml/doc/index.html b/zenxml/doc/index.html index d7245a6b..1549b9f2 100644 --- a/zenxml/doc/index.html +++ b/zenxml/doc/index.html @@ -3,46 +3,36 @@ <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> +<meta name="generator" content="Doxygen 1.8.6"/> <title>zen::Xml: Overview</title> - <link href="tabs.css" rel="stylesheet" type="text/css"/> -<link href="doxygen.css" rel="stylesheet" type="text/css" /> - -<link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> +<script type="text/javascript" src="dynsections.js"></script> +<link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="search/search.js"></script> <script type="text/javascript"> $(document).ready(function() { searchBox.OnSelectItem(0); }); </script> - +<link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> -<div id="top"><!-- do not remove this div! --> - - +<div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> - <td id="projectlogo"><img alt="Logo" src="zenXml.png"/></td> - - <td style="padding-left: 0.5em;"> <div id="projectname">zen::Xml - </div> <div id="projectbrief">Simple C++ XML Processing</div> </td> - - - </tr> </tbody> </table> </div> - -<!-- Generated by Doxygen 1.8.0 --> +<!-- end header part --> +<!-- Generated by Doxygen 1.8.6 --> <script type="text/javascript"> var searchBox = new SearchBox("searchBox", "search",false,'Search'); </script> @@ -51,7 +41,6 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); <li class="current"><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> - <li><a href="files.html"><span>Files</span></a></li> <li> <div id="MSearchBox" class="MSearchBoxInactive"> <span class="left"> @@ -70,13 +59,13 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); </li> </ul> </div> -</div> +</div><!-- top --> <!-- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> -<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a></div> +<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Pages</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> @@ -104,8 +93,8 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search'); <li><a class="el" href="index.html#sec_Structured_user_types">Structured user types</a> </li> <li><a class="el" href="index.html#sec_Type_Safety">Type Safety</a></li> </ul> -<h2><a class="anchor" id="sec_Rationale"></a> -Rationale</h2> +<h1><a class="anchor" id="sec_Rationale"></a> +Rationale</h1> <p>zen::Xml is an XML library serializing structured user data in a convenient way. Using compile-time information gathered by techniques of template metaprogramming it minimizes the manual overhead required and frees the user from implementing fundamental type conversions by himself. Basic data types such as</p> <ul> <li><b>all</b> built-in arithmetic numbers,</li> @@ -123,63 +112,63 @@ Rationale</h2> <li>easily extensible API: allow for internationalization, fine-granular error handling, and custom file I/O</li> </ul> <p>The design follows the philosophy of the Loki library: <br/> - <a href="http://loki-lib.sourceforge.net/index.php?n=Main.Philosophy">http://loki-lib.sourceforge.net/index.php?n=Main.Philosophy</a></p> -<h2><a class="anchor" id="sec_Quick_Start"></a> -Quick Start</h2> +<a href="http://loki-lib.sourceforge.net/index.php?n=Main.Philosophy">http://loki-lib.sourceforge.net/index.php?n=Main.Philosophy</a></p> +<h1><a class="anchor" id="sec_Quick_Start"></a> +Quick Start</h1> <p>1. Download zen::Xml: <a href="http://sourceforge.net/projects/zenxml">http://sourceforge.net/projects/zenxml</a></p> -<p>2. Setup a preprocessor macro for your project to identify the platform (this is only required if you use C-stream-based file IO) </p> -<div class="fragment"><pre class="fragment"> ZEN_PLATFORM_WINDOWS - or - ZEN_PLATFORM_OTHER -</pre></div><p>3. For optimal performance define this global macro in release build: (following convention of the <code>assert</code> macro) </p> -<div class="fragment"><pre class="fragment"> NDEBUG -</pre></div><p>4. Include the main header: </p> -<div class="fragment"><pre class="fragment"><span class="preprocessor">#include <zenxml/xml.h></span> -</pre></div><p>5. Start serializing user data:</p> -<div class="fragment"><pre class="fragment"><span class="keywordtype">size_t</span> a = 10; -<span class="keywordtype">double</span> b = 2.0; -<span class="keywordtype">int</span> c = -1; -</pre></div><div class="fragment"><pre class="fragment"><a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">zen::XmlDoc</a> doc; <span class="comment">//empty XML document</span> - -<a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); <span class="comment">//the simplest way to fill the document is to use a data output proxy</span> -out[<span class="stringliteral">"elem1"</span>](a); <span class="comment">//</span> -out[<span class="stringliteral">"elem2"</span>](b); <span class="comment">//map data types to XML elements</span> -out[<span class="stringliteral">"elem3"</span>](c); <span class="comment">//</span> - -<span class="keywordflow">try</span> -{ - <a class="code" href="namespacezen.html#adeeb6b2318097382ae47aa939fc15d4d" title="Save XML document to a file.">save</a>(doc, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw zen::XmlFileError</span> -} -<span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="structzen_1_1_xml_file_error.html" title="Exception thrown due to failed file I/O.">zen::XmlFileError</a>& e) { <span class="comment">/* handle error */</span> } -</pre></div><p>The following XML file will be created: </p> -<div class="fragment"><pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> +<p>2. Setup one of the following preprocessor macros for your project to identify the platform (this is only required if you use C-stream-based file IO) </p> +<div class="fragment"><div class="line">ZEN_WIN</div> +<div class="line">ZEN_LINUX </div> +<div class="line">ZEN_MAC</div> +</div><!-- fragment --><p>3. For optimal performance define this global macro in release build: (following convention of the <code>assert</code> macro) </p> +<div class="fragment"><div class="line">NDEBUG</div> +</div><!-- fragment --><p>4. Include the main header: </p> +<div class="fragment"><div class="line"><span class="preprocessor">#include <zenxml/xml.h></span></div> +</div><!-- fragment --><p>5. Start serializing user data:</p> +<div class="fragment"><div class="line"><span class="keywordtype">size_t</span> a = 10;</div> +<div class="line"><span class="keywordtype">double</span> b = 2.0;</div> +<div class="line"><span class="keywordtype">int</span> c = -1;</div> +</div><!-- fragment --><div class="fragment"><div class="line"><a class="code" href="classzen_1_1_xml_doc.html">zen::XmlDoc</a> doc; <span class="comment">//empty XML document</span></div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc); <span class="comment">//the simplest way to fill the document is to use a data output proxy</span></div> +<div class="line">out[<span class="stringliteral">"elem1"</span>](a); <span class="comment">//</span></div> +<div class="line">out[<span class="stringliteral">"elem2"</span>](b); <span class="comment">//map data types to XML elements</span></div> +<div class="line">out[<span class="stringliteral">"elem3"</span>](c); <span class="comment">//</span></div> +<div class="line"></div> +<div class="line"><span class="keywordflow">try</span></div> +<div class="line">{</div> +<div class="line"> <a class="code" href="namespacezen.html#adeeb6b2318097382ae47aa939fc15d4d">save</a>(doc, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw zen::XmlFileError</span></div> +<div class="line">}</div> +<div class="line"><span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="structzen_1_1_xml_file_error.html">zen::XmlFileError</a>& e) { <span class="comment">/* handle error */</span> }</div> +</div><!-- fragment --><p>The following XML file will be created: </p> +<pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> <Root> <elem1>10</elem1> <elem2>2.000000</elem2> <elem3>-1</elem3> </Root> -</pre></div><p>Load an XML file and map its content to user data: </p> -<div class="fragment"><pre class="fragment"><a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">zen::XmlDoc</a> doc; <span class="comment">//empty XML document</span> - -<span class="keywordflow">try</span> -{ - <a class="code" href="namespacezen.html#a900c1fb290f0eedc24354c487145dbee" title="Load XML document from a file.">load</a>(<span class="stringliteral">"file.xml"</span>, doc); <span class="comment">//throw XmlFileError, XmlParsingError</span> -} -<span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="structzen_1_1_xml_error.html" title="Exception base class for zen::Xml.">zen::XmlError</a>& e) { <span class="comment">/* handle error */</span> } - -<a class="code" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data.">zen::XmlIn</a> in(doc); <span class="comment">//the simplest way to read the document is to use a data input proxy</span> -in[<span class="stringliteral">"elem1"</span>](a); <span class="comment">//</span> -in[<span class="stringliteral">"elem2"</span>](b); <span class="comment">//map XML elements into user data</span> -in[<span class="stringliteral">"elem3"</span>](c); <span class="comment">//</span> - -<span class="comment">//check for mapping errors, i.e. missing elements or conversion errors: you may consider these as warnings only</span> -<span class="keywordflow">if</span> (in.errorsOccured()) -{ - std::vector<std::wstring> failedElements = in.getErrorsAs<std::wstring>(); - <span class="comment">/* generate error message showing the XML element names that failed to convert */</span> -} -</pre></div><h2><a class="anchor" id="sec_Supported_Platforms"></a> -Supported Platforms</h2> +</pre><p>Load an XML file and map its content to user data: </p> +<div class="fragment"><div class="line"><a class="code" href="classzen_1_1_xml_doc.html">zen::XmlDoc</a> doc; <span class="comment">//empty XML document</span></div> +<div class="line"></div> +<div class="line"><span class="keywordflow">try</span></div> +<div class="line">{</div> +<div class="line"> <a class="code" href="namespacezen.html#a872a48c0616e7f12ae8caca464835e00">load</a>(<span class="stringliteral">"file.xml"</span>, doc); <span class="comment">//throw XmlFileError, XmlParsingError</span></div> +<div class="line">}</div> +<div class="line"><span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="structzen_1_1_xml_error.html">zen::XmlError</a>& e) { <span class="comment">/* handle error */</span> }</div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_in.html">zen::XmlIn</a> in(doc); <span class="comment">//the simplest way to read the document is to use a data input proxy</span></div> +<div class="line">in[<span class="stringliteral">"elem1"</span>](a); <span class="comment">//</span></div> +<div class="line">in[<span class="stringliteral">"elem2"</span>](b); <span class="comment">//map XML elements into user data</span></div> +<div class="line">in[<span class="stringliteral">"elem3"</span>](c); <span class="comment">//</span></div> +<div class="line"></div> +<div class="line"><span class="comment">//check for mapping errors, i.e. missing elements or conversion errors: you may consider these as warnings only</span></div> +<div class="line"><span class="keywordflow">if</span> (in.errorsOccured())</div> +<div class="line">{</div> +<div class="line"> std::vector<std::wstring> failedElements = in.getErrorsAs<std::wstring>();</div> +<div class="line"> <span class="comment">/* generate error message showing the XML element names that failed to convert */</span></div> +<div class="line">}</div> +</div><!-- fragment --><h1><a class="anchor" id="sec_Supported_Platforms"></a> +Supported Platforms</h1> <p>zen::Xml is written in a platform independent manner and runs on any rudimentary C++11 compliant compiler. It has been tested successfully under:</p> <ul> <li>Windows:<ol type="1"> @@ -188,113 +177,103 @@ Supported Platforms</h2> <li>MinGW: GCC 4.5.2 - 32 bit</li> </ol> </li> -</ul> -<ul> <li>Linux:<ol type="1"> <li>GCC 4.5.2 - 32 bit</li> <li>GCC 4.5.2 - 64 bit</li> </ol> </li> -</ul> -<ul> <li>Mac OS X:<ol type="1"> <li>Clang 3.2 - 64 bit</li> </ol> </li> </ul> <p><b>Note:</b> In order to enable C++11 features in GCC it is required to specify either of the following compiler options: </p> -<div class="fragment"><pre class="fragment">-std=c++11 +<pre class="fragment">-std=c++11 -std=c++0x -std=gnu++0x -</pre></div><h2><a class="anchor" id="sec_Flexible_Programming_Model"></a> -Flexible Programming Model</h2> +</pre><h1><a class="anchor" id="sec_Flexible_Programming_Model"></a> +Flexible Programming Model</h1> <p>Depending on what granularity of control is required in a particular application, zen::Xml allows the user to choose between full control or simplicity. <br/> <br/> - The library is structured into the following parts, each of which can be used in isolation: <br/> +The library is structured into the following parts, each of which can be used in isolation: <br/> <br/> - <b><File></b> <br/> - |<br/> - | <a class="el" href="io_8h_source.html" title="Save and load byte streams from files.">io.h</a><br/> - |<br/> - <b><Byte Stream></b><br/> - |<br/> - | <a class="el" href="parser_8h_source.html" title="Convert an XML document object model (class XmlDoc) to and from a byte stream representation.">parser.h</a><br/> - |<br/> - <b><Document Object Model></b><br/> - |<br/> - | <a class="el" href="bind_8h_source.html" title="Map user data types to XML.">bind.h</a><br/> - |<br/> - <b><C++ user data></b> <br/> +<b><File></b> <br/> +|<br/> +| <a class="el" href="io_8h_source.html" title="Save and load byte streams from files. ">io.h</a><br/> +|<br/> +<b><Byte Stream></b><br/> +|<br/> +| <a class="el" href="parser_8h_source.html" title="Convert an XML document object model (class XmlDoc) to and from a byte stream representation. ">parser.h</a><br/> +|<br/> +<b><Document Object Model></b><br/> +|<br/> +| <a class="el" href="bind_8h_source.html" title="Map user data types to XML. ">bind.h</a><br/> +|<br/> +<b><C++ user data></b> <br/> <br/> </p> <ul> -<li>Save an XML document to memory <div class="fragment"><pre class="fragment"><a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">zen::XmlDoc</a> doc; - ... <span class="comment">//fill it</span> -std::string stream = <a class="code" href="namespacezen.html#afaa4920e275078e6c8009fbdf58b57ee" title="Save XML document as a byte stream.">serialize</a>(doc); <span class="comment">//throw ()</span> -<span class="comment">/* you now have a binary XML stream */</span> - -<a class="code" href="namespacezen.html#a4ba7bbaa14a787b07fc13da9145aabe2" title="Save byte stream to a file.">saveStream</a>(stream, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span> -<span class="comment">//if all you need is to store XmlDoc in a file direcly you can use zen::save() instead</span> -</pre></div></li> -</ul> -<ul> -<li>Load XML document from memory <div class="fragment"><pre class="fragment"><span class="comment">//get XML byte stream:</span> -std::string stream = <a class="code" href="namespacezen.html#a04fe23c3bd9b7d03309620b5ea763607" title="Load byte stream from a file.">loadStream</a>(<span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span> - -<a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">zen::XmlDoc</a> doc; -<span class="comment">//parse byte stream into an XML document:</span> -<a class="code" href="namespacezen.html#a6cf1ec0b57fc1ae9aa95761800e67ec7" title="Load XML document from a byte stream.">parse</a>(stream, doc); <span class="comment">//throw XmlParsingError</span> -<span class="comment">//if all you need is to load an XmlDoc from a file you can use zen::load() directly</span> -</pre></div></li> -</ul> -<ul> -<li>Fine-granular error checking with the data input proxy <div class="fragment"><pre class="fragment"><a class="code" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data.">zen::XmlIn</a> in(doc); -<span class="comment">//map XML elements into user data</span> -<span class="keywordflow">if</span> (!in[<span class="stringliteral">"elem1"</span>](a)) - <span class="keywordflow">throw</span> MyCustomException(); -<span class="keywordflow">if</span> (!in[<span class="stringliteral">"elem2"</span>](b)) - <span class="keywordflow">throw</span> MyCustomException(); -<span class="keywordflow">if</span> (!in[<span class="stringliteral">"elem3"</span>](c)) - <span class="keywordflow">throw</span> MyCustomException(); - -<span class="comment">//if (in.errorsOccured()) ... <- not required here: contains the same conversion errors checked manually before</span> -</pre></div></li> -</ul> -<ul> +<li>Save an XML document to memory <div class="fragment"><div class="line"><a class="code" href="classzen_1_1_xml_doc.html">zen::XmlDoc</a> doc;</div> +<div class="line"> ... <span class="comment">//fill it</span></div> +<div class="line">std::string stream = <a class="code" href="namespacezen.html#afaa4920e275078e6c8009fbdf58b57ee">serialize</a>(doc); <span class="comment">//throw ()</span></div> +<div class="line"><span class="comment">/* you now have a binary XML stream */</span></div> +<div class="line"></div> +<div class="line"><a class="code" href="namespacezen.html#a4ba7bbaa14a787b07fc13da9145aabe2">saveStream</a>(stream, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span></div> +<div class="line"><span class="comment">//if all you need is to store XmlDoc in a file direcly you can use zen::save() instead</span></div> +</div><!-- fragment --></li> +<li>Load XML document from memory <div class="fragment"><div class="line"><span class="comment">//get XML byte stream:</span></div> +<div class="line">std::string stream = <a class="code" href="namespacezen.html#a04fe23c3bd9b7d03309620b5ea763607">loadStream</a>(<span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span></div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_doc.html">zen::XmlDoc</a> doc;</div> +<div class="line"><span class="comment">//parse byte stream into an XML document:</span></div> +<div class="line"><a class="code" href="namespacezen.html#a1ae1a4688d724b554fe3bf4638700477">parse</a>(stream, doc); <span class="comment">//throw XmlParsingError</span></div> +<div class="line"><span class="comment">//if all you need is to load an XmlDoc from a file you can use zen::load() directly</span></div> +</div><!-- fragment --></li> +<li>Fine-granular error checking with the data input proxy <div class="fragment"><div class="line"><a class="code" href="classzen_1_1_xml_in.html">zen::XmlIn</a> in(doc);</div> +<div class="line"><span class="comment">//map XML elements into user data</span></div> +<div class="line"><span class="keywordflow">if</span> (!in[<span class="stringliteral">"elem1"</span>](a))</div> +<div class="line"> <span class="keywordflow">throw</span> MyCustomException();</div> +<div class="line"><span class="keywordflow">if</span> (!in[<span class="stringliteral">"elem2"</span>](b))</div> +<div class="line"> <span class="keywordflow">throw</span> MyCustomException();</div> +<div class="line"><span class="keywordflow">if</span> (!in[<span class="stringliteral">"elem3"</span>](c))</div> +<div class="line"> <span class="keywordflow">throw</span> MyCustomException();</div> +<div class="line"></div> +<div class="line"><span class="comment">//if (in.errorsOccured()) ... <- not required here: contains the same conversion errors checked manually before</span></div> +</div><!-- fragment --></li> <li>Access the Document Object Model directly (without input/output proxy) <br/> <br/> - The full power of type conversions which is available via the input/output proxy classes <a class="el" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data.">zen::XmlIn</a> and <a class="el" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> is also available for the document object model! <div class="fragment"><pre class="fragment"><span class="keyword">using namespace </span>zen; - -<a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">XmlDoc</a> doc; - -<a class="code" href="classzen_1_1_xml_element.html" title="An XML element.">XmlElement</a>& child = doc.<a class="code" href="classzen_1_1_xml_doc.html#ad4a9594d93885fc1a12db28e8246648d" title="Get a const reference to the document's root element.">root</a>().<a class="code" href="classzen_1_1_xml_element.html#a653caffa6fad89db7d14f67f987ad0f9" title="Create a new child element and return a reference to it.">addChild</a>(<span class="stringliteral">"elem1"</span>); -child.<a class="code" href="classzen_1_1_xml_element.html#aaf3a26f6199fc88cce7d9d911ba21b01" title="Set the value of this element.">setValue</a>(1234); - -<a class="code" href="namespacezen.html#adeeb6b2318097382ae47aa939fc15d4d" title="Save XML document to a file.">save</a>(doc, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span> -</pre></div> <br/> - <div class="fragment"><pre class="fragment"><span class="keyword">using namespace </span>zen; - -<a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">XmlDoc</a> doc; -<a class="code" href="namespacezen.html#a900c1fb290f0eedc24354c487145dbee" title="Load XML document from a file.">load</a>(<span class="stringliteral">"file.xml"</span>, doc); <span class="comment">//throw XmlFileError, XmlParsingError</span> - -<a class="code" href="classzen_1_1_xml_element.html" title="An XML element.">XmlElement</a>* child = doc.<a class="code" href="classzen_1_1_xml_doc.html#ad4a9594d93885fc1a12db28e8246648d" title="Get a const reference to the document's root element.">root</a>().<a class="code" href="classzen_1_1_xml_element.html#a3ab82b1720460487f4afabcd115d0c7e" title="Retrieve a child element with the given name.">getChild</a>(<span class="stringliteral">"elem1"</span>); -<span class="keywordflow">if</span> (child) -{ - <span class="keywordtype">int</span> value = -1; - <span class="keywordflow">if</span> (!child->getValue(value)) - ... <span class="comment">//handle conversion error</span> -} -<span class="keywordflow">else</span> - ... <span class="comment">//XML element not found</span> -</pre></div></li> +The full power of type conversions which is available via the input/output proxy classes <a class="el" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data. ">zen::XmlIn</a> and <a class="el" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure. ">zen::XmlOut</a> is also available for the document object model! <div class="fragment"><div class="line"><span class="keyword">using namespace </span>zen;</div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_doc.html">XmlDoc</a> doc;</div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_element.html">XmlElement</a>& child = doc.<a class="code" href="classzen_1_1_xml_doc.html#ad4a9594d93885fc1a12db28e8246648d">root</a>().<a class="code" href="classzen_1_1_xml_element.html#a653caffa6fad89db7d14f67f987ad0f9">addChild</a>(<span class="stringliteral">"elem1"</span>);</div> +<div class="line">child.<a class="code" href="classzen_1_1_xml_element.html#aaf3a26f6199fc88cce7d9d911ba21b01">setValue</a>(1234);</div> +<div class="line"></div> +<div class="line"><a class="code" href="namespacezen.html#adeeb6b2318097382ae47aa939fc15d4d">save</a>(doc, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span></div> +</div><!-- fragment --> <br/> +<div class="fragment"><div class="line"><span class="keyword">using namespace </span>zen;</div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_doc.html">XmlDoc</a> doc;</div> +<div class="line"><a class="code" href="namespacezen.html#a872a48c0616e7f12ae8caca464835e00">load</a>(<span class="stringliteral">"file.xml"</span>, doc); <span class="comment">//throw XmlFileError, XmlParsingError</span></div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_element.html">XmlElement</a>* child = doc.<a class="code" href="classzen_1_1_xml_doc.html#ad4a9594d93885fc1a12db28e8246648d">root</a>().<a class="code" href="classzen_1_1_xml_element.html#a3ab82b1720460487f4afabcd115d0c7e">getChild</a>(<span class="stringliteral">"elem1"</span>);</div> +<div class="line"><span class="keywordflow">if</span> (child)</div> +<div class="line">{</div> +<div class="line"> <span class="keywordtype">int</span> value = -1;</div> +<div class="line"> <span class="keywordflow">if</span> (!child->getValue(value))</div> +<div class="line"> ... <span class="comment">//handle conversion error</span></div> +<div class="line">}</div> +<div class="line"><span class="keywordflow">else</span></div> +<div class="line"> ... <span class="comment">//XML element not found</span></div> +</div><!-- fragment --></li> </ul> -<h2><a class="anchor" id="sec_Structured_XML_element_access"></a> -Structured XML element access</h2> -<div class="fragment"><pre class="fragment"><span class="comment">//write a value into one deeply nested XML element - note the different types used seamlessly: char[], wchar_t[], char, wchar_t, int</span> -<a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); -out[<span class="stringliteral">"elemento1"</span>][L<span class="stringliteral">"элемент2"</span>][L<span class="stringliteral">"要素3"</span>][L<span class="stringliteral">"στοιχείο4"</span>][<span class="stringliteral">"elem5"</span>][L<span class="stringliteral">"元素6"</span>][L<span class="stringliteral">'元'</span>][<span class="charliteral">'z'</span>](-1234); -</pre></div><p>The resulting XML: </p> -<div class="fragment"><pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> +<h1><a class="anchor" id="sec_Structured_XML_element_access"></a> +Structured XML element access</h1> +<div class="fragment"><div class="line"><span class="comment">//write a value into one deeply nested XML element - note the different types used seamlessly: char[], wchar_t[], char, wchar_t, int</span></div> +<div class="line"><a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc);</div> +<div class="line">out[<span class="stringliteral">"elemento1"</span>][L<span class="stringliteral">"элемент2"</span>][L<span class="stringliteral">"要素3"</span>][L<span class="stringliteral">"στοιχείο4"</span>][<span class="stringliteral">"elem5"</span>][L<span class="stringliteral">"元素6"</span>][L<span class="stringliteral">'元'</span>][<span class="charliteral">'z'</span>](-1234);</div> +</div><!-- fragment --><p>The resulting XML: </p> +<pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> <Root> <elemento1> <элемент2> @@ -312,33 +291,33 @@ out[<span class="stringliteral">"elemento1"</span>][L<span class="stri </элемент2> </elemento1> </Root> -</pre></div><h2><a class="anchor" id="sec_Access_XML_attributes"></a> -Access XML attributes</h2> -<div class="fragment"><pre class="fragment"><a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">zen::XmlDoc</a> doc; - -<a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); -out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr1"</span>, -1); <span class="comment">//</span> -out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr2"</span>, 2.1); <span class="comment">//write data into XML attributes</span> -out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr3"</span>, <span class="keyword">true</span>); <span class="comment">//</span> - -<a class="code" href="namespacezen.html#adeeb6b2318097382ae47aa939fc15d4d" title="Save XML document to a file.">save</a>(doc, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span> -</pre></div><p>The resulting XML: </p> -<div class="fragment"><pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> +</pre><h1><a class="anchor" id="sec_Access_XML_attributes"></a> +Access XML attributes</h1> +<div class="fragment"><div class="line"><a class="code" href="classzen_1_1_xml_doc.html">zen::XmlDoc</a> doc;</div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc);</div> +<div class="line">out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr1"</span>, -1); <span class="comment">//</span></div> +<div class="line">out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr2"</span>, 2.1); <span class="comment">//write data into XML attributes</span></div> +<div class="line">out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr3"</span>, <span class="keyword">true</span>); <span class="comment">//</span></div> +<div class="line"></div> +<div class="line"><a class="code" href="namespacezen.html#adeeb6b2318097382ae47aa939fc15d4d">save</a>(doc, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span></div> +</div><!-- fragment --><p>The resulting XML: </p> +<pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> <Root> <elem attr1="-1" attr2="2.1" attr3="true"/> </Root> -</pre></div><h2><a class="anchor" id="sec_Automatic_conversion_built_in"></a> -Automatic conversion for built-in arithmetic types</h2> +</pre><h1><a class="anchor" id="sec_Automatic_conversion_built_in"></a> +Automatic conversion for built-in arithmetic types</h1> <p>All built-in arithmetic types and <code>bool</code> are detected at compile time and a proper conversion is applied. Common conversions for integer-like types such as <code>int</code>, <code>long</code>, <code>long long</code>, ect. as well as floating point types are optimized for maximum performance.</p> -<div class="fragment"><pre class="fragment"><a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); - -out[<span class="stringliteral">"int"</span>] (-1234); -out[<span class="stringliteral">"double"</span>](1.23); -out[<span class="stringliteral">"float"</span>] (4.56f); -out[<span class="stringliteral">"ulong"</span>] (1234UL); -out[<span class="stringliteral">"bool"</span>] (<span class="keyword">false</span>); -</pre></div><p>The resulting XML: </p> -<div class="fragment"><pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> +<div class="fragment"><div class="line"><a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc);</div> +<div class="line"></div> +<div class="line">out[<span class="stringliteral">"int"</span>] (-1234);</div> +<div class="line">out[<span class="stringliteral">"double"</span>](1.23);</div> +<div class="line">out[<span class="stringliteral">"float"</span>] (4.56f);</div> +<div class="line">out[<span class="stringliteral">"ulong"</span>] (1234UL);</div> +<div class="line">out[<span class="stringliteral">"bool"</span>] (<span class="keyword">false</span>);</div> +</div><!-- fragment --><p>The resulting XML: </p> +<pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> <Root> <int>-1234</int> <double>1.23</double> @@ -346,8 +325,8 @@ out[<span class="stringliteral">"bool"</span>] (<span class="keyword" <ulong>1234</ulong> <bool>false</bool> </Root> -</pre></div><h2><a class="anchor" id="sec_Automatic_conversion_string"></a> -Automatic conversion for string-like types</h2> +</pre><h1><a class="anchor" id="sec_Automatic_conversion_string"></a> +Automatic conversion for string-like types</h1> <p>The document object model of zen::Xml internally stores all names and values as a std::string. Consequently everything that is not a std::string but is "string-like" is UTF-converted into a std::string representation. By default zen::Xml accepts all character arrays like <code>char[]</code>, <code>wchar_t[]</code>, <code>char*</code>, <code>wchar_t*</code>, single characters like <code>char</code>, <code>wchar_t</code>, standard string classes like <code>std::string</code>, <code>std::wstring</code> and user-defined string classes. If the input string is based on <code>char</code>, it will simply be copied and thereby preserves any local encodings. If the input string is based on <code>wchar_t</code> it will be converted to an UTF-8 encoded <code>std::string</code>. The correct <code>wchar_t</code> encoding of the system will be detected at compile time, for example UTF-16 on Windows, UTF-32 on most Linux distributions.</p> <p><b>Note:</b> User-defined string classes are automatically supported if they fulfill the following <b>string concept</b> by defining:</p> <ol type="1"> @@ -355,23 +334,23 @@ Automatic conversion for string-like types</h2> <li>A member function <code>c_str()</code> returning something that can be converted into a <code>const value_type*</code></li> <li>A member function <code>length()</code> returning the number of characters returned by <code>c_str()</code></li> </ol> -<div class="fragment"><pre class="fragment">std::string elem1 = <span class="stringliteral">"elemento1"</span>; -std::wstring elem2 = L<span class="stringliteral">"элемент2"</span>; -wxString elem3 = L<span class="stringliteral">"要素3"</span>; -MyString elem4 = L<span class="stringliteral">"στοιχείο4"</span>; - -<a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); - -out[<span class="stringliteral">"string"</span>] (elem1); -out[<span class="stringliteral">"wstring"</span>] (elem2); -out[<span class="stringliteral">"wxString"</span>] (elem3); -out[<span class="stringliteral">"MyString"</span>] (elem4); -out[<span class="stringliteral">"char[6]"</span>] (<span class="stringliteral">"elem5"</span>); -out[<span class="stringliteral">"wchar_t[4]"</span>](L<span class="stringliteral">"元素6"</span>); -out[<span class="stringliteral">"wchar_t"</span>] (L<span class="stringliteral">'元'</span>); -out[<span class="stringliteral">"char"</span>] (<span class="charliteral">'z'</span>); -</pre></div><p>The resulting XML: </p> -<div class="fragment"><pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> +<div class="fragment"><div class="line">std::string elem1 = <span class="stringliteral">"elemento1"</span>;</div> +<div class="line">std::wstring elem2 = L<span class="stringliteral">"элемент2"</span>;</div> +<div class="line">wxString elem3 = L<span class="stringliteral">"要素3"</span>;</div> +<div class="line">MyString elem4 = L<span class="stringliteral">"στοιχείο4"</span>;</div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc);</div> +<div class="line"></div> +<div class="line">out[<span class="stringliteral">"string"</span>] (elem1);</div> +<div class="line">out[<span class="stringliteral">"wstring"</span>] (elem2);</div> +<div class="line">out[<span class="stringliteral">"wxString"</span>] (elem3);</div> +<div class="line">out[<span class="stringliteral">"MyString"</span>] (elem4);</div> +<div class="line">out[<span class="stringliteral">"char[6]"</span>] (<span class="stringliteral">"elem5"</span>);</div> +<div class="line">out[<span class="stringliteral">"wchar_t[4]"</span>](L<span class="stringliteral">"元素6"</span>);</div> +<div class="line">out[<span class="stringliteral">"wchar_t"</span>] (L<span class="stringliteral">'元'</span>);</div> +<div class="line">out[<span class="stringliteral">"char"</span>] (<span class="charliteral">'z'</span>);</div> +</div><!-- fragment --><p>The resulting XML: </p> +<pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> <Root> <string>elemento1</string> <wstring>элемент2</wstring> @@ -382,8 +361,8 @@ out[<span class="stringliteral">"char"</span>] (<span class="char <wchar_t>元</wchar_t> <char>z</char> </Root> -</pre></div><h2><a class="anchor" id="sec_Automatic_conversion_STL"></a> -Automatic conversion for STL container types</h2> +</pre><h1><a class="anchor" id="sec_Automatic_conversion_STL"></a> +Automatic conversion for STL container types</h1> <ul> <li>User-defined STL compatible types are automatically supported if they fulfill the following <b>container concept</b> by defining:<ol type="1"> <li>A typedef named <code>value_type</code> for the underlying element type of the container</li> @@ -397,36 +376,34 @@ Automatic conversion for STL container types</h2> <li>A member function <code>clear()</code> removing all elements from the container</li> </ol> </li> -</ul> -<ul> <li>In order to support combinations of user types and STL containers such as <code>std::vector<MyType></code> or <code>std::vector<std::list<MyType>></code> it is sufficient to only integrate <code>MyType</code> into zen::Xml. <br/> - See <a class="el" href="index.html#sec_Support_user_defined">Support for user-defined types</a></li> +See <a class="el" href="index.html#sec_Support_user_defined">Support for user-defined types</a></li> </ul> -<div class="fragment"><pre class="fragment">std::deque <float> testDeque; -std::list <size_t> testList; -std::map <double, char> testMap; -std::multimap<short, double> testMultiMap; -std::set <int> testSet; -std::multiset<std::string> testMultiSet; -std::vector <wchar_t> testVector; -std::vector <std::list<wchar_t>> testVectorList; -std::pair <char, wchar_t> testPair; - -<span class="comment">/* fill container */</span> - -<a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); - -out[<span class="stringliteral">"deque"</span>] (testDeque); -out[<span class="stringliteral">"list"</span>] (testList); -out[<span class="stringliteral">"map"</span>] (testMap); -out[<span class="stringliteral">"multimap"</span>] (testMultiMap); -out[<span class="stringliteral">"set"</span>] (testSet); -out[<span class="stringliteral">"multiset"</span>] (testMultiSet); -out[<span class="stringliteral">"vector"</span>] (testVector); -out[<span class="stringliteral">"vect_list"</span>](testVectorList); -out[<span class="stringliteral">"pair"</span> ] (testPair); -</pre></div><p>The resulting XML: </p> -<div class="fragment"><pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> +<div class="fragment"><div class="line">std::deque <float> testDeque;</div> +<div class="line">std::list <size_t> testList;</div> +<div class="line">std::map <double, char> testMap;</div> +<div class="line">std::multimap<short, double> testMultiMap;</div> +<div class="line">std::set <int> testSet;</div> +<div class="line">std::multiset<std::string> testMultiSet;</div> +<div class="line">std::vector <wchar_t> testVector;</div> +<div class="line">std::vector <std::list<wchar_t>> testVectorList;</div> +<div class="line">std::pair <char, wchar_t> testPair;</div> +<div class="line"></div> +<div class="line"><span class="comment">/* fill container */</span></div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc);</div> +<div class="line"></div> +<div class="line">out[<span class="stringliteral">"deque"</span>] (testDeque);</div> +<div class="line">out[<span class="stringliteral">"list"</span>] (testList);</div> +<div class="line">out[<span class="stringliteral">"map"</span>] (testMap);</div> +<div class="line">out[<span class="stringliteral">"multimap"</span>] (testMultiMap);</div> +<div class="line">out[<span class="stringliteral">"set"</span>] (testSet);</div> +<div class="line">out[<span class="stringliteral">"multiset"</span>] (testMultiSet);</div> +<div class="line">out[<span class="stringliteral">"vector"</span>] (testVector);</div> +<div class="line">out[<span class="stringliteral">"vect_list"</span>](testVectorList);</div> +<div class="line">out[<span class="stringliteral">"pair"</span> ] (testPair);</div> +</div><!-- fragment --><p>The resulting XML: </p> +<pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> <Root> <deque> <Item>1.234</Item> @@ -490,106 +467,106 @@ out[<span class="stringliteral">"pair"</span> ] (testPair); <two>â</two> </pair> </Root> -</pre></div><h2><a class="anchor" id="sec_Support_user_defined"></a> -Support for user-defined types</h2> -<p>User types can be integrated into zen::Xml by providing specializations of <a class="el" href="namespacezen.html#acaf85ab94b61882f957afcd355386bff" title="Convert text to user data - used by XML elements and attributes.">zen::readText()</a> and <a class="el" href="namespacezen.html#a2ce2998296871fc2f4718ceceb22a23f" title="Convert user data into text - used by XML elements and attributes.">zen::writeText()</a> or <a class="el" href="namespacezen.html#a2bdcecfe7435ef11cedbce47d4e72ee1" title="Convert XML element to structured user data.">zen::readStruc()</a> and <a class="el" href="namespacezen.html#a29ddb823fe0a195f19a64448881b8bf6" title="Convert structured user data into an XML element.">zen::writeStruc()</a>. The first pair should be used for all non-structured types that can be represented as a simple text string. This specialization is then used to convert the type to XML elements and XML attributes. The second pair should be specialized for structured types that require an XML representation as a hierarchy of elements. This specialization is used when converting the type to XML elements only. <br/> +</pre><h1><a class="anchor" id="sec_Support_user_defined"></a> +Support for user-defined types</h1> +<p>User types can be integrated into zen::Xml by providing specializations of <a class="el" href="namespacezen.html#acaf85ab94b61882f957afcd355386bff" title="Convert text to user data - used by XML elements and attributes. ">zen::readText()</a> and <a class="el" href="namespacezen.html#a2ce2998296871fc2f4718ceceb22a23f" title="Convert user data into text - used by XML elements and attributes. ">zen::writeText()</a> or <a class="el" href="namespacezen.html#a2bdcecfe7435ef11cedbce47d4e72ee1" title="Convert XML element to structured user data. ">zen::readStruc()</a> and <a class="el" href="namespacezen.html#a29ddb823fe0a195f19a64448881b8bf6" title="Convert structured user data into an XML element. ">zen::writeStruc()</a>. The first pair should be used for all non-structured types that can be represented as a simple text string. This specialization is then used to convert the type to XML elements and XML attributes. The second pair should be specialized for structured types that require an XML representation as a hierarchy of elements. This specialization is used when converting the type to XML elements only. <br/> <br/> - See section <a class="el" href="index.html#sec_Type_Safety">Type Safety</a> for a discussion of type categories. <br/> +See section <a class="el" href="index.html#sec_Type_Safety">Type Safety</a> for a discussion of type categories. <br/> <br/> - <b>Example: Specialization for an enum type</b> </p> -<div class="fragment"><pre class="fragment"><span class="keyword">enum</span> UnitTime -{ - UNIT_SECOND, - UNIT_MINUTE, - UNIT_HOUR -}; - -<span class="keyword">namespace </span>zen -{ -<span class="keyword">template</span> <> <span class="keyword">inline</span> -<span class="keywordtype">void</span> <a class="code" href="namespacezen.html#a2ce2998296871fc2f4718ceceb22a23f" title="Convert user data into text - used by XML elements and attributes.">writeText</a>(<span class="keyword">const</span> UnitTime& value, std::string& output) -{ - <span class="keywordflow">switch</span> (value) - { - <span class="keywordflow">case</span> UNIT_SECOND: output = <span class="stringliteral">"second"</span>; <span class="keywordflow">break</span>; - <span class="keywordflow">case</span> UNIT_MINUTE: output = <span class="stringliteral">"minute"</span>; <span class="keywordflow">break</span>; - <span class="keywordflow">case</span> UNIT_HOUR: output = <span class="stringliteral">"hour"</span> ; <span class="keywordflow">break</span>; - } -} - -<span class="keyword">template</span> <> <span class="keyword">inline</span> -<span class="keywordtype">bool</span> <a class="code" href="namespacezen.html#acaf85ab94b61882f957afcd355386bff" title="Convert text to user data - used by XML elements and attributes.">readText</a>(<span class="keyword">const</span> std::string& input, UnitTime& value) -{ - std::string tmp = input; - zen::trim(tmp); - <span class="keywordflow">if</span> (tmp == <span class="stringliteral">"second"</span>) - value = UNIT_SECOND; - <span class="keywordflow">else</span> <span class="keywordflow">if</span> (tmp == <span class="stringliteral">"minute"</span>) - value = UNIT_MINUTE; - <span class="keywordflow">else</span> <span class="keywordflow">if</span> (tmp == <span class="stringliteral">"hour"</span>) - value = UNIT_HOUR; - <span class="keywordflow">else</span> - <span class="keywordflow">return</span> <span class="keyword">false</span>; - <span class="keywordflow">return</span> <span class="keyword">true</span>; -} -} -</pre></div><p><b>Example: Brute-force specialization for an enum type</b> </p> -<div class="fragment"><pre class="fragment"><span class="keyword">namespace </span>zen -{ -<span class="keyword">template</span> <> <span class="keyword">inline</span> -<span class="keywordtype">void</span> <a class="code" href="namespacezen.html#a2ce2998296871fc2f4718ceceb22a23f" title="Convert user data into text - used by XML elements and attributes.">writeText</a>(<span class="keyword">const</span> EnumType& value, std::string& output) -{ - output = zen::numberTo<std::string>(<span class="keyword">static_cast<</span><span class="keywordtype">int</span><span class="keyword">></span>(value)); <span class="comment">//treat enum like an integer</span> -} - -<span class="keyword">template</span> <> <span class="keyword">inline</span> -<span class="keywordtype">bool</span> <a class="code" href="namespacezen.html#acaf85ab94b61882f957afcd355386bff" title="Convert text to user data - used by XML elements and attributes.">readText</a>(<span class="keyword">const</span> std::string& input, EnumType& value) -{ - value = <span class="keyword">static_cast<</span>EnumType<span class="keyword">></span>(zen::stringTo<int>(input)); <span class="comment">//treat enum like an integer</span> - <span class="keywordflow">return</span> <span class="keyword">true</span>; -} -} -</pre></div><p><b>Example: Specialization for a structured user type</b> </p> -<div class="fragment"><pre class="fragment"><span class="keyword">struct </span>Config -{ - <span class="keywordtype">int</span> a; - std::wstring b; -}; - -<span class="keyword">namespace </span>zen -{ -<span class="keyword">template</span> <> <span class="keyword">inline</span> -<span class="keywordtype">void</span> <a class="code" href="namespacezen.html#a29ddb823fe0a195f19a64448881b8bf6" title="Convert structured user data into an XML element.">writeStruc</a>(<span class="keyword">const</span> Config& value, XmlElement& output) -{ - XmlOut out(output); - out[<span class="stringliteral">"number"</span> ](value.a); - out[<span class="stringliteral">"address"</span>](value.b); -} - -<span class="keyword">template</span> <> <span class="keyword">inline</span> -<span class="keywordtype">bool</span> <a class="code" href="namespacezen.html#a2bdcecfe7435ef11cedbce47d4e72ee1" title="Convert XML element to structured user data.">readStruc</a>(<span class="keyword">const</span> XmlElement& input, Config& value) -{ - XmlIn in(input); - <span class="keywordtype">bool</span> rv1 = in[<span class="stringliteral">"number"</span> ](value.a); - <span class="keywordtype">bool</span> rv2 = in[<span class="stringliteral">"address"</span>](value.b); - <span class="keywordflow">return</span> rv1 && rv2; -} -} - -<span class="keywordtype">int</span> main() -{ - Config cfg = { 2, L<span class="stringliteral">"Abc 3"</span> }; - - std::vector<Config> cfgList; - cfgList.push_back(cfg); - - <a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">zen::XmlDoc</a> doc; - <a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); <span class="comment">//write to Xml via output proxy</span> - out[<span class="stringliteral">"config"</span>](cfgList); - <a class="code" href="namespacezen.html#adeeb6b2318097382ae47aa939fc15d4d" title="Save XML document to a file.">save</a>(doc, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span> -} -</pre></div><p>The resulting XML: </p> -<div class="fragment"><pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> +<b>Example: Specialization for an enum type</b> </p> +<div class="fragment"><div class="line"><span class="keyword">enum</span> UnitTime</div> +<div class="line">{</div> +<div class="line"> UNIT_SECOND,</div> +<div class="line"> UNIT_MINUTE,</div> +<div class="line"> UNIT_HOUR</div> +<div class="line">};</div> +<div class="line"></div> +<div class="line"><span class="keyword">namespace </span>zen</div> +<div class="line">{</div> +<div class="line"><span class="keyword">template</span> <> <span class="keyword">inline</span></div> +<div class="line"><span class="keywordtype">void</span> <a class="code" href="namespacezen.html#a2ce2998296871fc2f4718ceceb22a23f">writeText</a>(<span class="keyword">const</span> UnitTime& value, std::string& output)</div> +<div class="line">{</div> +<div class="line"> <span class="keywordflow">switch</span> (value)</div> +<div class="line"> {</div> +<div class="line"> <span class="keywordflow">case</span> UNIT_SECOND: output = <span class="stringliteral">"second"</span>; <span class="keywordflow">break</span>;</div> +<div class="line"> <span class="keywordflow">case</span> UNIT_MINUTE: output = <span class="stringliteral">"minute"</span>; <span class="keywordflow">break</span>;</div> +<div class="line"> <span class="keywordflow">case</span> UNIT_HOUR: output = <span class="stringliteral">"hour"</span> ; <span class="keywordflow">break</span>;</div> +<div class="line"> }</div> +<div class="line">}</div> +<div class="line"></div> +<div class="line"><span class="keyword">template</span> <> <span class="keyword">inline</span></div> +<div class="line"><span class="keywordtype">bool</span> <a class="code" href="namespacezen.html#acaf85ab94b61882f957afcd355386bff">readText</a>(<span class="keyword">const</span> std::string& input, UnitTime& value)</div> +<div class="line">{</div> +<div class="line"> std::string tmp = input;</div> +<div class="line"> zen::trim(tmp);</div> +<div class="line"> <span class="keywordflow">if</span> (tmp == <span class="stringliteral">"second"</span>)</div> +<div class="line"> value = UNIT_SECOND;</div> +<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (tmp == <span class="stringliteral">"minute"</span>)</div> +<div class="line"> value = UNIT_MINUTE;</div> +<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (tmp == <span class="stringliteral">"hour"</span>)</div> +<div class="line"> value = UNIT_HOUR;</div> +<div class="line"> <span class="keywordflow">else</span></div> +<div class="line"> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div> +<div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div> +<div class="line">}</div> +<div class="line">}</div> +</div><!-- fragment --><p><b>Example: Brute-force specialization for an enum type</b> </p> +<div class="fragment"><div class="line"><span class="keyword">namespace </span>zen</div> +<div class="line">{</div> +<div class="line"><span class="keyword">template</span> <> <span class="keyword">inline</span></div> +<div class="line"><span class="keywordtype">void</span> <a class="code" href="namespacezen.html#a2ce2998296871fc2f4718ceceb22a23f">writeText</a>(<span class="keyword">const</span> EnumType& value, std::string& output)</div> +<div class="line">{</div> +<div class="line"> output = zen::numberTo<std::string>(<span class="keyword">static_cast<</span><span class="keywordtype">int</span><span class="keyword">></span>(value)); <span class="comment">//treat enum like an integer</span></div> +<div class="line">}</div> +<div class="line"></div> +<div class="line"><span class="keyword">template</span> <> <span class="keyword">inline</span></div> +<div class="line"><span class="keywordtype">bool</span> <a class="code" href="namespacezen.html#acaf85ab94b61882f957afcd355386bff">readText</a>(<span class="keyword">const</span> std::string& input, EnumType& value)</div> +<div class="line">{</div> +<div class="line"> value = <span class="keyword">static_cast<</span>EnumType<span class="keyword">></span>(zen::stringTo<int>(input)); <span class="comment">//treat enum like an integer</span></div> +<div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div> +<div class="line">}</div> +<div class="line">}</div> +</div><!-- fragment --><p><b>Example: Specialization for a structured user type</b> </p> +<div class="fragment"><div class="line"><span class="keyword">struct </span>Config</div> +<div class="line">{</div> +<div class="line"> <span class="keywordtype">int</span> a;</div> +<div class="line"> std::wstring b;</div> +<div class="line">};</div> +<div class="line"></div> +<div class="line"><span class="keyword">namespace </span>zen</div> +<div class="line">{</div> +<div class="line"><span class="keyword">template</span> <> <span class="keyword">inline</span></div> +<div class="line"><span class="keywordtype">void</span> <a class="code" href="namespacezen.html#a29ddb823fe0a195f19a64448881b8bf6">writeStruc</a>(<span class="keyword">const</span> Config& value, XmlElement& output)</div> +<div class="line">{</div> +<div class="line"> XmlOut out(output);</div> +<div class="line"> out[<span class="stringliteral">"number"</span> ](value.a);</div> +<div class="line"> out[<span class="stringliteral">"address"</span>](value.b);</div> +<div class="line">}</div> +<div class="line"></div> +<div class="line"><span class="keyword">template</span> <> <span class="keyword">inline</span></div> +<div class="line"><span class="keywordtype">bool</span> <a class="code" href="namespacezen.html#a2bdcecfe7435ef11cedbce47d4e72ee1">readStruc</a>(<span class="keyword">const</span> XmlElement& input, Config& value)</div> +<div class="line">{</div> +<div class="line"> XmlIn in(input);</div> +<div class="line"> <span class="keywordtype">bool</span> rv1 = in[<span class="stringliteral">"number"</span> ](value.a);</div> +<div class="line"> <span class="keywordtype">bool</span> rv2 = in[<span class="stringliteral">"address"</span>](value.b);</div> +<div class="line"> <span class="keywordflow">return</span> rv1 && rv2;</div> +<div class="line">}</div> +<div class="line">}</div> +<div class="line"></div> +<div class="line"><span class="keywordtype">int</span> main()</div> +<div class="line">{</div> +<div class="line"> Config cfg = { 2, L<span class="stringliteral">"Abc 3"</span> };</div> +<div class="line"></div> +<div class="line"> std::vector<Config> cfgList;</div> +<div class="line"> cfgList.push_back(cfg);</div> +<div class="line"></div> +<div class="line"> <a class="code" href="classzen_1_1_xml_doc.html">zen::XmlDoc</a> doc;</div> +<div class="line"> <a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc); <span class="comment">//write to Xml via output proxy</span></div> +<div class="line"> out[<span class="stringliteral">"config"</span>](cfgList);</div> +<div class="line"> <a class="code" href="namespacezen.html#adeeb6b2318097382ae47aa939fc15d4d">save</a>(doc, <span class="stringliteral">"file.xml"</span>); <span class="comment">//throw XmlFileError</span></div> +<div class="line">}</div> +</div><!-- fragment --><p>The resulting XML: </p> +<pre class="fragment"><?xml version="1.0" encoding="UTF-8"?> <Root> <config> <Item> @@ -598,40 +575,40 @@ Support for user-defined types</h2> </Item> </config> </Root> -</pre></div><h2><a class="anchor" id="sec_Structured_user_types"></a> -Structured user types</h2> -<p>Although it is possible to enable conversion of structured user types by specializing <a class="el" href="namespacezen.html#a2bdcecfe7435ef11cedbce47d4e72ee1" title="Convert XML element to structured user data.">zen::readStruc()</a> and <a class="el" href="namespacezen.html#a29ddb823fe0a195f19a64448881b8bf6" title="Convert structured user data into an XML element.">zen::writeStruc()</a> (see <a class="el" href="index.html#sec_Support_user_defined">Support for user-defined types</a>), this approach has one drawback: If a mapping error occurs when converting an XML element to structured user data, for example a child-element is missing, the input proxy class <a class="el" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data.">zen::XmlIn</a> is only able to detect that the whole conversion failed. It cannot say which child-elements in particular failed to convert. <br/> +</pre><h1><a class="anchor" id="sec_Structured_user_types"></a> +Structured user types</h1> +<p>Although it is possible to enable conversion of structured user types by specializing <a class="el" href="namespacezen.html#a2bdcecfe7435ef11cedbce47d4e72ee1" title="Convert XML element to structured user data. ">zen::readStruc()</a> and <a class="el" href="namespacezen.html#a29ddb823fe0a195f19a64448881b8bf6" title="Convert structured user data into an XML element. ">zen::writeStruc()</a> (see <a class="el" href="index.html#sec_Support_user_defined">Support for user-defined types</a>), this approach has one drawback: If a mapping error occurs when converting an XML element to structured user data, for example a child-element is missing, the input proxy class <a class="el" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data. ">zen::XmlIn</a> is only able to detect that the whole conversion failed. It cannot say which child-elements in particular failed to convert. <br/> <br/> - Therefore it may be appropriate to convert structured types by calling subroutines in order to enable fine-granular logging:</p> -<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> readConfig(<span class="keyword">const</span> <a class="code" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data.">zen::XmlIn</a>& in, Config& cfg) -{ - in[<span class="stringliteral">"number"</span> ](value.a); <span class="comment">//failed conversions will now be logged for each single item by XmlIn</span> - in[<span class="stringliteral">"address"</span>](value.b); <span class="comment">//instead of only once for the complete Config type!</span> -} - - -<span class="keywordtype">void</span> loadConfig(<span class="keyword">const</span> wxString& filename, Config& cfg) -{ - <a class="code" href="classzen_1_1_xml_doc.html" title="The complete XML document.">zen::XmlDoc</a> doc; <span class="comment">//empty XML document</span> - - <span class="keywordflow">try</span> - { - <a class="code" href="namespacezen.html#a900c1fb290f0eedc24354c487145dbee" title="Load XML document from a file.">load</a>(filename, doc); <span class="comment">//throw XmlFileError, XmlParsingError</span> - } - <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="structzen_1_1_xml_error.html" title="Exception base class for zen::Xml.">zen::XmlError</a>& e) { <span class="comment">/* handle error */</span> } - - <a class="code" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data.">zen::XmlIn</a> in(doc); - - <a class="code" href="classzen_1_1_xml_in.html" title="Proxy class to conveniently convert XML structure to user data.">zen::XmlIn</a> inConfig = in[<span class="stringliteral">"config"</span>]; <span class="comment">//get input proxy for child element "config"</span> - - readConfig(inConfig, cfg); <span class="comment">//map child element to user data by calling subroutine</span> - - <span class="comment">//check for mapping errors: errors occuring in subroutines are considered, too!</span> - <span class="keywordflow">if</span> (in.errorsOccured()) - <span class="comment">/* show mapping errors */</span> -} -</pre></div><h2><a class="anchor" id="sec_Type_Safety"></a> -Type Safety</h2> +Therefore it may be appropriate to convert structured types by calling subroutines in order to enable fine-granular logging:</p> +<div class="fragment"><div class="line"><span class="keywordtype">void</span> readConfig(<span class="keyword">const</span> <a class="code" href="classzen_1_1_xml_in.html">zen::XmlIn</a>& in, Config& cfg)</div> +<div class="line">{</div> +<div class="line"> in[<span class="stringliteral">"number"</span> ](value.a); <span class="comment">//failed conversions will now be logged for each single item by XmlIn</span></div> +<div class="line"> in[<span class="stringliteral">"address"</span>](value.b); <span class="comment">//instead of only once for the complete Config type!</span></div> +<div class="line">}</div> +<div class="line"></div> +<div class="line"></div> +<div class="line"><span class="keywordtype">void</span> loadConfig(<span class="keyword">const</span> wxString& filename, Config& cfg)</div> +<div class="line">{</div> +<div class="line"> <a class="code" href="classzen_1_1_xml_doc.html">zen::XmlDoc</a> doc; <span class="comment">//empty XML document</span></div> +<div class="line"></div> +<div class="line"> <span class="keywordflow">try</span></div> +<div class="line"> {</div> +<div class="line"> <a class="code" href="namespacezen.html#a872a48c0616e7f12ae8caca464835e00">load</a>(filename, doc); <span class="comment">//throw XmlFileError, XmlParsingError</span></div> +<div class="line"> }</div> +<div class="line"> <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="structzen_1_1_xml_error.html">zen::XmlError</a>& e) { <span class="comment">/* handle error */</span> }</div> +<div class="line"></div> +<div class="line"> <a class="code" href="classzen_1_1_xml_in.html">zen::XmlIn</a> in(doc); </div> +<div class="line"> </div> +<div class="line"> <a class="code" href="classzen_1_1_xml_in.html">zen::XmlIn</a> inConfig = in[<span class="stringliteral">"config"</span>]; <span class="comment">//get input proxy for child element "config"</span></div> +<div class="line"> </div> +<div class="line"> readConfig(inConfig, cfg); <span class="comment">//map child element to user data by calling subroutine</span></div> +<div class="line"></div> +<div class="line"> <span class="comment">//check for mapping errors: errors occuring in subroutines are considered, too!</span></div> +<div class="line"> <span class="keywordflow">if</span> (in.errorsOccured())</div> +<div class="line"> <span class="comment">/* show mapping errors */</span></div> +<div class="line">}</div> +</div><!-- fragment --><h1><a class="anchor" id="sec_Type_Safety"></a> +Type Safety</h1> <p>zen::Xml heavily uses methods of compile-time introspection in order to free the user from managing basic type conversions by himself. Thereby it is important to find the right balance between automatic conversions and type safety so that program correctness is not compromised. In the context of XML processing three fundamental type categories can be recognized:</p> <ul> <li><b>string-like types</b>: <code>std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ...</code></li> @@ -639,7 +616,7 @@ Type Safety</h2> <li><b>structured types</b>: any to-string-convertible type, STL containers, <code>std::pair</code>, structured user types</li> </ul> <p>These categories can be seen as a sequence of inclusive sets: </p> -<div class="fragment"><pre class="fragment">----------------------------- +<pre class="fragment">----------------------------- | structured | Used as: XML element value | ------------------------- | Conversion via: readStruc(), writeStruc() - may be specialized for user-defined types! | | to-string-convertible | | Used as: XML element/attribute value @@ -648,32 +625,30 @@ Type Safety</h2> | | --------------- | | Conversion via: utfCvrtTo<>() | ------------------------- | ----------------------------- -</pre></div><p>A practical implication of this design is that conversions that do not make sense in a particular context simply lead to compile-time errors: </p> -<div class="fragment"><pre class="fragment"><a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); -out[L<span class="charliteral">'Z'</span>](someValue); <span class="comment">//fine: a wchar_t is acceptable as an element name</span> -out[1234](someValue); <span class="comment">//compiler error: an integer is NOT "string-like"!</span> -</pre></div><p> <br/> - </p> -<div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> i = 0; -std::vector<int> v; - -<a class="code" href="classzen_1_1_xml_out.html" title="Proxy class to conveniently convert user data into XML structure.">zen::XmlOut</a> out(doc); -out[<span class="stringliteral">"elem1"</span>](i); <span class="comment">//fine: both i and v can be converted to an XML element</span> -out[<span class="stringliteral">"elem2"</span>](v); <span class="comment">//</span> - -out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr1"</span>, i); <span class="comment">//fine: an integer can be converted to an XML attribute</span> -out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr2"</span>, v); <span class="comment">//compiler error: a std::vector<int> is NOT "to-string-convertible"!</span> -</pre></div><dl class="section author"><dt>Author:</dt><dd><b>Zenju</b> <br/> +</pre><p>A practical implication of this design is that conversions that do not make sense in a particular context simply lead to compile-time errors: </p> +<div class="fragment"><div class="line"><a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc);</div> +<div class="line">out[L<span class="charliteral">'Z'</span>](someValue); <span class="comment">//fine: a wchar_t is acceptable as an element name</span></div> +<div class="line">out[1234](someValue); <span class="comment">//compiler error: an integer is NOT "string-like"!</span></div> +</div><!-- fragment --><p> <br/> +</p> +<div class="fragment"><div class="line"><span class="keywordtype">int</span> i = 0;</div> +<div class="line">std::vector<int> v;</div> +<div class="line"></div> +<div class="line"><a class="code" href="classzen_1_1_xml_out.html">zen::XmlOut</a> out(doc);</div> +<div class="line">out[<span class="stringliteral">"elem1"</span>](i); <span class="comment">//fine: both i and v can be converted to an XML element</span></div> +<div class="line">out[<span class="stringliteral">"elem2"</span>](v); <span class="comment">//</span></div> +<div class="line"></div> +<div class="line">out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr1"</span>, i); <span class="comment">//fine: an integer can be converted to an XML attribute</span></div> +<div class="line">out[<span class="stringliteral">"elem"</span>].attribute(<span class="stringliteral">"attr2"</span>, v); <span class="comment">//compiler error: a std::vector<int> is NOT "to-string-convertible"!</span></div> +</div><!-- fragment --><dl class="section author"><dt>Author</dt><dd><b>Zenju</b> <br/> <br/> <b>Email:</b> zenju AT gmx DOT de </dd></dl> </div></div><!-- contents --> - - +<!-- start footer part --> <hr class="footer"/><address class="footer"><small> Generated by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/> -</a> 1.8.0 +</a> 1.8.6 </small></address> - </body> </html> |