diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:29:28 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:29:28 +0200 |
commit | 75c07011b7c4d06acd7b45dabdcd60ab9d80f385 (patch) | |
tree | 8853c3978dd152ef377e652239448b1352320206 /zenxml/doc/index.html | |
parent | 5.22 (diff) | |
download | FreeFileSync-75c07011b7c4d06acd7b45dabdcd60ab9d80f385.tar.gz FreeFileSync-75c07011b7c4d06acd7b45dabdcd60ab9d80f385.tar.bz2 FreeFileSync-75c07011b7c4d06acd7b45dabdcd60ab9d80f385.zip |
5.23
Diffstat (limited to 'zenxml/doc/index.html')
-rw-r--r-- | zenxml/doc/index.html | 679 |
1 files changed, 679 insertions, 0 deletions
diff --git a/zenxml/doc/index.html b/zenxml/doc/index.html new file mode 100644 index 00000000..d7245a6b --- /dev/null +++ b/zenxml/doc/index.html @@ -0,0 +1,679 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> +<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> +<meta http-equiv="X-UA-Compatible" content="IE=9"/> +<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="search/search.js"></script> +<script type="text/javascript"> + $(document).ready(function() { searchBox.OnSelectItem(0); }); +</script> + +</head> +<body> +<div id="top"><!-- do not remove this div! --> + + +<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 --> +<script type="text/javascript"> +var searchBox = new SearchBox("searchBox", "search",false,'Search'); +</script> + <div id="navrow1" class="tabs"> + <ul class="tablist"> + <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"> + <img id="MSearchSelect" src="search/mag_sel.png" + onmouseover="return searchBox.OnSearchSelectShow()" + onmouseout="return searchBox.OnSearchSelectHide()" + alt=""/> + <input type="text" id="MSearchField" value="Search" accesskey="S" + onfocus="searchBox.OnSearchFieldFocus(true)" + onblur="searchBox.OnSearchFieldFocus(false)" + onkeyup="searchBox.OnSearchFieldChange(event)"/> + </span><span class="right"> + <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> + </span> + </div> + </li> + </ul> + </div> +</div> +<!-- 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> + +<!-- iframe showing the search results (closed by default) --> +<div id="MSearchResultsWindow"> +<iframe src="javascript:void(0)" frameborder="0" + name="MSearchResults" id="MSearchResults"> +</iframe> +</div> + +<div class="header"> + <div class="headertitle"> +<div class="title">Overview </div> </div> +</div><!--header--> +<div class="contents"> +<div class="textblock"><ul> +<li><a class="el" href="index.html#sec_Rationale">Rationale</a> </li> +<li><a class="el" href="index.html#sec_Quick_Start">Quick Start</a> </li> +<li><a class="el" href="index.html#sec_Supported_Platforms">Supported Platforms</a> </li> +<li><a class="el" href="index.html#sec_Flexible_Programming_Model">Flexible Programming Model</a> </li> +<li><a class="el" href="index.html#sec_Structured_XML_element_access">Structured XML element access</a> </li> +<li><a class="el" href="index.html#sec_Access_XML_attributes">Access XML attributes</a> </li> +<li><a class="el" href="index.html#sec_Automatic_conversion_built_in">Automatic conversion for built-in arithmetic types</a> </li> +<li><a class="el" href="index.html#sec_Automatic_conversion_string">Automatic conversion for string-like types</a> </li> +<li><a class="el" href="index.html#sec_Automatic_conversion_STL">Automatic conversion for STL container types</a> </li> +<li><a class="el" href="index.html#sec_Support_user_defined">Support for user-defined types</a> </li> +<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> +<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> +<li><b>all</b> kinds of string classes and "string-like" types,</li> +<li><b>all</b> types defined as STL containers</li> +</ul> +<p>are handled automatically. Thereby a large number of recurring problems is solved by the library:</p> +<ul> +<li>generic number to string conversions</li> +<li>generic char to wchar_t conversions (UTF) for custom string classes in a platform independent manner</li> +<li>serialization of arbitrary STL container types</li> +<li>simple integration: header-only, no extra dependencies, fully portable</li> +<li>support arbitrary string classes everywhere: for file names, XML element names, attribute names, values, ...</li> +<li>XML library built on C++11 with focus on elegance, minimal code size, flexibility and performance</li> +<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> +<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"?> +<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> +<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"> +<li>Visual C++ 2010 - 32 bit</li> +<li>Visual C++ 2010 - 64 bit</li> +<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 +-std=c++0x +-std=gnu++0x +</pre></div><h2><a class="anchor" id="sec_Flexible_Programming_Model"></a> +Flexible Programming Model</h2> +<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/> +<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>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> +</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"?> +<Root> + <elemento1> + <элемент2> + <要素3> + <στοιχείο4> + <elem5> + <元素6> + <元> + <z>-1234</z> + </元> + </元素6> + </elem5> + </στοιχείο4> + </要素3> + </элемент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"?> +<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> +<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"?> +<Root> + <int>-1234</int> + <double>1.23</double> + <float>4.56</float> + <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> +<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"> +<li>A typedef named <code>value_type</code> for the underlying character type: must be <code>char</code> or <code>wchar_t</code> </li> +<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"?> +<Root> + <string>elemento1</string> + <wstring>элемент2</wstring> + <wxString>要素3</wxString> + <MyString>στοιχείο4</MyString> + <char[6]>elem5</char[6]> + <wchar_t[4]>元素6</wchar_t[4]> + <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> +<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> +<li>A typedef named <code>iterator</code> for a non-const iterator into the container</li> +<li>A typedef named <code>const_iterator</code> for a const iterator into the container <br/> +<br/> +</li> +<li>A member function <code>begin()</code> returning an iterator pointing to the first element in the container</li> +<li>A member function <code>end()</code> returning an iterator pointing just after the last element in the container</li> +<li>A member function <code>insert()</code> with the signature <code>iterator insert(iterator position, const value_type& x)</code></li> +<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> +</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"?> +<Root> + <deque> + <Item>1.234</Item> + <Item>5.678</Item> + </deque> + <list> + <Item>1</Item> + <Item>2</Item> + </list> + <map> + <Item> + <one>1.1</one> + <two>a</two> + </Item> + <Item> + <one>2.2</one> + <two>b</two> + </Item> + </map> + <multimap> + <Item> + <one>3</one> + <two>99</two> + </Item> + <Item> + <one>3</one> + <two>100</two> + </Item> + <Item> + <one>4</one> + <two>101</two> + </Item> + </multimap> + <set> + <Item>1</Item> + <Item>2</Item> + </set> + <multiset> + <Item>1</Item> + <Item>1</Item> + <Item>2</Item> + </multiset> + <vector> + <Item>Ä</Item> + <Item>Ö</Item> + </vector> + <vect_list> + <Item> + <Item>ä</Item> + <Item>ö</Item> + <Item>ü</Item> + </Item> + <Item> + <Item>ä</Item> + <Item>ö</Item> + <Item>ü</Item> + </Item> + </vect_list> + <pair> + <one>a</one> + <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/> +<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"?> +<Root> + <config> + <Item> + <number>2</number> + <address>Abc 3</address> + </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/> +<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> +<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> +<li><b>to-string-convertible types</b>: any string-like type, all built-in arithmetic numbers, <code>bool</code></li> +<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">----------------------------- +| 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 +| | --------------- | | Conversion via: readText(), writeText() - may be specialized for user-defined types! +| | | string-like | | | Used as: XML element/attribute value or element name +| | --------------- | | 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/> +<br/> + <b>Email:</b> zenju AT gmx DOT de </dd></dl> +</div></div><!-- contents --> + + +<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 +</small></address> + +</body> +</html> |