Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > e5b501e96823201f44cb057859a8bf79 > files > 413

gsoap-2.8.67-2.mga7.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta http-equiv="cache-control" content="no-cache">
<title>Genivia - README</title>
<link href="genivia_tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="genivia_content.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="top">
 <div id="titlearea">
  <table height="72px" width="100%" cellspacing="0" cellpadding="0">
   <tbody>
    <tr>
     <td width="10%">&nbsp;</td>
     <td width="175px"><a href="http://www.genivia.com"><img alt="Genivia" src="GeniviaLogo2_trans_noslogan.png"/></a></td>
     <td class="tab_home"><a href="http://www.genivia.com">Home</a></td>
     <td class="tab_home"><a href="http://www.genivia.com/docs.html">Documentation</a></td>
     <td>
      <div style="float: right; font-size: 18px; font-weight: bold;">README</div>
      <br>
      <div style="float: right; font-size: 10px;">updated Sat Feb 6 2016</div>
     </td>
     <td width="10%">&nbsp;</td>
    </tr>
   </tbody>
  </table>
 </div>
<!-- Generated by Doxygen 1.8.10 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('md___users_engelen__projects_gsoap_samples_databinding__r_e_a_d_m_e.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">README </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#mainpage">XML Data Bindings                                                    </a></li>
<li class="level1"><a href="#intro">Introduction                                                            </a></li>
<li class="level1"><a href="#tocpp">Mapping WSDL and XML schemas to C/C++                                   </a></li>
<li class="level1"><a href="#typemap">Using typemap.dat to customize data bindings                          </a><ul><li class="level2"><a href="#typemap1">XML namespace bindings                                               </a></li>
<li class="level2"><a href="#typemap2">XSD type bindings                                                    </a></li>
<li class="level2"><a href="#custom">Custom serializers for XSD types                                       </a><ul><li class="level3"><a href="#custom-1">xsd:integer</a></li>
<li class="level3"><a href="#custom-2">xsd:decimal</a></li>
<li class="level3"><a href="#custom-3">xsd:dateTime</a></li>
<li class="level3"><a href="#custom-4">xsd:date</a></li>
<li class="level3"><a href="#custom-5">xsd:time</a></li>
<li class="level3"><a href="#custom-6">xsd:duration</a></li>
</ul>
</li>
<li class="level2"><a href="#typemap3">Class/struct member additions                                        </a></li>
<li class="level2"><a href="#typemap4">Replacing XSD types by equivalent alternatives                       </a></li>
<li class="level2"><a href="#typemap5">The built-in typemap.dat variables $CONTAINER and $POINTER           </a></li>
<li class="level2"><a href="#typemap6">User-defined content                                                 </a></li>
</ul>
</li>
<li class="level1"><a href="#toxsd">Mapping C/C++ to XML schema                                             </a><ul><li class="level2"><a href="#toxsd1">Overview of serializable C/C++ types                                   </a></li>
<li class="level2"><a href="#toxsd2">Colon notation versus name prefixing                                   </a></li>
<li class="level2"><a href="#toxsd3">C++ Bool and C alternatives                                            </a></li>
<li class="level2"><a href="#toxsd4">Enumerations and bitmasks                                              </a></li>
<li class="level2"><a href="#toxsd5">Numerical types                                                        </a></li>
<li class="level2"><a href="#toxsd6">String types                                                           </a></li>
<li class="level2"><a href="#toxsd7">Date and time types                                                    </a></li>
<li class="level2"><a href="#toxsd8">Time duration types                                                    </a></li>
<li class="level2"><a href="#toxsd9">Classes and structs                                                    </a><ul><li class="level3"><a href="#toxsd9-1">Serializable versus transient types and members</a></li>
<li class="level3"><a href="#toxsd9-2">Volatile classes and structs</a></li>
<li class="level3"><a href="#toxsd9-3">Mutable classes and structs</a></li>
<li class="level3"><a href="#toxsd9-4">Default member values in C and C++</a></li>
<li class="level3"><a href="#toxsd9-5">Attribute members</a></li>
<li class="level3"><a href="#toxsd9-6">Qualified and unqualified members</a></li>
<li class="level3"><a href="#toxsd9-7">Defining document root elements</a></li>
<li class="level3"><a href="#toxsd9-8">(Smart) pointer members and their occurrence constraints</a></li>
<li class="level3"><a href="#toxsd9-9">Container members and their occurrence constraints</a></li>
<li class="level3"><a href="#toxsd9-10">Tagged union members</a></li>
<li class="level3"><a href="#toxsd9-11">Tagged void pointer members</a></li>
<li class="level3"><a href="#toxsd9-12">Adding get and set methods</a></li>
<li class="level3"><a href="#toxsd9-13">Operations on classes and structs</a></li>
</ul>
</li>
<li class="level2"><a href="#toxsd10">Special classes and structs                                           </a><ul><li class="level3"><a href="#toxsd10-1">SOAP encoded arrays</a></li>
<li class="level3"><a href="#toxsd10-2">XSD hexBinary and base64Binary types</a></li>
<li class="level3"><a href="#toxsd10-3">MIME/MTOM attachment binary types</a></li>
<li class="level3"><a href="#toxsd10-4">Wrapper class/struct with simpleContent</a></li>
<li class="level3"><a href="#toxsd10-5">DOM anyType and anyAttribute</a></li>
</ul>
</li>
</ul>
</li>
<li class="level1"><a href="#directives">Directives                                                         </a><ul><li class="level2"><a href="#directives-1">Service directives                                               </a></li>
<li class="level2"><a href="#directives-2">Service method directives                                        </a></li>
<li class="level2"><a href="#directives-3">Schema directives                                                </a></li>
<li class="level2"><a href="#directives-4">Schema type directives                                           </a></li>
<li class="level2"><a href="#directives-ex">Examples of directives                                          </a></li>
</ul>
</li>
<li class="level1"><a href="#rules">Serialization rules                                                     </a><ul><li class="level2"><a href="#doc-rpc">SOAP document versus rpc style                                        </a></li>
<li class="level2"><a href="#lit-enc">SOAP literal versus encoding                                          </a></li>
<li class="level2"><a href="#soap">SOAP 1.1 versus SOAP 1.2                                                 </a></li>
<li class="level2"><a href="#non-soap">Non-SOAP XML serialization                                           </a></li>
</ul>
</li>
<li class="level1"><a href="#io">Input and output                                                           </a><ul><li class="level2"><a href="#io1">Reading and writing from/to files and streams                             </a></li>
<li class="level2"><a href="#io2">Reading and writing from/to string buffers                                </a></li>
</ul>
</li>
<li class="level1"><a href="#memory">Memory management                                                      </a><ul><li class="level2"><a href="#memory1">Memory management in C                                                </a></li>
<li class="level2"><a href="#memory2">Memory management in C++                                              </a></li>
</ul>
</li>
<li class="level1"><a href="#features">Features and limitations                                             </a></li>
<li class="level1"><a href="#nsmap">Removing SOAP namespaces from XML payloads                              </a></li>
<li class="level1"><a href="#examples">Examples                                                             </a></li>
</ul>
</div>
<div class="textblock"><h1><a class="anchor" id="mainpage"></a>
XML Data Bindings                                                    </h1>
<h1><a class="anchor" id="intro"></a>
Introduction                                                            </h1>
<p>This is a detailed overview of the gSOAP XML data bindings concepts, usage, and implementation. At the end of this document two examples are given to illustrate the application of XML data bindings.</p>
<p>The first simple example <code><a class="el" href="address_8cpp.html">address.cpp</a></code> shows how to use wsdl2h to bind an XML schema to C++. The C++ application reads and writes an XML file into and from a C++ "address book" data structure. The C++ data structure is an STL vector of address objects.</p>
<p>The second example <code><a class="el" href="graph_8cpp.html">graph.cpp</a></code> shows how XML is serialized as a tree, digraph, and cyclic graph. The digraph and cyclic graph serialization rules are similar to SOAP 1.1/1.2 encoded multi-ref elements with id-ref attributes to link elements through IDREF XML references, creating a an XML graph with pointers to XML nodes.</p>
<p>These examples demonstrate XML data bindings only for relatively simple data structures and types. The gSOAP tools support more than just these type of structures to serialize in XML. There are practically no limits to enable XML serialization of C and C++ types.</p>
<p>Support for XML schema components is unlimited. The wsdl2h tool maps schemas to C and C++ using built-in intuitive mapping rules, while allowing the mappings to be customized using a <code>typemap.dat</code> file with mapping instructions for wsdl2h.</p>
<p>The information in this document is applicable to gSOAP 2.8.26 and higher, which supports C++11 features. However, C++11 is not required to use this material and the examples included, unless we need smart pointers and scoped enumerations. While most of the examples in this document are given in C++, the concepts also apply to C with the exception of containers, smart pointers, classes and their methods. None of these exceptions limit the use of the gSOAP tools for C in any way.</p>
<p>The data binding concepts described in this document were first envisioned in 1999 by Prof. Robert van Engelen at the Florida State University. An implementation was created in 2000, named "stub/skeleton compiler". The first articles on its successor version "gSOAP" appeared in 2002. The principle of mapping XSD components to C/C++ types and vice versa is now widely adopted in systems and programming languages, including Java web services and by C# WCF.</p>
<p>We continue to be committed to our goal to empower C/C++ developers with powerful autocoding tools for XML. Our commitment started in the very early days of SOAP by actively participating in <a href="http://www.whitemesa.com/interop.htm">SOAP interoperability testing</a>, participating in the development and testing of the <a href="https://www.w3.org/2002/ws/databinding/edcopy/report/all.html">W3C XML Schema Patterns for Databinding Interoperability</a>, and continues by contributing to the development of <a href="https://www.oasis-open.org">OASIS open standards</a> in partnership with leading IT companies.</p>
<h1><a class="anchor" id="tocpp"></a>
Mapping WSDL and XML schemas to C/C++                                   </h1>
<p>To convert WSDL and XML schemas (XSD files) to code, use the wsdl2h command to generate the data binding interface code that is saved to a special gSOAP header file: </p><pre class="fragment">wsdl2h [options] -o file.h ... XSD and WSDL files ...
</pre><p>This command converts WSDL and XSD files to C++ (or pure C with wsdl2h option <code>-c</code>) and saves the data binding interface to a gSOAP header file <code>file.h</code> that uses familar C/C++ syntax extended with <code>//gsoap</code> <a href="#directives">directives</a> and annotations. Notational conventions are used in the data binding interface to declare serializable C/C++ types and functions for Web service operations.</p>
<p>The WSDL 1.1/2.0, SOAP 1.1/1.2, and XSD 1.0/1.1 standards are supported by the gSOAP tools. In addition, the most popular WS specifications are also supported, including WS-Addressing, WS-ReliableMessaging, WS-Discovery, WS-Security, WS-Policy, WS-SecurityPolicy, and WS-SecureConversation.</p>
<p>This document focusses on XML data bindings. XML data bindings for C/C++ bind XML schema types to C/C++ types. So integers in XML are bound to C integers, strings in XML are bound to C or C++ strings, complex types in XML are bound to C structs or C++ classes, and so on.</p>
<p>A data binding is dual. Either you start with WSDLs and/or XML schemas that are mapped to equivalent C/C++ types, or you start with C/C++ types that are mapped to XSD types. Either way, the end result is that you can serialize C/C++ types in XML such that your XML is an instance of XML schema(s) and is validated against these schema(s).</p>
<p>This covers all of the following standard XSD components with their optional attributes and properties:</p>
<table class="doxtable">
<tr>
<th>XSD Component </th><th>Attributes and Properties  </th></tr>
<tr>
<td>schema </td><td>targetNamespace, version, elementFormDefault, attributeFormDefault, defaultAttributes </td></tr>
<tr>
<td>attribute </td><td>name, ref, type, use, default, fixed, form, targetNamespace, wsdl:arrayType </td></tr>
<tr>
<td>element </td><td>name, ref, type, default, fixed, form, nillable, abstract, substitutionGroup, minOccurs, maxOccurs, targetNamespace </td></tr>
<tr>
<td>simpleType </td><td>name </td></tr>
<tr>
<td>complexType </td><td>name, abstract, mixed, defaultAttributesApply </td></tr>
<tr>
<td>all </td><td></td></tr>
<tr>
<td>choice </td><td>minOccurs, maxOccurs </td></tr>
<tr>
<td>sequence </td><td>minOccurs, maxOccurs </td></tr>
<tr>
<td>group </td><td>name, ref, minOccurs, maxOccurs </td></tr>
<tr>
<td>attributeGroup </td><td>name, ref </td></tr>
<tr>
<td>any </td><td>minOccurs, maxOccurs </td></tr>
<tr>
<td>anyAttribute </td><td></td></tr>
</table>
<p>And also the following standard XSD directives are covered:</p>
<table class="doxtable">
<tr>
<th>Directive </th><th>Description  </th></tr>
<tr>
<td>import </td><td>Imports a schema into the importing schema for referencing </td></tr>
<tr>
<td>include </td><td>Include schema component definitions into a schema </td></tr>
<tr>
<td>override </td><td>Override by replacing schema component definitions </td></tr>
<tr>
<td>redefine </td><td>Extend or restrict schema component definitions </td></tr>
<tr>
<td>annotation </td><td>Annotates a component </td></tr>
</table>
<p>The XSD facets and their mappings to C/C++ are:</p>
<table class="doxtable">
<tr>
<th>XSD Facet </th><th>Maps to  </th></tr>
<tr>
<td>enumeration </td><td><code>enum</code> </td></tr>
<tr>
<td>simpleContent </td><td>class/struct wrapper with <code>__item</code> member </td></tr>
<tr>
<td>complexContent </td><td>class/struct </td></tr>
<tr>
<td>list </td><td><code>enum*</code> bitmask (<code>enum*</code> enumerates up to 64 bit masks) </td></tr>
<tr>
<td>extension </td><td>class/struct inheritance/extension </td></tr>
<tr>
<td>restriction </td><td><code>typedef</code> and class/struct inheritance/redeclaration </td></tr>
<tr>
<td>length </td><td><code>typedef</code> with restricted content length annotation </td></tr>
<tr>
<td>minLength </td><td><code>typedef</code> with restricted content length annotation </td></tr>
<tr>
<td>maxLength </td><td><code>typedef</code> with restricted content length annotation </td></tr>
<tr>
<td>minInclusive </td><td><code>typedef</code> with numerical value range restriction annotation </td></tr>
<tr>
<td>maxInclusive </td><td><code>typedef</code> with numerical value range restriction annotation </td></tr>
<tr>
<td>minExclusive </td><td><code>typedef</code> with numerical value range restriction annotation </td></tr>
<tr>
<td>maxExclusive </td><td><code>typedef</code> with numerical value range restriction annotation </td></tr>
<tr>
<td>precision </td><td><code>typedef</code> with pattern annotation (pattern used for output, but input is not validated) </td></tr>
<tr>
<td>scale </td><td><code>typedef</code> with pattern annotation (pattern used for output, but input is not validated) </td></tr>
<tr>
<td>totalDigits </td><td><code>typedef</code> with pattern annotation (pattern used for output, but input is not validated) </td></tr>
<tr>
<td>fractionDigits </td><td><code>typedef</code> with pattern annotation (pattern used for output, but input is not validated) </td></tr>
<tr>
<td>pattern </td><td><code>typedef</code> with pattern annotation (define <code>soap::fsvalidate</code> callback to validate patterns) </td></tr>
<tr>
<td>union </td><td>string with union of values </td></tr>
</table>
<p>All primitive XSD types are supported, including but not limited to the following XSD types:</p>
<table class="doxtable">
<tr>
<th>XSD Type </th><th>Maps to  </th></tr>
<tr>
<td>any/anyType </td><td><code>_XML</code> string with literal XML content (or enable DOM with wsdl2h option <code>-d</code>) </td></tr>
<tr>
<td>anyURI </td><td>string (i.e. <code>char*</code>, <code>wchar_t*</code>, <code>std::string</code>, <code>std::wstring</code>) </td></tr>
<tr>
<td>string </td><td>string (i.e. <code>char*</code>, <code>wchar_t*</code>, <code>std::string</code>, <code>std::wstring</code>) </td></tr>
<tr>
<td>boolean </td><td><code>bool</code> (C++) or <code>enum xsd__boolean</code> (C) </td></tr>
<tr>
<td>byte </td><td><code>char</code> (i.e. <code>int8_t</code>) </td></tr>
<tr>
<td>short </td><td><code>short</code> (i.e. <code>int16_t</code>) </td></tr>
<tr>
<td>int </td><td><code>int</code> (i.e. <code>int32_t</code>) </td></tr>
<tr>
<td>long </td><td><code>LONG64</code> (i.e. <code>long long</code> and <code>int64_t</code>) </td></tr>
<tr>
<td>unsignedByte </td><td><code>unsigned char</code> (i.e. <code>uint8_t</code>) </td></tr>
<tr>
<td>unsignedShort </td><td><code>unsigned short</code> (i.e. <code>uint16_t</code>) </td></tr>
<tr>
<td>unsignedInt </td><td><code>unsigned int</code> (i.e. <code>uint32_t</code>) </td></tr>
<tr>
<td>unsignedLong </td><td><code>ULONG64</code> (i.e. <code>unsigned long long</code> and <code>uint64_t</code>) </td></tr>
<tr>
<td>float </td><td><code>float</code> </td></tr>
<tr>
<td>double </td><td><code>double</code> </td></tr>
<tr>
<td>integer </td><td>string or <code>#import "custom/int128.h"</code> to use 128 bit <code>xsd__integer</code> </td></tr>
<tr>
<td>decimal </td><td>string or <code>#import "custom/long_double.h"</code> to use <code>long double</code> </td></tr>
<tr>
<td>precisionDecimal </td><td>string </td></tr>
<tr>
<td>duration </td><td>string or <code>#import "custom/duration.h"</code> to use 64 bit <code>xsd__duration</code> </td></tr>
<tr>
<td>dateTime </td><td><code>time_t</code> or <code>#import "custom/struct_tm.h"</code> to use <code>struct tm</code> for <code>xsd__dateTime</code> </td></tr>
<tr>
<td>time </td><td>string or <code>#import "custom/long_time.h"</code> to use 64 bit <code>xsd__time</code> </td></tr>
<tr>
<td>date </td><td>string or <code>#import "custom/struct_tm_date.h"</code> to use <code>struct tm</code> for <code>xsd__date</code> </td></tr>
<tr>
<td>hexBinary </td><td>special class/struct <code>xsd__hexBinary</code> </td></tr>
<tr>
<td>base64Bianry </td><td>special class/struct <code>xsd__base64Binary</code> </td></tr>
<tr>
<td>QName </td><td><code>_QName</code> string (URI normalization rules are applied) </td></tr>
</table>
<p>All other primitive XSD types not listed above are mapped to strings, by wsdl2h generating a typedef to string for these types. For example, xsd:token is bound to a C++ or C string:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string  xsd__token; <span class="comment">// C++</span></div>
<div class="line"><span class="keyword">typedef</span> <span class="keywordtype">char</span>        *xsd__token; <span class="comment">// C (wsdl2h option -c)</span></div>
</div><!-- fragment --><p>This associates a compatible value space to the type with the appropriate XSD type name used by the soapcpp2-generated serializers.</p>
<p>It is possible to remap types by adding the appropriate mapping rules to <code>typemap.dat</code> as we will explain in more detail in the next section.</p>
<p>Imported custom serializers are intended to extend the C/C++ type bindings when the default binding to string is not satisfactory to your taste and if the target platform supports these C/C++ types. To add custom serializers to <code>typemap.dat</code> for wsdl2h, see <a href="#custom">adding custom serializers</a> below.</p>
<h1><a class="anchor" id="typemap"></a>
Using typemap.dat to customize data bindings                          </h1>
<p>Use a <code>typemap.dat</code> file to redefine namespace prefixes and to customize type bindings for the the generated header files produced by the wsdl2h tool. The <code>typemap.dat</code> is the default file processed by wsdl2h. Use wsdl2h option <code>-t</code> to specify a different file.</p>
<p>Declarations in <code>typemap.dat</code> can be broken up over multiple lines by continuing on the next line by ending each line to be continued with a backslash <code>\</code>. The limit is 4095 characters per line, whether the line is broken up or not.</p>
<h2><a class="anchor" id="typemap1"></a>
XML namespace bindings                                               </h2>
<p>The wsdl2h tool generates C/C++ type declarations that use <code>ns1</code>, <code>ns2</code>, etc. as schema-binding URI prefixes. These default prefixes are generated somewhat arbitrarily for each schema targetNamespace URI, meaning that their ordering may change depending on the WSDL and XSD order of processing with wsdl2h.</p>
<p>Therefore, it is <b>strongly recommended</b> to declare your own prefix for each schema URI in <code>typemap.dat</code> to reduce maintaince effort of your code. This is more robust when anticipating possible changes of the schema(s) and/or the binding URI(s) and/or the tooling algorithms.</p>
<p>The first and foremost important thing to do is to define prefix-URI bindings for our C/C++ code by adding the following line(s) to our <code>typemap.dat</code> or make a copy of this file and add the line(s) that bind our choice of prefix name to each URI: </p><pre class="fragment">prefix = "URI"
</pre><p>For example: </p><pre class="fragment">g = "urn:graph"
</pre><p>This produces <code>g__name</code> C/C++ type names that are bound to the "urn:graph" schema by association of <code>g</code> to the generated C/C++ types.</p>
<p>This means that <code>&lt;g:name xmlns:g="urn:graph"&gt;</code> is parsed as an instance of a <code>g__name</code> C/C++ type. Also <code>&lt;x:name xmlns:x="urn:graph"&gt;</code> parses as an instance of <code>g__name</code>, because the prefix <code>x</code> has the same URI value <code>urn:graph</code>. Prefixes in XML have local scopes (like variables in a block).</p>
<p>The first run of wsdl2h will reveal the URIs, so you do not need to search WSDLs and XSD files for all of the target namespaces. Just copy them from the generated header file after the first run into <code>typemap.dat</code> for editing.</p>
<h2><a class="anchor" id="typemap2"></a>
XSD type bindings                                                    </h2>
<p>Custom C/C++ type bindings can be declared in <code>typemap.dat</code> to associate C/C++ types with specific schema types. These type bindings have four parts: </p><pre class="fragment">prefix__type = declaration | use | ptruse
</pre><p>where</p>
<ul>
<li><code>prefix__type</code> is the schema type to be customized (the <code>prefix__type</code> name uses the common double underscore naming convention);</li>
<li><code>declaration</code> declares the C/C++ type in the wsdl2h-generated header file. This part can be empty if no explicit declaration is needed;</li>
<li><code>use</code> is an optional part that specifies how the C/C++ type is used in the code. When omitted, it is the same as <code>prefix__type</code>;</li>
<li><code>ptruse</code> is an optional part that specifies how the type is used as a pointer type. By default it is the <code>use</code> type name with a <code>*</code> or C++11 <code>std::shared_ptr&lt;&gt;</code> when enabled (see further below).</li>
</ul>
<p>For example, to map xsd:duration to a <code>long long</code> (<code>LONG64</code>) type that holds millisecond duration values, we can use the custom serializer declared in <code>custom/duration.h</code> by adding the following line to <code>typemap.dat</code>: </p><pre class="fragment">xsd__duration = #import "custom/duration.h"
</pre><p>Here, we omitted the second field, because <code>xsd__duration</code> is the name that wsdl2h uses to identify and use this type for our code. The third field is omitted to let wsdl2h use <code>xsd__duration *</code> for pointers or <code>std::shared_ptr&lt;xsd__duration&gt;</code> if smart pointers are enabled.</p>
<p>To map xsd:string to <code>wchar_t*</code> wide strings: </p><pre class="fragment">xsd__string = | wchar_t* | wchar_t*
</pre><p>Note that the first field is empty, because <code>wchar_t</code> is a C type and does not need to be declared. A <code>ptruse</code> field is given so that we do not end up generating the wrong pointer types, such as <code>wchar_t**</code> and <code>std::shared_ptr&lt;wchar_t&gt;</code>.</p>
<p>When the auto-generated declaration should be preserved but the <code>use</code> or <code>ptruse</code> fields replaced, then we use an ellipsis for the declaration part: </p><pre class="fragment">prefix__type = ... | use | ptruse
</pre><p>This is useful to map schema polymorphic types to C types for example, where we need to be able to both handle a base type and its extensions as per schema extensibility. Say we have a base type called ns:base that is extended, then we can remap this to a C type that permits referening the extended types via a <code>void*</code> as follows: </p><pre class="fragment">ns__base = ... | int __type_base; void*
</pre><p>such that <code>__type_base</code> and <code>void*</code> will be used to (de)serialize any data type, including base and its derived types. The <code>__type_base</code> integer is set to a <code>SOAP_TYPE_T</code> value to indicate what type of data the <code>void*</code> pointer points to.</p>
<h2><a class="anchor" id="custom"></a>
Custom serializers for XSD types                                       </h2>
<p>In the previous part we saw how a custom serializer is used to bind xsd:duration to a <code>long long</code> (<code>LONG64</code> or <code>int64_t</code>) type to store millisecond duration values: </p><pre class="fragment">xsd__duration = #import "custom/duration.h"
</pre><p>The <code>xsd__duration</code> type is an alias of <code>long long</code> (<code>LONG64</code> or <code>int64_t</code>).</p>
<p>While wsdl2h will use this binding declared in <code>typemap.dat</code> automatically, you will also need to compile <code>custom/duration.c</code>. Each custom serializer has a header file and an implementation file written in C. You can compile these in C++ (rename files to <code>.cpp</code> if needed).</p>
<p>We will discuss the custom serializers that are available to you.</p>
<h3><a class="anchor" id="custom-1"></a>
xsd:integer</h3>
<p>The wsdl2h tool maps xsd:integer to a string by default. To map xsd:integer to the 128 bit big int type <code>__int128_t</code>: </p><pre class="fragment">xsd__integer = #import "custom/int128.h"
</pre><p>The <code>xsd__integer</code> type is an alias of <code>__int128_t</code>.</p>
<dl class="section warning"><dt>Warning</dt><dd>Beware that the xsd:integer value space of integers is in principle unbounded and values can be of arbitrary length. A value range fault <code>SOAP_TYPE</code> (value exceeds native representation) or <code>SOAP_LENGTH</code> (value exceeds range bounds) will be thrown by the deserializer if the value is out of range.</dd></dl>
<p>Other XSD integer types that are restrictions of xsd:integer, are xsd:nonNegativeInteger and xsd:nonPositiveInteger, which are further restricted by xsd:positiveInteger and xsd:negativeInteger. To bind these types to <code>__int128_t</code> we should also add the following definitions to <code>typemap.dat</code>: </p><pre class="fragment">xsd__nonNegativeInteger = typedef xsd__integer xsd__nonNegativeInteger 0 :    ;
xsd__nonPositiveInteger = typedef xsd__integer xsd__nonPositiveInteger   : 0  ;
xsd__positiveInteger    = typedef xsd__integer xsd__positiveInteger    1 :    ;
xsd__negativeInteger    = typedef xsd__integer xsd__negativeInteger      : -1 ;
</pre><dl class="section note"><dt>Note</dt><dd>If <code>__int128_t</code> 128 bit integers are not supported on your platform and if it is certain that xsd:integer values are within 64 bit value bounds for your application's use, then you can map this type to <code>LONG64</code>: <pre class="fragment">xsd__integer = typedef LONG64 xsd__integer;
</pre></dd>
<dd>
Again, a value range fault <code>SOAP_TYPE</code> or <code>SOAP_LENGTH</code> will be thrown by the deserializer if the value is out of range.</dd></dl>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd5">numerical types</a>.</dd></dl>
<h3><a class="anchor" id="custom-2"></a>
xsd:decimal</h3>
<p>The wsdl2h tool maps xsd:decimal to a string by default. To map xsd:decimal to extended precision floating point: </p><pre class="fragment">xsd__decimal = #import "custom/long_double.h" | long double
</pre><p>By contrast to all other custom serializers, this serializer enables <code>long double</code> natively without requiring a new binding name (<code>xsd__decimal</code> is NOT defined).</p>
<p>If your system supports <code>&lt;quadmath.h&gt;</code> quadruple precision floating point <code>__float128</code>, you can map xsd:decimal to <code>xsd__decimal</code> that is an alias of <code>__float128</code>: </p><pre class="fragment">xsd__decimal = #import "custom/float128.h"
</pre><dl class="section warning"><dt>Warning</dt><dd>Beware that xsd:decimal is in principle a decimal value with arbitraty lengths. A value range fault <code>SOAP_TYPE</code> will be thrown by the deserializer if the value is out of range.</dd></dl>
<p>In the XML payload the special values <code>INF</code>, <code>-INF</code>, <code>NaN</code> represent plus or minus infinity and not-a-number, respectively.</p>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd5">numerical types</a>.</dd></dl>
<h3><a class="anchor" id="custom-3"></a>
xsd:dateTime</h3>
<p>The wsdl2h tool maps xsd:dateTime to <code>time_t</code> by default.</p>
<p>The trouble with <code>time_t</code> when represented as 32 bit <code>long</code> integers is that it is limited to dates between 1970 and 2038. A 64 bit <code>time_t</code> is safe to use if the target platform supports it, but lack of 64 bit <code>time_t</code> portability may still cause date range issues.</p>
<p>For this reason <code>struct tm</code> should be used to represent wider date ranges. This custom serializer avoids using date and time information in <code>time_t</code>. You get the raw date and time information. You only lose the day of the week information. It is always Sunday (<code>tm_wday=0</code>).</p>
<p>To map xsd:dateTime to <code>xsd__dateTime</code> which is an alias of <code>struct tm</code>: </p><pre class="fragment">xsd__dateTime = #import "custom/struct_tm.h"
</pre><p>If the limited date range of <code>time_t</code> is not a problem but you want to increase the time precision with fractional seconds, then we suggest to map xsd:dateTime to <code>struct timeval</code>: </p><pre class="fragment">xsd__dateTime = #import "custom/struct_timeval.h"
</pre><p>If the limited date range of <code>time_t</code> is not a problem but you want to use the C++11 time point type <code>std::chrono::system_clock::time_point</code> (which internally uses <code>time_t</code>): </p><pre class="fragment">xsd__dateTime = #import "custom/chrono_time_point.h"
</pre><p>Again, we should make sure that the dates will not exceed the date range when using the default <code>time_t</code> binding for xsd:dateTime or when binding xsd:dateTime to <code>struct timeval</code> or to <code>std::chrono::system_clock::time_point</code>. These are safe to use in applications that use xsd:dateTime to record date stamps within a given window. Otherwise, we recommend the <code>struct tm</code> custom serializer. You could even map xsd:dateTime to a plain string (use <code>char*</code> with C and <code>std::string</code> with C++). For example: </p><pre class="fragment">xsd__dateTime = | char*
</pre><dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd7">date and time types</a>.</dd></dl>
<h3><a class="anchor" id="custom-4"></a>
xsd:date</h3>
<p>The wsdl2h tool maps xsd:date to a string by default. We can map xsd:date to <code>struct tm</code>: </p><pre class="fragment">xsd__date = #import "custom/struct_tm_date.h"
</pre><p>The <code>xsd__date</code> type is an alias of <code>struct tm</code>. The serializer ignores the time part and the deserializer only populates the date part of the struct, setting the time to 00:00:00. There is no unreasonable limit on the date range because the year field is stored as an integer (<code>int</code>).</p>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd7">date and time types</a>.</dd></dl>
<h3><a class="anchor" id="custom-5"></a>
xsd:time</h3>
<p>The wsdl2h tool maps xsd:time to a string by default. We can map xsd:time to an <code>unsigned long long</code> (<code>ULONG64</code> or <code>uint64_t</code>) integer with microsecond time precision: </p><pre class="fragment">xsd__time = #import "custom/long_time.h"
</pre><p>This type represents 00:00:00.000000 to 23:59:59.999999, from <code>0</code> to an upper bound of <code>86399999999</code>. A microsecond resolution means that a 1 second increment requires an increment of 1000000 in the integer value. The serializer adds a UTC time zone.</p>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd7">date and time types</a>.</dd></dl>
<h3><a class="anchor" id="custom-6"></a>
xsd:duration</h3>
<p>The wsdl2h tool maps xsd:duration to a string by default, unless xsd:duration is mapped to a <code>long long</code> (<code>LONG64</code> or <code>int64_t</code>) type with with millisecond (ms) time duration precision: </p><pre class="fragment">xsd__duration = #import "custom/duration.h"
</pre><p>The <code>xsd__duration</code> type is a 64 bit signed integer that can represent 106,751,991,167 days forwards (positive) and backwards (negative) in time in increments of 1 ms (1/1000 of a second).</p>
<p>Rescaling of the duration value by may be needed when adding the duration value to a <code>time_t</code> value, because <code>time_t</code> may or may not have a seconds resolution, depending on the platform and possible changes to <code>time_t</code>.</p>
<p>Rescaling is done automatically when you add a C++11 <code>std::chrono::nanoseconds</code> value to a <code>std::chrono::system_clock::time_point</code> value. To use <code>std::chrono::nanoseconds</code> as xsd:duration: </p><pre class="fragment">xsd__duration = #import "custom/chrono_duration.h"
</pre><p>This type can represent 384,307,168 days (2^63 nanoseconds) forwards and backwards in time in increments of 1 ns (1/1,000,000,000 of a second).</p>
<p>Certain observations with respect to receiving durations in years and months apply to both of these serializer decoders for xsd:duration.</p>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd8">time duration types</a>.</dd></dl>
<h2><a class="anchor" id="typemap3"></a>
Class/struct member additions                                        </h2>
<p>All generated classes and structs can be augmented with additional members such as methods, constructors and destructors, and private members: </p><pre class="fragment">prefix__type = $ member-declaration
</pre><p>For example, we can add method declarations and private members to a class, say <code>ns__record</code> as follows: </p><pre class="fragment">ns__record = $ ns__record(const ns__record &amp;); // copy constructor
ns__record = $ void print();                   // a print method
ns__record = $ private: int status;            // a private member
</pre><p>Note that method declarations cannot include any code, because soapcpp2's input permits only type declarations, not code.</p>
<h2><a class="anchor" id="typemap4"></a>
Replacing XSD types by equivalent alternatives                       </h2>
<p>Type replacements can be given to replace one type entirely with another given type: </p><pre class="fragment">prefix__type1 == prefix__type2
</pre><p>This replaces all <code>prefix__type1</code> by <code>prefix__type2</code> in the wsdl2h output.</p>
<dl class="section warning"><dt>Warning</dt><dd>Do not agressively replace types, because this can cause XML validation to fail when a value-type mismatch is encountered in the XML input. Therefore, only replace similar types with other similar types that are wider (e.g. <code>short</code> by <code>int</code> and <code>float</code> by <code>double</code>).</dd></dl>
<h2><a class="anchor" id="typemap5"></a>
The built-in typemap.dat variables $CONTAINER and $POINTER           </h2>
<p>The <code>typemap.dat</code> <code>$CONTAINER</code> variable defines the container to emit in the generated declarations, which is <code>std::vector</code> by default. For example, to emit <code>std::list</code> as the container in the wsdl2h-generated declarations: </p><pre class="fragment">$CONTAINER = std::list
</pre><p>The <code>typemap.dat</code> <code>$POINTER</code> variable defines the smart pointer to emit in the generated declarations, which replaces the use of <code>*</code> pointers. For example: </p><pre class="fragment">$POINTER = std::shared_ptr
</pre><p>Not all pointers in the generated output can be replaced by smart pointers. Regular pointers are still used as union members and for pointers to arrays of objects.</p>
<dl class="section note"><dt>Note</dt><dd>The standard smart pointer <code>std::shared_ptr</code> is generally safe to use. Other smart pointers such as <code>std::unique_ptr</code> and <code>std::auto_ptr</code> may cause compile-time errors when classes have smart pointer members but no copy constructor (a default copy constructor). A copy constructor is required for non-shared smart pointer copying or swapping.</dd></dl>
<p>Alternatives to <code>std::shared_ptr</code> of the form <code>NAMESPACE::shared_ptr</code> can be assigned to <code>$POINTER</code> when the namespace <code>NAMESPACE</code> also implements <code>NAMESPACE::make_shared</code> and when the shared pointer class provides <code>reset()</code> and<code>get()</code> methods and the dereference operator. For example Boost <code>boost::shared_ptr</code>: </p><pre class="fragment">[
#include &lt;boost/shared_ptr.hpp&gt;
]
$POINTER = boost::shared_ptr
</pre><p>The user-defined content between <code>[</code> and <code>]</code> ensures that we include the Boost header files that are needed to support <code>boost::shared_ptr</code> and <code>boost::make_shared</code>.</p>
<h2><a class="anchor" id="typemap6"></a>
User-defined content                                                 </h2>
<p>Any other content to be generated by wsdl2h can be included in <code>typemap.dat</code> by enclosing it within brackets <code>[</code> and <code>]</code> anywhere in the <code>typemap.dat</code> file. Each of the two brackets MUST appear at the start of a new line.</p>
<p>For example, we can add an <code>#import "wsa5.h"</code> to the wsdl2h-generated output as follows: </p><pre class="fragment">[
#import "import/wsa5.h"
]
</pre><p>which emits the <code>#import "import/wsa5.h"</code> literally at the start of the wsdl2h-generated header file.</p>
<h1><a class="anchor" id="toxsd"></a>
Mapping C/C++ to XML schema                                             </h1>
<p>The soapcpp2 command generates the data binding implementation code from a data binding interface <code>file.h</code>: </p><pre class="fragment">soapcpp2 [options] file.h
</pre><p>where <code>file.h</code> is a gSOAP header file that declares the XML data binding interface. The <code>file.h</code> is typically generated by wsdl2h, but you can also declare one yourself. If so, add <code>//gsaop</code> <a href="#directives">directives</a> and declare in this file all our C/C++ types you want to serialize in XML.</p>
<p>You can also declare functions that will be converted to Web service operations by soapcpp2. Global function declarations define service operations, which are of the form:</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> prefix__name(arg1, arg2, ..., argn, result);</div>
</div><!-- fragment --><p>where <code>arg1</code>, <code>arg2</code>, ..., <code>argn</code> are formal argument declarations of the input and <code>result</code> is a formal argument for the output, which must be a pointer or reference to the result object to be populated. More information can be found in the <a href="http://www.genivia.com/doc/soapdoc2.html">gSOAP user guide.</a></p>
<h2><a class="anchor" id="toxsd1"></a>
Overview of serializable C/C++ types                                   </h2>
<p>The following C/C++ types are supported by soapcpp2 and mapped to XSD types and constructs. See the subsections below for more details or follow the links.</p>
<h3>List of Boolean types</h3>
<table class="doxtable">
<tr>
<th>Boolean Type </th><th>Notes  </th></tr>
<tr>
<td><code>bool</code> </td><td>C++ bool </td></tr>
<tr>
<td><code>enum xsd__boolean</code> </td><td>C alternative to C++ <code>bool</code> with <code>false_</code> and <code>true_</code> </td></tr>
</table>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd3">C++ bool and C alternative</a>.</dd></dl>
<h3>List of enumeration and bitmask types</h3>
<table class="doxtable">
<tr>
<th>Enumeration Type </th><th>Notes  </th></tr>
<tr>
<td><code>enum</code> </td><td>enumeration </td></tr>
<tr>
<td><code>enum class</code> </td><td>C++11 scoped enumeration (soapcpp2 <code>-c++11</code>) </td></tr>
<tr>
<td><code>enum*</code> </td><td>a bitmask that enumerates values 1, 2, 4, 8, ... </td></tr>
<tr>
<td><code>enum* class</code> </td><td>C++11 scoped enumeration bitmask (soapcpp2 <code>-c++11</code>) </td></tr>
</table>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd4">enumerations and bitmasks</a>.</dd></dl>
<h3>List of numerical types</h3>
<table class="doxtable">
<tr>
<th>Numerical Type </th><th>Notes  </th></tr>
<tr>
<td><code>char</code> </td><td>byte </td></tr>
<tr>
<td><code>short</code> </td><td>16 bit integer </td></tr>
<tr>
<td><code>int</code> </td><td>32 bit integer </td></tr>
<tr>
<td><code>long</code> </td><td>32 bit integer </td></tr>
<tr>
<td><code>LONG64</code> </td><td>64 bit integer </td></tr>
<tr>
<td><code>xsd__integer</code> </td><td>128 bit integer, use <code>#import "custom/int128.h"</code> </td></tr>
<tr>
<td><code>long long</code> </td><td>same as <code>LONG64</code> </td></tr>
<tr>
<td><code>unsigned char</code> </td><td>unsigned byte </td></tr>
<tr>
<td><code>unsigned short</code> </td><td>unsigned 16 bit integer </td></tr>
<tr>
<td><code>unsigned int</code> </td><td>unsigned 32 bit integer </td></tr>
<tr>
<td><code>unsigned long</code> </td><td>unsigned 32 bit integer </td></tr>
<tr>
<td><code>ULONG64</code> </td><td>unsigned 64 bit integer </td></tr>
<tr>
<td><code>unsigned long long</code> </td><td>same as <code>ULONG64</code> </td></tr>
<tr>
<td><code>int8_t</code> </td><td>same as <code>char</code> </td></tr>
<tr>
<td><code>int16_t</code> </td><td>same as <code>short</code> </td></tr>
<tr>
<td><code>int32_t</code> </td><td>same as <code>int</code> </td></tr>
<tr>
<td><code>int64_t</code> </td><td>same as <code>LONG64</code> </td></tr>
<tr>
<td><code>uint8_t</code> </td><td>same as <code>unsigned char</code> </td></tr>
<tr>
<td><code>uint16_t</code> </td><td>same as <code>unsigned short</code> </td></tr>
<tr>
<td><code>uint32_t</code> </td><td>same as <code>unsigned int</code> </td></tr>
<tr>
<td><code>uint64_t</code> </td><td>same as <code>ULONG64</code> </td></tr>
<tr>
<td><code>size_t</code> </td><td>transient type (not serializable) </td></tr>
<tr>
<td><code>float</code> </td><td>32 bit float </td></tr>
<tr>
<td><code>double</code> </td><td>64 bit float </td></tr>
<tr>
<td><code>long double</code> </td><td>extended precision float, use <code>#import "custom/long_double.h"</code> </td></tr>
<tr>
<td><code>xsd__decimal</code> </td><td><code>&lt;quadmath.h&gt;</code> 128 bit quadruple precision float, use <code>#import "custom/float128.h"</code> </td></tr>
<tr>
<td><code>typedef</code> </td><td>declares a type name, with optional value range and string length bounds </td></tr>
</table>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd5">numerical types</a>.</dd></dl>
<h3>List of string types</h3>
<table class="doxtable">
<tr>
<th>String Type </th><th>Notes  </th></tr>
<tr>
<td><code>char*</code> </td><td>string (may contain UTF-8 with flag <code>SOAP_C_UTFSTRING</code>) </td></tr>
<tr>
<td><code>wchar_t*</code> </td><td>wide string </td></tr>
<tr>
<td><code>std::string</code> </td><td>C++ string (may contain UTF-8 with flag <code>SOAP_C_UTFSTRING</code>) </td></tr>
<tr>
<td><code>std::wstring</code> </td><td>C++ wide string </td></tr>
<tr>
<td><code>char[N]</code> </td><td>fixed-size string, requires soapcpp2 option <code>-b</code> </td></tr>
<tr>
<td><code>_QName</code> </td><td>normalized QName content </td></tr>
<tr>
<td><code>_XML</code> </td><td>literal XML string content with wide characters in UTF-8 </td></tr>
<tr>
<td><code>typedef</code> </td><td>declares a new string type name, may restrict string length </td></tr>
</table>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd6">string types</a>.</dd></dl>
<h3>List of date and time types</h3>
<table class="doxtable">
<tr>
<th>Date and Time Type </th><th>Notes  </th></tr>
<tr>
<td><code>time_t</code> </td><td>date and time point since epoch </td></tr>
<tr>
<td><code>struct tm</code> </td><td>date and time point, use <code>#import "custom/struct_tm.h"</code> </td></tr>
<tr>
<td><code>struct tm</code> </td><td>date point, use <code>#import "custom/struct_tm_date.h"</code> </td></tr>
<tr>
<td><code>struct timeval</code> </td><td>date and time point, use <code>#import "custom/struct_timeval.h"</code> </td></tr>
<tr>
<td><code>unsigned long long</code> </td><td>time point in microseconds, use <code>#import "custom/long_time.h"</code> </td></tr>
<tr>
<td><code>std::chrono::system_clock::time_point</code> </td><td>date and time point, use <code>#import "custom/chrono_time_point.h"</code> </td></tr>
</table>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd7">date and time types</a>.</dd></dl>
<h3>List of time duration types</h3>
<table class="doxtable">
<tr>
<th>Time Duration Type </th><th>Notes  </th></tr>
<tr>
<td><code>long long</code> </td><td>duration in milliseconds, use <code>#import "custom/duration.h"</code> </td></tr>
<tr>
<td><code>std::chrono::nanoseconds</code> </td><td>duration in nanoseconds, use <code>#import "custom/chrono_duration.h"</code> </td></tr>
</table>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd8">time duration types</a>.</dd></dl>
<h3>List of classes and structs</h3>
<table class="doxtable">
<tr>
<th>Classes, Structs, and Members </th><th>Notes  </th></tr>
<tr>
<td><code>class</code> </td><td>C++ class with single inheritance only </td></tr>
<tr>
<td><code>struct</code> </td><td>C struct or C++ struct without inheritance </td></tr>
<tr>
<td><code>std::shared_ptr&lt;T&gt;</code> </td><td>C++11 smart shared pointer </td></tr>
<tr>
<td><code>std::unique_ptr&lt;T&gt;</code> </td><td>C++11 smart pointer </td></tr>
<tr>
<td><code>std::auto_ptr&lt;T&gt;</code> </td><td>C++ smart pointer </td></tr>
<tr>
<td><code>std::deque&lt;T&gt;</code> </td><td>use <code>#import "import/stldeque.h"</code> </td></tr>
<tr>
<td><code>std::list&lt;T&gt;</code> </td><td>use <code>#import "import/stllist.h"</code> </td></tr>
<tr>
<td><code>std::vector&lt;T&gt;</code> </td><td>use <code>#import "import/stlvector.h"</code> </td></tr>
<tr>
<td><code>std::set&lt;T&gt;</code> </td><td>use <code>#import "import/stlset.h"</code> </td></tr>
<tr>
<td><code>template&lt;T&gt; class</code> </td><td>a container with <code>begin()</code>, <code>end()</code>, <code>size()</code>, <code>clear()</code>, and <code>insert()</code> methods </td></tr>
<tr>
<td><code>T*</code> </td><td>data member: pointer to data of type <code>T</code> or points to array of <code>T</code> of size <code>__size</code> </td></tr>
<tr>
<td><code>T[N]</code> </td><td>data member: fixed-size array of type <code>T</code> </td></tr>
<tr>
<td><code>union</code> </td><td>data member: requires a variant selector member <code>__union</code> </td></tr>
<tr>
<td><code>void*</code> </td><td>data member: requires a <code>__type</code> member to indicate the type of object pointed to </td></tr>
</table>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd9">classes and structs</a>.</dd></dl>
<h3>List of special classes and structs</h3>
<table class="doxtable">
<tr>
<th>Special Classes and Structs </th><th>Notes  </th></tr>
<tr>
<td>Special Array class/struct </td><td>single and multidimensional SOAP Arrays </td></tr>
<tr>
<td>Special Wrapper class/struct </td><td>complexTypes with simpleContent, wraps <code>__item</code> member </td></tr>
<tr>
<td><code>xsd__hexBinary</code> </td><td>binary content </td></tr>
<tr>
<td><code>xsd__base64Binary</code> </td><td>binary content and optional MIME/MTOM attachments </td></tr>
<tr>
<td><code>xsd__anyType</code> </td><td>DOM elements, use <code>#import "dom.h"</code> </td></tr>
<tr>
<td><code>@xsd__anyAttribute</code> </td><td>DOM attributes, use <code>#import "dom.h"</code> </td></tr>
</table>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd10">special classes and structs</a>.</dd></dl>
<h2><a class="anchor" id="toxsd2"></a>
Colon notation versus name prefixing                                   </h2>
<p>To bind C/C++ type names to XSD types, a simple form of name prefixing is used by the gSOAP tools by prepending the XML namespace prefix to the C/C++ type name with a pair of undescrores. This also ensures that name clashes cannot occur when multiple WSDL and XSD files are converted to C/C++. Also, C++ namespaces are not sufficiently rich to capture XML schema namespaces accurately, for example when class members are associated with schema elements defined in another XML namespace and thus the XML namespace scope of the member's name is relevant, not just its type.</p>
<p>However, from a C/C++ centric point of view this can be cumbersome. Therefore, colon notation is an alternative to physically augmenting C/C++ names with prefixes.</p>
<p>For example, the following class uses colon notation to bind the <code>record</code> class to the <code>urn:types</code> schema:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns schema namespace: urn:types</span></div>
<div class="line"><span class="keyword">class </span>ns:record        <span class="comment">// binding &#39;ns:&#39; to a type name</span></div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::string name;</div>
<div class="line">  uint64_t    SSN;</div>
<div class="line">  ns:record   *spouse; <span class="comment">// using &#39;ns:&#39; with the type name</span></div>
<div class="line">  ns:record();         <span class="comment">// using &#39;ns:&#39; here too</span></div>
<div class="line">  ~ns:record();        <span class="comment">// and here</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>The colon notation is stripped away by soapcpp2 when generating the data binding implementation code for our project. So the final code just uses <code>record</code> to identify this class and its constructor/destructor.</p>
<p>When using colon notation we have to be consistent and not use colon notation mixed with prefixed forms. The name <code>ns:record</code> differs from <code>ns__record</code>, because <code>ns:record</code> is compiled to an unqualified <code>record</code> name.</p>
<p>Colon notation also facilitates overruling the elementFormDefault and attributeFormDefault declaration that is applied to local elements and attributes, when declared as members of classes, structs, and unions. For more details, see <a href="#toxsd9-6">qualified and unqualified members</a>.</p>
<h2><a class="anchor" id="toxsd3"></a>
C++ Bool and C alternatives                                            </h2>
<p>The C++ <code>bool</code> type is bound to built-in XSD type xsd:boolean.</p>
<p>The C alternative is to define an enumeration:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> xsd__boolean { false_, true_ };</div>
</div><!-- fragment --><p>or by defining an enumeration in C with pseudo-scoped enumeration constants:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> xsd__boolean { xsd__boolean__false, xsd__boolean__true };</div>
</div><!-- fragment --><p>The XML value space of these types is <code>false</code> and <code>true</code>, but also accepted are <code>0</code> and <code>1</code> values for false and true, respectively.</p>
<p>To prevent name clashes, <code>false_</code> and <code>true_</code> have an underscore. Trailing underscores are removed from the XML value space.</p>
<h2><a class="anchor" id="toxsd4"></a>
Enumerations and bitmasks                                              </h2>
<p>Enumerations are mapped to XSD simpleType enumeration restrictions of xsd:string, xsd:QName, and xsd:long.</p>
<p>Consider for example:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> ns__Color { RED, WHITE, BLUE };</div>
</div><!-- fragment --><p>which maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="Color"&gt;
  &lt;restriction base="xsd:string"&gt;
    &lt;enumeration value="RED"/&gt;
    &lt;enumeration value="WHITE"/&gt;
    &lt;enumeration value="BLUE"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>Enumeration name constants can be pseudo-scoped to prevent name clashes, because enumeration name constants have a global scope in C and C++:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> ns__Color { ns__Color__RED, ns__Color__WHITE, ns__Color__BLUE };</div>
</div><!-- fragment --><p>You can also use C++11 scoped enumerations to prevent name clashes:</p>
<div class="fragment"><div class="line"><span class="keyword">enum class</span> ns__Color : int { RED, WHITE, BLUE };</div>
</div><!-- fragment --><p>Here, the enumeration class base type <code>: int</code> is optional. In place of <code>int</code> in the example above, we can also use <code>int8_t</code>, <code>int16_t</code>, <code>int32_t</code>, or <code>int64_t</code>.</p>
<p>The XML value space of the enumertions defined above is <code>RED</code>, <code>WHITE</code>, and <code>BLUE</code>.</p>
<p>Prefix-qualified enumeration name constants are mapped to simpleType restrictions of xsd:QName, for example:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> ns__types { xsd__int, xsd__float };</div>
</div><!-- fragment --><p>which maps to a simpleType restriction of xsd:QName in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="types"&gt;
  &lt;restriction base="xsd:QName"&gt;
    &lt;enumeration value="xsd:int"/&gt;
    &lt;enumeration value="xsd:float"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>Enumeration name constants can be pseudo-numeric as follows:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> ns__Primes { _3 = 3, _5 = 5, _7 = 7, _11 = 11 };</div>
</div><!-- fragment --><p>which maps to a simpleType restriction of <code>xsd:long</code>: </p><pre class="fragment">&lt;simpleType name="Color"&gt;
  &lt;restriction base="xsd:long"&gt;
    &lt;enumeration value="3"/&gt;
    &lt;enumeration value="5"/&gt;
    &lt;enumeration value="7"/&gt;
    &lt;enumeration value="11"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>The XML value space of this type is <code>3</code>, <code>5</code>, <code>7</code>, and <code>11</code>.</p>
<p>Besides (pseudo-) scoped enumerations, another way to prevent name clashes accross enumerations is to start an enumeration name constant with one underscore or followed it by any number of underscores, which makes it unique. The leading and trailing underscores are removed from the XML value space.</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> ns__ABC { A, B, C };</div>
<div class="line"><span class="keyword">enum</span> ns__BA  { B, A };    <span class="comment">// BAD: B = 1 but B is already defined as 2</span></div>
<div class="line"><span class="keyword">enum</span> ns__BA_ { B_, A_ };  <span class="comment">// OK</span></div>
</div><!-- fragment --><p>The gSOAP soapcpp2 tool permits reusing enumeration name constants across (non-scoped) enumerations as long as these values are assigned the same constant. Therefore, the following is permitted:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> ns__Primes { _3 = 3, _5 = 5, _7 = 7, _11 = 11 };</div>
<div class="line"><span class="keyword">enum</span> ns__Throws { _1 = 1, _2 = 2, _3 = 3, _4 = 4, _5 = 5, _6 = 6 };</div>
</div><!-- fragment --><p>A bitmask type is an <code>enum*</code> "product" enumeration with a geometric, power-of-two sequence of values assigned to the enumeration constants:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span>* ns__Options { SSL3, TLS10, TLS11, TLS12 };</div>
</div><!-- fragment --><p>where the product enum assigns 1 to <code>SSL3</code>, 2 to <code>TLS10</code>, 4 to <code>TLS11</code>, and 8 to <code>TLS12</code>, which allows these enumeration constants to be used in composing bitmasks with <code>|</code> (bitwise or) <code>&amp;</code> (bitwise and), and <code>~</code> (bitwise not):</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span> ns__Options options = (<span class="keyword">enum</span> ns__Options)(SSL3 | TLS10 | TLS11 | TLS12);</div>
<div class="line"><span class="keywordflow">if</span> (options &amp; SSL3) <span class="comment">// if SSL3 is an option, warn and remove from options</span></div>
<div class="line">{</div>
<div class="line">  warning();</div>
<div class="line">  options &amp;= ~SSL3;</div>
<div class="line">}</div>
</div><!-- fragment --><p>The bitmask type maps to a simpleType list restriction of xsd:string in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="Options"&gt;
  &lt;list&gt;
    &lt;restriction base="xsd:string"&gt;
      &lt;enumeration value="SSL3"/&gt;
      &lt;enumeration value="TLS10"/&gt;
      &lt;enumeration value="TLS11"/&gt;
      &lt;enumeration value="TLS12"/&gt;
    &lt;/restriction&gt;
  &lt;/list&gt;
&lt;/simpleType&gt;
</pre><p>The XML value space of this type consists of all 16 possible subsets of the four values, represented by an XML string with space-separated values. For example, the bitmask <code>TLS10 | TLS11 | TLS12</code> equals 14 and is represented in by the XML string <code>TLS10 TLS11 TLS12</code>.</p>
<p>You can also use C++11 scoped enumerations with bitmasks:</p>
<div class="fragment"><div class="line"><span class="keyword">enum</span>* <span class="keyword">class </span>ns__Options { SSL3, TLS10, TLS11, TLS12 };</div>
</div><!-- fragment --><p>The base type of a scoped enumeration bitmask, when explicitly given, is ignored. The base type is either <code>int</code> or <code>int64_t</code>, depending on the number of constants enumerated in the bitmask.</p>
<p>To convert <code>enum</code> name constants and bitmasks to a string, we use the auto-generated function for enum <code>T</code>:</p>
<div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> *soap_T2s(<span class="keyword">struct</span> soap*, <span class="keyword">enum</span> T val)</div>
</div><!-- fragment --><p>The string returned is stored in an internal buffer of the current <code>soap</code> context, so you MUST copy it to keep it from being overwritten. For example, use <code>char *soap_strdup(struct soap*, const char*)</code>.</p>
<p>To convert a string to an <code>enum</code> constant or bitmask, we use the auto-generated function</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> soap_s2T(<span class="keyword">struct</span> soap*, <span class="keyword">const</span> <span class="keywordtype">char</span> *str, <span class="keyword">enum</span> T *val)</div>
</div><!-- fragment --><p>This function takes the name (or names, space-separated for bitmasks) of the enumeration constant in a string <code>str</code>. Names should be given without the pseudo-scope prefix and without trailing underscores. The function sets <code>val</code> to the corresponding integer enum constant or to a bitmask. The function returns <code>SOAP_OK</code> (zero) on success or an error if the string is not a valid enumeration name.</p>
<h2><a class="anchor" id="toxsd5"></a>
Numerical types                                                        </h2>
<p>Integer and floating point types are mapped to the equivalent built-in XSD types with the same sign and bit width.</p>
<p>The <code>size_t</code> type is transient (not serializable) because its width is platform dependent. We recommend to use <code>uint64_t</code> instead.</p>
<p>The XML value space of integer types are their decimal representations without loss of precision.</p>
<p>The XML value space of floating point types are their decimal representations. The decimal representations are formatted with the printf format string "%.9G" for floats and the printf format string "%.17lG" for double. To change the format strings, we can assign new strings to the following <code>struct soap</code> context members:</p>
<div class="fragment"><div class="line">soap.float_format       = <span class="stringliteral">&quot;%g&quot;</span>;</div>
<div class="line">soap.double_format      = <span class="stringliteral">&quot;%lg&quot;</span>;</div>
<div class="line">soap.long_double_format = <span class="stringliteral">&quot;%Lg&quot;</span>;</div>
</div><!-- fragment --><p>Note that decimal representations may result in a loss of precision of the least significant decimal. Therefore, the format strings that are used by default are sufficiently precise to avoid loss, but this may result in long decimal fractions in the XML value space.</p>
<p>The <code>long double</code> extended floating point type requires a custom serializer:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/long_double.h&quot;</span></div>
<div class="line">... use <span class="keywordtype">long</span> <span class="keywordtype">double</span> ...</div>
</div><!-- fragment --><p>You can now use <code>long double</code>, which has a serializer that serializes this type as <code>xsd:decimal</code>. Compile and link your code with <code>custom/long_double.c</code>.</p>
<p>The value space of floating point values includes the special values <code>INF</code>, <code>-INF</code>, and <code>NaN</code>. You can check a value for plus or minus infinity and not-a-number as follows:</p>
<div class="fragment"><div class="line">soap_isinf(x) &amp;&amp; x &gt; 0 <span class="comment">// is x INF?</span></div>
<div class="line">soap_isinf(x) &amp;&amp; x &lt; 0 <span class="comment">// is x -INF?</span></div>
<div class="line">soap_isnan(x)          <span class="comment">// is x NaN?</span></div>
</div><!-- fragment --><p>To assign these values, use:</p>
<div class="fragment"><div class="line"><span class="comment">// x is float       // x is double, long double, or __float128</span></div>
<div class="line">x = FLT_PINFY;      x = DBL_PINFTY;</div>
<div class="line">x = FLT_NINFY;      x = DBL_NINFTY;</div>
<div class="line">x = FLT_NAN;        x = DBL_NAN;</div>
</div><!-- fragment --><p>If your system supports <code>__float128</code> then you can also use this 128 bit floating point type with a custom serializer:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/float128.h&quot;</span></div>
<div class="line">... use xsd__decimal ...</div>
</div><!-- fragment --><p>Then use the <code>xsd__decimal</code> alias of <code>__float128</code>, which has a serializer. Do not use <code>__float128</code> directly, which is transient (not serializable).</p>
<p>To check for <code>INF</code>, <code>-INF</code>, and <code>NaN</code> of a <code>__float128</code> value use:</p>
<div class="fragment"><div class="line">isinfq(x) &amp;&amp; x &gt; 0 <span class="comment">// is x INF?</span></div>
<div class="line">isinfq(x) &amp;&amp; x &lt; 0 <span class="comment">// is x -INF?</span></div>
<div class="line">isnanq(x)          <span class="comment">// is x NaN?</span></div>
</div><!-- fragment --><p>The range of a typedef-defined numerical type can be restricted using the range <code>:</code> operator with inclusive lower and upper bounds. For example:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> <span class="keywordtype">int</span> ns__narrow -10 : 10;</div>
</div><!-- fragment --><p>This maps to a simpleType restriction of xsd:int in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="narrow"&gt;
  &lt;restriction base="xsd:int"&gt;
    &lt;minInclusive value="-10"/&gt;
    &lt;maxInclusive value="10"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>The lower and upper bound of a range are optional. When omitted, values are not bound from below or from above, respectively.</p>
<p>The range of a floating point typedef-defined type can be restricted within floating point constant bounds.</p>
<p>Also with a floating point typedef a printf format pattern can be given of the form <code>"%[width][.precision]f"</code> to format decimal values using the given width and precision fields:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> <span class="keywordtype">float</span> ns__PH <span class="stringliteral">&quot;%5.2f&quot;</span> 0.0 : 14.0;</div>
</div><!-- fragment --><p>This maps to a simpleType restriction of xsd:float in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="PH"&gt;
  &lt;restriction base="xsd:float"&gt;
    &lt;totalDigits value="5"/&gt;
    &lt;fractionDigits value="2"/&gt;
    &lt;minInclusive value="0"/&gt;
    &lt;maxInclusive value="14"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>For exclusive bounds, we use the <code>&lt;</code> operator instead of the <code>:</code> range operator:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> <span class="keywordtype">float</span> ns__epsilon 0.0 &lt; 1.0;</div>
</div><!-- fragment --><p>Values <code>eps</code> of <code>ns__epsilon</code> are restricted between <code>0.0 &lt; eps &lt; 1.0</code>.</p>
<p>This maps to a simpleType restriction of xsd:float in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="epsilon"&gt;
  &lt;restriction base="xsd:float"&gt;
    &lt;minExclusive value="0"/&gt;
    &lt;maxExclusive value="1"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>To make just one of the bounds exclusive, while keeping the other bound inclusive, we add a <code>&lt;</code> on the left or on the right side of the range ':' operator. For example:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> <span class="keywordtype">float</span> ns__pos 0.0 &lt; : ; <span class="comment">// 0.0 &lt; pos</span></div>
<div class="line"><span class="keyword">typedef</span> <span class="keywordtype">float</span> ns__neg : &lt; 0.0 ; <span class="comment">// neg &lt; 0.0</span></div>
</div><!-- fragment --><p>It is valid to make both left and right side exclusive with <code>&lt; : &lt;</code> which is in fact identical to the exlusive range <code>&lt;</code> operator:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> <span class="keywordtype">float</span> ns__epsilon 0.0 &lt; : &lt; 1.0; <span class="comment">// 0.0 &lt; eps &lt; 1.0</span></div>
</div><!-- fragment --><p>It helps to think of the <code>:</code> as a placeholder of the value between the two bounds, which is easier to memorize than the shorthand forms of bounds from which the <code>:</code> is removed:</p>
<table class="doxtable">
<tr>
<th>Bounds </th><th>Validation Check </th><th>Shorthand  </th></tr>
<tr>
<td>1 : </td><td>1 &lt;= x </td><td>1 </td></tr>
<tr>
<td>1 : 10 </td><td>1 &lt;= x &lt;= 10 </td><td></td></tr>
<tr>
<td>: 10 </td><td>x &lt;= 10 </td><td></td></tr>
<tr>
<td>1 &lt; : &lt; 10 </td><td>1 &lt; x &lt; 10 </td><td>1 &lt; 10 </td></tr>
<tr>
<td>1 : &lt; 10 </td><td>1 &lt;= x &lt; 10 </td><td></td></tr>
<tr>
<td>: &lt; 10 </td><td>x &lt; 10 </td><td>&lt; 10 </td></tr>
<tr>
<td>1 &lt; : </td><td>1 &lt; x </td><td>1 &lt; </td></tr>
<tr>
<td>1 &lt; : 10 </td><td>1 &lt; x &lt;= 10 </td><td></td></tr>
</table>
<p>Besides <code>float</code>, also <code>double</code> and <code>long double</code> values can be restricted. For example, consider a nonzero probability extended floating point precision type:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/long_double.h&quot;</span></div>
<div class="line"><span class="keyword">typedef</span> <span class="keywordtype">long</span> <span class="keywordtype">double</span> ns__probability <span class="stringliteral">&quot;%16Lg&quot;</span> 0.0 &lt; : 1.0;</div>
</div><!-- fragment --><p>Value range restrictions are validated by the parser for all inbound XML data. A type fault <code>SOAP_TYPE</code> will be thrown by the deserializer if the value is out of range.</p>
<p>Finally, if your system supports <code>__int128_t</code> then you can also use this 128 bit integer type with a custom serializer:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/int128.h&quot;</span></div>
<div class="line">... use xsd__integer ...</div>
</div><!-- fragment --><p>Use the <code>xsd__integer</code> alias of <code>__int128_t</code>, which has a serializer. Do not use <code>__int128_t</code> directly, which is transient (not serializable).</p>
<p>To convert numeric values to a string, we use the auto-generated function for numeric type <code>T</code>:</p>
<div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> *soap_T2s(<span class="keyword">struct</span> soap*, T val)</div>
</div><!-- fragment --><p>For numeric types <code>T</code>, the string returned is stored in an internal buffer of the current <code>soap</code> context, so you MUST copy it to keep it from being overwritten. For example, use <code>char *soap_strdup(struct soap*, const char*)</code>.</p>
<p>To convert a string to a numeric value, we use the auto-generated function</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> soap_s2T(<span class="keyword">struct</span> soap*, <span class="keyword">const</span> <span class="keywordtype">char</span> *str, T *val)</div>
</div><!-- fragment --><p>where <code>T</code> is for example <code>int</code>, <code>LONG64</code>, <code>float</code>, <code>decimal</code> (the custom serializer name of <code>long double</code>) or <code>xsd__integer</code> (the custom serializer name of <code>__int128_t</code>). The function <code>soap_s2T</code> returns <code>SOAP_OK</code> on success or an error when the value is not numeric. For floating point types, "INF", "-INF" and "NaN" are valid strings to convert to numbers.</p>
<h2><a class="anchor" id="toxsd6"></a>
String types                                                           </h2>
<p>String types are mapped to the built-in xsd:string and xsd:QName XSD types.</p>
<p>The wide strings <code>wchar_t*</code> and <code>std::wstring</code> may contain Unicode that is preserved in the XML value space.</p>
<p>Strings <code>char*</code> and <code>std::string</code> can only contain extended Latin, but we can store UTF-8 content that is preserved in the XML value space when the <code>struct soap</code> context is initialized with the flag <code>XML_C_UTFSTRING</code>.</p>
<dl class="section warning"><dt>Warning</dt><dd>Beware that many XML 1.0 parsers reject all control characters (those between <code>#x1</code> and <code>#x1F</code>) except <code>#x9</code>, <code>#xA</code>, and <code>#xD</code>. With the newer XML 1.1 version parsers (including gSOAP) you should be fine.</dd></dl>
<p>The length of a string of a typedef-defined string type can be restricted:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string ns__password 6 : 16;</div>
</div><!-- fragment --><p>which maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="password"&gt;
  &lt;restriction base="xsd:string"&gt;
    &lt;minLength value="6"/&gt;
    &lt;maxLength value="16"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>String length restrictions are validated by the parser for inbound XML data. A value length fault <code>SOAP_LENGTH</code> will be thrown by the deserializer if the string is too long or too short.</p>
<p>In addition, an XSD regex pattern restriction can be associated with a string typedef:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string ns__password <span class="stringliteral">&quot;([a-zA-Z]|[0-9]|-)+&quot;</span> 6 : 16;</div>
</div><!-- fragment --><p>which maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="password"&gt;
  &lt;restriction base="xsd:string"&gt;
    &lt;pattern value="([a-zA-Z0-9]|-)+"/&gt;
    &lt;minLength value="6"/&gt;
    &lt;maxLength value="16"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>Pattern restrictions are validated by the parser for inbound XML data only if the <code>soap::fsvalidate</code> and <code>soap::fwvalidate</code> callbacks are defined, see the <a href="http://www.genivia.com/doc/soapdoc2.html">gSOAP user guide.</a></p>
<p>Exclusive length bounds can be used with strings:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string ns__string255 : &lt; 256; <span class="comment">// same as 0 : 255</span></div>
</div><!-- fragment --><p>Fixed-size strings (<code>char[N]</code>) are rare occurrences in the wild, but apparently still used in some projects to store strings. To facilitate fixed-size string serialization, use soapcpp2 option <code>-b</code>. For example:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> <span class="keywordtype">char</span> ns__buffer[10]; <span class="comment">// requires soapcpp2 option -b</span></div>
</div><!-- fragment --><p>which maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="buffer"&gt;
  &lt;restriction base="xsd:string"&gt;
    &lt;maxLength value="9"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><p>Note that fixed-size strings MUST contain NUL-terminated text and SHOULD NOT contain raw binary data. Also, the length limitation is more restrictive for UTF-8 content (enabled with the <code>SOAP_C_UTFSTRING</code>) that requires multibyte character encodings. As a consequence, UTF-8 content may be truncated to fit.</p>
<p>Note that raw binary data can be stored in a <code>xsd__base64Binary</code> or <code>xsd__hexBinary</code> structure, or transmitted as a MIME attachment.</p>
<p>The built-in <code>_QName</code> type is a regular C string type (<code>char*</code>) that maps to xsd:QName but has the added advantage that it holds normalized qualified names. There are actually two forms of normalized QName content, to ensure any QName is represented accurately and uniquely:</p>
<div class="fragment"><div class="line"><span class="stringliteral">&quot;prefix:name&quot;</span></div>
<div class="line"><span class="stringliteral">&quot;\&quot;URI\&quot;:name&quot;</span></div>
</div><!-- fragment --><p>The first form of string is used when the prefix (and the binding URI) is defined in the namespace table and is bound to a URI (see the .nsmap file). The second form is used when the URI is not defined in the namespace table and therefore no prefix is available to bind and normalize the URI to.</p>
<p>A <code>_QName</code> string may contain a sequence of space-separated QName values, not just one, and all QName values are normalized to the format shown above.</p>
<p>To define a <code>std::string</code> base type for xsd:QName, we use a typedef:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string xsd__QName;</div>
</div><!-- fragment --><p>The <code>xsd__QName</code> string content is normalized, just as with the <code>_QName</code> normalization.</p>
<p>To serialize strings that contain literal XML content to be reproduced in the XML value space, use the built-in <code>_XML</code> string type, which is a regular C string type (<code>char*</code>) that maps to plain XML CDATA.</p>
<p>To define a <code>std::string</code> base type for literal XML content, use a typedef:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string XML;</div>
</div><!-- fragment --><p>Strings can hold any of the values of the XSD built-in primitive types. We can use a string typedef to declare the use of the string type as a XSD built-in type:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string xsd__token;</div>
</div><!-- fragment --><p>You MUST ensure that the string values we populate in this type conform to the XML standard, which in case of xsd:token is the lexical and value spaces of xsd:token are the sets of all strings after whitespace replacement of any occurrence of <code>#x9</code>, <code>#xA</code> , and <code>#xD</code> by <code>#x20</code> and collapsing.</p>
<p>To copy <code>char*</code> or <code>wchar_t*</code> strings with a context that manages the allocated memory, use functions</p>
<div class="fragment"><div class="line"><span class="keywordtype">char</span> *soap_strdup(<span class="keyword">struct</span> soap*, <span class="keyword">const</span> <span class="keywordtype">char</span>*)</div>
<div class="line">wchar_t *soap_wstrdup(struct soap*, const <span class="keywordtype">wchar_t</span>*)</div>
</div><!-- fragment --><p>To convert a wide string to a UTF-8 encoded string, use function</p>
<div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* SOAP_FMAC2 soap_wchar2s(<span class="keyword">struct</span> soap*, <span class="keyword">const</span> <span class="keywordtype">wchar_t</span> *s)</div>
</div><!-- fragment --><p>The function allocates and returns a string, with its memory being managed by the context.</p>
<p>To convert a UTF-8 encoded string to a wide string, use function</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> soap_s2wchar(<span class="keyword">struct</span> soap*, <span class="keyword">const</span> <span class="keywordtype">char</span> *from, <span class="keywordtype">wchar_t</span> **to, <span class="keywordtype">long</span> minlen, <span class="keywordtype">long</span> maxlen)</div>
</div><!-- fragment --><p>where <code>to</code> is set to point to an allocated <code>wchar_t*</code> string. Pass <code>-1</code> for <code>minlen</code> and <code>maxlen</code> to ignore length constraints on the target string. The function returns <code>SOAP_OK</code> or an error when the length constraints are not met.</p>
<h2><a class="anchor" id="toxsd7"></a>
Date and time types                                                    </h2>
<p>The C/C++ <code>time_t</code> type is mapped to the built-in xsd:dateTime XSD type that represents a date and time within a time zone (typically UTC).</p>
<p>The XML value space contains ISO 8601 Gregorian time instances of the form <code>[-]CCYY-MM-DDThh:mm:ss.sss[Z|(+|-)hh:mm]</code>, where <code>Z</code> is the UTC time zone or a time zone offset <code>(+|-)hh:mm]</code> from UTC is used.</p>
<p>A <code>time_t</code> value is considered and represented in UTC by the serializer.</p>
<p>Because the <code>time_t</code> value range is restricted to dates after 01/01/1970 and before 2038 assuming <code>time_t</code> is a <code>long</code> 32 bit, care must be taken to ensure the range of xsd:dateTime values in XML exchanges do not exceed the <code>time_t</code> range.</p>
<p>This restriction does not hold for <code>struct tm</code> (<code>&lt;time.h&gt;</code>), which we can use to store and exchange a date and time in UTC without date range restrictions. The serializer uses the <code>struct tm</code> members directly for the XML value space of xsd:dateTime:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>tm</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">int</span>    tm_sec;   <span class="comment">// seconds (0 - 60)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_min;   <span class="comment">// minutes (0 - 59)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_hour;  <span class="comment">// hours (0 - 23)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_mday;  <span class="comment">// day of month (1 - 31)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_mon;   <span class="comment">// month of year (0 - 11)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_year;  <span class="comment">// year - 1900</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_wday;  <span class="comment">// day of week (Sunday = 0) (NOT USED)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_yday;  <span class="comment">// day of year (0 - 365) (NOT USED)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_isdst; <span class="comment">// is summer time in effect?</span></div>
<div class="line">  <span class="keywordtype">char</span>*  tm_zone;  <span class="comment">// abbreviation of timezone (NOT USED)</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>You will lose the day of the week information. It is always Sunday (<code>tm_wday=0</code>) and the day of the year is not set either. The time zone is UTC.</p>
<p>This <code>struct tm</code> type is mapped to the built-in xsd:dateTime XSD type and serialized with the custom serializer <code>custom/struct_tm.h</code> that declares a <code>xsd__dateTime</code> type:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/struct_tm.h&quot;</span> <span class="comment">// import typedef struct tm xsd__dateTime;</span></div>
<div class="line">... use xsd__dateTime ...</div>
</div><!-- fragment --><p>Compile and link your code with <code>custom/struct_tm.c</code>.</p>
<p>The <code>struct timeval</code> (<code>&lt;sys/time.h&gt;</code>) type is mapped to the built-in xsd:dateTime XSD type and serialized with the custom serializer <code>custom/struct_timeval.h</code> that declares a <code>xsd__dateTime</code> type:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/struct_timeval.h&quot;</span> <span class="comment">// import typedef struct timeval xsd__dateTime;</span></div>
<div class="line">... use xsd__dateTime ...</div>
</div><!-- fragment --><p>Compile and link your code with <code>custom/struct_timeval.c</code>.</p>
<p>Note that the same value range restrictions apply to <code>struct timeval</code> as they apply to <code>time_t</code>. The added benefit of <code>struct timeval</code> is the addition of a microsecond-precise clock:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>timeval</div>
<div class="line">{</div>
<div class="line">  time_t       tv_sec;  <span class="comment">// seconds since Jan. 1, 1970</span></div>
<div class="line">  suseconds_t  tv_usec; <span class="comment">// and microseconds</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>A C++11 <code>std::chrono::system_clock::time_point</code> type is mapped to the built-in xsd:dateTime XSD type and serialized with the custom serializer <code>custom/chrono_time_point.h</code> that declares a <code>xsd__dateTime</code> type:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/chrono_time_point.h&quot;</span> <span class="comment">// import typedef std::chrono::system_clock::time_point xsd__dateTime;</span></div>
<div class="line">... use xsd__dateTime ...</div>
</div><!-- fragment --><p>Compile and link your code with <code>custom/chrono_time_point.cpp</code>.</p>
<p>The <code>struct tm</code> type is mapped to the built-in xsd:date XSD type and serialized with the custom serializer <code>custom/struct_tm_date.h</code> that declares a <code>xsd__date</code> type:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/struct_tm_date.h&quot;</span> <span class="comment">// import typedef struct tm xsd__date;</span></div>
<div class="line">... use xsd__date ...</div>
</div><!-- fragment --><p>Compile and link your code with <code>custom/struct_tm_date.c</code>.</p>
<p>The XML value space of xsd:date are Gregorian calendar dates of the form <code>[-]CCYY-MM-DD[Z|(+|-)hh:mm]</code> with a time zone.</p>
<p>The serializer ignores the time part and the deserializer only populates the date part of the struct, setting the time to 00:00:00. There is no unreasonable limit on the date range because the year field is stored as an integer (<code>int</code>).</p>
<p>An <code>unsigned long long</code> (<code>ULONG64</code> or <code>uint64_t</code>) type that contains a 24 hour time in microseconds UTC is mapped to the built-in xsd:time XSD type and serialized with the custom serializer <code>custom/long_time.h</code> that declares a <code>xsd__time</code> type:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/long_time.h&quot;</span> <span class="comment">// import typedef unsigned long long xsd__time;</span></div>
<div class="line">... use xsd__time ...</div>
</div><!-- fragment --><p>Compile and link your code with <code>custom/long_time.c</code>.</p>
<p>This type represents 00:00:00.000000 to 23:59:59.999999, from <code>0</code> to an upper bound of <code>86399999999</code>. A microsecond resolution means that a 1 second increment requires an increment of 1000000 in the integer value.</p>
<p>The XML value space of xsd:time are points in time recurring each day of the form <code>hh:mm:ss.sss[Z|(+|-)hh:mm]</code>, where <code>Z</code> is the UTC time zone or a time zone offset from UTC is used. The <code>xsd__time</code> value is always considered and represented in UTC by the serializer.</p>
<p>To convert date and/or time values to a string, we use the auto-generated function for type <code>T</code>:</p>
<div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> *soap_T2s(<span class="keyword">struct</span> soap*, T val)</div>
</div><!-- fragment --><p>For date and time types <code>T</code>, the string returned is stored in an internal buffer of the current <code>soap</code> context, so you MUST copy it to keep it from being overwritten. For example, use <code>char *soap_strdup(struct soap*, const char*)</code>.</p>
<p>To convert a string to a date/time value, we use the auto-generated function</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> soap_s2T(<span class="keyword">struct</span> soap*, <span class="keyword">const</span> <span class="keywordtype">char</span> *str, T *val)</div>
</div><!-- fragment --><p>where <code>T</code> is for example <code>dateTime</code> (for <code>time_t</code>), <code>xsd__dateTime</code> (for <code>struct tm</code>, <code>struct timeval</code>, or <code>std::chrono::system_clock::time_point</code>). The function <code>soap_s2T</code> returns <code>SOAP_OK</code> on success or an error when the value is not a date/time.</p>
<h2><a class="anchor" id="toxsd8"></a>
Time duration types                                                    </h2>
<p>The XML value space of xsd:duration are values of the form <code>PnYnMnDTnHnMnS</code> where the capital letters are delimiters. Delimiters may be omitted when the corresponding member is not used.</p>
<p>A <code>long long</code> (<code>LONG64</code> or <code>int64_t</code>) type that contains a duration (time lapse) in milliseconds is mapped to the built-in xsd:duration XSD type and serialized with the custom serializer <code>custom/duration.h</code> that declares a <code>xsd__duration</code> type:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/duration.h&quot;</span> <span class="comment">// import typedef long long xsd__duration;</span></div>
<div class="line">... use xsd__duration ...</div>
</div><!-- fragment --><p>Compile and link your code with <code>custom/duration.c</code>.</p>
<p>The duration type <code>xsd__duration</code> can represent 106,751,991,167 days forward and backward with millisecond precision.</p>
<p>Durations that exceed a month are always output in days, rather than months to avoid days-per-month conversion inacurracies.</p>
<p>Durations that are received in years and months instead of total number of days from a reference point are not well defined, since there is no accepted reference time point (it may or may not be the current time). The decoder simple assumes that there are 30 days per month. For example, conversion of "P4M" gives 120 days. Therefore, the durations "P4M" and "P120D" are assumed to be identical, which is not necessarily true depending on the reference point in time.</p>
<p>Rescaling of the duration value by may be needed when adding the duration value to a <code>time_t</code> value, because <code>time_t</code> may or may not have a seconds resolution, depending on the platform and possible changes to <code>time_t</code>.</p>
<p>Rescaling is done automatically when you add a C++11 <code>std::chrono::nanoseconds</code> value to a <code>std::chrono::system_clock::time_point</code> value. To use <code>std::chrono::nanoseconds</code> as xsd:duration:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;custom/chrono_duration.h&quot;</span> <span class="comment">// import typedef std::chrono::duration xsd__duration;</span></div>
<div class="line">... use xsd__duration ...</div>
</div><!-- fragment --><p>Compile and link your code with <code>custom/chrono_duration.cpp</code>.</p>
<p>This type can represent 384,307,168 days (2^63 nanoseconds) forwards and backwards in time in increments of 1 ns (1/1000000000 second).</p>
<p>The same observations with respect to receiving durations in years and months apply to this serializer's decoder.</p>
<p>To convert duration values to a string, we use the auto-generated function</p>
<div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> *soap_xsd__duration2s(<span class="keyword">struct</span> soap*, xsd__duration val)</div>
</div><!-- fragment --><p>The string returned is stored in an internal buffer, so you MUST copy it to keep it from being overwritten, Use <code>soap_strdup(struct soap*, const char*)</code> for example to copy this string.</p>
<p>To convert a string to a duration value, we use the auto-generated function</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> soap_s2xsd__dateTime(<span class="keyword">struct</span> soap*, <span class="keyword">const</span> <span class="keywordtype">char</span> *str, xsd__dateTime *val)</div>
</div><!-- fragment --><p>The function returns <code>SOAP_OK</code> on success or an error when the value is not a duration.</p>
<h2><a class="anchor" id="toxsd9"></a>
Classes and structs                                                    </h2>
<p>Classes and structs are mapped to XSD complexTypes. The XML value space consists of XML elements with attributes and subelements, possibly constrained by validation rules that enforce element and attribute occurrence contraints, numerical value range constraints, and string length and pattern constraints.</p>
<p>Classes that are declared with the gSOAP tools are limited to single inheritence only. Structs cannot be inherited.</p>
<p>The class and struct name is bound to an XML namespace by means of the prefix naming convention or by using <a href="#toxsd1">colon notation</a>:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns schema namespace: urn:types</span></div>
<div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::string  name;</div>
<div class="line">  uint64_t     SSN;</div>
<div class="line">  ns__record  *spouse;</div>
<div class="line">  ns__record();</div>
<div class="line">  ~ns__record();</div>
<div class="line"> <span class="keyword">protected</span>:</div>
<div class="line">  <span class="keyword">struct </span>soap  *soap;</div>
<div class="line">};</div>
</div><!-- fragment --><p>In the example above, we also added a context pointer to the <code>struct soap</code> that manages this instance. It is set when the instance is created in the engine's context, for example when deserialized and populated by the engine.</p>
<p>The class maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><h3><a class="anchor" id="toxsd9-1"></a>
Serializable versus transient types and members</h3>
<p>Public data members of a class or struct are serialized. Private and protected members are transient and not serializable.</p>
<p>Also <code>const</code> and <code>static</code> members are not serializable, with the exception of <code>const char*</code> and <code>const wchar_t*</code>.</p>
<p>Types and specific class/struct members can be made transient by using the <code>extern</code> qualifier:</p>
<div class="fragment"><div class="line"><span class="keyword">extern</span> <span class="keyword">class </span>std::ostream;     <span class="comment">// declare &#39;std::ostream&#39; transient</span></div>
<div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keyword">extern</span> <span class="keywordtype">int</span>       num;        <span class="comment">// not serialized</span></div>
<div class="line">  std::ostream     out;        <span class="comment">// not serialized</span></div>
<div class="line">  <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> MAX = 1024; <span class="comment">// not serialized</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>By declaring <code>std::ostream</code> transient we can use this type where we need it and without soapcpp2 complaining that this class is not defined.</p>
<h3><a class="anchor" id="toxsd9-2"></a>
Volatile classes and structs</h3>
<p>Classes and structs can be declared <code>volatile</code> with the gSOAP tools. This means that they are already declared elsewhere in your project's source code. We do not want soapcpp2 to generate a second definition for these types.</p>
<p>For example, <code>struct tm</code> is declared in <code>&lt;time.h&gt;</code>. We want it serializable and serialize only a selection of its data members:</p>
<div class="fragment"><div class="line"><span class="keyword">volatile</span> <span class="keyword">struct </span>tm</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">int</span>    tm_sec;    <span class="comment">// seconds (0 - 60)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_min;    <span class="comment">// minutes (0 - 59)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_hour;   <span class="comment">// hours (0 - 23)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_mday;   <span class="comment">// day of month (1 - 31)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_mon;    <span class="comment">// month of year (0 - 11)</span></div>
<div class="line">  <span class="keywordtype">int</span>    tm_year;   <span class="comment">// year - 1900</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>You can declare classes and structs <code>volatile</code> for any such types you want to serialize by only providing the public data members we want to serialize.</p>
<p>Colon notation is a simple and effective way to bind an existing class or struct to a schema. For example, we can change the <code>tm</code> name as follows without affecting the code that uses <code>struct tm</code> generated by soapcpp2:</p>
<div class="fragment"><div class="line"><span class="keyword">volatile</span> <span class="keyword">struct </span>ns:tm { ... }</div>
</div><!-- fragment --><p>This struct maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="tm"&gt;
  &lt;sequence&gt;
    &lt;element name="tm-sec" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="tm-min" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="tm-hour" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="tm-mday" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="tm-mon" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="tm-year" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><h3><a class="anchor" id="toxsd9-3"></a>
Mutable classes and structs</h3>
<p>Classes and structs can be declared <code>mutable</code> with the gSOAP tools. This means that their definition can be spread out over the source code. This promotes the concept of a class or struct as a <em>row of named values</em>, also known as a <em>named tuple</em>, that can be extended at compile time in your source code with additional members. Because these types differ from the traditional object-oriented principles and design concepts of classes and objects, constructors and destructors cannot be defined (also because we cannot guarantee merging these into one such that all members will be initialized). A default constructor, copy constructor, assignment operation, and destructor will be assigned automatically by soapcpp2.</p>
<div class="fragment"><div class="line"><span class="keyword">mutable</span> <span class="keyword">struct </span>ns__tuple</div>
<div class="line">{</div>
<div class="line">  @std::string  id;</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="keyword">mutable</span> <span class="keyword">struct </span>ns__tuple</div>
<div class="line">{</div>
<div class="line">  std::string  name;</div>
<div class="line">  std::string  value;</div>
<div class="line">};</div>
</div><!-- fragment --><p>The members are collected into one definition generated by soapcpp2. Members may be repeated from one definition to another, but only if their associated types are identical. So, for example, a third extension with a <code>value</code> member with a different type fails:</p>
<div class="fragment"><div class="line"><span class="keyword">mutable</span> <span class="keyword">struct </span>ns__tuple</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">float</span>        value; <span class="comment">// BAD: value is already declared std::string</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>The <code>mutable</code> concept has proven to be very useful when declaring and collecting SOAP Headers for multiple services, which are collected into one <code>struct <a class="el" href="struct_s_o_a_p___e_n_v_____header.html">SOAP_ENV__Header</a></code> by the soapcpp2 tool.</p>
<h3><a class="anchor" id="toxsd9-4"></a>
Default member values in C and C++</h3>
<p>Class and struct data members in C and C++ may be declared with an optional default initialization value that is provided "inline" with the declaration of the member:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::string name = <span class="stringliteral">&quot;Joe&quot;</span>;</div>
<div class="line">  ...</div>
<div class="line">};</div>
</div><!-- fragment --><p>These initializations are made by the default constructor that is added by soapcpp2 to each class and struct. A constructor is only added when a default constructor is not already defined with the class declaration. You can explicitly (re)initialize an object with these initial values by using the soapcpp2 auto-generated functions:</p>
<ul>
<li><code>void T::soap_default(struct soap*)</code> for <code>class T</code> (C++ only)</li>
<li><code>void soap_default_T(struct soap*, T*)</code> for <code>struct T</code> (C and C++).</li>
</ul>
<p>Initializations can only be provided for members that have primitive types (<code>bool</code>, <code>enum</code>, <code>time_t</code>, numeric and string types).</p>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#toxsd9-13">operations on classes and structs</a>.</dd></dl>
<h3><a class="anchor" id="toxsd9-5"></a>
Attribute members</h3>
<p>Class and struct data members can be declared as XML attributes by annotating their type with a <code>@</code> with the declaration of the member:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  @std::string name;</div>
<div class="line">  @uint64_t    SSN;</div>
<div class="line">  ns__record  *spouse;</div>
<div class="line">};</div>
</div><!-- fragment --><p>This class maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/&gt;
  &lt;/sequence&gt;
  &lt;attribute name="name" type="xsd:string" use="required"/&gt;
  &lt;attribute name="SSN" type="xsd:unsignedLong" use="required"/&gt;
&lt;/complexType&gt;
</pre><p>An example XML instance of <code>ns__record</code> is: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" name="Joe" SSN="1234567890"&gt;
  &lt;spouse name="Jane" SSN="1987654320"&gt;
  &lt;/spouse&gt;
&lt;/ns:record&gt;
</pre><p>Attribute data members are restricted to primitive types (<code>bool</code>, <code>enum</code>, <code>time_t</code>, numeric and string types), <code>xsd__hexBinary</code>, <code>xsd__base64Binary</code>, and custom serializers, such as <code>xsd__dateTime</code>. Custom serializers for types that may be used as attributes MUST define <code>soap_s2T</code> and <code>soap_T2s</code> functions that convert values of type <code>T</code> to strings and back.</p>
<p>Attribute data members can be pointers and smart pointers to these types, which permits attributes to be optional.</p>
<h3><a class="anchor" id="toxsd9-6"></a>
Qualified and unqualified members</h3>
<p>Class, struct, and union data members are mapped to namespace qualified or unqualified tag names of local elements and attributes. If a data member has no prefix then the default form of qualification is applied based on the element/attribute form that is declared with the schema of the class, struct, or union type. If the member name has a namespace prefix by colon notation, then the prefix overrules the default (un)qualified form. Colon notation is an effective mechanism to control qualification of tag names of individual members of classes, structs, and unions.</p>
<p>The XML schema elementFormDefault and attributeFormDefault declarations control the tag name qualification of local elements and attributes, respectively.</p>
<ul>
<li>"unqualified" indicates that local elements/attributes are not qualified with the namespace prefix.</li>
<li>"qualified" indicates that local elements/attributes must be qualified with the namespace prefix.</li>
</ul>
<p>Individual schema declarations of local elements and attributes may overrule this by using the form declaration in a schema and by using colon notation to add namespace prefixes to class, struct, and union members in the header file for soapcpp2.</p>
<p>Consider for example an <code>ns__record</code> class in the <code>ns</code> namespace in which local elements are qualified and local attributes are unqualified by default:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns schema namespace:     urn:types</span></div>
<div class="line"><span class="comment">//gsoap ns schema elementForm:   qualified</span></div>
<div class="line"><span class="comment">//gsoap ns schema attributeForm: unqualified</span></div>
<div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  @std::string name;</div>
<div class="line">  @uint64_t    SSN;</div>
<div class="line">  ns__record  *spouse;</div>
<div class="line">};</div>
</div><!-- fragment --><p>This class maps to a complexType in the soapcpp2-generated schema with targetNamespace "urn:types", elementFormDefault qualified and attributeFormDefault unqualified: </p><pre class="fragment">&lt;schema targetNamespace="urn:types"
  ...
  elementFormDefault="qualified"
  attributeFormDefault="unqualified"
  ...  &gt;
  &lt;complexType name="record"&gt;
    &lt;sequence&gt;
      &lt;element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/&gt;
    &lt;/sequence&gt;
    &lt;attribute name="name" type="xsd:string" use="required"/&gt;
    &lt;attribute name="SSN" type="xsd:unsignedLong" use="required"/&gt;
  &lt;/complexType&gt;
&lt;/schema&gt;
</pre><p>An example XML instance of <code>ns__record</code> is: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" name="Joe" SSN="1234567890"&gt;
  &lt;ns:spouse&gt; name="Jane" SSN="1987654320"&gt;
  &lt;/ns:spouse&gt;
&lt;/ns:record&gt;
</pre><p>Note that the root element ns:record is qualified because it is a root element of the schema with target namespace "urn:types". Its local element ns:spouse is namespace qualified because the elementFormDefault of local elements is qualified. Attributes are unqualified.</p>
<p>The default namespace (un)qualification of local elements and attributes can be overruled by adding a prefix to the member name by using colon notation:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns schema namespace:     urn:types</span></div>
<div class="line"><span class="comment">//gsoap ns schema elementForm:   qualified</span></div>
<div class="line"><span class="comment">//gsoap ns schema attributeForm: unqualified</span></div>
<div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  @std::string ns:name; <span class="comment">// &#39;ns:&#39; qualified</span></div>
<div class="line">  @uint64_t    SSN;</div>
<div class="line">  ns__record  *:spouse; <span class="comment">// &#39;:&#39; unqualified</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>The colon notation for member <code>ns:name</code> forces qualification of its attribute tag in XML. The colon notation for member <code>:spouse</code> removes qualification from its local element tag: </p><pre class="fragment">&lt;schema targetNamespace="urn:types"
  ...
  elementFormDefault="unqualified"
  attributeFormDefault="unqualified"
  ... &gt;
  &lt;complexType name="record"&gt;
    &lt;sequence&gt;
      &lt;element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true" form="unqualified"/&gt;
    &lt;/sequence&gt;
    &lt;attribute name="name" type="xsd:string" use="required" form="qualified"/&gt;
    &lt;attribute name="SSN" type="xsd:unsignedLong" use="required"/&gt;
  &lt;/complexType&gt;
&lt;/schema&gt;
</pre><p>XML instances of <code>ns__record</code> have unqualified spouse elements and qualified ns:name attributes: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" ns:name="Joe" SSN="1234567890"&gt;
  &lt;spouse&gt; ns:name="Jane" SSN="1987654320"&gt;
  &lt;/spouse&gt;
&lt;/ns:record&gt;
</pre><p>Note that data members can also be prefixed using the <code>prefix__name</code> convention. However, this has a different effect by referring to global (root) elements and attributes, see <a href="#toxsd9-7">defining document root elements</a>.</p>
<dl class="section note"><dt>Note</dt><dd>You must declare a target namespace with a <code>//gsoap ns schema namespace:</code> directive to enable the <code>elementForm</code> and <code>attributeForm</code> directives in order to generate valid schemas with soapcpp2. See <a href="#directives">directives</a> for more details.</dd></dl>
<h3><a class="anchor" id="toxsd9-7"></a>
Defining document root elements</h3>
<p>To define and reference XML document root elements we use type names that start with an underscore:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>_ns__record</div>
</div><!-- fragment --><p>Alternatively, we can use a typedef to define a document root element with a given type:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> ns__record _ns__record;</div>
</div><!-- fragment --><p>This typedef maps to a global root element that is added to the soapcpp2-generated schema: </p><pre class="fragment">&lt;element name="record" type="ns:record"/&gt;
</pre><p>An example XML instance of <code>_ns__record</code> is: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types"&gt;
  &lt;name&gt;Joe&lt;/name&gt;
  &lt;SSN&gt;1234567890&lt;/SSN&gt;
  &lt;spouse&gt;
    &lt;name&gt;Jane&lt;/name&gt;
    &lt;SSN&gt;1987654320&lt;/SSN&gt;
  &lt;/spouse&gt;
&lt;/ns:record&gt;
</pre><p>Global-level element/attribute definitions are also referenced and/or added to the generated schema when serializable data members reference these by their qualified name:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string _ns__name 1 : 100;</div>
<div class="line"><span class="keyword">class </span>_ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  @<a class="code" href="address_stub_8h.html#aa178a46d0cf703ff226a5c148483286d">_QName</a>      xsi__type; <span class="comment">// built-in XSD attribute xsi:type</span></div>
<div class="line">  _ns__name    ns__name;  <span class="comment">// ref to global ns:name element</span></div>
<div class="line">  uint64_t     SSN;</div>
<div class="line">  _ns__record *spouse;</div>
<div class="line">};</div>
</div><!-- fragment --><p>These types map to the following comonents in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="name"&gt;
  &lt;restriction base="xsd:string"&gt;
    &lt;minLength value="1"/&gt;
    &lt;maxLength value="100"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
&lt;element name="name" type="ns:name"/&gt;
&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element ref="ns:name" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/&gt;
  &lt;/sequence&gt;
  &lt;attribute ref="xsi:type" use="optional"/&gt;
&lt;/complexType&gt;
&lt;element name="record" type="ns:record"/&gt;
</pre><p>Use only use qualified member names when their types match the global-level element types that they refer to. For example:</p>
<div class="fragment"><div class="line"><span class="keyword">typedef</span> std::string _ns__name; <span class="comment">// global element ns:name of type xsd:string</span></div>
<div class="line"><span class="keyword">class </span>_ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keywordtype">int</span>          ns__name;       <span class="comment">// BAD: global element ns:name is NOT type int</span></div>
<div class="line">  _ns__record  ns__record;     <span class="comment">// OK: ns:record is a global-level root element</span></div>
<div class="line">  ...</div>
<div class="line">};</div>
</div><!-- fragment --><p>Therefore, we recommend to use qualified member names only when necessary to refer to standard XSD elements and attributes, such as <code>xsi__type</code>, and <code>xsd__lang</code>.</p>
<p>By contrast, colon notation has the desired effect to (un)qualify local tag names by overruling the default element/attribute namespace qualification, see <a href="#toxsd9-6">qualified and unqualified members</a>.</p>
<h3><a class="anchor" id="toxsd9-8"></a>
(Smart) pointer members and their occurrence constraints</h3>
<p>A public pointer-typed data member is serialized by following its (smart) pointer(s) to the value pointed to. To serialize pointers to dynamic arrays of data, please see the next section on <a href="#toxsd9-9">container members and their occurrence constraints</a>.</p>
<p>Pointers that are NULL and smart pointers that are empty are serialized to produce omitted element and attribute values, unless an element is required and is nillable.</p>
<p>To control the occurrence requirements of pointer-based data members, occurrence constraints are associated with data members in the form of a range <code>minOccurs : maxOccurs</code>. For non-repeatable (meaning, not a container or array) data members, there are only three reasonable occurrence constraints:</p>
<ul>
<li><code>0:0</code> means that this element or attribute is prohibited.</li>
<li><code>0:1</code> means that this element or attribute is optional.</li>
<li><code>1:1</code> means that this element or attribute is required.</li>
</ul>
<p>Pointer-based data members have a default <code>0:1</code> occurrence constraint, making them optional, and their XML schema local element/attribute definition is marked as nillable. Non-pointer data members have a default <code>1:1</code> occurence constraint, making them required.</p>
<p>A pointer data member that is explicitly marked as required with <code>1:1</code> will be serialized as an element with an xsi:nil attribute, thus effectively revealing the NULL property of its value.</p>
<p>A non-pointer data member that is explicitly marked as optional with <code>0:1</code> will be set to its default value when no XML value is presented to the deserializer. A default value can be assigned to data members that have primitive types.</p>
<p>Consider for example:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::shared_ptr&lt;std::string&gt;  name;             <span class="comment">// optional (0:1)</span></div>
<div class="line">  uint64_t                      SSN    0:1 = 999; <span class="comment">// forced this to be optional with default 999</span></div>
<div class="line">  ns__record                   *spouse 1:1;       <span class="comment">// forced this to be required (only married people)</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>This class maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="name" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/&gt;
    &lt;element name="SSN" type="xsd:unsignedLong" minOccurs="0" maxOccurs="1" default="999"/&gt;
    &lt;element name="spouse" type="ns:record" minOccurs="1" maxOccurs="1" nillable="true"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><p>An example XML instance of <code>ns__record</code> with its <code>name</code> string value set to <code>Joe</code>, <code>SSN</code> set to its default, and <code>spouse</code> set to NULL: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" ...&gt;
  &lt;name&gt;Joe&lt;/name&gt;
  &lt;SSN&gt;999&lt;/SSN&gt;
  &lt;spouse xsi:nil="true"/&gt;
&lt;/ns:record&gt;
</pre><dl class="section note"><dt>Note</dt><dd>In general, a smart pointer is simply declared as a <code>volatile</code> template in a gSOAP header file for soapcpp2:</dd></dl>
<div class="fragment"><div class="line"><span class="keyword">volatile</span> <span class="keyword">template</span> &lt;<span class="keyword">class</span> T&gt; <span class="keyword">class </span>NAMESPACE::shared_ptr;</div>
</div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>The soapcpp2 tool generates code that uses <code>NAMESPACE::shared_ptr</code> and <code>NAMESPACE::make_shared</code> to create shared pointers to objects, where <code>NAMESPACE</code> is any valid C++ namespace such as <code>std</code> and <code>boost</code> if you have Boost installed.</dd></dl>
<h3><a class="anchor" id="toxsd9-9"></a>
Container members and their occurrence constraints</h3>
<p>Class and struct data member types that are containers <code>std::deque</code>, <code>std::list</code>, <code>std::vector</code> and <code>std::set</code> are serialized as a collection of the values they contain. You can also serialize dynamic arrays, which is the alternative for C to store collections of data. Let's start with STL containers.</p>
<p>You can use <code>std::deque</code>, <code>std::list</code>, <code>std::vector</code>, and <code>std::set</code> containers by importing:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;import/stl.h&quot;</span>       <span class="comment">// import all containers</span></div>
<div class="line"><span class="preprocessor">#import &quot;import/stldeque.h&quot;</span>  <span class="comment">// import deque</span></div>
<div class="line"><span class="preprocessor">#import &quot;import/stllist.h&quot;</span>   <span class="comment">// import list</span></div>
<div class="line"><span class="preprocessor">#import &quot;import/stlvector.h&quot;</span> <span class="comment">// import vector</span></div>
<div class="line"><span class="preprocessor">#import &quot;import/stlset.h&quot;</span>    <span class="comment">// import set</span></div>
</div><!-- fragment --><p>For example, to use a vector data mamber to store names in a record:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;import/stlvector.h&quot;</span></div>
<div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::vector&lt;std::string&gt;  names;</div>
<div class="line">  uint64_t                  SSN;</div>
<div class="line">};</div>
</div><!-- fragment --><p>To limit the number of names in the vector within reasonable bounds, occurrence constraints are associated with the container. Occurrence constraints are of the form <code>minOccurs : maxOccurs</code>:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;import/stlvector.h&quot;</span></div>
<div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::vector&lt;std::string&gt;  names 1:10;</div>
<div class="line">  uint64_t                  SSN;</div>
<div class="line">};</div>
</div><!-- fragment --><p>This class maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="name" type="xsd:string" minOccurs="1" maxOccurs="10"/&gt;
    &lt;element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1""/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><dl class="section note"><dt>Note</dt><dd>In general, a container is simply declared as a template in a gSOAP header file for soapcpp2. All class templates are considered containers (except when declared <code>volatile</code>, see smart pointers). For example, <code>std::vector</code> is declared in <code>gsoap/import/stlvector.h</code> as:</dd></dl>
<div class="fragment"><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">class</span> T&gt; <span class="keyword">class </span>std::vector;</div>
</div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>You can define and use your own containers. The soapcpp2 tool generates code that uses the following members of the <code>template &lt;typename T&gt; class C</code> container:</dd></dl>
<div class="fragment"><div class="line"><span class="keywordtype">void</span>              C::clear()</div>
<div class="line">C::iterator       C::begin()</div>
<div class="line">C::const_iterator C::begin() const</div>
<div class="line">C::iterator       C::end()</div>
<div class="line">C::const_iterator C::end() const</div>
<div class="line"><span class="keywordtype">size_t</span>            C::size() const</div>
<div class="line">C::iterator       C::insert(C::iterator pos, const T&amp; val)</div>
</div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>For more details see the example <code>simple_vector</code> container with documentation in the package under <code>gsoap/samples/template</code>.</dd></dl>
<p>Because C does not support a container template library, we can use a dynamically-sized array of values. This array is declared as a size-pointer pair of members within a struct or class. The array size information is stored in a special size tag member with the name <code>__size</code> or <code>__sizeX</code>, where <code>X</code> can be any name, or by an <code>$int</code> member to identify the member as a special size tag:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>ns__record</div>
<div class="line">{</div>
<div class="line">  $int      sizeofnames; <span class="comment">// array size</span></div>
<div class="line">  <span class="keywordtype">char</span>*    *names;       <span class="comment">// array of char* names</span></div>
<div class="line">  uint64_t  SSN;</div>
<div class="line">};</div>
</div><!-- fragment --><p>This class maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="name" type="xsd:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/&gt;
    &lt;element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1""/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><p>To limit the number of names in the array within reasonable bounds, occurrence constraints are associated with the array size member. Occurrence constraints are of the form <code>minOccurs : maxOccurs</code>:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>ns__record</div>
<div class="line">{</div>
<div class="line">  $int      sizeofnames 1:10; <span class="comment">// array size 1..10</span></div>
<div class="line">  <span class="keywordtype">char</span>*    *names;            <span class="comment">// array of one to ten char* names</span></div>
<div class="line">  uint64_t  SSN;</div>
<div class="line">};</div>
</div><!-- fragment --><p>This class maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="name" type="xsd:string" minOccurs="1" maxOccurs="10" nillable="true"/&gt;
    &lt;element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1""/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><h3><a class="anchor" id="toxsd9-10"></a>
Tagged union members</h3>
<p>A union member in a class or in a struct cannot be serialized unless a discriminating <em>variant selector</em> member is provided that tells the serializer which union field to serialize. This effectively creates a <em>tagged union</em>.</p>
<p>The variant selector is associated with the union as a selector-union pair of members. The variant selector is a member with the name <code>__union</code> or <code>__unionX</code>, where <code>X</code> can be any name, or by an <code>$int</code> member to identify the member as a variant selector tag:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  $int  xORnORs;    <span class="comment">// variant selector with values SOAP_UNION_fieldname</span></div>
<div class="line">  <span class="keyword">union </span>choice</div>
<div class="line">  {</div>
<div class="line">    <span class="keywordtype">float</span> x;</div>
<div class="line">    <span class="keywordtype">int</span>   n;</div>
<div class="line">    <span class="keywordtype">char</span> *s;</div>
<div class="line">  } u;</div>
<div class="line">  std::string name;</div>
<div class="line">};</div>
</div><!-- fragment --><p>The variant selector values are auto-generated based on the union name <code>choice</code> and the names of its members <code>x</code>, <code>n</code>, and <code>s</code>:</p>
<ul>
<li><code>xORnORs = SOAP_UNION_choice_x</code> when <code>u.x</code> is valid.</li>
<li><code>xORnORs = SOAP_UNION_choice_n</code> when <code>u.n</code> is valid.</li>
<li><code>xORnORs = SOAP_UNION_choice_s</code> when <code>u.s</code> is valid.</li>
<li><code>xORnORs = 0</code> when none are valid (should only be used with great care, because XML content validation may fail when content is required but absent).</li>
</ul>
<p>This class maps to a complexType with a sequence and choice in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;choice&gt;
      &lt;element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/&gt;
      &lt;element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
      &lt;element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/&gt;
    &lt;/choice&gt;
    &lt;element name="names" type="xsd:string" minOccurs="1" maxOccurs="1" nillable="true"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><p>An STL container or dynamic array of a union requires wrapping the variant selector and union member in a struct:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::vector&lt;</div>
<div class="line">  <span class="keyword">struct </span>ns__data  <span class="comment">// data with a choice of x, n, or s</span></div>
<div class="line">  {</div>
<div class="line">    $int  xORnORs; <span class="comment">// variant selector with values SOAP_UNION_fieldname</span></div>
<div class="line">    <span class="keyword">union </span>choice</div>
<div class="line">    {</div>
<div class="line">      <span class="keywordtype">float</span> x;</div>
<div class="line">      <span class="keywordtype">int</span>   n;</div>
<div class="line">      <span class="keywordtype">char</span> *s;</div>
<div class="line">    } u;</div>
<div class="line">  }&gt; data;         <span class="comment">// vector with data</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>and an equivalent definition with a dynamic array instead of a <code>std::vector</code> (you can use this in C with structs):</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  $int  sizeOfdata; <span class="comment">// size of dynamic array</span></div>
<div class="line">  <span class="keyword">struct </span>ns__data   <span class="comment">// data with a choice of x, n, or s</span></div>
<div class="line">  {</div>
<div class="line">    $int  xORnORs;  <span class="comment">// variant selector with values SOAP_UNION_fieldname</span></div>
<div class="line">    <span class="keyword">union </span>choice</div>
<div class="line">    {</div>
<div class="line">      <span class="keywordtype">float</span> x;</div>
<div class="line">      <span class="keywordtype">int</span>   n;</div>
<div class="line">      <span class="keywordtype">char</span> *s;</div>
<div class="line">    } u;</div>
<div class="line">  } *data;          <span class="comment">// points to the data array of length sizeOfdata</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>This maps to two complexTypes in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="data"&gt;
  &lt;choice&gt;
    &lt;element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
    &lt;element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/&gt;
  &lt;/choice&gt;
&lt;/complexType&gt;
&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="data" type="ns:data" minOccurs="0" maxOccurs="unbounded"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><p>The XML value space consists of a sequence of item elements each wrapped in an data element: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" ...&gt;
  &lt;data&gt;
    &lt;n&gt;123&lt;/n&gt;
  &lt;/data&gt;
  &lt;data&gt;
    &lt;x&gt;3.1&lt;/x&gt;
  &lt;/data&gt;
  &lt;data&gt;
    &lt;s&gt;hello&lt;/s&gt;
  &lt;/data&gt;
  &lt;data&gt;
    &lt;s&gt;world&lt;/s&gt;
  &lt;/data&gt;
&lt;/ns:record&gt;
</pre><p>To remove the wrapping data element, simply rename the wrapping struct and member to <code>__data</code> to make this member invisible to the serializer with the double underscore prefix naming convention. Also use a dynamic array instead of a STL container (you can use this in C with structs):</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  $int  sizeOfdata; <span class="comment">// size of dynamic array</span></div>
<div class="line">  <span class="keyword">struct </span>__data     <span class="comment">// contains choice of x, n, or s</span></div>
<div class="line">  {</div>
<div class="line">    $int  xORnORs;  <span class="comment">// variant selector with values SOAP_UNION_fieldname</span></div>
<div class="line">    <span class="keyword">union </span>choice</div>
<div class="line">    {</div>
<div class="line">      <span class="keywordtype">float</span> x;</div>
<div class="line">      <span class="keywordtype">int</span>   n;</div>
<div class="line">      <span class="keywordtype">char</span> *s;</div>
<div class="line">    } u;</div>
<div class="line">  } *__data;        <span class="comment">// points to the data array of length sizeOfdata</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>This maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence minOccurs="0" maxOccurs="unbounded"&gt;
    &lt;choice&gt;
      &lt;element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/&gt;
      &lt;element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/&gt;
      &lt;element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/&gt;
    &lt;/choice&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><p>The XML value space consists of a sequence of x, n, and/or s elements: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" ...&gt;
  &lt;n&gt;123&lt;/n&gt;
  &lt;x&gt;3.1&lt;/x&gt;
  &lt;s&gt;hello&lt;/s&gt;
  &lt;s&gt;world&lt;/s&gt;
&lt;/ns:record&gt;
</pre><p>Please note that structs, classes, and unions are unnested by soapcpp2 (as in the C standard of nested structs and unions). Therefore, the <code>choice</code> union in the <code>ns__record</code> class is redeclared at the top level despite its nesting within the <code>ns__record</code> class. This means that you will have to choose a unique name for each nested struct, class, and union.</p>
<h3><a class="anchor" id="toxsd9-11"></a>
Tagged void pointer members</h3>
<p>To serialize data pointed to by <code>void*</code> requires run-time type information that tells the serializer what type of data to serialize by means of a <em>tagged void pointer</em>. This type information is stored in a special type tag member of a struct/class with the name <code>__type</code> or <code>__typeX</code>, where <code>X</code> can be any name, or alternatively by an <code>$int</code> special member of any name as a type tag:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  $int  typeOfdata; <span class="comment">// type tag with values SOAP_TYPE_T</span></div>
<div class="line">  <span class="keywordtype">void</span> *data;       <span class="comment">// points to some data of type T</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>A type tag member has nonzero values <code>SOAP_TYPE_T</code> where <code>T</code> is the name of a struct/class or the name of a primitive type, such as <code>int</code>, <code>std__string</code> (for <code>std::string</code>), <code>string</code> (for <code>char*</code>).</p>
<p>This class maps to a complexType with a sequence in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="data" type="xsd:anyType" minOccurs="0" maxOccurs="1"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><p>The XML value space consists of the XML value space of the type with the addition of an xsi:type attribute to the enveloping element: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" ...&gt;
  &lt;data xsi:type="xsd:int"&gt;123&lt;/data&gt;
&lt;/ns:record&gt;
</pre><p>This xsi:type attribute is important for the receiving end to distinguish the type of data to instantiate. The receiver cannot deserialize the data without an xsd:type attribute.</p>
<p>You can find the <code>SOAP_TYPE_T</code> name of each serializable type in the auto-generated soapStub.h file.</p>
<p>Also all serializable C++ classes have a virtual <code>int T::soap_type()</code> member that returns their <code>SOAP_TYPE_T</code> value that you can use.</p>
<p>When the <code>void*</code> pointer is NULL or when <code>typeOfdata</code> is zero, the data is not serialized.</p>
<p>An STL container or dynamic array of <code>void*</code> pointers to xsd:anyType data requires wrapping the type tag and <code>void*</code> members in a struct:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::vector&lt;</div>
<div class="line">  <span class="keyword">struct </span>ns__data     <span class="comment">// data with an xsd:anyType item</span></div>
<div class="line">  {</div>
<div class="line">    $int  typeOfitem; <span class="comment">// type tag with values SOAP_TYPE_T</span></div>
<div class="line">    <span class="keywordtype">void</span> *item;       <span class="comment">// points to some item of type T</span></div>
<div class="line">  }&gt; data;            <span class="comment">// vector with data</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>and an equivalent definition with a dynamic array instead of a <code>std::vector</code> (you can use this in C with structs):</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  $int  sizeOfdata;   <span class="comment">// size of dynamic array</span></div>
<div class="line">  <span class="keyword">struct </span>ns__data     <span class="comment">// data with an xsd:anyType item</span></div>
<div class="line">  {</div>
<div class="line">    $int  typeOfitem; <span class="comment">// type tag with values SOAP_TYPE_T</span></div>
<div class="line">    <span class="keywordtype">void</span> *item;       <span class="comment">// points to some item of type T</span></div>
<div class="line">  } *data;            <span class="comment">// points to the data array of length sizeOfdata</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>This maps to two complexTypes in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="data"&gt;
  &lt;sequence&gt;
    &lt;element name="item" type="xsd:anyType" minOccurs="1" maxOccurs="1" nillable="true"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
&lt;complexType name="record"&gt;
  &lt;sequence&gt;
    &lt;element name="data" type="ns:data" minOccurs="0" maxOccurs="unbounded"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><p>The XML value space consists of a sequence of item elements each wrapped in a data element: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" ...&gt;
  &lt;data&gt;
    &lt;item xsi:type="xsd:int"&gt;123&lt;/item&gt;
  &lt;/data&gt;
  &lt;data&gt;
    &lt;item xsi:type="xsd:double"&gt;3.1&lt;/item&gt;
  &lt;/data&gt;
  &lt;data&gt;
    &lt;item xsi:type="xsd:string"&gt;abc&lt;/item&gt;
  &lt;/data&gt;
&lt;/ns:record&gt;
</pre><p>To remove the wrapping data elements, simply rename the wrapping struct and member to <code>__data</code> to make this member invisible to the serializer with the double underscore prefix naming convention. Also use a dynamic array instead of a STL container (you can use this in C with structs):</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  $int  sizeOfdata;   <span class="comment">// size of dynamic array</span></div>
<div class="line">  <span class="keyword">struct </span>__data       <span class="comment">// contains xsd:anyType item</span></div>
<div class="line">  {</div>
<div class="line">    $int  typeOfitem; <span class="comment">// type tag with values SOAP_TYPE_T</span></div>
<div class="line">    <span class="keywordtype">void</span> *item;       <span class="comment">// points to some item of type T</span></div>
<div class="line">  } *__data;          <span class="comment">// points to the data array of length sizeOfdata</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>This maps to a complexType in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="record"&gt;
  &lt;sequence minOccurs="0" maxOccurs="unbounded"&gt;
    &lt;element name="item" type="xsd:anyType" minOccurs="1" maxOccurs="1"/&gt;
  &lt;/sequence&gt;
&lt;/complexType&gt;
</pre><p>The XML value space consists of a sequence of data elements: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" ...&gt;
  &lt;item xsi:type="xsd:int"&gt;123&lt;/item&gt;
  &lt;item xsi:type="xsd:double"&gt;3.1&lt;/item&gt;
  &lt;item xsi:type="xsd:string"&gt;abc&lt;/item&gt;
&lt;/ns:record&gt;
</pre><p>Again, please note that structs, classes, and unions are unnested by soapcpp2 (as in the C standard of nested structs and unions). Therefore, the <code>__data</code> struct in the <code>ns__record</code> class is redeclared at the top level despite its nesting within the <code>ns__record</code> class. This means that you will have to choose a unique name for each nested struct, class, and union.</p>
<dl class="section see"><dt>See also</dt><dd>Section <a href="#typemap2">XSD type bindings</a>.</dd></dl>
<h3><a class="anchor" id="toxsd9-12"></a>
Adding get and set methods</h3>
<p>A public <code>get</code> method may be added to a class or struct, which will be triggered by the deserializer. This method will be invoked right after the instance is populated by the deserializer. The <code>get</code> method can be used to update or verify deserialized content. It should return <code>SOAP_OK</code> or set <code>soap::error</code> to a nonzero error code and return it.</p>
<p>A public <code>set</code> method may be added to a class or struct, which will be triggered by the serializer. The method will be invoked just before the instance is serialized. Likewise, the <code>set</code> method should return <code>SOAP_OK</code> or set set <code>soap::error</code> to a nonzero error code and return it.</p>
<p>For example, adding a <code>set</code> and <code>get</code> method to a class declaration:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keywordtype">int</span> set(<span class="keyword">struct</span> soap*); <span class="comment">// triggered before serialization</span></div>
<div class="line">  <span class="keywordtype">int</span> <span class="keyword">get</span>(<span class="keyword">struct </span>soap*); <span class="comment">// triggered after deserialization</span></div>
<div class="line">  ...</div>
<div class="line">};</div>
</div><!-- fragment --><p>To add these and othe rmethods to classes and structs with wsdl2h and <code>typemap.dat</code>, please see <a href="#typemap3">class/struct member additions</a>.</p>
<h3><a class="anchor" id="toxsd9-13"></a>
Operations on classes and structs</h3>
<p>The following functions/macros are generated by soapcpp2 for each type <code>T</code>, which should make it easier to send, receive, and copy XML data in C and in C++:</p>
<ul>
<li><code>int soap_write_T(struct soap*, T*)</code> writes an instance of <code>T</code> to a file via file descriptor <code>int soap::sendfd)</code> or to a stream via <code>std::ostream *soap::os</code> (C++ only) or saves into a NUL-terminated string by setting <code>const char **soap::os</code> to a string pointer to be set (C only). Returns <code>SOAP_OK</code> on success or an error code, also stored in <code>soap-&gt;error</code>.</li>
<li><code>int soap_read_T(struct soap*, T*)</code> reads an instance of <code>T</code> from a file via file descriptor <code>int soap::recvfd)</code> or from a stream via <code>std::istream *soap::is</code> (C++ only) or reads from a NUL-termianted string <code>const char *soap::is</code> (C only). Returns <code>SOAP_OK</code> on success or an error code, also stored in <code>soap-&gt;error</code>.</li>
<li><code>void soap_default_T(struct soap*, T*)</code> sets an instance <code>T</code> to its default value, resetting members of a struct to their initial values (for classes we use method <code>T::soap_default</code>, see below).</li>
<li><code>T * soap_dup_T(struct soap*, T *dst, const T *src)</code> (soapcpp2 option <code>-Ec</code>) deep copy <code>src</code> into <code>dst</code>, replicating all deep cycles and shared pointers when a managing soap context is provided as argument. When <code>dst</code> is NULL, allocates space for <code>dst</code>. Deep copy is a tree when argument is NULL, but the presence of deep cycles will lead to non-termination. Use flag <code>SOAP_XML_TREE</code> with managing context to copy into a tree without cycles and pointers to shared objects. Returns <code>dst</code> (or allocated space when <code>dst</code> is NULL).</li>
<li><code>void soap_del_T(const T*)</code> (soapcpp2 option <code>-Ed</code>) deletes all heap-allocated members of this object by deep deletion ONLY IF this object and all of its (deep) members are not managed by a soap context AND the deep structure is a tree (no cycles and co-referenced objects by way of multiple (non-smart) pointers pointing to the same data). Can be safely used after <code>soap_dup(NULL)</code> to delete the deep copy. Does not delete the object itself.</li>
</ul>
<p>When in C++ mode, soapcpp2 tool adds several methods to classes in addition to adding a default constructor and destructor (when these were not explicitly declared).</p>
<p>The public methods added to a class <code>T</code>:</p>
<ul>
<li><code>virtual int T::soap_type(void)</code> returns a unique type ID (<code>SOAP_TYPE_T</code>). This numeric ID can be used to distinguish base from derived instances.</li>
<li><code>virtual void T::soap_default(struct soap*)</code> sets all data members to default values.</li>
<li><code>virtual void T::soap_serialize(struct soap*) const</code> serializes object to prepare for SOAP 1.1/1.2 encoded output (or with <code>SOAP_XML_GRAPH</code>) by analyzing its (cyclic) structures.</li>
<li><code>virtual int T::soap_put(struct soap*, const char *tag, const char *type) const</code> emits object in XML, compliant with SOAP 1.1 encoding style, return error code or <code>SOAP_OK</code>. Requires <code>soap_begin_send(soap)</code> and <code>soap_end_send(soap)</code>.</li>
<li><code>virtual int T::soap_out(struct soap*, const char *tag, int id, const char *type) const</code> emits object in XML, with tag and optional id attribute and xsi:type, return error code or <code>SOAP_OK</code>. Requires <code>soap_begin_send(soap)</code> and <code>soap_end_send(soap)</code>.</li>
<li><code>virtual void * T::soap_get(struct soap*, const char *tag, const char *type)</code> Get object from XML, compliant with SOAP 1.1 encoding style, return pointer to object or NULL on error. Requires <code>soap_begin_recv(soap)</code> and <code>soap_end_recv(soap)</code>.</li>
<li><code>virtual void *soap_in(struct soap*, const char *tag, const char *type)</code> Get object from XML, with matching tag and type (NULL matches any tag and type), return pointer to object or NULL on error. Requires <code>soap_begin_recv(soap)</code> and <code>soap_end_recv(soap)</code></li>
<li><code>virtual T * T::soap_alloc(void) const</code> returns a new object of type <code>T</code>, default initialized and not managed by a soap context.</li>
<li><code>virtual T * T::soap_dup(struct soap*) const</code> (soapcpp2 option <code>-Ec</code>) returns a duplicate of this object by deep copying, replicating all deep cycles and shared pointers when a managing soap context is provided as argument. Deep copy is a tree when argument is NULL, but the presence of deep cycles will lead to non-termination. Use flag <code>SOAP_XML_TREE</code> with the managing context to copy into a tree without cycles and pointers to shared objects.</li>
<li><code>virtual void T::soap_del() const</code> (soapcpp2 option <code>-Ed</code>) deletes all heap-allocated members of this object by deep deletion ONLY IF this object and all of its (deep) members are not managed by a soap context AND the deep structure is a tree (no cycles and co-referenced objects by way of multiple (non-smart) pointers pointing to the same data). Can be safely used after <code>soap_dup(NULL)</code> to delete the deep copy. Does not delete the object itself.</li>
</ul>
<p>Also for C++, there are four variations of <code>soap_new_T</code> for class/struct/template type <code>T</code> that soapcpp2 auto-generates to create instances on a context-managed heap:</p>
<ul>
<li><code>T * soap_new_T(struct soap*)</code> returns a new instance of <code>T</code> with default data member initializations that are set with the soapcpp2 auto-generated <code>void T::soap_default(struct soap*)</code> method), but ONLY IF the soapcpp2 auto-generated default constructor is used that invokes <code>soap_default()</code> and was not replaced by a user-defined default constructor.</li>
<li><code>T * soap_new_T(struct soap*, int n)</code> returns an array of <code>n</code> new instances of <code>T</code>. Similar to the above, instances are initialized.</li>
<li><code>T * soap_new_req_T(struct soap*, ...)</code> returns a new instance of <code>T</code> and sets the required data members to the values specified in <code>...</code>. The required data members are those with nonzero minOccurs, see the subsections on <a href="#toxsd9-8">(smart) pointer members and their occurrence constraints</a> and <a href="#toxsd9-9">container members and their occurrence constraints</a>.</li>
<li><code>T * soap_new_set_T(struct soap*, ...)</code> returns a new instance of <code>T</code> and sets the public/serializable data members to the values specified in <code>...</code>.</li>
</ul>
<p>The above functions can be invoked with a NULL <code>soap</code> context, but we will be responsible to use <code>delete T</code> to remove this instance from the unmanaged heap.</p>
<h2><a class="anchor" id="toxsd10"></a>
Special classes and structs                                           </h2>
<h3><a class="anchor" id="toxsd10-1"></a>
SOAP encoded arrays</h3>
<p>A class or struct with the following layout is a one-dimensional SOAP encoded Array type:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ArrayOfT</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  T   *__ptr;  <span class="comment">// array pointer</span></div>
<div class="line">  <span class="keywordtype">int</span>  __size; <span class="comment">// array size</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>where <code>T</code> is the array element type. A multidimensional SOAP Array is:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ArrayOfT</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  T   *__ptr;     <span class="comment">// array pointer</span></div>
<div class="line">  <span class="keywordtype">int</span>  __size[N]; <span class="comment">// array size of each dimension</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>where <code>N</code> is the constant number of dimensions. The pointer points to an array of <code>__size[0]*__size[1]* ... * __size[N-1]</code> elements.</p>
<p>This maps to a complexType restriction of SOAP-ENC:Array in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="ArrayOfT"&gt;
  &lt;complexContent&gt;
    &lt;restriction base="SOAP-ENC:Array"&gt;
      &lt;sequence&gt;
        &lt;element name="item" type="T" minOccurs="0" maxOccurs="unbounded" nillable="true"/&gt;
      &lt;/sequence&gt;
      &lt;attribute ref="SOAP-ENC:arrayType" WSDL:arrayType="ArrayOfT[]"/&gt;
    &lt;/restriction&gt;
  &lt;/complexContent&gt;
&lt;/complexType&gt;
</pre><p>The name of the class can be arbitrary. We often use <code>ArrayOfT</code> without a prefix to distinguish arrays from other classes and structs.</p>
<p>With SOAP 1.1 encoding, an optional offset member can be added that controls the start of the index range for each dimension:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ArrayOfT</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  T   *__ptr;       <span class="comment">// array pointer</span></div>
<div class="line">  <span class="keywordtype">int</span>  __size[N];   <span class="comment">// array size of each dimension</span></div>
<div class="line">  <span class="keywordtype">int</span>  __offset[N]; <span class="comment">// array offsets to start each dimension</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>For example, we can define a matrix of floats as follows:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>Matrix</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keywordtype">double</span> *__ptr;</div>
<div class="line">  <span class="keywordtype">int</span>     __size[2];</div>
<div class="line">};</div>
</div><!-- fragment --><p>The following code populates the matrix and serializes it in XML:</p>
<div class="fragment"><div class="line">soap *soap = soap_new1(SOAP_XML_INDENT);</div>
<div class="line">Matrix A;</div>
<div class="line"><span class="keywordtype">double</span> a[6] = { 1, 2, 3, 4, 5, 6 };</div>
<div class="line">A.__ptr = a;</div>
<div class="line">A.__size[0] = 2;</div>
<div class="line">A.__size[1] = 3;</div>
<div class="line">soap_write_Matrix(soap, &amp;A);</div>
</div><!-- fragment --><p>Matrix A is serialized as an array with 2x3 values: </p><pre class="fragment">&lt;SOAP-ENC:Array SOAP-ENC:arrayType="xsd:double[2,3]" ...&gt;
  &lt;item&gt;1&lt;/item&gt;
  &lt;item&gt;2&lt;/item&gt;
  &lt;item&gt;3&lt;/item&gt;
  &lt;item&gt;4&lt;/item&gt;
  &lt;item&gt;5&lt;/item&gt;
  &lt;item&gt;6&lt;/item&gt;
&lt;/SOAP-ENC:Array&gt;
</pre><h3><a class="anchor" id="toxsd10-2"></a>
XSD hexBinary and base64Binary types</h3>
<p>A special case of a one-dimensional array is used to define xsd:hexBinary and xsd:base64Binary types when the pointer type is <code>unsigned char</code>:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>xsd__hexBinary</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *__ptr;  <span class="comment">// points to raw binary data</span></div>
<div class="line">  <span class="keywordtype">int</span>            __size; <span class="comment">// size of data</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>and</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>xsd__base64Binary</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *__ptr;  <span class="comment">// points to raw binary data</span></div>
<div class="line">  <span class="keywordtype">int</span>            __size; <span class="comment">// size of data</span></div>
<div class="line">};</div>
</div><!-- fragment --><h3><a class="anchor" id="toxsd10-3"></a>
MIME/MTOM attachment binary types</h3>
<p>A class or struct with a binary content layout can be extended to support MIME/MTOM (and older DIME) attachments, such as in xop:Include elements:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap xop schema import: http://www.w3.org/2004/08/xop/include</span></div>
<div class="line"><span class="keyword">class </span>_xop__Include</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *__ptr;   <span class="comment">// points to raw binary data</span></div>
<div class="line">  <span class="keywordtype">int</span>            __size;  <span class="comment">// size of data</span></div>
<div class="line">  <span class="keywordtype">char</span>          *id;      <span class="comment">// NULL to generate an id, or set to a unique UUID</span></div>
<div class="line">  <span class="keywordtype">char</span>          *type;    <span class="comment">// MIME type of the data</span></div>
<div class="line">  <span class="keywordtype">char</span>          *options; <span class="comment">// optional description of MIME attachment</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>Attachments are beyond the scope of this document. See the <a href="http://www.genivia.com/doc/soapdoc2.html">gSOAP user guide</a> for more details.</p>
<h3><a class="anchor" id="toxsd10-4"></a>
Wrapper class/struct with simpleContent</h3>
<p>A class or struct with the following layout is a complexType that wraps simpleContent:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__simple</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  T   __item;</div>
<div class="line">};</div>
</div><!-- fragment --><p>The type <code>T</code> is a primitive type (<code>bool</code>, <code>enum</code>, <code>time_t</code>, numeric and string types), <code>xsd__hexBinary</code>, <code>xsd__base64Binary</code>, and custom serializers, such as <code>xsd__dateTime</code>.</p>
<p>This maps to a complexType with simpleContent in the soapcpp2-generated schema: </p><pre class="fragment">&lt;complexType name="simple"&gt;
  &lt;simpleContent&gt;
    &lt;extension base="T"/&gt;
  &lt;/simpleContent&gt;
&lt;/complexType&gt; 
</pre><p>A wrapper class/struct may include any number of attributes declared with <code>@</code>.</p>
<h3><a class="anchor" id="toxsd10-5"></a>
DOM anyType and anyAttribute</h3>
<p>Use of a DOM is optional and enabled by <code>#import "dom.h"</code> to use the DOM <code>xsd__anyType</code> element node and <code>xsd__anyAttribute</code> attribute node:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;dom.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  @xsd__anyAttribute  attributes; <span class="comment">// list of DOM attributes</span></div>
<div class="line">  ...</div>
<div class="line">  xsd__anyType       *name;       <span class="comment">// optional DOM element</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>where <code>name</code> contains XML stored in a DOM node set and <code>attributes</code> is a list of all visibly rendered attributes. The name <code>attributes</code> is arbitrary and any name will suffice.</p>
<p>You should place the <code>xsd__anyType</code> members at the end of the struct or class. This ensures that the DOM members are populated last as a "catch all". A member name starting with double underscore is a wildcard member name and matches any XML tag. These members are placed at the end of a struct or class automatically by soapcpp2.</p>
<p>An <code>#import "dom.h"</code> import is automatically added by wsdl2h with option <code>-d</code> to bind xsd:anyType to DOM nodes, and also to populate xsd:any, xsd:anyAttribute and xsd:mixed XML content:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;dom.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  ...</div>
<div class="line">  @xsd__anyAttribute        __anyAttribute; <span class="comment">// optional DOM attributes</span></div>
<div class="line">  std::vector&lt;xsd__anyType&gt; __any   0;      <span class="comment">// optional DOM elements</span></div>
<div class="line">  xsd__anyType              __mixed 0;      <span class="comment">// optional mixed content</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>where the members prefixed with <code>__</code> are "invisible" to the XML parser, meaning that these members are not bound to XML tag names.</p>
<p>In C you can use a dynamic arrary instead of <code>std::vector</code>:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;dom.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">struct </span>ns__record</div>
<div class="line">{</div>
<div class="line">  ...</div>
<div class="line">  @xsd__anyAttribute        __anyAttribute; <span class="comment">// optional DOM attributes</span></div>
<div class="line">  $int                      __sizeOfany;    <span class="comment">// size of the array</span></div>
<div class="line">  xsd__anyType             *__any;          <span class="comment">// optional DOM elements</span></div>
<div class="line">  xsd__anyType              __mixed 0;      <span class="comment">// optional mixed content</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>Classes can inherit DOM, which enables full use of polymorphism with one base DOM class:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;dom.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>ns__record : <span class="keyword">public</span> xsd__anyType</div>
<div class="line">{</div>
<div class="line">  ...</div>
<div class="line">  std::vector&lt;xsd__anyType*&gt; array; <span class="comment">// array of objects of any class</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>This permits an <code>xsd__anyType</code> pointer to refer to a derived class such as <code>ns__record</code>, which will be serialized with an xsi:type attribute that is set to "ns:record". The xsi:type attributes add the necessary type information to distinguish the XML content from the DOM base type. This is important for the receiving end: without xsd:type attributes with type names, only base DOM objects are recognized and instantiated.</p>
<p>Because C lacks OOP principles such as class inheritance and polymorphism, you will need to use the special <a href="#toxsd9-11">`void*` members</a> to serialize data pointed to by a <code>void*</code> member.</p>
<p>To ensure that wsdl2h generates pointer-based <code>xsd__anyType</code> DOM nodes with option <code>-d</code> for xsd:any, add the following line to <code>typemap.dat</code>: </p><pre class="fragment">xsd__any = | xsd__anyType*
</pre><p>This lets wsdl2h produce class/struct members and containers with <code>xsd__anyType*</code> for xsd:any instead of <code>xsd__anyType</code>. To just force all xsd:anyType uses to be pointer-based, declare in <code>typemap.dat</code>: </p><pre class="fragment">xsd__anyType = | xsd__anyType*
</pre><p>If you use wsdl2h with option <code>-p</code> with option <code>-d</code> then every class will inherit DOM as shown above. Without option <code>-d</code>, an <code>xsd__anyType</code> type is generated to serve as the root type in the type hierarchy:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>xsd__anyType { <a class="code" href="address_stub_8h.html#a5c62d26b4823b76c5b4ef29e7865d3f0">_XML</a> __item; <span class="keyword">struct </span>soap *soap; };</div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>ns__record : <span class="keyword">public</span> xsd__anyType</div>
<div class="line">{</div>
<div class="line">  ...</div>
<div class="line">};</div>
</div><!-- fragment --><p>where the <code>_XML __item</code> member holds any XML content as a literal XML string.</p>
<p>To use the DOM API, compile <code>dom.c</code> (or <code>dom.cpp</code> for C++), or link with <code>-lgsoapssl</code> (or <code>-lgsoapssl++</code> for C++).</p>
<dl class="section see"><dt>See also</dt><dd>Documentation of <a href="http://www.genivia.com/doc/dom/html">XML DOM and XPath</a> for more details.</dd></dl>
<h1><a class="anchor" id="directives"></a>
Directives                                                         </h1>
<p>Use <code>//gsoap</code> directives in the gSOAP header file with the data binding interface for soapcpp2 to declare Web service and XML schema properties. Directives are used to configure the code generated by soapcpp2. If you are using the wsdl2h tool then you will notice that wsdl2h generates directives automatically based on the WSDL and XSD input.</p>
<p>Service directives are applicable to service and operations described by WSDL. Schema directives are applicable to types, elements, and attributes defined by XML schemas.</p>
<h2><a class="anchor" id="directives-1"></a>
Service directives                                               </h2>
<p>A service directive is of the form:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap &lt;prefix&gt; service &lt;property&gt;: &lt;value&gt;</span></div>
</div><!-- fragment --><p>where <code>&lt;prefix&gt;</code> is the XML namespace prefix of a service binding. The <code>&lt;property&gt;</code> and <code>&lt;value&gt;</code> fields are one of the following:</p>
<table class="doxtable">
<tr>
<th>Property </th><th>Value  </th></tr>
<tr>
<td><code>name</code> </td><td>name of the service, optionally followed by text describing the service </td></tr>
<tr>
<td><code>namespace</code> </td><td>URI of the WSDL targetNamespace </td></tr>
<tr>
<td><code>documentation</code> </td><td>text describing the service (see also the <code>name</code> property) </td></tr>
<tr>
<td><code>port</code> </td><td>URL of the service endpoint, usually an http or https address </td></tr>
<tr>
<td><code>transport</code> </td><td>URI declaration of the transport, usually <code><a href="http://schemas.xmlsoap.org/soap/http">http://schemas.xmlsoap.org/soap/http</a></code> </td></tr>
<tr>
<td><code>definitions</code> </td><td>name of the WSDL definitions/@name </td></tr>
<tr>
<td><code>type</code> </td><td>name of the WSDL definitions/portType/@name (WSDL2.0 interface/@name) </td></tr>
<tr>
<td><code>binding</code> </td><td>name of the WSDL definitions/binding/@name </td></tr>
<tr>
<td><code>portName</code> </td><td>name of the WSDL definitions/service/port/@name </td></tr>
<tr>
<td><code>style</code> </td><td><code>document</code> (default) SOAP messaging style or <code>rpc</code> for SOAP RPC </td></tr>
<tr>
<td><code>encoding</code> </td><td><code>literal</code> (default), <code>encoded</code> for SOAP encoding, or a custom URI </td></tr>
<tr>
<td><code>protocol</code> </td><td>specifies SOAP or REST, see below </td></tr>
<tr>
<td><code>portType</code> </td><td>alias for <code>type</code> property </td></tr>
<tr>
<td><code>interface</code> </td><td>alias for <code>type</code> property </td></tr>
<tr>
<td><code>location</code> </td><td>alias for <code>port</code> property </td></tr>
<tr>
<td><code>endpoint</code> </td><td>alias for <code>port</code> property </td></tr>
</table>
<p>The service <code>name</code> and <code>namespace</code> properties are required in order to generate a valid WSDL with soapcpp2. The other properties are optional.</p>
<p>The <code>style</code> and <code>encoding</code> property defaults are changed with soapcpp2 option <code>-e</code> to <code>rpc</code> and <code>encoded</code>, respectively.</p>
<p>The <code>protocol</code> property is <code>SOAP</code> by default (SOAP 1.1). Protocol property values are:</p>
<table class="doxtable">
<tr>
<th>Protocol Value </th><th>Description  </th></tr>
<tr>
<td><code>SOAP</code> </td><td>SOAP transport, supporting both SOAP 1.1 and 1.2 </td></tr>
<tr>
<td><code>SOAP1.1</code> </td><td>SOAP 1.1 transport (same as soapcpp2 option <code>-1</code>) </td></tr>
<tr>
<td><code>SOAP1.2</code> </td><td>SOAP 1.2 transport (same as soapcpp2 option <code>-2</code>) </td></tr>
<tr>
<td><code>SOAP-GET</code> </td><td>one-way SOAP 1.1 or 1.2 with HTTP GET </td></tr>
<tr>
<td><code>SOAP1.1-GET</code> </td><td>one-way SOAP 1.1 with HTTP GET </td></tr>
<tr>
<td><code>SOAP1.2-GET</code> </td><td>one-way SOAP 1.2 with HTTP GET </td></tr>
<tr>
<td><code>HTTP</code> </td><td>non-SOAP REST protocol with HTTP POST or PUT </td></tr>
<tr>
<td><code>POST</code> </td><td>non-SOAP REST protocol with HTTP POST </td></tr>
<tr>
<td><code>GET</code> </td><td>non-SOAP REST protocol with HTTP GET </td></tr>
<tr>
<td><code>PUT</code> </td><td>non-SOAP REST protocol with HTTP PUT </td></tr>
<tr>
<td><code>DELETE</code> </td><td>non-SOAP REST protocol with HTTP DELETE </td></tr>
</table>
<p>You can bind service operations to the WSDL namespace of a service by using the namespace prefix with the the identifier name of the function that defines the service operation:</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> prefix__name(arg1, arg2, ..., argn, result);</div>
</div><!-- fragment --><h2><a class="anchor" id="directives-2"></a>
Service method directives                                        </h2>
<p>Service properties are applicable to a service and to all of its operations. Service method directives are specifically applicable to a service operation.</p>
<p>A service method directive is of the form:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap &lt;prefix&gt; service method-&lt;property&gt;: &lt;method&gt; &lt;value&gt;</span></div>
</div><!-- fragment --><p>where <code>&lt;prefix&gt;</code> is the XML namespace prefix of a service binding and <code>&lt;method&gt;</code> is the unqualified name of a service operation. The <code>&lt;property&gt;</code> and <code>&lt;value&gt;</code> fields are one of the following:</p>
<table class="doxtable">
<tr>
<th>Method Property </th><th>Value  </th></tr>
<tr>
<td><code>method-documentation</code> </td><td>text describing the service operation </td></tr>
<tr>
<td><code>method-action</code> </td><td><code>""</code> or URI SOAPAction HTTP header, or URL query string for REST protocols </td></tr>
<tr>
<td><code>method-input-action</code> </td><td><code>""</code> or URI SOAPAction HTTP header of service request messages </td></tr>
<tr>
<td><code>method-output-action</code> </td><td><code>""</code> or URI SOAPAction HTTP header of service response messages </td></tr>
<tr>
<td><code>method-fault-action</code> </td><td><code>""</code> or URI SOAPAction HTTP header of service fault messages </td></tr>
<tr>
<td><code>method-header-part</code> </td><td>member name of the <code><a class="el" href="struct_s_o_a_p___e_n_v_____header.html">SOAP_ENV__Header</a></code> struct used in SOAP Header </td></tr>
<tr>
<td><code>method-input-header-part</code> </td><td>member name of the <code><a class="el" href="struct_s_o_a_p___e_n_v_____header.html">SOAP_ENV__Header</a></code> struct used in SOAP Headers of requests </td></tr>
<tr>
<td><code>method-output-header-part</code> </td><td>member name of the <code><a class="el" href="struct_s_o_a_p___e_n_v_____header.html">SOAP_ENV__Header</a></code> struct used in SOAP Headers of responses </td></tr>
<tr>
<td><code>method-fault</code> </td><td>type name of a struct or class member used in <code>SOAP_ENV__Details</code> </td></tr>
<tr>
<td><code>method-mime-type</code> </td><td>REST content type or SOAP MIME attachment content type(s) </td></tr>
<tr>
<td><code>method-input-mime-type</code> </td><td>REST content type or SOAP MIME attachment content type(s) of request message </td></tr>
<tr>
<td><code>method-output-mime-type</code> </td><td>REST content type or SOAP MIME attachment content type(s) of response message </td></tr>
<tr>
<td><code>method-style</code> </td><td><code>document</code> or <code>rpc</code> </td></tr>
<tr>
<td><code>method-encoding</code> </td><td><code>literal</code>, <code>encoded</code>, or a custom URI for encodingStyle of messages </td></tr>
<tr>
<td><code>method-response-encoding</code> </td><td><code>literal</code>, <code>encoded</code>, or a custom URI for encodingStyle of response messages </td></tr>
<tr>
<td><code>method-protocol</code> </td><td>SOAP or REST, see <a href="#directives-1">service directives</a> </td></tr>
</table>
<p>The <code>method-header-part</code> properties can be repeated for a service operation to declare multiple SOAP Header parts that the service operation requires. You can use <code>method-input-header-part</code> and <code>method-output-header-part</code> to differentiate between request and response messages.</p>
<p>The <code>method-fault</code> property can be repeated for a service operation to declare multiple faults that the service operation may return.</p>
<p>The <code>method-action</code> property serves two purposes:</p>
<ol type="1">
<li>Sets the SOAPAction header for SOAP protocols, i.e. sets the definitions/binding/operation/SOAP:operation/@soapAction.</li>
<li>Sets the URL query string for REST protocols, i.e. sets the definitions/binding/operation/HTTP:operation/@location.</li>
</ol>
<p>Use <code>method-input-action</code> and <code>method-output-action</code> to differentiate the SOAPAction between SOAP request and response messages.</p>
<p>The <code>method-mime-type</code> property serves two purposes:</p>
<ol type="1">
<li>Sets the type of MIME/MTOM attachments used with SOAP protocols. Multiple attachment types can be declared for a SOAP service operation, i.e. adds definitions/binding/operation/input/MIME:multipartRelated/MIME:part/MIME:content/ for each type specified.</li>
<li>To set the MIME type of a REST operation. This replaces XML declared in WSDL by definitions/binding/operation/(input|output)/MIME:mimeXml with MIME:content/@type. Use <code>application/x-www-form-urlencoded</code> with REST POST and PUT protocols to send encoded form data automatically instead of XML. Only primitive type values can be transmitted with form data, such as numbers and strings, i.e. only types that are legal to use as <a href="#toxsd9-5">attributes members</a>.</li>
</ol>
<p>Use <code>method-input-mime-type</code> and <code>method-output-mime-type</code> to differentiate the attachment types between SOAP request and response messages.</p>
<h2><a class="anchor" id="directives-3"></a>
Schema directives                                                </h2>
<p>A schema directive is of the form:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap &lt;prefix&gt; schema &lt;property&gt;: &lt;value&gt;</span></div>
</div><!-- fragment --><p>where <code>&lt;prefix&gt;</code> is the XML namespace prefix of a schema.</p>
<p>TODO</p>
<h2><a class="anchor" id="directives-4"></a>
Schema type directives                                           </h2>
<p>A schema type directive is of the form:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap &lt;prefix&gt; schema type-&lt;property&gt;: &lt;name&gt; &lt;value&gt;</span></div>
</div><!-- fragment --><p>where <code>&lt;prefix&gt;</code> is the XML namespace prefix of a schema and <code>&lt;name&gt;</code> is an unqualified name of a C/C++ type.</p>
<p>You can give a description of the type:</p>
<table class="doxtable">
<tr>
<th>Type Property </th><th>Value  </th></tr>
<tr>
<td><code>type-documentation</code> </td><td>text describing the schema type </td></tr>
</table>
<p>For example, you can add a description to an enumeration:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns schema type-documentation: Vowels The letters A, E, I, O, U, and sometimes Y</span></div>
<div class="line"><span class="keyword">enum class</span> ns__Vowels : char { A = <span class="charliteral">&#39;A&#39;</span>, E = <span class="charliteral">&#39;E&#39;</span>, I = <span class="charliteral">&#39;I&#39;</span>, O = <span class="charliteral">&#39;O&#39;</span>, U = <span class="charliteral">&#39;U&#39;</span>, Y = <span class="charliteral">&#39;Y&#39;</span> };</div>
</div><!-- fragment --><p>This documented enumeration maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema: </p><pre class="fragment">&lt;simpleType name="Color"&gt;
  &lt;annotation&gt;
    &lt;documentation&gt;The letters A, E, I, O, U, and sometimes Y&lt;/documentation&gt;
  &lt;/annotation&gt;
  &lt;restriction base="xsd:string"&gt;
    &lt;enumeration value="A"/&gt;
    &lt;enumeration value="E"/&gt;
    &lt;enumeration value="I"/&gt;
    &lt;enumeration value="O"/&gt;
    &lt;enumeration value="U"/&gt;
    &lt;enumeration value="Y"/&gt;
  &lt;/restriction&gt;
&lt;/simpleType&gt;
</pre><h2><a class="anchor" id="directives-ex"></a>
Examples of directives                                          </h2>
<p>TODO</p>
<h1><a class="anchor" id="rules"></a>
Serialization rules                                                     </h1>
<p>A presentation on XML data bindings is not complete without discussing the serialization rules and options that put your data in XML on the wire or store it a file or buffer.</p>
<p>There are several options to choose from to serialize data in XML. The choice depends on the use of the SOAP protocol or if SOAP is not required. The wsdl2h tool automates this for you by taking the WSDL transport bindings into account when generating the service functions in C and C++ that use SOAP or REST.</p>
<p>The gSOAP tools are not limited to SOAP. The tools implement generic XML data bindings for SOAP, REST, and other uses of XML. So you can read and write XML using the serializing <a href="#toxsd9-13">operations on classes and structs</a>.</p>
<p>The following sections briefly explain the serialization rules with respect to the SOAP protocol for XML Web services. A basic understanding of the SOAP protocol is useful when developing client and server applications that must interoperate with other SOAP applications.</p>
<p>SOAP/REST Web service client and service operations are represented as functions in your gSOAP header file with the data binding interface for soapcpp2. The soapcpp2 tool will translate these function to client-side service invocation calls and server-side service operation dispatchers.</p>
<p>A discussion of SOAP clients and servers is beyond the scope of this document. However, the SOAP options discussed here also apply to SOAP client and server development.</p>
<h2><a class="anchor" id="doc-rpc"></a>
SOAP document versus rpc style                                        </h2>
<p>The <code>wsdl:binding/soap:binding/@style</code> attribute in the wsdl:binding section of a WSDL is either "document" or "rpc". The "rpc" style refers to SOAP RPC (Remote Procedure Call), which is more restrictive than the "document" style by requiring one XML element in the SOAP Body to act as the procedure name with XML subelements as its parameters.</p>
<p>For example, the following directives in the gSOAP header file for soapcpp2 declare that <code>DBupdate</code> is a SOAP RPC encoding service method:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns service namespace:       urn:DB</span></div>
<div class="line"><span class="comment">//gsoap ns service method-protocol: DBupdate SOAP</span></div>
<div class="line"><span class="comment">//gsoap ns service method-style:    DBupdate rpc</span></div>
<div class="line"><span class="keywordtype">int</span> ns__DBupdate(...);</div>
</div><!-- fragment --><p>The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with one element representing the operation with the parameters as subelements: </p><pre class="fragment">&lt;SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlsn:ns="urn:DB"&gt;
  &lt;SOAP-ENV:Body&gt;
    &lt;ns:DBupdate&gt;
      ...
    &lt;/ns:DBupdate&gt;
  &lt;/SOAP-ENV:Body&gt;
&lt;/SOAP-ENV:Envelope&gt;
</pre><p>The "document" style puts no restrictions on the SOAP Body content. However, we recommend that the first element's tag name in the SOAP Body should be unique to each type of operation, so that the receiver can dispatch the operation based on this element's tag name. Alternatively, the HTTP URL path can be used to specify the operation, or the HTTP action header can be used to dispatch operations automatically on the server side (soapcpp2 options -a and -A).</p>
<h2><a class="anchor" id="lit-enc"></a>
SOAP literal versus encoding                                          </h2>
<p>The <code>wsdl:operation/soap:body/@use</code> attribute in the wsdl:binding section of a WSDL is either "literal" or "encoded". The "encoded" use refers to the SOAP encoding rules that support id-ref multi-referenced elements to serialize data as graphs.</p>
<p>SOAP encoding is very useful if the data internally forms a graph (including cycles) and we want the graph to be serialized in XML in a format that ensures that its structure is preserved. In that case, SOAP 1.2 encoding is the best option.</p>
<p>SOAP encoding also adds encoding rules for [SOAP arrays](toxsd10) to serialize multi-dimensional arrays. The use of XML attributes to exchange XML data in SOAP encoding is not permitted. The only attributes permitted are the standard XSD attributes, SOAP encoding attributes (such as for arrays), and id-ref.</p>
<p>For example, the following directives in the gSOAP header file for soapcpp2 declare that <code>DBupdate</code> is a SOAP RPC encoding service method:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns service namespace:       urn:DB</span></div>
<div class="line"><span class="comment">//gsoap ns service method-protocol: DBupdate SOAP</span></div>
<div class="line"><span class="comment">//gsoap ns service method-style:    DBupdate rpc</span></div>
<div class="line"><span class="comment">//gsoap ns service method-encoding: DBupdate encoded</span></div>
<div class="line"><span class="keywordtype">int</span> ns__DBupdate(...);</div>
</div><!-- fragment --><p>The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with an encodingStyle attribute for SOAP 1.1 encoding and an element representing the operation with parameters that are SOAP 1.1 encoded: </p><pre class="fragment">&lt;SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlsn:ns="urn:DB"&gt;
  &lt;SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
    &lt;ns:DBupdate&gt;
      &lt;records SOAP-ENC:arrayType="ns:record[3]"&gt;
        &lt;item&gt;
          &lt;name href="#_1"/&gt;
          &lt;SSN&gt;1234567890&lt;/SSN&gt;
        &lt;/item&gt;
        &lt;item&gt;
          &lt;name&gt;Jane&lt;/name&gt;
          &lt;SSN&gt;1987654320&lt;/SSN&gt;
        &lt;/item&gt;
        &lt;item&gt;
          &lt;name href="#_1"/&gt;
          &lt;SSN&gt;2345678901&lt;/SSN&gt;
        &lt;/item&gt;
      &lt;/records&gt;
    &lt;/ns:DBupdate&gt;
    &lt;id id="_1" xsi:type="xsd:string"&gt;Joe&lt;/id&gt;
  &lt;/SOAP-ENV:Body&gt;
&lt;/SOAP-ENV:Envelope&gt;
</pre><p>Note that the name "Joe" is shared by two records and the string is referenced by SOAP 1.1 href and id attributes.</p>
<p>While gSOAP only introduces multi-referenced elements in the payload when they are actually multi-referenced in the data graph, other SOAP applications may render multi-referenced elements more aggressively. The example could also be rendered as: </p><pre class="fragment">&lt;SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlsn:ns="urn:DB"&gt;
  &lt;SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
    &lt;ns:DBupdate&gt;
      &lt;records SOAP-ENC:arrayType="ns:record[3]"&gt;
        &lt;item href="#id1"/&gt;
        &lt;item href="#id2"/&gt;
        &lt;item href="#id3"/&gt;
      &lt;/records&gt;
    &lt;/ns:DBupdate&gt;
    &lt;id id="id1" xsi:type="ns:record"&gt;
      &lt;name href="#id4"/&gt;
      &lt;SSN&gt;1234567890&lt;/SSN&gt;
    &lt;/id&gt;
    &lt;id id="id2" xsi:type="ns:record"&gt;
      &lt;name href="#id5"/&gt;
      &lt;SSN&gt;1987654320&lt;/SSN&gt;
    &lt;/id&gt;
    &lt;id id="id3" xsi:type="ns:record"&gt;
      &lt;name href="#id4"/&gt;
      &lt;SSN&gt;2345678901&lt;/SSN&gt;
    &lt;/id&gt;
    &lt;id id="id4" xsi:type="xsd:string"&gt;Joe&lt;/id&gt;
    &lt;id id="id5" xsi:type="xsd:string"&gt;Jane&lt;/id&gt;
  &lt;/SOAP-ENV:Body&gt;
&lt;/SOAP-ENV:Envelope&gt;
</pre><p>SOAP 1.2 encoding is cleaner and produces more accurate XML encodings of data graphs by setting the id attribute on the element that is referenced: </p><pre class="fragment">&lt;SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
  xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlsn:ns="urn:DB"&gt;
  &lt;SOAP-ENV:Body&gt;
    &lt;ns:DBupdate SOAP-ENV:encodingStyle="http://www.w3.org/2003/05/soap-encoding"&gt;
      &lt;records SOAP-ENC:itemType="ns:record" SOAP-ENC:arraySize="3"&gt;
        &lt;item&gt;
          &lt;name SOAP-ENC:id="_1"&gt;Joe&lt;/name&gt;
          &lt;SSN&gt;1234567890&lt;/SSN&gt;
        &lt;/item&gt;
        &lt;item&gt;
          &lt;name&gt;Jane&lt;/name&gt;
          &lt;SSN&gt;1987654320&lt;/SSN&gt;
        &lt;/item&gt;
        &lt;item&gt;
          &lt;name SOAP-ENC:ref="_1"/&gt;
          &lt;SSN&gt;2345678901&lt;/SSN&gt;
        &lt;/item&gt;
      &lt;/records&gt;
    &lt;/ns:DBupdate&gt;
  &lt;/SOAP-ENV:Body&gt;
&lt;/SOAP-ENV:Envelope&gt;
</pre><dl class="section note"><dt>Note</dt><dd>Some SOAP 1.2 applications consider the namespace <code>SOAP-ENC</code> of <code>SOAP-ENC:id</code> and <code>SOAP-ENC:ref</code> optional. The gSOAP SOAP 1.2 encoding serialization follows the 2007 standard, while accepting unqualified id and ref attributes.</dd></dl>
<p>To remove all rendered id-ref multi-referenced elements in gSOAP, use the <code>SOAP_XML_TREE</code> flag to initialize the gSOAP engine context.</p>
<p>Some XML validation rules are turned off with SOAP encoding, because of the presence of additional attributes, such as id and ref/href, SOAP arrays with arbitrary element tags for array elements, and the occurrence of additional multi-ref elements in the SOAP 1.1 Body.</p>
<p>The use of "literal" puts no restrictions on the XML in the SOAP Body. Full XML validation is possible, which can be enabled with the <code>SOAP_XML_STRICT</code> flag to initialize the gSOAP engine context. However, data graphs will be serialized as trees and cycles in the data will be cut from the XML rendition.</p>
<h2><a class="anchor" id="soap"></a>
SOAP 1.1 versus SOAP 1.2                                                 </h2>
<p>There are two SOAP protocol versions: 1.1 and 1.2. The gSOAP tools can switch between the two versions seamlessly. You can declare the default SOAP version for a service operation as follows:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns service method-protocol: DBupdate SOAP1.2</span></div>
</div><!-- fragment --><p>The gSOAP soapcpp2 auto-generates client and server code. At the client side, this operation sends data with SOAP 1.2 but accepts responses also in SOAP 1.1. At the server side, this operation accepts requests in SOAP 1.1 and 1.2 and will return responses in the same SOAP version.</p>
<p>As we discussed in the previous section, the SOAP 1.2 protocol has a cleaner multi-referenced element serialization format that greatly enhances the accuracy of data graph serialization with SOAP RPC encoding and is therefore recommended.</p>
<p>The SOAP 1.2 protocol default can also be set by importing and loading <code>gsoap/import/soap12.h</code>:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#import &quot;soap12.h&quot;</span></div>
</div><!-- fragment --><h2><a class="anchor" id="non-soap"></a>
Non-SOAP XML serialization                                           </h2>
<p>You can serialize data that is stored on the heap, on the stack (locals), and static data as long as the serializable (i.e. non-transient) members are properly initialized and pointers in the structures are either NULL or point to valid structures. Deserialized data is put on the heap and managed by the gSOAP engine context <code>struct soap</code>, see also <a href="#memory">memory management</a>.</p>
<p>You can read and write XML directly to a file or stream with the serializing <a href="#toxsd9-13">operations on classes and structs</a>.</p>
<p>To define and use XML Web service client and service operations, we can declare these operations in your gSOAP header file with the data binding interface for soapcpp2 as functions. The function are translated by soapcpp2 to client-side service invocation calls and server-side service operation dispatchers.</p>
<p>The REST operations POST, GET, and PUT are declared with <code>//gsoap</code> directives in the gSOAP header file for soapcpp2. For example, a REST POST operation is declared as follows:</p>
<div class="fragment"><div class="line"><span class="comment">//gsoap ns service namespace:       urn:DB</span></div>
<div class="line"><span class="comment">//gsoap ns service method-protocol: DBupdate POST</span></div>
<div class="line"><span class="keywordtype">int</span> ns__DBupdate(...);</div>
</div><!-- fragment --><p>There is no SOAP Envelope and no SOAP Body in the payload for <code>DBupdate</code>. Also the XML serialization rules are identical to SOAP document/literal. The XML payload only has the operation name as an element with its parameters serialized as subelements: </p><pre class="fragment">&lt;ns:DBupdate xmln:ns="urn:DB" ...&gt;
 ...
&lt;/ns:DBupdate&gt;
</pre><p>To force id-ref serialization with REST similar to SOAP 1.2 multi-reference encoding, use the <code>SOAP_XML_GRAPH</code> flag to initialize the gSOAP engine context. The XML serialization includes id and ref attributes for multi-referenced elements as follows: </p><pre class="fragment">&lt;ns:DBupdate xmln:ns="urn:DB" ...&gt;
  &lt;records&gt;
    &lt;item&gt;
      &lt;name id="_1"&gt;Joe&lt;/name&gt;
      &lt;SSN&gt;1234567890&lt;/SSN&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;Jane&lt;/name&gt;
      &lt;SSN&gt;1987654320&lt;/SSN&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name ref="_1"/&gt;
      &lt;SSN&gt;2345678901&lt;/SSN&gt;
    &lt;/item&gt;
  &lt;/records&gt;
&lt;/ns:DBupdate&gt;
</pre><h1><a class="anchor" id="io"></a>
Input and output                                                           </h1>
<p>Reading and writing XML from/to files, streams and string buffers is done via the managing context by setting one of the following context members that control IO sources and sinks:</p>
<div class="fragment"><div class="line">soap-&gt;recvfd = fd; <span class="comment">// an int file descriptor to read from (0 by default)</span></div>
<div class="line">soap-&gt;sendfd = fd; <span class="comment">// an int file descriptor to write to (1 by default)</span></div>
<div class="line">soap-&gt;is = &amp;is;    <span class="comment">// C++ only: a std::istream is object to read from</span></div>
<div class="line">soap-&gt;os = &amp;os;    <span class="comment">// C++ only: a std::ostream os object to write to</span></div>
<div class="line">soap-&gt;is = cs;     <span class="comment">// C only: a const char* string to read from (soap-&gt;is will advance)</span></div>
<div class="line">soap-&gt;os = &amp;cs;    <span class="comment">// C only: pointer to a const char*, will be set to point to the string output</span></div>
</div><!-- fragment --><p>Normally, all of these context members are NULL, which is required to send and receive data over sockets by gSOAP clients and servers. Therefore, if you set any of these context members in a client or server application then you MUST reset them to NULL to ensure that socket communications are not blocked.</p>
<p>Note: the use of <code>soap-&gt;is</code> and <code>soap-&gt;os</code> in C requires gSOAP 2.8.28 or later.</p>
<p>In the following sections, we present more details on how to read and write to files and streams, and use string buffers as sources and sinks for XML data.</p>
<p>In addition, you can set IO callback functions to handle IO at a lower level.</p>
<p>For more details, see the <a href="http://www.genivia.com/doc/soapdoc2.html">gSOAP user guide.</a></p>
<h2><a class="anchor" id="io1"></a>
Reading and writing from/to files and streams                             </h2>
<p>The default IO is standard input and output. Other sources and sinks (those listed above) will be used until you (re)set them. For example with file-based input and output:</p>
<div class="fragment"><div class="line">FILE *fp = fopen(<span class="stringliteral">&quot;record.xml&quot;</span>, <span class="stringliteral">&quot;r&quot;</span>);</div>
<div class="line"><span class="keywordflow">if</span> (fp != NULL)</div>
<div class="line">{</div>
<div class="line">  soap-&gt;recvfd = fileno(fp);    <span class="comment">// get file descriptor of file to read from</span></div>
<div class="line">  <span class="keywordflow">if</span> (soap_read_ns__record(soap, &amp;pers1))</div>
<div class="line">    ... <span class="comment">// handle IO error</span></div>
<div class="line">  fclose(fp);</div>
<div class="line">  soap-&gt;recvfd = 0;             <span class="comment">// read from stdin, or -1 to block reading</span></div>
<div class="line">}</div>
<div class="line"></div>
<div class="line">FILE *fp = fopen(<span class="stringliteral">&quot;record.xml&quot;</span>, <span class="stringliteral">&quot;w&quot;</span>);</div>
<div class="line"><span class="keywordflow">if</span> (fp != NULL)</div>
<div class="line">{</div>
<div class="line">  soap-&gt;sendfd = fileno(fp);    <span class="comment">// get file descriptor of file to write to</span></div>
<div class="line">  <span class="keywordflow">if</span> (soap_write_ns__record(soap, &amp;pers1))</div>
<div class="line">    ... <span class="comment">// handle IO error</span></div>
<div class="line">  fclose(fp);</div>
<div class="line">  soap-&gt;sendfd = 1;             <span class="comment">// write to stdout, or -1 to block writing</span></div>
<div class="line">}</div>
</div><!-- fragment --><p>Similar code with streams in C++:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;fstream&gt;</span></div>
<div class="line"></div>
<div class="line">std::fstream fs;</div>
<div class="line">fs.open(<span class="stringliteral">&quot;record.xml&quot;</span>, std::ios::in);</div>
<div class="line"><span class="keywordflow">if</span> (fs)</div>
<div class="line">{</div>
<div class="line">  soap-&gt;is = &amp;fs;</div>
<div class="line">  <span class="keywordflow">if</span> (soap_read__ns__record(soap, &amp;pers1))</div>
<div class="line">    ... <span class="comment">// handle IO error</span></div>
<div class="line">  fs.close();</div>
<div class="line">  soap-&gt;is = NULL;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line">fs.open(<span class="stringliteral">&quot;record.xml&quot;</span>, std::ios::out);</div>
<div class="line"><span class="keywordflow">if</span> (fs)</div>
<div class="line">{</div>
<div class="line">  soap-&gt;os = &amp;fs;</div>
<div class="line">  <span class="keywordflow">if</span> (soap_write__ns__record(soap, &amp;pers1))</div>
<div class="line">    ... <span class="comment">// handle IO error</span></div>
<div class="line">  fs.close();</div>
<div class="line">  soap-&gt;os = NULL;</div>
<div class="line">}</div>
</div><!-- fragment --><h2><a class="anchor" id="io2"></a>
Reading and writing from/to string buffers                                </h2>
<p>For C++ we recommend to use <code>std::stringstream</code> objects from <code>&lt;sstream&gt;</code> as illustrated in the following example:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;sstream&gt;</span></div>
<div class="line"></div>
<div class="line">std::stringstream ss;</div>
<div class="line">ss.str(<span class="stringliteral">&quot;...&quot;</span>); <span class="comment">// XML to parse</span></div>
<div class="line">soap-&gt;is = &amp;ss;</div>
<div class="line"><span class="keywordflow">if</span> (soap_read__ns__record(soap, &amp;pers1))</div>
<div class="line">  ... <span class="comment">// handle IO error</span></div>
<div class="line">soap-&gt;is = NULL;</div>
<div class="line"></div>
<div class="line">soap-&gt;os = &amp;ss;</div>
<div class="line"><span class="keywordflow">if</span> (soap_write__ns__record(soap, &amp;pers1))</div>
<div class="line">  ... <span class="comment">// handle IO error</span></div>
<div class="line">soap-&gt;os = NULL;</div>
<div class="line">std::string s = ss.str(); <span class="comment">// string with XML</span></div>
</div><!-- fragment --><p>For C we can use <code>soap-&gt;is</code> and <code>soap-&gt;os</code> to point to strings of XML content as follows (this requires gSOAP 2.8.28 or later):</p>
<div class="fragment"><div class="line">soap-&gt;is = <span class="stringliteral">&quot;...&quot;</span>; <span class="comment">// XML to parse</span></div>
<div class="line"><span class="keywordflow">if</span> (soap_read__ns__record(soap, &amp;pers1))</div>
<div class="line">  ... <span class="comment">// handle IO error</span></div>
<div class="line">soap-&gt;is = NULL;</div>
<div class="line"></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> *cs = NULL;</div>
<div class="line">soap-&gt;os = &amp;cs;</div>
<div class="line"><span class="keywordflow">if</span> (soap_write__ns__record(soap, &amp;pers1))</div>
<div class="line">  ... <span class="comment">// handle IO error</span></div>
<div class="line">soap-&gt;os = NULL;</div>
<div class="line">... = cs; <span class="comment">// string with XML (do not free(cs): managed by the context and freed with soap_end())</span></div>
</div><!-- fragment --><p>Note that <code>soap-&gt;os</code> is a pointer to a <code>const char*</code> string. The pointer is set by the managing context to point to the XML data that is stored on the context-managed heap.</p>
<p>For earlier gSOAP versions we recommend to use IO callbacks <code>soap-&gt;frecv</code> and <code>soap-&gt;fsend</code>, see the <a href="http://www.genivia.com/doc/soapdoc2.html">gSOAP user guide.</a></p>
<h1><a class="anchor" id="memory"></a>
Memory management                                                      </h1>
<p>Memory management with the <code>soap</code> context enables us to allocate data in context-managed heap space that can be collectively deleted. All deserialized data is placed on the context-managed heap by the gSOAP engine.</p>
<h2><a class="anchor" id="memory1"></a>
Memory management in C                                                </h2>
<p>In C (wsdl2h option <code>-c</code> and soapcpp2 option <code>-c</code>), the gSOAP engine allocates data on a context-managed heap with:</p>
<ul>
<li><code>void *soap_malloc(struct soap*, size_t len)</code>.</li>
</ul>
<p>You can also make shallow copies of data with <code>soap_memdup</code> that uses <code>soap_malloc</code> and a safe version of <code>memcpy</code> to copy a chunk of data <code>src</code> with length <code>len</code> to the context-managed heap:</p>
<ul>
<li><code>void *soap_memdup(struct soap*, const void *src, size_t len)</code></li>
</ul>
<p>This function returns a pointer to the copy. This function requires gSOAP 2.8.27 or later.</p>
<p>The <code>soap_malloc</code> function is a wrapper around <code>malloc</code>, but which also permits the <code>struct soap</code> context to track all heap allocations for collective deletion with <code>soap_end(soap)</code>:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;soapH.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;ns.nsmap&quot;</span></div>
<div class="line">...</div>
<div class="line">struct soap *soap = soap_new(); <span class="comment">// new context</span></div>
<div class="line">...</div>
<div class="line">struct ns__record *record = soap_malloc(soap, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> ns__record));</div>
<div class="line">soap_default_ns__record(soap, record); <span class="comment">// auto-generated struct initializer</span></div>
<div class="line">...</div>
<div class="line">soap_destroy(soap); <span class="comment">// only for C++, see section on C++ below</span></div>
<div class="line">soap_end(soap);     <span class="comment">// delete record and all other heap allocations</span></div>
<div class="line">soap_free(soap);    <span class="comment">// delete context</span></div>
</div><!-- fragment --><p>The soapcpp2 auto-generated deserializers in C use <code>soap_malloc</code> to allocate and populate deserialized structures, which are managed by the context for collective deletion.</p>
<p>To make <code>char*</code> and <code>wchar_t*</code> string copies to the context-managed heap, we can use the functions:</p>
<ul>
<li><code>char *soap_strdup(struct soap*, const char *str)</code> and</li>
<li><code>wchar_t *soap_wstrdup(struct soap*, const wchar_t *wstr)</code>.</li>
</ul>
<p>If your C compiler supports <code>typeof</code> then you can use the following macro to simplify the managed heap allocation and initialization of primitive values:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#define soap_assign(soap, lhs, rhs) (*(lhs = (typeof(lhs))soap_malloc(soap, sizeof(*lhs))) = rhs)</span></div>
</div><!-- fragment --><p>Pointers to primitive values are often used for optional members. For example, assume we have the following struct:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>ns__record</div>
<div class="line">{</div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span>        *name;   <span class="comment">// required name</span></div>
<div class="line">  uint64_t          *SSN;    <span class="comment">// optional SSN</span></div>
<div class="line">  <span class="keyword">struct </span>ns__record *spouse; <span class="comment">// optional spouse</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>Use <code>soap_assign</code> to create a SSN value on the managed heap:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>soap *soap = soap_new(); <span class="comment">// new context</span></div>
<div class="line">...</div>
<div class="line">struct ns__record *record = soap_malloc(soap, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> ns__record));</div>
<div class="line">soap_default_ns__record(soap, record);</div>
<div class="line">record-&gt;name = soap_strdup(soap, <span class="stringliteral">&quot;Joe&quot;</span>);</div>
<div class="line">soap_assign(soap, record-&gt;SSN, 1234567890LL);</div>
<div class="line">...</div>
<div class="line">soap_end(soap);     <span class="comment">// delete managed soap_malloc&#39;ed heap data</span></div>
<div class="line">soap_free(soap);    <span class="comment">// delete context</span></div>
</div><!-- fragment --><p>Without the <code>soap_assign</code> macro, you will need two lines of code, one to allocate and one to assign (you should also use this if your system can run out of memory):</p>
<div class="fragment"><div class="line">assert((record-&gt;SSN = (uint64_t*)soap_malloc(soap, <span class="keyword">sizeof</span>(utint64_t))) != NULL);</div>
<div class="line">*record-&gt;SSN = 1234567890LL;</div>
</div><!-- fragment --><p>The gSOAP serializer can serialize any heap, stack, or static allocated data. So we can also create a new record as follows:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>soap *soap = soap_new(); <span class="comment">// new context</span></div>
<div class="line">...</div>
<div class="line">struct ns__record *record = soap_malloc(soap, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> ns__record));</div>
<div class="line"><span class="keyword">static</span> uint64_t SSN = 1234567890LL;</div>
<div class="line">soap_default_ns__record(soap, record);</div>
<div class="line">record-&gt;name = <span class="stringliteral">&quot;Joe&quot;</span>;</div>
<div class="line">record-&gt;SSN = &amp;SSN; <span class="comment">// safe to use static values: the value of record-&gt;SSN is never changed by gSOAP</span></div>
<div class="line">...</div>
<div class="line">soap_end(soap);     <span class="comment">// delete managed soap_malloc&#39;ed heap data</span></div>
<div class="line">soap_free(soap);    <span class="comment">// delete context</span></div>
</div><!-- fragment --><p>Use the soapcpp2 auto-generated <code>soap_dup_T</code> functions to duplicate data into another context (this requires soapcpp2 option <code>-Ec</code> to generate), here shown for C with the second argument <code>dst</code> NULL because we want to allocate a new managed structure:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>soap *other_soap = soap_new(); <span class="comment">// another context</span></div>
<div class="line"><span class="keyword">struct </span>ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);</div>
<div class="line">...</div>
<div class="line">soap_destroy(other_soap); <span class="comment">// only for C++, see section on C++ below</span></div>
<div class="line">soap_end(other_soap);     <span class="comment">// delete other_record and all of its deep data</span></div>
<div class="line">soap_free(other_soap);    <span class="comment">// delete context</span></div>
</div><!-- fragment --><p>Note that the only reason to use another context and not to use the primary context is when the primary context must be destroyed together with all of the objects it manages while some of the objects must be kept alive. If the objects that are kept alive contain deep cycles then this is the only option we have, because deep copy with a managing context detects and preserves these cycles unless the <code>SOAP_XML_TREE</code> flag is used with the context:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>soap *other_soap = soap_new1(SOAP_XML_TREE); <span class="comment">// another context</span></div>
<div class="line"><span class="keyword">struct </span>ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);</div>
</div><!-- fragment --><p>The resulting deep copy will be a full copy of the source data structure as a tree without co-referenced data (i.e. no digraph) and without cycles. Cycles are pruned and (one of the) pointers that forms a cycle is repaced by NULL.</p>
<p>You can also deep copy into unmanaged space and use the auto-generated <code>soap_del_T()</code> function (requires soapcpp2 option <code>-Ed</code> to generate) to delete it later, but you MUST NOT do this for any data that has deep cycles in its runtime data structure:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>ns__record *other_record = soap_dup_ns__record(NULL, NULL, record);</div>
<div class="line">...</div>
<div class="line">soap_del_ns__record(other_record); <span class="comment">// deep delete record data members</span></div>
<div class="line">free(other_record);                <span class="comment">// delete the record</span></div>
</div><!-- fragment --><p>Cycles in the data structure will lead to non-termination when making unmanaged deep copies. Consider for example:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>ns__record</div>
<div class="line">{</div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span>         *name;   <span class="comment">// required name</span></div>
<div class="line">  uint64_t            SSN;    <span class="comment">// required SSN</span></div>
<div class="line">  <span class="keyword">struct </span>ns__record  *spouse; <span class="comment">// optional spouse</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>The code to populate a structure with a mutual spouse relationship:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>soap *soap = soap_new();</div>
<div class="line">...</div>
<div class="line">struct ns__record pers1, pers2;</div>
<div class="line">soap_default_ns__record(soap, &amp;pers1);</div>
<div class="line">soap_default_ns__record(soap, &amp;pers2);</div>
<div class="line">pers1.name = <span class="stringliteral">&quot;Joe&quot;</span>;                     <span class="comment">// OK to serialize static data</span></div>
<div class="line">pers1.SSN = 1234567890;</div>
<div class="line">pers1.spouse = &amp;pers2;</div>
<div class="line">pers2.name = soap_strdup(soap, <span class="stringliteral">&quot;Jane&quot;</span>); <span class="comment">// allocates and copies a string</span></div>
<div class="line">pers2.SSN = 1987654320;</div>
<div class="line">pers2.spouse = &amp;pers1;</div>
<div class="line">...</div>
<div class="line">struct ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &amp;pers1); <span class="comment">// BAD</span></div>
<div class="line"><span class="keyword">struct </span>ns__record *pers4 = soap_dup_ns__record(soap, NULL, &amp;pers1); <span class="comment">// OK</span></div>
<div class="line">soap_set_mode(soap, SOAP_XML_TREE);</div>
<div class="line"><span class="keyword">struct </span>ns__record *pers5 = soap_dup_ns__record(soap, NULL, &amp;pers1); <span class="comment">// OK</span></div>
</div><!-- fragment --><p>As we can see, the gSOAP serializer can serialize any heap, stack, or static allocated data, such as in the code above. So we can serialize the stack-allocated <code>pers1</code> record as follows:</p>
<div class="fragment"><div class="line">FILE *fp = fopen(<span class="stringliteral">&quot;record.xml&quot;</span>, <span class="stringliteral">&quot;w&quot;</span>);</div>
<div class="line"><span class="keywordflow">if</span> (fp != NULL)</div>
<div class="line">{</div>
<div class="line">  soap-&gt;sendfd = fileno(fp);           <span class="comment">// file descriptor to write to</span></div>
<div class="line">  soap_set_mode(soap, SOAP_XML_GRAPH); <span class="comment">// support id-ref w/o requiring SOAP</span></div>
<div class="line">  soap_clr_mode(soap, SOAP_XML_TREE);  <span class="comment">// if set, clear</span></div>
<div class="line">  soap_write_ns__record(soap, &amp;pers1);</div>
<div class="line">  fclose(fp);</div>
<div class="line">  soap-&gt;sendfd = -1;                   <span class="comment">// block further writing</span></div>
<div class="line">}</div>
</div><!-- fragment --><p>which produces an XML document record.xml that is similar to: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" id="Joe"&gt;
  &lt;name&gt;Joe&lt;/name&gt;
  &lt;SSN&gt;1234567890&lt;/SSN&gt;
  &lt;spouse id="Jane"&gt;
    &lt;name&gt;Jane&lt;/name&gt;
    &lt;SSN&gt;1987654320&lt;/SSN&gt;
    &lt;spouse ref="#Joe"/&gt;
  &lt;/spouse&gt;
&lt;/ns:record&gt;
</pre><p>Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph leads to the same non-termination problem when we later try to copy the data into unmanaged space:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>soap *soap = soap_new1(SOAP_XML_GRAPH); <span class="comment">// support id-ref w/o SOAP</span></div>
<div class="line">...</div>
<div class="line">struct ns__record pers1;</div>
<div class="line">FILE *fp = fopen(<span class="stringliteral">&quot;record.xml&quot;</span>, <span class="stringliteral">&quot;r&quot;</span>);</div>
<div class="line"><span class="keywordflow">if</span> (fp != NULL)</div>
<div class="line">{</div>
<div class="line">  soap-&gt;recvfd = fileno(fp);</div>
<div class="line">  <span class="keywordflow">if</span> (soap_read_ns__record(soap, &amp;pers1))</div>
<div class="line">    ... <span class="comment">// handle IO error</span></div>
<div class="line">  fclose(fp);</div>
<div class="line">  soap-&gt;recvfd = -1;                    <span class="comment">// blocks further reading</span></div>
<div class="line">}</div>
<div class="line">...</div>
<div class="line">struct ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &amp;pers1); <span class="comment">// BAD</span></div>
<div class="line"><span class="keyword">struct </span>ns__record *pers4 = soap_dup_ns__record(soap, NULL, &amp;pers1); <span class="comment">// OK</span></div>
<div class="line">soap_set_mode(soap, SOAP_XML_TREE);</div>
<div class="line"><span class="keyword">struct </span>ns__record *pers5 = soap_dup_ns__record(soap, NULL, &amp;pers1); <span class="comment">// OK</span></div>
</div><!-- fragment --><p>Copying data with <code>soap_dup_T(soap)</code> into managed space is always safe. Copying into unmanaged space requires diligence. But deleting unmanaged data is easy with <code>soap_del_T()</code>.</p>
<p>You can also use <code>soap_del_T()</code> to delete structures that you created in C, but only if these structures are created with <code>malloc</code> and do NOT contain pointers to stack and static data.</p>
<h2><a class="anchor" id="memory2"></a>
Memory management in C++                                              </h2>
<p>In C++, the gSOAP engine allocates data on a managed heap using a combination of <code>void *soap_malloc(struct soap*, size_t len)</code> and <code>soap_new_T()</code>, where <code>T</code> is the name of a class, struct, or class template (container or smart pointer). Heap allocation is tracked by the <code>struct soap</code> context for collective deletion with <code>soap_destroy(soap)</code> and <code>soap_end(soap)</code>.</p>
<p>Only structs, classes, and class templates are allocated with <code>new</code> via <code>soap_new_T(struct soap*)</code> and mass-deleted with <code>soap_destroy(soap)</code>.</p>
<p>There are four variations of <code>soap_new_T</code> for class/struct/template type <code>T</code> that soapcpp2 auto-generates to create instances on a context-managed heap:</p>
<ul>
<li><code>T * soap_new_T(struct soap*)</code> returns a new instance of <code>T</code> with default data member initializations that are set with the soapcpp2 auto-generated <code>void T::soap_default(struct soap*)</code> method), but ONLY IF the soapcpp2 auto-generated default constructor is used that invokes <code>soap_default()</code> and was not replaced by a user-defined default constructor.</li>
<li><code>T * soap_new_T(struct soap*, int n)</code> returns an array of <code>n</code> new instances of <code>T</code>. Similar to the above, instances are initialized.</li>
<li><code>T * soap_new_req_T(struct soap*, ...)</code> returns a new instance of <code>T</code> and sets the required data members to the values specified in <code>...</code>. The required data members are those with nonzero minOccurs, see the subsections on <a href="#toxsd9-8">(smart) pointer members and their occurrence constraints</a> and <a href="#toxsd9-9">container members and their occurrence constraints</a>.</li>
<li><code>T * soap_new_set_T(struct soap*, ...)</code> returns a new instance of <code>T</code> and sets the public/serializable data members to the values specified in <code>...</code>.</li>
</ul>
<p>The above functions can be invoked with a NULL <code>soap</code> context, but we will be responsible to use <code>delete T</code> to remove this instance from the unmanaged heap.</p>
<p>Primitive types and arrays of these are allocated with <code>soap_malloc</code> by the gSOAP engine. As we stated above, all types except for classes, structs, class templates (containers and smart pointers) are allocated with <code>soap_malloc</code> for reasons of efficiency.</p>
<p>You can use a C++ template to simplify the managed allocation and initialization of primitive values as follows (this is for primitive types only, because structs and classes are allocated with <code>soap_new_T</code>):</p>
<div class="fragment"><div class="line"><span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;</div>
<div class="line">T * soap_make(<span class="keyword">struct</span> soap *soap, T val)</div>
<div class="line">{</div>
<div class="line">  T *p = (T*)soap_malloc(soap, <span class="keyword">sizeof</span>(T));</div>
<div class="line">  <span class="keywordflow">if</span> (p)      <span class="comment">// out of memory? Can also guard with assert(p != NULL) or throw an error</span></div>
<div class="line">    *p = val;</div>
<div class="line">  <span class="keywordflow">return</span> p;</div>
<div class="line">}</div>
</div><!-- fragment --><p>For example, assuming we have the following class:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  std::string  name;   <span class="comment">// required name</span></div>
<div class="line">  uint64_t    *SSN;    <span class="comment">// optional SSN</span></div>
<div class="line">  ns__record  *spouse; <span class="comment">// optional spouse</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>You can instantiate a record by using the auto-generated <code>soap_new_set_ns__record</code> and use <code>soap_make</code> to create a SSN value on the managed heap:</p>
<div class="fragment"><div class="line">soap *soap = soap_new(); <span class="comment">// new context</span></div>
<div class="line">...</div>
<div class="line">ns__record *record = soap_new_set_ns__record(</div>
<div class="line">    soap,</div>
<div class="line">    <span class="stringliteral">&quot;Joe&quot;</span>,</div>
<div class="line">    soap_make&lt;uint64_t&gt;(soap, 1234567890LL),</div>
<div class="line">    NULL);</div>
<div class="line">...</div>
<div class="line">soap_destroy(soap); <span class="comment">// delete record and all other managed instances</span></div>
<div class="line">soap_end(soap);     <span class="comment">// delete managed soap_malloc&#39;ed heap data</span></div>
<div class="line">soap_free(soap);    <span class="comment">// delete context</span></div>
</div><!-- fragment --><p>Note however that the gSOAP serializer can serialize any heap, stack, or static allocated data. So we can also create a new record as follows:</p>
<div class="fragment"><div class="line">uint64_t SSN = 1234567890LL;</div>
<div class="line">ns__record *record = soap_new_set_ns__record(soap, <span class="stringliteral">&quot;Joe&quot;</span>, &amp;SSN, NULL);</div>
</div><!-- fragment --><p>which will be fine to serialize this record as long as the local <code>SSN</code> stack-allocated value remains in scope when invoking the serializer and/or using <code>record</code>. It does not matter if <code>soap_destroy</code> and <code>soap_end</code> are called beyond the scope of <code>SSN</code>.</p>
<p>To facilitate class methods to access the managing context, we can add a soap context pointer to a class/struct:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line">  ...</div>
<div class="line">  <span class="keywordtype">void</span> create_more(); <span class="comment">// needs a context to create more internal data</span></div>
<div class="line"> <span class="keyword">protected</span>:</div>
<div class="line">  <span class="keyword">struct </span>soap *soap;  <span class="comment">// the context that manages this instance, or NULL</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>The context is set when invoking <code>soap_new_T</code> (and similar) with a non-NULL context argument.</p>
<p>Use the soapcpp2 auto-generated <code>soap_dup_T</code> functions to duplicate data into another context (this requires soapcpp2 option <code>-Ec</code> to generate), here shown for C++ with the second argument <code>dst</code> NULL to allocate a new managed object:</p>
<div class="fragment"><div class="line">soap *other_soap = soap_new(); <span class="comment">// another context</span></div>
<div class="line">ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);</div>
<div class="line">...</div>
<div class="line">soap_destroy(other_soap); <span class="comment">// delete record and other managed instances</span></div>
<div class="line">soap_end(other_soap);     <span class="comment">// delete other data (the SSNs on the heap)</span></div>
<div class="line">soap_free(other_soap);    <span class="comment">// delete context</span></div>
</div><!-- fragment --><p>To duplicate base and derived instances when a base class pointer or reference is provided, use the auto-generated method <code>T * T::soap_dup(struct soap*)</code>:</p>
<div class="fragment"><div class="line">soap *other_soap = soap_new(); <span class="comment">// another context</span></div>
<div class="line">ns__record *other_record = record-&gt;soap_dup(other_soap);</div>
<div class="line">...</div>
<div class="line">soap_destroy(other_soap); <span class="comment">// delete record and other managed instances</span></div>
<div class="line">soap_end(other_soap);     <span class="comment">// delete other data (the SSNs on the heap)</span></div>
<div class="line">soap_free(other_soap);    <span class="comment">// delete context</span></div>
</div><!-- fragment --><p>Note that the only reason to use another context and not to use the primary context is when the primary context must be destroyed together with all of the objects it manages while some of the objects must be kept alive. If the objects that are kept alive contain deep cycles then this is the only option we have, because deep copy with a managing context detects and preserves these cycles unless the <code>SOAP_XML_TREE</code> flag is used with the context:</p>
<div class="fragment"><div class="line">soap *other_soap = soap_new1(SOAP_XML_TREE);             <span class="comment">// another context</span></div>
<div class="line">ns__record *other_record = record-&gt;soap_dup(other_soap); <span class="comment">// deep tree copy</span></div>
</div><!-- fragment --><p>The resulting deep copy will be a full copy of the source data structure as a tree without co-referenced data (i.e. no digraph) and without cycles. Cycles are pruned and (one of the) pointers that forms a cycle is repaced by NULL.</p>
<p>You can also deep copy into unmanaged space and use the auto-generated <code>soap_del_T()</code> function or the <code>T::soap_del()</code> method (requires soapcpp2 option <code>-Ed</code> to generate) to delete it later, but we MUST NOT do this for any data that has deep cycles in its runtime data structure graph:</p>
<div class="fragment"><div class="line">ns__record *other_record = record-&gt;soap_dup(NULL);</div>
<div class="line">...</div>
<div class="line">other_record-&gt;soap_del(); <span class="comment">// deep delete record data members</span></div>
<div class="line"><span class="keyword">delete</span> other_record;      <span class="comment">// delete the record</span></div>
</div><!-- fragment --><p>Cycles in the data structure will lead to non-termination when making unmanaged deep copies. Consider for example:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ns__record</div>
<div class="line">{</div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span>  *name;   <span class="comment">// required name</span></div>
<div class="line">  uint64_t     SSN;    <span class="comment">// required SSN</span></div>
<div class="line">  ns__record  *spouse; <span class="comment">// optional spouse</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>The code to populate a structure with a mutual spouse relationship:</p>
<div class="fragment"><div class="line">soap *soap = soap_new();</div>
<div class="line">...</div>
<div class="line">ns__record pers1, pers2;</div>
<div class="line">pers1.name = <span class="stringliteral">&quot;Joe&quot;</span>;</div>
<div class="line">pers1.SSN = 1234567890;</div>
<div class="line">pers1.spouse = &amp;pers2;</div>
<div class="line">pers2.name = <span class="stringliteral">&quot;Jane&quot;</span>;</div>
<div class="line">pers2.SSN = 1987654320;</div>
<div class="line">pers2.spouse = &amp;pers1;</div>
<div class="line">...</div>
<div class="line">ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &amp;pers1); <span class="comment">// BAD</span></div>
<div class="line">ns__record *pers4 = soap_dup_ns__record(soap, NULL, &amp;pers1); <span class="comment">// OK</span></div>
<div class="line">soap_set_mode(soap, SOAP_XML_TREE);</div>
<div class="line">ns__record *pers5 = soap_dup_ns__record(soap, NULL, &amp;pers1); <span class="comment">// OK</span></div>
</div><!-- fragment --><p>Note that the gSOAP serializer can serialize any heap, stack, or static allocated data, such as in the code above. So we can serialize the stack-allocated <code>pers1</code> record as follows:</p>
<div class="fragment"><div class="line">FILE *fp = fopen(<span class="stringliteral">&quot;record.xml&quot;</span>, <span class="stringliteral">&quot;w&quot;</span>);</div>
<div class="line"><span class="keywordflow">if</span> (fp != NULL)</div>
<div class="line">{</div>
<div class="line">  soap-&gt;sendfd = fileno(fp);           <span class="comment">// file descriptor to write to</span></div>
<div class="line">  soap_set_mode(soap, SOAP_XML_GRAPH); <span class="comment">// support id-ref w/o requiring SOAP</span></div>
<div class="line">  soap_clr_mode(soap, SOAP_XML_TREE);  <span class="comment">// if set, clear</span></div>
<div class="line">  <span class="keywordflow">if</span> (soap_write_ns__record(soap, &amp;pers1))</div>
<div class="line">    ... <span class="comment">// handle IO error</span></div>
<div class="line">      fclose(fp);</div>
<div class="line">  soap-&gt;sendfd = -1;                   <span class="comment">// block further writing</span></div>
<div class="line">}</div>
</div><!-- fragment --><p>which produces an XML document record.xml that is similar to: </p><pre class="fragment">&lt;ns:record xmlns:ns="urn:types" id="Joe"&gt;
  &lt;name&gt;Joe&lt;/name&gt;
  &lt;SSN&gt;1234567890&lt;/SSN&gt;
  &lt;spouse id="Jane"&gt;
    &lt;name&gt;Jane&lt;/name&gt;
    &lt;SSN&gt;1987654320&lt;/SSN&gt;
    &lt;spouse ref="#Joe"/&gt;
  &lt;/spouse&gt;
&lt;/ns:record&gt;
</pre><p>Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph leads to the same non-termination problem when we later try to copy the data into unmanaged space:</p>
<div class="fragment"><div class="line">soap *soap = soap_new1(SOAP_XML_GRAPH); <span class="comment">// support id-ref w/o SOAP</span></div>
<div class="line">...</div>
<div class="line">ns__record pers1;</div>
<div class="line">FILE *fp = fopen(<span class="stringliteral">&quot;record.xml&quot;</span>, <span class="stringliteral">&quot;r&quot;</span>);</div>
<div class="line"><span class="keywordflow">if</span> (fp != NULL)</div>
<div class="line">{</div>
<div class="line">  soap-&gt;recvfd = fileno(fp);            <span class="comment">// file descriptor to read from</span></div>
<div class="line">  <span class="keywordflow">if</span> (soap_read_ns__record(soap, &amp;pers1))</div>
<div class="line">    ... <span class="comment">// handle IO error</span></div>
<div class="line">  fclose(fp);</div>
<div class="line">  soap-&gt;recvfd = -1;                    <span class="comment">// block further reading</span></div>
<div class="line">}</div>
<div class="line">...</div>
<div class="line">ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &amp;pers1); <span class="comment">// BAD</span></div>
<div class="line">ns__record *pers4 = soap_dup_ns__record(soap, NULL, &amp;pers1); <span class="comment">// OK</span></div>
<div class="line">soap_set_mode(soap, SOAP_XML_TREE);</div>
<div class="line">ns__record *pers5 = soap_dup_ns__record(soap, NULL, &amp;pers1); <span class="comment">// OK</span></div>
</div><!-- fragment --><p>Copying data with <code>soap_dup_T(soap)</code> into managed space is always safe. Copying into unmanaged space requires diligence. But deleting unmanaged data is easy with <code>soap_del_T()</code>.</p>
<p>You can also use <code>soap_del_T()</code> to delete structures in C++, but only if these structures are created with <code>new</code> (and <code>new []</code> for arrays when applicable) for classes, structs, and class templates and with <code>malloc</code> for anything else, and the structures do NOT contain pointers to stack and static data.</p>
<h1><a class="anchor" id="features"></a>
Features and limitations                                             </h1>
<p>In general, to use the generated code:</p>
<ul>
<li>Make sure to <code>#include "soapH.h"</code> in your code and also define a namespace table or <code>#include "ns.nsmap"</code> with the generated table, where <code>ns</code> is the namespace prefix for services.</li>
<li>Use soapcpp2 option -j (C++ only) to generate C++ proxy and service objects. The auto-generated files include documented inferfaces. Compile with soapC.cpp and link with -lgsoap++, or alternatively compile stdsoap2.cpp.</li>
<li>Without soapcpp2 option -j: client-side uses the auto-generated soapClient.cpp and soapC.cpp (or C versions of those). Compile and link with -lgsoap++ (-lgsoap for C), or alternatively compile stdsoap2.cpp (stdsoap2.c for C).</li>
<li>Without soapcpp2 option -j: server-side uses the auto-generated soapServer.cpp and soapC.cpp (or C versions of those). Compile and link with -lgsoap++ (-lgsoap for C), or alternatively compile stdsoap2.cpp (stdsoap2.c for C).</li>
<li>Use <code>soap_new()</code> or <code>soap_new1(int flags)</code> to allocate and initialize a heap-allocated context with or without flags. Delete this context with <code>soap_free(struct soap*)</code>, but only after <code>soap_destroy(struct soap*)</code> and <code>soap_end(struct soap*)</code>.</li>
<li>Use <code>soap_init(struct *soap)</code> or <code>soap_init1(struct soap*, int flags)</code> to initialize a stack-allocated context with or without flags. End the use of this context with <code>soap_done(struct soap*)</code>, but only after <code>soap_destroy(struct soap*)</code> and <code>soap_end(struct soap*)</code>.</li>
</ul>
<p>There are several context initialization flags and context mode flags to control XML serialization at runtime:</p>
<ul>
<li><code>SOAP_C_UTFSTRING</code>: enables all <code>std::string</code> and <code>char*</code> strings to contain UTF-8 content. This option is recommended.</li>
<li><code>SOAP_XML_STRICT</code>: strictly validates XML while deserializing. Should not be used together with SOAP 1.1/1.2 encoding style of messaging. Use soapcpp2 option <code>-s</code> to hard code <code>SOAP_XML_STRICT</code> in the generated serializers. Not recommended with SOAP 1.1/1.2 encoding style messaging.</li>
<li><code>SOAP_XML_INDENT</code>: produces indented XML.</li>
<li><code>SOAP_XML_CANONICAL</code>: c14n canonocalization, removes unused <code>xmlns</code> bindings and adds them to appropriate places by applying c14n normalization rules. Should not be used together with SOAP 1.1/1.2 encoding style messaging.</li>
<li><code>SOAP_XML_TREE</code>: write tree XML without id-ref, while pruning data structure cycles to prevent nontermination of the serializer for cyclic structures.</li>
<li><code>SOAP_XML_GRAPH</code>: write graph (digraph and cyclic graphs with shared pointers to objects) using id-ref attributes. That is, XML with SOAP multi-ref encoded id-ref elements. This is a structure-preserving serialization format, because co-referenced data and also cyclic relations are accurately represented.</li>
<li><code>SOAP_XML_DEFAULTNS</code>: uses xmlns default namespace declarations, assuming that the schema attribute form is "qualified" by default (be warned if it is not, since attributes in the null namespace will get bound to namespaces!).</li>
<li><code>SOAP_XML_NOTYPE</code>: removes all xsi:type attribuation. This option is usually not needed unless the receiver rejects all xs:type attributes. This option may affect the quality of the deserializer, which relies on xsi:type attributes to distinguish base class instances from derived class instances transported in the XML payloads.</li>
</ul>
<p>Additional notes with respect to the wsdl2h and soapcpp2 tools:</p>
<ul>
<li>Nested classes, structs, and unions in a gSOAP header file are unnested by soapcpp2.</li>
<li>Use <code>#import "file.h"</code> instead of <code>#include</code> to import other header files in a gSOAP header file for soapcpp2. The <code>#include</code>, <code>#define</code>, and <code>#pragma</code> are accepted by soapcpp2, but are moved to the very start of the generated code for the C/C++ compiler to include before all generated definitions. Often it is useful to add an <code>#include</code> with a <a href="#toxsd9-2">volatile type</a> that includes the actual type declaration, and to ensure transient types are declared when these are used in a data binding interface declared in a gSOAP header file for soapcpp2.</li>
<li>To remove any SOAP-specific bindings, use soapcpp2 option <code>-0</code>.</li>
<li>A gSOAP header file for soapcpp2 should not include any code statements, only data type declarations. This includes constructor initialization lists that are not permitted. Use member initializations instead.</li>
<li>C++ namespaces are supported. Use wsdl2h option <code>-qname</code>. Or add a <code>namespace name { ... }</code> to the header file, but the <code>{ ... }</code> MUST cover the entire header file content from begin to end.</li>
<li>Optional XML DOM support can be used to store mixed content or literal XML content. Otherwise, mixed content may be lost. Use wsdl2h option <code>-d</code> for XML DOM support and compile and link with <code>dom.c</code> or <code>dom.cpp</code>. For details, see <a href="http://www.genivia.com/doc/dom/html">XML DOM and XPath</a>.</li>
</ul>
<h1><a class="anchor" id="nsmap"></a>
Removing SOAP namespaces from XML payloads                              </h1>
<p>The soapcpp2 tool generates a <code>.nsmap</code> file that includes two bindings for SOAP namespaces. We can remove all SOAP namespaces (and SOAP processing logic) with soapcpp2 option <code>-0</code> or by simply setting the two entries to NULL:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>Namespace namespaces[] =</div>
<div class="line">{</div>
<div class="line">  {<span class="stringliteral">&quot;SOAP-ENV&quot;</span>, NULL, NULL, NULL},</div>
<div class="line">  {<span class="stringliteral">&quot;SOAP-ENC&quot;</span>, NULL, NULL, NULL},</div>
<div class="line">  ...</div>
<div class="line">};</div>
</div><!-- fragment --><p>Note that once the <code>.nsmap</code> is generated, you can copy-paste the content into your project code. However, if we rerun wsdl2h on updated WSDL/XSD files or <code>typemap.dat</code> declarations then we need to use the updated table.</p>
<p>In cases that no XML namespaces are used at all, for example with <a href="http://www.genivia.com/doc/xml-rpc-json/html">XML-RPC</a>, you may use an empty namespace table:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>Namespace namespaces[] = {{NULL,NULL,NULL,NULL}};</div>
</div><!-- fragment --><p>However, beware that any built-in xsi attributes that are rendered will lack the proper namespace binding. At least we suggest to use <code>SOAP_XML_NOTYPE</code> for this reason.</p>
<h1><a class="anchor" id="examples"></a>
Examples                                                             </h1>
<p>Select the project files below to peruse the source code examples.</p>
<h2>Source files </h2>
<ul>
<li><code>address.xsd</code> Address book schema</li>
<li><code><a class="el" href="address_8cpp.html">address.cpp</a></code> Address book app (reads/writes address.xml file)</li>
<li><code>addresstypemap.dat</code> Schema namespace prefix name preference for wsdl2h</li>
<li><code><a class="el" href="graph_8h.html">graph.h</a></code> <a class="el" href="class_graph.html">Graph</a> data binding (tree, digraph, cyclic graph)</li>
<li><code><a class="el" href="graph_8cpp.html">graph.cpp</a></code> Test graph serialization as tree, digraph, and cyclic</li>
</ul>
<h2>Generated files </h2>
<ul>
<li><code><a class="el" href="address_8h.html">address.h</a></code> gSOAP-specific data binding definitions from address.xsd</li>
<li><code><a class="el" href="address_stub_8h.html">addressStub.h</a></code> C++ data binding definitions</li>
<li><code><a class="el" href="address_h_8h.html">addressH.h</a></code> Serializers</li>
<li><code>addressC.cpp</code> Serializers</li>
<li><code>address.xml</code> Address book data generated by address app</li>
<li><code><a class="el" href="graph_stub_8h.html">graphStub.h</a></code> C++ data binding definitions</li>
<li><code><a class="el" href="graph_h_8h.html">graphH.h</a></code> Serializers</li>
<li><code>graphC.cpp</code> Serializers</li>
<li><code>g.xsd</code> XSD schema with <code>g:<a class="el" href="class_graph.html">Graph</a></code> complexType</li>
<li><code>g.nsmap</code> xmlns bindings namespace mapping table</li>
</ul>
<h2>Build steps </h2>
<p>Building the AddressBook example: </p><pre class="fragment">wsdl2h -g -t addresstypemap.dat address.xsd
soapcpp2 -0 -CS -I../../import -p address address.h
c++ -I../.. address.cpp addressC.cpp -o address -lgsoap++
</pre><p>Option <code>-g</code> produces bindings for global (root) elements in addition to types. In this case the root element <code>a:address-book</code> is bound to <code><a class="el" href="class__a____address__book.html" title="The root element of the address book schema. ">_a__address_book</a></code>. The complexType <code>a:address</code> is bound to class <code><a class="el" href="classa____address.html" title="An address information item. ">a__address</a></code>, which is also the type of <code><a class="el" href="class__a____address__book.html" title="The root element of the address book schema. ">_a__address_book</a></code>. This option is not required, but allows you to use global element tag names when referring to their serializers, instead of their type name. Option <code>-0</code> removes the SOAP protocol. Options <code>-C</code> and <code>-S</code> removes client and server code generation. Option <code>-p</code> renames the output <code>soap</code> files to <code>address</code> files.</p>
<p>See the <code><a class="el" href="address_8cpp.html">address.cpp</a></code> implementation and <a href="pages.html">related pages</a>.</p>
<p>The <code>addresstypemap.dat</code> file specifies the XML namespace prefix for the bindings: </p><pre class="fragment">#       Bind the address book schema namespace to prefix 'a'

a = "urn:address-book-example"

#       By default the xsd:dateTime schema type is translated to time_t
#       To map xsd:dateTime to struct tm, enable the following line:

# xsd__dateTime = #import "../../custom/struct_tm.h"

#       ... and compile/link with custom/struct_tm.c
</pre><p>The DOB field is a xsd:dateTime, which is bound to <code>time_t</code> by default. To change this to <code>struct tm</code>, enable the import of the <code>xsd__dateTime</code> custom serializer by uncommenting the definition of <code>xsd__dateTime</code> in <code>addresstypemap.dat</code>. Then change <code>soap_dateTime2s</code> to <code>soap_xsd__dateTime2s</code> in the code.</p>
<p>Building the graph serialization example: </p><pre class="fragment">soapcpp2 -CS -I../../import -p graph graph.h
c++ -I../.. graph.cpp graphC.cpp -o graph -lgsoap++
</pre><p>To compile without using the <code>libgsoap++</code> library: simply compile <code>stdsoap2.cpp</code> together with the above.</p>
<h2>Usage </h2>
<p>To execute the AddressBook example: </p><pre class="fragment">./address
</pre><p>To execute the <a class="el" href="class_graph.html">Graph</a> serialization example: </p><pre class="fragment">./graph</pre> </div></div><!-- contents -->
</div><!-- doc-content -->
<hr class="footer">
<address class="footer">
Copyright (C) 2016, Robert van Engelen, Genivia Inc., All Rights Reserved.
</address>
<address class="footer"><small>
Converted on Sat Feb 6 2016 12:11:34 by <a target="_blank" href="http://www.doxygen.org/index.html">Doxygen</a> 1.8.10</small></address>
<br>
<div style="height: 246px; background: #DBDBDB;">
</body>
</html>