|  | Home | Documentation | README.md Source File updated Fri Aug 16 2024 by Robert van Engelen |

- Main Page
- Classes
- Files

- File List
- File Members

README.md
Go to the documentation of this file.
1
2 C and C++ XML Data Bindings {#mainpage}
3 ===========================
4
5 [TOC]
6
7 Introduction {#intro}
8 ============
9
10 This article presents a detailed overview of the gSOAP XML data bindings for C
11 and C++. The XML data bindings for C and C++ are extensively used with gSOAP
12 Web services to serialize C and C++ data in XML as part of the SOAP/XML Web
13 services payloads. Also REST XML with gSOAP relies on XML serialization of C
14 and C++ data via XML data bindings.
15
16 The major advantage of XML data bindings is that your application data is
17 always **type safe** in C and C++ by binding XML schema types to C/C++ types.
18 So integers in XML are bound to C integers, strings in XML are bound to C or
19 C++ strings, complex types in XML are bound to C structs or C++ classes, and so
20 on. The structured data you create and accept will fit the data model and is
21 **static type safe**. In other words, by leveraging strong typing in C/C++,
22 your XML data meets **XML schema validation requirements** and satisfies **XML
23 interoperability requirements**.
24
25 In fact, gSOAP data bindings are more powerful than simply representing C/C++
26 data in XML. The gSOAP tools implement true and tested **structure-preserving
27 serialization** of C/C++ data in XML, including the serialization of cyclic
28 graph structures with id-ref XML attributes. The gSOAP tools also generate
29 routines for deep copying and deep deletion of C/C++ data structures to
30 simplify memory management. In addition, C/C++ structures are deserialized
31 into managed memory, managed by the gSOAP `soap` context.
32
33 At the end of this article two examples are given to illustrate the application
34 of XML data bindings. The first simple example <i>`address.cpp`</i> shows how to use
35 wsdl2h to bind an XML schema to C++. The C++ application reads and writes an
36 XML file into and from a C++ "address book" data structure as a simple example.
37 The C++ data structure is a vector of address objects. The second example
38 <i>`graph.cpp`</i> shows how C++ data can be accurately serialized as a tree,
39 digraph, and cyclic graph in XML. The digraph and cyclic graph serialization
40 rules implement SOAP 1.1/1.2 multi-ref encoding with id-ref attributes to link
41 elements through IDREF XML references, creating a an XML graph with pointers to
42 XML nodes that preserves the structural integrity of the serialized C++ data.
43
44 These examples demonstrate XML data bindings only for relatively simple data
45 structures and types. The gSOAP tools support more than just these type of
46 structures to serialize in XML. There are practically no limits to the
47 serialization of C and C++ data types in XML.
48
49 Also the support for XML schema (XSD) components is unlimited. The wsdl2h tool
50 maps schemas to C and C++ using built-in intuitive mapping rules, while
51 allowing the mappings to be customized using a <i>`typemap.dat`</i> file with mapping
52 instructions for wsdl2h.
53
54 The information in this article is applicable to gSOAP 2.8.26 and greater that
55 support C++11 features. However, C++11 is not required. The material and the
56 examples in this article use plain C and C++, until the point where we
57 introduce C++11 smart pointers and scoped enumerations. While most of the
58 examples in this article are given in C++, the concepts also apply to C with
59 the exception of containers, smart pointers, classes and their methods. None
60 of these exceptions limit the use of the gSOAP tools for C in any way.
61
62 The data binding concepts described in this article were first envisioned in
63 1999 by Prof. Robert van Engelen at the Florida State University. An
64 implementation was created in 2000, named "stub/skeleton compiler". The first
65 articles on its successor version "gSOAP" appeared in 2002. The principle of
66 mapping XSD components to C/C++ types and vice versa is now widely adopted in
67 systems and programming languages, including Java web services and by C# WCF.
68
69 We continue to be committed to our goal to empower C/C++ developers with
70 powerful autocoding tools for XML. Our commitment started in the very early
71 days of SOAP by actively participating in
72 [SOAP interoperability testing](http://www.whitemesa.com/interop.htm),
73 participating in the development and testing of the
74 [W3C XML Schema Patterns for Databinding Interoperability](http://www.w3.org/2002/ws/databinding),
75 and continues by contributing to the development of
76 [OASIS open standards](https://www.oasis-open.org) in partnership with leading
77 IT companies in the world.
78
79 🔝 [Back to table of contents](#)
80
81 Notational Conventions {#conventions}
82 ======================
83
84 The typographical conventions used by this document are:
85
86 * `Courier` denotes C and C++ source code.
87
88 * <i>`Courier`</i> denotes XML content, JSON content, file and path names, and URIs.
89
90 * <b>`Courier`</b> denotes HTTP content, text file content, and shell commands with command line options and arguments.
91
92 The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
93 "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to
94 be interpreted as described in RFC-2119.
95
96 🔝 [Back to table of contents](#)
97
98 Mapping WSDL and XML schemas to C/C++ {#tocpp}
99 =====================================
100
101 To convert WSDL and XML schemas (XSD files) to code, we use the wsdl2h command
102 on the command line (or command prompt), after opening a terminal. The wsdl2h
103 command generates the data binding interface code that is saved to a special
104 Web services and data bindings interface header file with extension <i>`.h`</i>
105 that contains the WSDL service declarations and the data binding interface
106 declarations in a familiar C/C++ format:
107
108 wsdl2h [options] -o file.h ... XSD and WSDL files ...
109
110 This command converts WSDL and XSD files to C++ (or pure C with
111 <b>`wsdl2h -c`</b>) and saves the data binding interface to a interface header
112 file <i>`file.h`</i> that uses familiar C/C++ syntax extended with `//gsoap`
113 [directives](#directives) and annotations. Notational conventions are used in
114 the data binding interface to declare serializable C/C++ types and functions
115 for Web service operations.
116
117 The WSDL 1.1/2.0, SOAP 1.1/1.2, and XSD 1.0/1.1 standards are supported by the
118 gSOAP tools. In addition, the most popular WS specifications are also
119 supported, including WS-Addressing, WS-ReliableMessaging, WS-Discovery,
120 WS-Security, WS-Policy, WS-SecurityPolicy, and WS-SecureConversation.
121
122 This article focusses mainly on XML data bindings. XML data bindings for C/C++
123 bind XML schema types to C/C++ types. So integers in XML are bound to C
124 integers, strings in XML are bound to C or C++ strings, complex types in XML
125 are bound to C structs or C++ classes, and so on.
126
127 A data binding is dual, meaning supporting a two way direction for development.
128 Either you start with WSDLs and/or XML schemas that are mapped to equivalent
129 C/C++ types, or you start with C/C++ types that are mapped to XSD types.
130 Either way, the end result is that you can serialize C/C++ types in XML such
131 that your XML is an instance of XML schema(s) and is validated against these
132 schema(s).
133
134 This covers all of the following standard XSD components with their optional
135 attributes and properties:
136
137 XSD component | attributes and properties
138 -------------- | -------------------------
139 schema | targetNamespace, version, elementFormDefault, attributeFormDefault, defaultAttributes
140 attribute | name, ref, type, use, default, fixed, form, targetNamespace, wsdl:arrayType
141 element | name, ref, type, default, fixed, form, nillable, abstract, substitutionGroup, minOccurs, maxOccurs, targetNamespace
142 simpleType | name
143 complexType | name, abstract, mixed, defaultAttributesApply
144 all | *n/a*
145 choice | minOccurs, maxOccurs
146 sequence | minOccurs, maxOccurs
147 group | name, ref, minOccurs, maxOccurs
148 attributeGroup | name, ref
149 any | minOccurs, maxOccurs
150 anyAttribute | *n/a*
151
152 And also the following standard XSD directives are covered:
153
154 directive | description
155 ---------- | -----------
156 import | Imports a schema into the importing schema for referencing
157 include | Include schema component definitions into a schema
158 override | Override by replacing schema component definitions
159 redefine | Extend or restrict schema component definitions
160 annotation | Annotates a component
161
162 The XSD facets and their mappings to C/C++ are:
163
164 XSD facet | maps to
165 -------------- | -------
166 enumeration | `enum`
167 simpleContent | class/struct wrapper with `__item` member
168 complexContent | class/struct
169 list | `enum*` bitmask (`enum*` enumerates a bitmask up to 64 bits)
170 extension | class/struct inheritance/extension
171 restriction | `typedef` and class/struct inheritance/redeclaration
172 length | `typedef` with restricted content length annotation
173 minLength | `typedef` with restricted content length annotation
174 maxLength | `typedef` with restricted content length annotation
175 minInclusive | `typedef` with numerical value range restriction annotation
176 maxInclusive | `typedef` with numerical value range restriction annotation
177 minExclusive | `typedef` with numerical value range restriction annotation
178 maxExclusive | `typedef` with numerical value range restriction annotation
179 precision | `typedef` with pattern annotation (pattern used for output, but input is not validated)
180 scale | `typedef` with pattern annotation (pattern used for output, but input is not validated)
181 totalDigits | `typedef` with pattern annotation (pattern used for output, but input is not validated)
182 fractionDigits | `typedef` with pattern annotation (pattern used for output, but input is not validated)
183 pattern | `typedef` with pattern annotation (define `soap::fsvalidate` callback to validate patterns)
184 union | string with union of value
185
186 All primitive XSD types are supported, including but not limited to the
187 following XSD types:
188
189 XSD type | maps to
190 ---------------- | -------
191 any/anyType | `_XML` string with literal XML content (or enable DOM with wsdl2h option `-d`)
192 anyURI | string (i.e. `char*`, `wchar_t*`, `std::string`, `std::wstring`)
193 string | string (i.e. `char*`, `wchar_t*`, `std::string`, `std::wstring`)
194 boolean | `bool` (C++) or `enum xsd__boolean` (C)
195 byte | `char` (i.e. `int8_t`)
196 short | `short` (i.e. `int16_t`)
197 int | `int` (i.e. `int32_t`)
198 long | `LONG64` (i.e. `long long` and `int64_t`)
199 unsignedByte | `unsigned char` (i.e. `uint8_t`)
200 unsignedShort | `unsigned short` (i.e. `uint16_t`)
201 unsignedInt | `unsigned int` (i.e. `uint32_t`)
202 unsignedLong | `ULONG64` (i.e. `unsigned long long` and `uint64_t`)
203 float | `float`
204 double | `double`
205 integer | string or `#import "custom/int128.h"` to use 128 bit `xsd__integer`
206 decimal | string or `#import "custom/long_double.h"` to use `long double`
207 precisionDecimal | string
208 duration | string or `#import "custom/duration.h"` to use 64 bit `xsd__duration`
209 dateTime | `time_t` or `#import "custom/struct_tm.h"` to use `struct tm` for `xsd__dateTime`
210 time | string or `#import "custom/long_time.h"` to use 64 bit `xsd__time`
211 date | string or `#import "custom/struct_tm_date.h"` to use `struct tm` for `xsd__date`
212 hexBinary | special class/struct `xsd__hexBinary`
213 base64Binary | special class/struct `xsd__base64Binary`
214 QName | `_QName` string (URI normalization rules are applied)
215
216 All other primitive XSD types not listed above are mapped to strings, by
217 wsdl2h generating a `typedef` to string for these types. For example,
218 <i>`xsd:token`</i> is bound to a C++ or C string:
219
220 ~~~{.cpp}
221 typedef std::string xsd__token; // C++
222 typedef char *xsd__token; // C (wsdl2h option -c)
223 ~~~
224
225 This associates a compatible value space to the type with the appropriate XSD
226 type name used by the soapcpp2-generated serializers.
227
228 It is possible to remap types by adding the appropriate mapping rules to
229 <i>`typemap.dat`</i> as we will explain in more detail in the next section.
230
231 Imported custom serializers are intended to extend the C/C++ type bindings when
232 the default binding to string is not satisfactory to your taste and if the
233 target platform supports these C/C++ types. To add custom serializers to
234 <i>`typemap.dat`</i> for wsdl2h, see [adding custom serializers](#custom) below.
235
236 🔝 [Back to table of contents](#)
237
238 Using typemap.dat to customize data bindings {#typemap}
239 ============================================
240
241 Use a <i>`typemap.dat`</i> file to redefine namespace prefixes and to customize type
242 bindings for the the generated header files produced by the wsdl2h tool. The
243 <i>`typemap.dat`</i> is the default file processed by wsdl2h. Use <b>`wsdl2h -tfile.dat`</b>
244 option <b>`-tfile.dat`</b> to specify a different mapping file <i>`file.dat`</i>.
245
246 Declarations in <i>`typemap.dat`</i> can be broken up over multiple lines by
247 continuing on the next line by ending each line to be continued with a
248 backslash <b>`\`</b>. The limit is 4095 characters per line, whether the line is
249 broken up or not.
250
251 🔝 [Back to table of contents](#)
252
253 XML namespace bindings {#typemap1}
254 ----------------------
255
256 The wsdl2h tool generates C/C++ type declarations that use `ns1`, `ns2`, etc.
257 as schema-binding URI prefixes. These default prefixes are generated somewhat
258 arbitrarily for each schema targetNamespace URI, meaning that their ordering
259 may change depending on the WSDL and XSD order of processing with wsdl2h.
260
261 Therefore, it is **strongly recommended** to declare your own prefix for each
262 schema URI in <i>`typemap.dat`</i> to reduce maintaince effort of your code. This
263 is more robust when anticipating possible changes of the schema(s) and/or the
264 binding URI(s) and/or the tooling algorithms.
265
266 The first and foremost important thing to do is to define prefix-URI bindings
267 for our C/C++ code by adding the following line(s) to our <i>`typemap.dat`</i> or make
268 a copy of this file and add the line(s) that bind our choice of prefix name to
269 each URI:
270
271 prefix = "URI"
272
273 For example, to use `g` as a prefix for the "urn:graph" XML namespace:
274
275 g = "urn:graph"
276
277 This produces `g__name` C/C++ type names that are bound to the "urn:graph"
278 schema by association of `g` to the generated C/C++ types.
279
280 This means that <i>`<g:name xmlns:g="urn:graph">`</i> is parsed as an instance of a
281 `g__name` C/C++ type. Also <i>`<x:name xmlns:x="urn:graph">`</i> parses as an
282 instance of `g__name`, because the prefix <i>`x`</i> has the same URI value
283 <i>`urn:graph`</i>. Prefixes in XML have local scopes (like variables in a block).
284
285 The first run of wsdl2h will reveal the XML namespace URIs, so you do not need
286 to search WSDLs and XSD files for all of the target namespaces. Just copy them
287 from the generated header file after the first run into <i>`typemap.dat`</i> for
288 editing.
289
290 @note Only define a namespace prefix once in <i>`typemap.dat`</i>. That is, do not
291 use the same prefix for multiple XML namespace URIs. This is to avoid
292 namespace conflicts that may cause failed builds and failures in XML parsing
293 and XML schema validation.
294
295 🔝 [Back to table of contents](#)
296
297 XSD type bindings {#typemap2}
298 -----------------
299
300 Custom C/C++ type bindings can be declared in <i>`typemap.dat`</i> to associate C/C++
301 types with specific schema types. These type bindings have four parts:
302
303 prefix__type = declaration | use | ptr-use
304
305 where
306
307 - <b>`prefix__type`</b> is the schema type to be customized (the <b>`prefix__type`</b> name
308 uses the common double underscore naming convention);
309
310 - <b>`declaration`</b> declares the C/C++ type in the wsdl2h-generated header file.
311 This part can be empty if no explicit declaration is needed;
312
313 - <b>`use`</b> is an optional part that specifies how the C/C++ type is used in the
314 code. When omitted, it is the same as <b>`prefix__type`</b>;
315
316 - <b>`ptr-use`</b> is an optional part that specifies how the type is used as a pointer
317 type. By default it is the <b>`use`</b> type name with a <b>`*`</b> or C++11
318 <b>`std::shared_ptr<type>`</b> when enabled (see further below). If <b>`use`</b> is already a
319 pointer type by the presence of a <b>`*`</b> in the <b>`use`</b> part, then the default
320 <b>`ptr-use`</b> type is the same as the <b>`use`</b> type (that is, no double
321 pointers <b>`**`</b> will be created in this case).
322
323 For example, to map <i>`xsd:duration`</i> to a `long long` (`LONG64`) type that holds
324 millisecond duration values, we can use the custom serializer declared in
325 <i>`gsoap/custom/duration.h`</i> by adding the following line to <i>`typemap.dat`</i>:
326
327 xsd__duration = #import "custom/duration.h"
328
329 Here, we omitted the second and third parts, because `xsd__duration` is the
330 name that wsdl2h uses for this type in our generated code so we should leave
331 the <b>`use`</b> part unspecified. The third part is omitted to let wsdl2h use
332 `xsd__duration *` for pointers or `std::shared_ptr<xsd__duration>` if smart
333 pointers are enabled.
334
335 To map <i>`xsd:string`</i> to `wchar_t*` wide strings for C source code output:
336
337 xsd__string = | wchar_t* | wchar_t*
338
339 For C++ we can use the `std::wstring` wide string:
340
341 xsd__string = | std::wstring
342
343 Note that the first part is empty, because these types do not require a
344 declaration. A <b>`ptr-use`</b> part is also defined for `wchar_t*`, but this
345 is actually needed because the wsdl2h tool recognizes that the <b>`use`</b>
346 part `wchar_t*` is already a pointer. By contrast, when using 8-bit strings,
347 it is recommended to use the `SOAP_C_UTFSTRING` flag to enable UTF-8 formatted
348 strings.
349
350 When the <b>`ptr-use`</b> part is not specified, it will be auto-generated
351 as pointer `T*` for <b>`use`</b> type `T` or `std::shared_ptr<T>` when
352 the variable `$POINTER = std::shared`, see
353 [the special variables $CONTAINER, $POINTER, $SIZE and $OPTIONAL](#typemap5).
354
355 The <b>`declaration`</b> part need not be empty, for example if a type must be
356 declared. For example:
357
358 xsd__string = typedef std::string mystring; | mystring | std::optional<mystring>
359
360 When a auto-generated <b>`declaration`</b> should be preserved but the
361 <b>`use`</b> or <b>`ptr-use`</b> parts must be redefined, then we use an
362 ellipsis for the <b>`declaration`</b> part:
363
364 prefix__type = ... | use | ptr-use
365
366 The <b>`ptr-use`</b> part is also useful to map schema polymorphic types to C
367 types for example, where we need to be able to both handle a base type and its
368 extensions as per schema extensibility. Say we have a base type called
369 <i>`ns:base`</i> that is extended, then we can remap this to a C type that
370 permits referening the extended types via a `void*` as follows:
371
372 ns__base = ... | int __type_base; void*
373
374 such that `__type_base` and `void*` will be used to (de)serialize any data
375 type, including base and its derived types. The `__type_base` integer is set
376 to a `SOAP_TYPE_T` value to indicate what type of data the `void*` pointer
377 points to.
378
379 🔝 [Back to table of contents](#)
380
381 Custom serializers for XSD types {#custom}
382 --------------------------------
383
384 In the previous part we saw how a custom serializer is used to bind
385 <i>`xsd:duration`</i> to a `long long` (`LONG64` or `int64_t`) type to store millisecond
386 duration values:
387
388 xsd__duration = #import "custom/duration.h"
389
390 The `xsd__duration` type is an alias of `long long` (`LONG64` or `int64_t`).
391
392 While wsdl2h will use this binding declared in <i>`typemap.dat`</i>
393 automatically, you will also need to compile <i>`gsoap/custom/duration.c`</i>.
394 Each custom serializer has an interface header file to be imported into another
395 interface header file that declares the custom type for soapcpp2 and a
396 serializer implementation file written in C, which should be compiled with the
397 application. You can compile these in C++ (rename files to <i>`.cpp`</i> if
398 needed).
399
400 A custom serializer is declared in an interface header file for soapcpp2 using
401 `extern typedef`. The typedef name declared is serializable, whereas the
402 type on which it is based is not serializable. This declaration can be
403 combined with `volatile` when the type should not be redeclared, see
404 [volatile classes and structs](#toxsd9-2). For example, the custom serializer
405 for `struct tm` is the type `xsd__datetime` declared as follows in
406 `gsoap/custom/struct_tm.h`:
407
408 ~~~{.cpp}
409 extern typedef volatile struct tm
410 {
411 int tm_sec; ///< seconds (0 - 60)
412 int tm_min; ///< minutes (0 - 59)
413 int tm_hour; ///< hours (0 - 23)
414 int tm_mday; ///< day of month (1 - 31)
415 int tm_mon; ///< month of year (0 - 11)
416 int tm_year; ///< year - 1900
417 int tm_wday; ///< day of week (Sunday = 0) (NOT USED)
418 int tm_yday; ///< day of year (0 - 365) (NOT USED)
419 int tm_isdst; ///< is summer time in effect?
420 char* tm_zone; ///< abbreviation of timezone (NOT USED)
421 } xsd__dateTime;
422 ~~~
423
424 Another example is `xsd__duration` as a custom serializer for the C++11 type
425 `std::chrono::nanoseconds`:
426
427 ~~~{.cpp}
428 extern typedef class std::chrono::nanoseconds xsd__duration;
429 ~~~
430
431 Next, we present all pre-defined custom serializers that are available to you.
432
433 🔝 [Back to table of contents](#)
434
435 ### xsd:integer {#custom-1}
436
437 The wsdl2h tool maps <i>`xsd:integer`</i> to a string by default. To map <i>`xsd:integer`</i> to
438 the 128 bit big int type `__int128_t`:
439
440 xsd__integer = #import "custom/int128.h"
441
442 The `xsd__integer` type is an alias of `__int128_t`.
443
444 @warning Beware that the <i>`xsd:integer`</i> value space of integers is in principle
445 unbounded and values can be of arbitrary length. A value range fault
446 `SOAP_TYPE` (value exceeds native representation) or `SOAP_LENGTH` (value
447 exceeds range bounds) will be thrown by the deserializer if the value is out of
448 range.
449
450 Other XSD integer types that are restrictions of <i>`xsd:integer`</i>, are
451 <i>`xsd:nonNegativeInteger`</i> and <i>`xsd:nonPositiveInteger`</i>, which are further restricted
452 by <i>`xsd:positiveInteger`</i> and <i>`xsd:negativeInteger`</i>. To bind these types to
453 `__int128_t` add the following definitions to <i>`typemap.dat`</i>:
454
455 xsd__nonNegativeInteger = typedef xsd__integer xsd__nonNegativeInteger 0 : ;
456 xsd__nonPositiveInteger = typedef xsd__integer xsd__nonPositiveInteger : 0 ;
457 xsd__positiveInteger = typedef xsd__integer xsd__positiveInteger 1 : ;
458 xsd__negativeInteger = typedef xsd__integer xsd__negativeInteger : -1 ;
459
460 Or simply uncomment these definitions in <i>`typemap.dat`</i> when you are using the
461 latest gSOAP releases.
462
463 @note If `__int128_t` 128 bit integers are not supported on your platform and if it
464 is certain that <i>`xsd:integer`</i> values are within 64 bit value bounds for your
465 application's use, then you can map this type to `LONG64`:
466
467 xsd__integer = typedef LONG64 xsd__integer;
468
469 @note Again, a value range fault `SOAP_TYPE` or `SOAP_LENGTH` will be thrown by
470 the deserializer if the value is out of range.
471
472 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/int128.c`</i> with your project.
473
474 @see Section [numerical types](#toxsd5).
475
476 🔝 [Back to table of contents](#)
477
478 ### xsd:decimal {#custom-2}
479
480 The wsdl2h tool maps <i>`xsd:decimal`</i> to a string by default. To map <i>`xsd:decimal`</i> to
481 extended precision floating point:
482
483 xsd__decimal = #import "custom/long_double.h" | long double
484
485 By contrast to all other custom serializers, this serializer enables `long
486 double` natively without requiring a new binding name (`xsd__decimal` is NOT
487 defined).
488
489 If your system supports <i>`quadmath.h`</i> quadruple precision floating point
490 `__float128`, you can map <i>`xsd:decimal`</i> to `xsd__decimal` that is an alias of
491 `__float128`:
492
493 xsd__decimal = #import "custom/float128.h"
494
495 @warning Beware that <i>`xsd:decimal`</i> is in principle a decimal value with arbitraty
496 lengths. A value range fault `SOAP_TYPE` will be thrown by the deserializer if
497 the value is out of range.
498
499 In the XML payload the special values <i>`INF`</i>, <i>`-INF`</i>, <i>`NaN`</i>
500 represent plus or minus infinity and not-a-number, respectively.
501
502 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/long_double.c`</i> with your
503 project.
504
505 @see Section [numerical types](#toxsd5).
506
507 🔝 [Back to table of contents](#)
508
509 ### xsd:dateTime {#custom-3}
510
511 The wsdl2h tool maps <i>`xsd:dateTime`</i> to `time_t` by default.
512
513 The trouble with `time_t` when represented as 32 bit `long` integers is that it
514 is limited to dates between 1970 and 2038. A 64 bit `time_t` is safe to use if
515 the target platform supports it, but lack of 64 bit `time_t` portability may
516 still cause date range issues.
517
518 For this reason `struct tm` should be used to represent wider date ranges. This
519 custom serializer avoids using date and time information in `time_t`. You get
520 the raw date and time information. You only lose the day of the week
521 information. It is always Sunday (`tm_wday=0`).
522
523 To map <i>`xsd:dateTime`</i> to `xsd__dateTime` which is an alias of `struct tm`:
524
525 xsd__dateTime = #import "custom/struct_tm.h"
526
527 If the limited date range of `time_t` is not a problem but you want to increase
528 the time precision with fractional seconds, then we suggest to map <i>`xsd:dateTime`</i>
529 to `struct timeval`:
530
531 xsd__dateTime = #import "custom/struct_timeval.h"
532
533 If the limited date range of `time_t` is not a problem but you want to use the
534 C++11 time point type `std::chrono::system_clock::time_point` (which internally
535 uses `time_t`):
536
537 xsd__dateTime = #import "custom/chrono_time_point.h"
538
539 Again, we should make sure that the dates will not exceed the date range when
540 using the default `time_t` binding for <i>`xsd:dateTime`</i> or when binding
541 <i>`xsd:dateTime`</i> to `struct timeval` or to `std::chrono::system_clock::time_point`.
542 These are safe to use in applications that use <i>`xsd:dateTime`</i> to record date
543 stamps within a given window. Otherwise, we recommend the `struct tm` custom
544 serializer.
545
546 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/struct_tm.c`</i> with your
547 project.
548
549 You could even map <i>`xsd:dateTime`</i> to a plain string (use `char*` with C and
550 `std::string` with C++). For example:
551
552 xsd__dateTime = | char*
553
554 @see Section [date and time types](#toxsd7).
555
556 🔝 [Back to table of contents](#)
557
558 ### xsd:date {#custom-4}
559
560 The wsdl2h tool maps <i>`xsd:date`</i> to a string by default. We can map <i>`xsd:date`</i> to
561 `struct tm`:
562
563 xsd__date = #import "custom/struct_tm_date.h"
564
565 The `xsd__date` type is an alias of `struct tm`. The serializer ignores the
566 time part and the deserializer only populates the date part of the struct,
567 setting the time to 00:00:00. There is no unreasonable limit on the date range
568 because the year field is stored as an integer (`int`).
569
570 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/struct_tm_date.c`</i> with your
571 project.
572
573 @see Section [date and time types](#toxsd7).
574
575 🔝 [Back to table of contents](#)
576
577 ### xsd:time {#custom-5}
578
579 The wsdl2h tool maps <i>`xsd:time`</i> to a string by default. We can map <i>`xsd:time`</i> to
580 an `unsigned long long` (`ULONG64` or `uint64_t`) integer with microsecond time
581 precision:
582
583 xsd__time = #import "custom/long_time.h"
584
585 This type represents 00:00:00.000000 to 23:59:59.999999, from `0` to an upper
586 bound of `86399999999`. A microsecond resolution means that a 1 second
587 increment requires an increment of 1000000 in the integer value. The serializer
588 adds a UTC time zone.
589
590 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/long_time.c`</i> with your
591 project.
592
593 @see Section [date and time types](#toxsd7).
594
595 🔝 [Back to table of contents](#)
596
597 ### xsd:duration {#custom-6}
598
599 The wsdl2h tool maps <i>`xsd:duration`</i> to a string by default, unless <i>`xsd:duration`</i>
600 is mapped to a `long long` (`LONG64` or `int64_t`) type with with millisecond
601 (ms) time duration precision:
602
603 xsd__duration = #import "custom/duration.h"
604
605 The `xsd__duration` type is a 64 bit signed integer that can represent
606 106,751,991,167 days forwards (positive) and backwards (negative) in time in
607 increments of 1 ms (1/1000 of a second).
608
609 Rescaling of the duration value by may be needed when adding the duration value
610 to a `time_t` value, because `time_t` may or may not have a seconds resolution,
611 depending on the platform and possible changes to `time_t`.
612
613 Rescaling is done automatically when you add a C++11 `std::chrono::nanoseconds`
614 value to a `std::chrono::system_clock::time_point` value. To use
615 `std::chrono::nanoseconds` as <i>`xsd:duration`</i>:
616
617 xsd__duration = #import "custom/chrono_duration.h"
618
619 This type can represent 384,307,168 days (2^63 nanoseconds) forwards and
620 backwards in time in increments of 1 ns (1/1,000,000,000 of a second).
621
622 Certain observations with respect to receiving durations in years and months
623 apply to both of these serializer decoders for <i>`xsd:duration`</i>.
624
625 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/duration.c`</i> with your
626 project.
627
628 @see Section [time duration types](#toxsd8).
629
630 🔝 [Back to table of contents](#)
631
632 Custom Qt serializers for XSD types {#qt}
633 -----------------------------------
634
635 The gSOAP distribution includes several custom serializers for Qt types. Also
636 Qt container classes are supported, see
637 [the special variables $CONTAINER, $POINTER, $SIZE and $OPTIONAL](#typemap5).
638
639 This feature requires gSOAP 2.8.34 or higher and Qt 4.8 or higher.
640
641 Each Qt custom serializer has an interface header file for soapcpp2 and a C++
642 implementation file to be compiled with your project.
643
644 Other Qt primitive types that are Qt `typedef`s of C/C++ types do not require a
645 custom serializer.
646
647 🔝 [Back to table of contents](#)
648
649 ### xsd:string {#qt-1}
650
651 To use Qt strings instead of C++ strings, add the following definition to
652 <i>`typemap.dat`</i>:
653
654 xsd__string = #import "custom/qstring.h"
655
656 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qstring.cpp`</i> with your
657 project.
658
659 🔝 [Back to table of contents](#)
660
661 ### xsd:base64Binary {#qt-2}
662
663 To use Qt byte arrays for <i>`xsd:base64Binary`</i> instead of the
664 `xsd__base64Binary` class, add the following definition to <i>`typemap.dat`</i>:
665
666 xsd__base64Binary = #import "custom/qbytearray_base64.h"
667
668 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qbytearray_base64.cpp`</i> with
669 your project.
670
671 🔝 [Back to table of contents](#)
672
673 ### xsd:hexBinary {#qt-3}
674
675 To use Qt byte arrays for <i>`xsd:hexBinary`</i> instead of the `xsd__base64Binary`
676 class, add the following definition to <i>`typemap.dat`</i>:
677
678 xsd__hexBinary = #import "custom/qbytearray_hex.h"
679
680 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qbytearray_hex.cpp`</i> with
681 your project.
682
683 🔝 [Back to table of contents](#)
684
685 ### xsd:dateTime {#qt-4}
686
687 To use Qt QDateTime for <i>`xsd:dateTime`</i>, add the following definition to
688 <i>`typemap.dat`</i>:
689
690 xsd__dateTime = #import "custom/datetime.h"
691
692 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qdatetime.cpp`</i> with
693 your project.
694
695 🔝 [Back to table of contents](#)
696
697 ### xsd:date {#qt-5}
698
699 To use Qt QDate for <i>`xsd:date`</i>, add the following definition to
700 <i>`typemap.dat`</i>:
701
702 xsd__date = #import "custom/qdate.h"
703
704 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qdate.cpp`</i> with your
705 project.
706
707 🔝 [Back to table of contents](#)
708
709 ### xsd:time {#qt-6}
710
711 To use Qt QDate for <i>`xsd:time`</i>, add the following definition to
712 <i>`typemap.dat`</i>:
713
714 xsd__time = #import "custom/qtime.h"
715
716 After running wsdl2h and soapcpp2, compile <i>`gsoap/custom/qtime.cpp`</i> with your
717 project.
718
719 🔝 [Back to table of contents](#)
720
721 Class/struct member additions {#typemap3}
722 -----------------------------
723
724 All generated classes and structs can be augmented with additional
725 members such as methods, constructors and destructors, and private members:
726
727 prefix__type = $ member-declaration
728
729 For example, we can add method declarations and private members to a class, say
730 `ns__record` as follows:
731
732 ns__record = $ ns__record(const ns__record &); // copy constructor
733 ns__record = $ void print(); // a print method
734 ns__record = $ private: int status; // a private member
735
736 Method declarations cannot include any code, because soapcpp2's input permits
737 only type declarations, not code.
738
739 🔝 [Back to table of contents](#)
740
741 Replacing XSD types by equivalent alternatives {#typemap4}
742 ----------------------------------------------
743
744 Type replacements can be given to replace one type entirely with another given
745 type:
746
747 prefix__type1 == prefix__type2
748
749 This replaces all `prefix__type1` by `prefix__type2` in the wsdl2h output.
750
751 @warning Do not agressively replace types, because this can cause XML schema
752 validation to fail when a value-type mismatch is encountered in the XML input.
753 Therefore, only replace similar types with other similar types that are wider
754 (e.g. `short` by `int` and `float` by `double`).
755
756 🔝 [Back to table of contents](#)
757
758 The special variables $CONTAINER, $POINTER, $SIZE and $OPTIONAL {#typemap5}
759 ---------------------------------------------------------------
760
761 The <i>`typemap.dat`</i> <b>`$CONTAINER`</b> variable defines the container type to use in
762 the wsdl2h-generated declarations for C++, which is `std::vector` by default.
763 For example, to use `std::list` as the container in the wsdl2h-generated
764 declarations we add the following line to <i>`typemap.dat`</i>:
765
766 $CONTAINER = std::list
767
768 Also a Qt container can be used instead of the default `std::vector`, for
769 example `QVector`:
770
771 [
772 #include <QVector>
773 ]
774 $CONTAINER = QVector
775
776 To remove containers, use <b>`wsdl2h -s`</b>. This also removes `std::string`,
777 but you can re-introduce `std::string` with
778 <b>`xsd__string = | std::string`</b> in <i>`typemap.dat`</i>.
779
780 The <i>`typemap.dat`</i> <b>`$POINTER`</b> variable defines the smart pointer to use in the
781 wsdl2h-generated declarations for C++, which replaces the use of `*` pointers.
782 For example:
783
784 $POINTER = std::shared_ptr
785
786 Not all pointers in the generated output are replaced by smart pointers by
787 wsdl2h, such as pointers as union members and pointers as struct/class members
788 that point to arrays of values.
789
790 @note The standard smart pointer `std::shared_ptr` is generally safe to use.
791 Other smart pointers such as `std::unique_ptr` and `std::auto_ptr` may cause
792 compile-time errors when classes have smart pointer members but no copy
793 constructor (a default copy constructor). A copy constructor is required for
794 non-shared smart pointer copying or swapping.
795
796 Alternatives to `std::shared_ptr` of the form `NAMESPACE::shared_ptr` can be
797 assigned to <b>`$POINTER`</b> when the namespace `NAMESPACE` also implements
798 `NAMESPACE::make_shared` and when the shared pointer class provides `reset()`
799 and`get()` methods and the dereference operator. For example Boost
800 `boost::shared_ptr`:
801
802 [
803 #include <boost/shared_ptr.hpp>
804 ]
805 $POINTER = boost::shared_ptr
806
807 The user-defined content between <b>`[`</b> and <b>`]`</b> ensures that we include the Boost
808 header files that are needed to support `boost::shared_ptr` and
809 `boost::make_shared`.
810
811 The variable <b>`$SIZE`</b> defines the type of array sizes, which is `int` by
812 default. For example, to change array size types to `size_t`:
813
814 $SIZE = size_t
815
816 Permissible types are `int` and `size_t`. This variable does not affect the
817 size of dynamic arrays, `xsd__hexBinary` and `xsd__base64Binary` types, which
818 is always `int`.
819
820 When C++17 is enabled with wsdl2h and soapcpp2 option <b>`-c++17`</b>, you can
821 also semi-automatically enable `std::optional` declarations with optional class
822 and structure member variables. This means that `std::optional` is used
823 instead of a (smart) pointer to make a member optional.
824
825 To enable `std::optional` with member variables that are primitive types,
826 `typedef`, and `enum` automatically:
827
828 $OPTIONAL = std::optional
829
830 Local unnamed simpleType restrictions may not adopt the specified optional type
831 and still use pointers instead. This limitation may be lifted in a future
832 release.
833
834 🔝 [Back to table of contents](#)
835
836 User-defined content {#typemap6}
837 --------------------
838
839 Any other content to be generated by wsdl2h can be included in <i>`typemap.dat`</i> by
840 enclosing it within brackets <b>`[`</b> and <b>`]`</b> anywhere in the <i>`typemap.dat`</i> file.
841 Each of the two brackets must appear at the start of a new line.
842
843 For example, we can add an `#import "wsa5.h"` to the wsdl2h-generated output as
844 follows:
845
846 [
847 #import "import/wsa5.h"
848 ]
849
850 which emits the `#import "import/wsa5.h"` literally at the start of the
851 wsdl2h-generated header file.
852
853 🔝 [Back to table of contents](#)
854
855 Mapping C/C++ to XML schema {#toxsd}
856 ===========================
857
858 The soapcpp2 command generates the data binding implementation code from a data
859 binding interface <i>`file.h`</i>:
860
861 soapcpp2 [options] file.h
862
863 where <i>`file.h`</i> is a interface header file that declares the XML data
864 binding interface. The <i>`file.h`</i> is typically generated by wsdl2h, but
865 you can also declare one yourself. If so, add `//gsoap`
866 [directives](#directives) and declare in this file all our C/C++ types you want
867 to serialize in XML.
868
869 You can also declare functions that will be converted to Web service operations
870 by soapcpp2. Global function declarations define service operations, which are
871 of the form:
872
873 ~~~{.cpp}
874 int prefix__func(arg1, arg2, ..., argn, result);
875 ~~~
876
877 where `arg1`, `arg2`, ..., `argn` are formal argument declarations of the input
878 and `result` is a formal argument for the output, which must be a pointer or
879 reference to the result object to be populated. More information on declaring
880 and implementing service operation functions can be found in the
881 [gSOAP user guide.](../../guide/html/index.html)
882
883 🔝 [Back to table of contents](#)
884
885 Overview of serializable C/C++ types {#toxsd1}
886 ------------------------------------
887
888 The following C/C++ types are supported by soapcpp2 and mapped to XSD types
889 and constructs. See the subsections below for more details or follow the links.
890
891 🔝 [Back to table of contents](#)
892
893 ### List of Boolean types
894
895 Boolean Type | Notes
896 ----------------------------- | -----
897 `bool` | C++ bool
898 `enum xsd__boolean` | C alternative to C++ `bool` with `false_` and `true_`
899
900 @see Section [C++ bool and C alternative](#toxsd3).
901
902 🔝 [Back to table of contents](#)
903
904 ### List of enumeration and bitmask types
905
906 Enumeration Type | Notes
907 ----------------------------- | -----
908 `enum` | enumeration
909 `enum class` | C++11 scoped enumeration, requires `soapcpp2 -c++11`
910 `enum*` | a bitmask that enumerates values 1, 2, 4, 8, ...
911 `enum* class` | C++11 scoped enumeration bitmask, requires `soapcpp2 -c++11`
912
913 @see Section [enumerations and bitmasks](#toxsd4).
914
915 🔝 [Back to table of contents](#)
916
917 ### List of numerical types
918
919 Numerical Type | Notes
920 ----------------------------- | -----
921 `char` | byte
922 `short` | 16 bit integer
923 `int` | 32 bit integer
924 `long` | 32 bit integer
925 `LONG64` | 64 bit integer
926 `xsd__integer` | 128 bit integer, use `#import "custom/int128.h"`
927 `long long` | same as `LONG64`
928 `unsigned char` | unsigned byte
929 `unsigned short` | unsigned 16 bit integer
930 `unsigned int` | unsigned 32 bit integer
931 `unsigned long` | unsigned 32 bit integer
932 `ULONG64` | unsigned 64 bit integer
933 `unsigned long long` | same as `ULONG64`
934 `int8_t` | same as `char`
935 `int16_t` | same as `short`
936 `int32_t` | same as `int`
937 `int64_t` | same as `LONG64`
938 `uint8_t` | same as `unsigned char`
939 `uint16_t` | same as `unsigned short`
940 `uint32_t` | same as `unsigned int`
941 `uint64_t` | same as `ULONG64`
942 `size_t` | transient type (not serializable)
943 `float` | 32 bit float
944 `double` | 64 bit float
945 `long double` | extended precision float, use `#import "custom/long_double.h"`
946 `xsd__decimal` | `quadmath.h` library 128 bit quadruple precision float, use `#import "custom/float128.h"`
947 `typedef` | declares a type name, with optional value range and string length bounds
948
949 @see Section [numerical types](#toxsd5).
950
951 🔝 [Back to table of contents](#)
952
953 ### List of string types
954
955 String Type | Notes
956 ----------------------------- | -----
957 `char*` | string (may contain UTF-8 with flag `SOAP_C_UTFSTRING`)
958 `wchar_t*` | wide string
959 `std::string` | C++ string (may contain UTF-8 with flag `SOAP_C_UTFSTRING`)
960 `std::wstring` | C++ wide string
961 `char[N]` | fixed-size string, requires `soapcpp2 -b`
962 `_QName` | normalized QName content
963 `_XML` | literal XML string content with wide characters in UTF-8
964 `typedef` | declares a new string type name, may restrict string length
965
966 @see Section [string types](#toxsd6).
967
968 🔝 [Back to table of contents](#)
969
970 ### List of date and time types
971
972 Date and Time Type | Notes
973 --------------------------------------- | -----
974 `time_t` | date and time point since epoch
975 `struct tm` | date and time point, use `#import "custom/struct_tm.h"`
976 `struct tm` | date point, use `#import "custom/struct_tm_date.h"`
977 `struct timeval` | date and time point, use `#import "custom/struct_timeval.h"`
978 `unsigned long long` | time point in microseconds, use `#import "custom/long_time.h"`
979 `std::chrono::system_clock::time_point` | date and time point, use `#import "custom/chrono_time_point.h"`
980
981 @see Section [date and time types](#toxsd7).
982
983 🔝 [Back to table of contents](#)
984
985 ### List of time duration types
986
987 Time Duration Type | Notes
988 ----------------------------- | -----
989 `long long` | duration in milliseconds, use `#import "custom/duration.h"`
990 `std::chrono::nanoseconds` | duration in nanoseconds, use `#import "custom/chrono_duration.h"`
991
992 @see Section [time duration types](#toxsd8).
993
994 🔝 [Back to table of contents](#)
995
996 ### List of classes, structs, unions, pointers, containers, and arrays
997
998 Classes, Structs, and Members | Notes
999 ----------------------------- | -----
1000 `class` | C++ class with single inheritance only
1001 `struct` | C struct or C++ struct without inheritance
1002 `std::shared_ptr<T>` | C++11 smart shared pointer
1003 `std::unique_ptr<T>` | C++11 smart pointer
1004 `std::auto_ptr<T>` | C++ smart pointer
1005 `std::deque<T>` | use `#import "import/stldeque.h"`
1006 `std::list<T>` | use `#import "import/stllist.h"`
1007 `std::vector<T>` | use `#import "import/stlvector.h"`
1008 `std::set<T>` | use `#import "import/stlset.h"`
1009 `template<T> class` | a container with `begin()`, `end()`, `size()`, `clear()`, and `insert()` methods
1010 `T*` | pointer to data of type `T`
1011 `T*` | as a class or struct member: points to data of type `T` or array of `T` with member `__size`
1012 `T[N]` | as a class or struct member: fixed-size array of type `T`
1013 `union` | as a class or struct member: requires a variant selector member `__union`
1014 `void*` | as a class or struct member: requires a `__type` member to indicate the type of object pointed to
1015
1016 @see Section [classes and structs](#toxsd9).
1017
1018 🔝 [Back to table of contents](#)
1019
1020 ### List of special classes and structs
1021
1022 Special Classes and Structs | Notes
1023 ----------------------------- | -----
1024 Special Array class/struct | single and multidimensional SOAP Arrays
1025 Special Wrapper class/struct | complexTypes with simpleContent, wraps `__item` member
1026 `xsd__hexBinary` | binary content
1027 `xsd__base64Binary` | binary content and optional DIME/MIME/MTOM attachments
1028 `xsd__anyType` | DOM elements, use `#import "dom.h"`
1029 `@xsd__anyAttribute` | DOM attributes, use `#import "dom.h"`
1030
1031 @see Section [special classes and structs](#toxsd10).
1032
1033 🔝 [Back to table of contents](#)
1034
1035 Colon notation versus name prefixing with XML tag name translation {#toxsd2}
1036 ------------------------------------------------------------------
1037
1038 To bind C/C++ type names to XSD types, a simple form of name prefixing is used
1039 by the gSOAP tools by prepending the XML namespace prefix to the C/C++ type
1040 name with a pair of undescrores. This also ensures that name clashes cannot
1041 occur when multiple WSDL and XSD files are converted to C/C++. Also, C++
1042 namespaces are not sufficiently rich to capture XML schema namespaces
1043 accurately, for example when class members are associated with schema elements
1044 defined in another XML namespace and thus the XML namespace scope of the
1045 member's name is relevant, not just its type.
1046
1047 However, from a C/C++ centric point of view this can be cumbersome. Therefore,
1048 colon notation is an alternative to physically augmenting C/C++ names with
1049 prefixes.
1050
1051 For example, the following class uses colon notation to bind the `record` class
1052 to the <i>`urn:types`</i> schema:
1053
1054 ~~~{.cpp}
1055 //gsoap ns schema namespace: urn:types
1056 class ns:record // binding 'ns:' to a type name
1057 {
1058 public:
1059 std::string name;
1060 uint64_t SSN;
1061 ns:record *spouse; // using 'ns:' with the type name
1062 ns:record(); // using 'ns:' here too
1063 ~ns:record(); // and here
1064 };
1065 ~~~
1066
1067 The colon notation is stripped away by soapcpp2 when generating the data
1068 binding implementation code for our project. So the final code just uses
1069 `record` to identify this class and its constructor/destructor.
1070
1071 When using colon notation make sure to be consistent and not use colon notation
1072 mixed with prefixed forms. The qualified name `ns:record` differs from
1073 `ns__record`, because `ns:record` is compiled to an unqualified `record` name
1074 in the source code output by the soapcpp2 tool.
1075
1076 Colon notation also facilitates overruling the elementFormDefault and
1077 attributeFormDefault declaration that is applied to local elements and
1078 attributes, when declared as members of classes, structs, and unions. With
1079 colon notation for class, struct and union members, the corresponding XML
1080 elements and attributes are selectively qualified with the specified prefix or
1081 unqualified when the prefix is omitted in front of the colon. For more
1082 details, see [qualified and unqualified members](#toxsd9-6).
1083
1084 A C/C++ identifier name (a type name, member name, function name, or parameter
1085 name) is translated to an XML tag name by the following rules:
1086
1087 - Two leading underscores indicates that the identifier name has no XML tag
1088 name, i.e. this name is not visible in XML and is not translated.
1089
1090 - A leading underscore is removed, but the underscore indicates that: **a**) a
1091 struct/class member name or parameter name has a wildcard XML tag name (i.e.
1092 matches any XML tag), or **b**) a type name that has a
1093 [document root element definition](#toxsd9-7).
1094
1095 - Trailing underscores are removed (i.e. trailing underscores can be used to
1096 avoid name clashes with keywords).
1097
1098 - Two underscores `__` in a name represent the namespace prefix of a name.
1099
1100 - A colon (`:`) within a name represent the namespace prefix of a name that is
1101 present in XML, but this prefix is not present in the generated C/C++ code.
1102
1103 - A leading colon (`:`) before a name makes the name unqualified in XML.
1104
1105 - Underscores within names are translated to hyphens (hyphens are more common
1106 in XML tag names).
1107
1108 - `_USCORE` is translated to an underscore in the translated XML tag name.
1109
1110 - `_DOT` is translated to a dot (<i>`.`</i>) in the translated XML tag name.
1111
1112 - `_xHHHH` is translated to the Unicode character with code point HHHH (hex).
1113
1114 - C++11 Unicode identifier name characters in UTF-8 are translated as-is.
1115
1116 For example, the C/C++ identifier name `s_a__my_way` is represented in XML
1117 by the tag name <i>`s-a:my-way`</i> by translating the prefix `s_a` and the
1118 local name `my_way`. With colon notation, the colon-qualified C/C++ identifier
1119 name `s_a:my_way` is also represented in XML by the same tag name
1120 <i>`s-a:my-way`</i>, but the generated C/C++ source code defines and uses the
1121 local name `my_way` only.
1122
1123 Struct/class member and parameter name translation can be overruled by using
1124 [backtick XML tags](#toxsd9-5-1) (with gSOAP 2.8.30 and greater).
1125
1126 🔝 [Back to table of contents](#)
1127
1128 C++ bool and C alternative {#toxsd3}
1129 --------------------------
1130
1131 The C++ `bool` type is bound to built-in XSD type <i>`xsd:boolean`</i>.
1132
1133 The C alternative is to define an enumeration:
1134
1135 ~~~{.cpp}
1136 enum xsd__boolean { false_, true_ };
1137 ~~~
1138
1139 or by defining an enumeration in C with pseudo-scoped enumeration constants:
1140
1141 ~~~{.cpp}
1142 enum xsd__boolean { xsd__boolean__false, xsd__boolean__true };
1143 ~~~
1144
1145 The XML value space of these types is <i>`false`</i> and <i>`true`</i>, but also accepted
1146 are <i>`0`</i> and <i>`1`</i> values for <i>`false`</i> and <i>`true`</i>, respectively.
1147
1148 To prevent name clashes, `false_` and `true_` have a trailing underscore in
1149 their `enum` symbols. Trailing underscores are removed from the XML value space.
1150
1151 🔝 [Back to table of contents](#)
1152
1153 Enumerations and bitmasks {#toxsd4}
1154 -------------------------
1155
1156 Enumerations are mapped to XSD simpleType enumeration restrictions of
1157 <i>`xsd:string`</i>, <i>`xsd:QName`</i>, and <i>`xsd:long`</i>.
1158
1159 Consider for example:
1160
1161 ~~~{.cpp}
1162 enum ns__Color { RED, WHITE, BLUE };
1163 ~~~
1164
1165 which maps to a simpleType restriction of <i>`xsd:string`</i> in the soapcpp2-generated
1166 schema:
1167
1168 <div class="alt">
1169 ~~~{.xml}
1170 <simpleType name="Color">
1171 <restriction base="xsd:string">
1172 <enumeration value="RED"/>
1173 <enumeration value="WHITE"/>
1174 <enumeration value="BLUE"/>
1175 </restriction>
1176 </simpleType>
1177 ~~~
1178 </div>
1179
1180 Enumeration name constants can be pseudo-scoped to prevent name clashes,
1181 because enumeration name constants have a global scope in C and C++:
1182
1183 ~~~{.cpp}
1184 enum ns__Color { ns__Color__RED, ns__Color__WHITE, ns__Color__BLUE };
1185 ~~~
1186
1187 You can also use C++11 scoped enumerations to prevent name clashes:
1188
1189 ~~~{.cpp}
1190 enum class ns__Color : int { RED, WHITE, BLUE };
1191 ~~~
1192
1193 Here, the enumeration class base type `: int` is optional. In place of `int`
1194 in the example above, we can also use `int8_t`, `int16_t`, `int32_t`, or
1195 `int64_t`.
1196
1197 The XML value space of the enumertions defined above is <i>`RED`</i>, <i>`WHITE`</i>, and
1198 <i>`BLUE`</i>.
1199
1200 Prefix-qualified enumeration name constants are mapped to simpleType
1201 restrictions of <i>`xsd:QName`</i>, for example:
1202
1203 ~~~{.cpp}
1204 enum ns__types { xsd__int, xsd__float };
1205 ~~~
1206
1207 which maps to a simpleType restriction of <i>`xsd:QName`</i> in the soapcpp2-generated
1208 schema:
1209
1210 <div class="alt">
1211 ~~~{.xml}
1212 <simpleType name="types">
1213 <restriction base="xsd:QName">
1214 <enumeration value="xsd:int"/>
1215 <enumeration value="xsd:float"/>
1216 </restriction>
1217 </simpleType>
1218 ~~~
1219 </div>
1220
1221 Enumeration name constants can be pseudo-numeric as follows:
1222
1223 ~~~{.cpp}
1224 enum ns__Primes { _3 = 3, _5 = 5, _7 = 7, _11 = 11 };
1225 ~~~
1226
1227 which maps to a simpleType restriction of <i>`xsd:long`</i>:
1228
1229 <div class="alt">
1230 ~~~{.xml}
1231 <simpleType name="Color">
1232 <restriction base="xsd:long">
1233 <enumeration value="3"/>
1234 <enumeration value="5"/>
1235 <enumeration value="7"/>
1236 <enumeration value="11"/>
1237 </restriction>
1238 </simpleType>
1239 ~~~
1240 </div>
1241
1242 The XML value space of this type is <i>`3`</i>, <i>`5`</i>, <i>`7`</i>, and <i>`11`</i>.
1243
1244 Besides (pseudo-) scoped enumerations, another way to prevent name clashes
1245 accross enumerations is to start an enumeration name constant with one
1246 underscore or followed it by any number of underscores, which makes it
1247 unique. The leading and trailing underscores are removed from the XML value
1248 space.
1249
1250 ~~~{.cpp}
1251 enum ns__ABC { A, B, C };
1252 enum ns__BA { B, A }; // BAD: B = 1 but B is already defined as 2
1253 enum ns__BA_ { B_, A_ }; // OK
1254 ~~~
1255
1256 The gSOAP soapcpp2 tool permits reusing enumeration name constants across
1257 (non-scoped) enumerations as long as these values are assigned the same
1258 constant. Therefore, the following is permitted:
1259
1260 ~~~{.cpp}
1261 enum ns__Primes { _3 = 3, _5 = 5, _7 = 7, _11 = 11 };
1262 enum ns__Throws { _1 = 1, _2 = 2, _3 = 3, _4 = 4, _5 = 5, _6 = 6 };
1263 ~~~
1264
1265 A bitmask type is an `enum*` "product enumeration" with a geometric,
1266 power-of-two sequence of values assigned to the enumeration constants:
1267
1268 ~~~{.cpp}
1269 enum* ns__Options { SSL3, TLS10, TLS11, TLS12, TLS13 };
1270 ~~~
1271
1272 where the product enum assigns 1 to `SSL3`, 2 to `TLS10`, 4 to `TLS11`, 8
1273 to `TLS12`, and 16 to `TLS13`, which allows these enumeration constants to be
1274 used in composing bitmasks with `|` (bitwise or) `&` (bitwise and), and `~`
1275 (bitwise not):
1276
1277 ~~~{.cpp}
1278 enum ns__Options options = (enum ns__Options)(SSL3 | TLS10 | TLS11 | TLS12 | TLS13);
1279 if (options & SSL3) // if SSL3 is an option, warn and remove from options
1280 {
1281 warning();
1282 options &= ~SSL3;
1283 }
1284 ~~~
1285
1286 The bitmask type maps to a simpleType list restriction of <i>`xsd:string`</i> in the
1287 soapcpp2-generated XML schema:
1288
1289 <div class="alt">
1290 ~~~{.xml}
1291 <simpleType name="Options">
1292 <list>
1293 <restriction base="xsd:string">
1294 <enumeration value="SSL3"/>
1295 <enumeration value="TLS10"/>
1296 <enumeration value="TLS11"/>
1297 <enumeration value="TLS12"/>
1298 <enumeration value="TLS13"/>
1299 </restriction>
1300 </list>
1301 </simpleType>
1302 ~~~
1303 </div>
1304
1305 The XML value space of this type consists of all 16 possible subsets of the
1306 four values, represented by an XML string with space-separated values. For
1307 example, the bitmask `TLS10 | TLS11 | TLS12` equals 14 and is represented by
1308 the XML text <i>`TLS10 TLS11 TLS12`</i>.
1309
1310 You can also use C++11 scoped enumerations with bitmasks using `enum*` product
1311 enumerations:
1312
1313 ~~~{.cpp}
1314 enum* class ns__Options { SSL3, TLS10, TLS11, TLS12, TLS13 };
1315 ~~~
1316
1317 The base type of a scoped enumeration bitmask, when explicitly given, is
1318 ignored. The base type is either `int` or `int64_t`, depending on the number
1319 of constants enumerated in the bitmask.
1320
1321 To convert `enum` name constants and bitmasks to a string, we use the
1322 auto-generated function for enum `T`:
1323
1324 ~~~{.cpp}
1325 const char *soap_T2s(struct soap*, enum T val)
1326 ~~~
1327
1328 The string returned is stored in an internal buffer of the current `soap`
1329 context, so you should copy it to keep it from being overwritten. For example,
1330 use `char *soap_strdup(struct soap*, const char*)`.
1331
1332 To convert a string to an `enum` constant or bitmask, we use the auto-generated
1333 function
1334
1335 ~~~{.cpp}
1336 int soap_s2T(struct soap*, const char *str, enum T *val)
1337 ~~~
1338
1339 This function takes the name (or names, space-separated for bitmasks) of
1340 the enumeration constant in a string `str`. Names should be given without the
1341 pseudo-scope prefix and without trailing underscores. The function sets `val`
1342 to the corresponding integer enum constant or to a bitmask. The function
1343 returns `SOAP_OK` (zero) on success or an error if the string is not a valid
1344 enumeration name.
1345
1346 🔝 [Back to table of contents](#)
1347
1348 Numerical types {#toxsd5}
1349 ---------------
1350
1351 Integer and floating point types are mapped to the equivalent built-in XSD
1352 types with the same sign and bit width.
1353
1354 The `size_t` type is transient (not serializable) because its width is platform
1355 dependent. We recommend to use `uint64_t` instead.
1356
1357 The XML value space of integer types are their decimal representations without
1358 loss of precision.
1359
1360 The XML value space of floating point types are their decimal representations.
1361 The decimal representations are formatted with the printf format string `"%.9G"`
1362 for floats and the printf format string `"%.17lG"` for double. To change the
1363 format strings, we can assign new strings to the following `soap` context
1364 members:
1365
1366 ~~~{.cpp}
1367 soap.float_format = "%g";
1368 soap.double_format = "%lg";
1369 soap.long_double_format = "%Lg";
1370 ~~~
1371
1372 Decimal representations may result in a loss of precision of the least
1373 significant decimal. Therefore, the format strings that are used by default
1374 are sufficiently precise to avoid loss, but this may result in long decimal
1375 fractions in the XML value space.
1376
1377 The `long double` extended floating point type requires a custom serializer:
1378
1379 ~~~{.cpp}
1380 #import "custom/long_double.h"
1381 ... // use long double
1382 ~~~
1383
1384 You can now use `long double`, which has a serializer that serializes this type
1385 as <i>`xsd:decimal`</i>. Compile and link your code with the file
1386 <i>`gsoap/custom/long_double.c`</i>.
1387
1388 The value space of floating point values includes the special values
1389 <i>`INF`</i>, <i>`-INF`</i>, and <i>`NaN`</i>. You can check a value for plus
1390 or minus infinity and not-a-number as follows:
1391
1392 ~~~{.cpp}
1393 soap_isinf(x) && x > 0 // is x INF?
1394 soap_isinf(x) && x < 0 // is x -INF?
1395 soap_isnan(x) // is x NaN?
1396 ~~~
1397
1398 To assign these values, use:
1399
1400 ~~~{.cpp}
1401 // x is float // x is double, long double, or __float128
1402 x = FLT_PINFY; x = DBL_PINFTY;
1403 x = FLT_NINFY; x = DBL_NINFTY;
1404 x = FLT_NAN; x = DBL_NAN;
1405 ~~~
1406
1407 If your system supports `__float128` then you can also use this 128 bit
1408 floating point type with a custom serializer:
1409
1410 ~~~{.cpp}
1411 #import "custom/float128.h"
1412 ... // use xsd__decimal
1413 ~~~
1414
1415 Then use the `xsd__decimal` alias of `__float128`, which has a serializer. Do
1416 not use `__float128` directly, which is transient (not serializable).
1417
1418 To check for <i>`INF`</i>, <i>`-INF`</i>, and <i>`NaN`</i> of a `__float128`
1419 value use:
1420
1421 ~~~{.cpp}
1422 isinfq(x) && x > 0 // is x INF?
1423 isinfq(x) && x < 0 // is x -INF?
1424 isnanq(x) // is x NaN?
1425 ~~~
1426
1427 The range of a `typedef`-defined numerical type can be restricted using the range
1428 `:` operator with inclusive lower and upper bounds. For example:
1429
1430 ~~~{.cpp}
1431 typedef int ns__narrow -10 : 10;
1432 ~~~
1433
1434 This maps to a simpleType restriction of <i>`xsd:int`</i> in the soapcpp2-generated
1435 schema:
1436
1437 <div class="alt">
1438 ~~~{.xml}
1439 <simpleType name="narrow">
1440 <restriction base="xsd:int">
1441 <minInclusive value="-10"/>
1442 <maxInclusive value="10"/>
1443 </restriction>
1444 </simpleType>
1445 ~~~
1446 </div>
1447
1448 The lower and upper bound of a range are optional. When omitted, values are
1449 not bound from below or from above, respectively.
1450
1451 The range of a floating point `typedef`-defined type can be restricted within
1452 floating point constant bounds.
1453
1454 Also with a floating point `typedef` a `printf`-format pattern can be given of the
1455 form `"%[width][.precision]f"` to format decimal values using the given width
1456 and precision fields:
1457
1458 ~~~{.cpp}
1459 typedef float ns__PH "%5.2f" 0.0 : 14.0;
1460 ~~~
1461
1462 This maps to a simpleType restriction of <i>`xsd:float`</i> in the soapcpp2-generated
1463 schema:
1464
1465 <div class="alt">
1466 ~~~{.xml}
1467 <simpleType name="PH">
1468 <restriction base="xsd:float">
1469 <totalDigits value="5"/>
1470 <fractionDigits value="2"/>
1471 <minInclusive value="0"/>
1472 <maxInclusive value="14"/>
1473 </restriction>
1474 </simpleType>
1475 ~~~
1476 </div>
1477
1478 For exclusive bounds, we use the `<` operator instead of the `:` range
1479 operator:
1480
1481 ~~~{.cpp}
1482 typedef float ns__epsilon 0.0 < 1.0;
1483 ~~~
1484
1485 Values `eps` of `ns__epsilon` are restricted between `0.0 < eps < 1.0`.
1486
1487 This maps to a simpleType restriction of <i>`xsd:float`</i> in the soapcpp2-generated
1488 schema:
1489
1490 <div class="alt">
1491 ~~~{.xml}
1492 <simpleType name="epsilon">
1493 <restriction base="xsd:float">
1494 <minExclusive value="0"/>
1495 <maxExclusive value="1"/>
1496 </restriction>
1497 </simpleType>
1498 ~~~
1499 </div>
1500
1501 To make just one of the bounds exclusive, while keeping the other bound
1502 inclusive, we add a `<` on the left or on the right side of the range ':'
1503 operator. For example:
1504
1505 ~~~{.cpp}
1506 typedef float ns__pos 0.0 < : ; // 0.0 < pos
1507 typedef float ns__neg : < 0.0 ; // neg < 0.0
1508 ~~~
1509
1510 It is valid to make both left and right side exclusive with `< : <` which is in
1511 fact identical to the exlusive range `<` operator:
1512
1513 ~~~{.cpp}
1514 typedef float ns__epsilon 0.0 < : < 1.0; // 0.0 < eps < 1.0
1515 ~~~
1516
1517 It helps to think of the `:` as a placeholder of the value between the two
1518 bounds, which is easier to memorize than the shorthand forms of bounds from
1519 which the `:` is removed:
1520
1521 | bounds | validation check | shorthand |
1522 | ------------ | ---------------- | ----------- |
1523 | `1 : ` | 1 <= x | `1 ` |
1524 | `1 : 10 ` | 1 <= x <= 10 | |
1525 | ` : 10 ` | x <= 10 | |
1526 | `1 < : < 10` | 1 < x < 10 | `1 < 10 ` |
1527 | `1 : < 10` | 1 <= x < 10 | |
1528 | ` : < 10` | x < 10 | ` < 10 ` |
1529 | `1 < : ` | 1 < x | `1 < ` |
1530 | `1 < : 10 ` | 1 < x <= 10 | |
1531
1532 Besides `float`, also `double` and `long double` values can be restricted. For
1533 example, consider a nonzero probability extended floating point precision type:
1534
1535 ~~~{.cpp}
1536 #import "custom/long_double.h"
1537 typedef long double ns__probability "%16Lg" 0.0 < : 1.0;
1538 ~~~
1539
1540 Value range restrictions are validated by the parser for all inbound XML data.
1541 A type fault `SOAP_TYPE` will be thrown by the deserializer if the value is out
1542 of range.
1543
1544 Finally, if your system supports `__int128_t` then you can also use this 128
1545 bit integer type with a custom serializer:
1546
1547 ~~~{.cpp}
1548 #import "custom/int128.h"
1549 ... // use xsd__integer
1550 ~~~
1551
1552 Use the `xsd__integer` alias of `__int128_t`, which has a serializer. Do not
1553 use `__int128_t` directly, which is transient (not serializable).
1554
1555 To convert numeric values to a string, we use the auto-generated function for
1556 numeric type `T`:
1557
1558 ~~~{.cpp}
1559 const char *soap_T2s(struct soap*, T val)
1560 ~~~
1561
1562 For numeric types `T`, the string returned is stored in an internal buffer of
1563 the current `soap` context, so you should copy it to keep it from being
1564 overwritten. For example, use `char *soap_strdup(struct soap*, const char*)`.
1565
1566 To convert a string to a numeric value, we use the auto-generated function
1567
1568 ~~~{.cpp}
1569 int soap_s2T(struct soap*, const char *str, T *val)
1570 ~~~
1571
1572 where `T` is for example `int`, `LONG64`, `float`, `decimal` (the custom
1573 serializer name of `long double`) or `xsd__integer` (the custom serializer name
1574 of `__int128_t`). The function `soap_s2T` returns `SOAP_OK` on success or an
1575 error when the value is not numeric. For floating point types, `"INF"`, `"-INF"`
1576 and `"NaN"` are valid strings to convert to numbers.
1577
1578 🔝 [Back to table of contents](#)
1579
1580 String types {#toxsd6}
1581 ------------
1582
1583 String types are mapped to the built-in <i>`xsd:string`</i> and <i>`xsd:QName`</i> XSD types.
1584
1585 The wide strings `wchar_t*` and `std::wstring` may contain Unicode that is
1586 preserved in the XML value space.
1587
1588 Strings `char*` and `std::string` can only contain extended Latin, but we can
1589 store UTF-8 content that is preserved in the XML value space when the `soap`
1590 context is initialized with the flag `SOAP_C_UTFSTRING`.
1591
1592 @warning Beware that many XML 1.0 parsers reject all control characters (those
1593 between `#x1` and `#x1F`) except for `#x9`, `#xA`, and `#xD`. With the
1594 newer XML 1.1 version parsers (including gSOAP) you should be fine.
1595
1596 The length of a string of a `typedef`-defined string type can be restricted:
1597
1598 ~~~{.cpp}
1599 typedef std::string ns__password 6 : 16;
1600 ~~~
1601
1602 which maps to a simpleType restriction of <i>`xsd:string`</i> in the soapcpp2-generated
1603 schema:
1604
1605 <div class="alt">
1606 ~~~{.xml}
1607 <simpleType name="password">
1608 <restriction base="xsd:string">
1609 <minLength value="6"/>
1610 <maxLength value="16"/>
1611 </restriction>
1612 </simpleType>
1613 ~~~
1614 </div>
1615
1616 String length restrictions are validated by the parser for inbound XML data.
1617 A value length fault `SOAP_LENGTH` will be thrown by the deserializer if the
1618 string is too long or too short.
1619
1620 In addition, an XSD regex pattern restriction can be associated with a string
1621 typedef:
1622
1623 ~~~{.cpp}
1624 typedef std::string ns__password "([a-zA-Z]|[0-9]|-)+" 6 : 16;
1625 ~~~
1626
1627 which maps to a simpleType restriction of <i>`xsd:string`</i> in the soapcpp2-generated
1628 schema:
1629
1630 <div class="alt">
1631 ~~~{.xml}
1632 <simpleType name="password">
1633 <restriction base="xsd:string">
1634 <pattern value="([a-zA-Z0-9]|-)+"/>
1635 <minLength value="6"/>
1636 <maxLength value="16"/>
1637 </restriction>
1638 </simpleType>
1639 ~~~
1640 </div>
1641
1642 Pattern restrictions are validated by the parser for inbound XML data only if
1643 the `soap::fsvalidate` and `soap::fwvalidate` callbacks are defined.
1644
1645 Exclusive length bounds can be used with strings:
1646
1647 ~~~{.cpp}
1648 typedef std::string ns__string255 : < 256; // same as 0 : 255
1649 ~~~
1650
1651 Fixed-size strings (`char[N]`) are rare occurrences in the wild, but apparently
1652 still used in some projects to store strings. To facilitate fixed-size string
1653 serialization, use <b>`soapcpp2 -b`</b> option <b>`-b`</b>. For example:
1654
1655 ~~~{.cpp}
1656 typedef char ns__buffer[10]; // requires soapcpp2 option -b
1657 ~~~
1658
1659 which maps to a simpleType restriction of <i>`xsd:string`</i> in the soapcpp2-generated
1660 schema:
1661
1662 <div class="alt">
1663 ~~~{.xml}
1664 <simpleType name="buffer">
1665 <restriction base="xsd:string">
1666 <maxLength value="9"/>
1667 </restriction>
1668 </simpleType>
1669 ~~~
1670 </div>
1671
1672 Fixed-size strings must contain NUL-terminated text and should not contain raw
1673 binary data. Also, the length limitation is more restrictive for UTF-8 content
1674 (enabled with the `SOAP_C_UTFSTRING`) that requires multibyte character
1675 encodings. As a consequence, UTF-8 content may be truncated to fit.
1676
1677 Raw binary data can be stored in a `xsd__base64Binary` or `xsd__hexBinary`
1678 structure, or transmitted as a MIME attachment.
1679
1680 The built-in `_QName` type is a regular C string type (`char*`) that maps to
1681 <i>`xsd:QName`</i> but has the added advantage that it holds normalized qualified names.
1682 There are actually two forms of normalized QName content, to ensure any QName
1683 is represented accurately:
1684
1685 ~~~{.cpp}
1686 "prefix:name"
1687 "\"URI\":name"
1688 ~~~
1689
1690 The first form of string is used when the prefix (and the binding URI) is
1691 defined in the namespace table and is bound to a URI (see the .nsmap file).
1692 The second form is used when the URI is not defined in the namespace table and
1693 therefore no prefix is available to bind and normalize the URI to.
1694
1695 A `_QName` string may contain a sequence of space-separated QName values, not
1696 just one, and all QName values are normalized to the format shown above.
1697
1698 To define a `std::string` base type for <i>`xsd:QName`</i>, we use a `typedef`:
1699
1700 ~~~{.cpp}
1701 typedef std::string xsd__QName;
1702 ~~~
1703
1704 The `xsd__QName` string content is normalized, just as with the `_QName`
1705 normalization.
1706
1707 To serialize strings that contain literal XML content to be reproduced in the
1708 XML value space, use the built-in `_XML` string type, which is a regular C
1709 string type (`char*`) that maps to plain XML CDATA.
1710
1711 To define a `std::string` base type for literal XML content, use a `typedef`:
1712
1713 ~~~{.cpp}
1714 typedef std::string XML;
1715 ~~~
1716
1717 Strings can hold any of the values of the XSD built-in primitive types. We can
1718 use a string `typedef` to declare the use of the string type as a XSD built-in
1719 type:
1720
1721 ~~~{.cpp}
1722 typedef std::string xsd__token;
1723 ~~~
1724
1725 You must ensure that the string values we populate in this type conform to the
1726 XML standard, which in case of <i>`xsd:token`</i> is the lexical and value spaces of
1727 <i>`xsd:token`</i> are the sets of all strings after whitespace replacement of any
1728 occurrence of `#x9`, `#xA` , and `#xD` by `#x20` and collapsing.
1729
1730 As of version 2.8.49, the gSOAP parser will automatically collapse or replace
1731 the white space content when receiving data for XSD types that require white
1732 space collapsed or replaced. This normalization is applied to strings
1733 directly. The decision to collapse or replace is based on the `typedef` name
1734 corresponding to the built-in string-based XSD type.
1735
1736 To copy `char*` or `wchar_t*` strings with a context that manages the allocated
1737 memory, use functions
1738
1739 ~~~{.cpp}
1740 char *soap_strdup(struct soap*, const char*)
1741 wchar_t *soap_wstrdup(struct soap*, const wchar_t*)
1742 ~~~
1743
1744 To convert a wide string to a UTF-8 encoded string, use function
1745
1746 ~~~{.cpp}
1747 const char* SOAP_FMAC2 soap_wchar2s(struct soap*, const wchar_t *s)
1748 ~~~
1749
1750 The function allocates and returns a string, with its memory being managed by
1751 the context.
1752
1753 To convert a UTF-8 encoded string to a wide string, use function
1754
1755 ~~~{.cpp}
1756 int soap_s2wchar(struct soap*, const char *from, wchar_t **to, long minlen, long maxlen)
1757 ~~~
1758
1759 where `to` is set to point to an allocated `wchar_t*` string. Pass `-1` for
1760 `minlen` and `maxlen` to ignore length constraints on the target string. The
1761 function returns `SOAP_OK` or an error when the length constraints are not met.
1762
1763 🔝 [Back to table of contents](#)
1764
1765 Date and time types {#toxsd7}
1766 -------------------
1767
1768 The C/C++ `time_t` type is mapped to the built-in <i>`xsd:dateTime`</i> XSD type that
1769 represents a date and time within a time zone (typically UTC).
1770
1771 The XML value space contains ISO 8601 Gregorian time instances of the form
1772 <i>`[-]CCYY-MM-DDThh:mm:ss.sss[Z|(+|-)hh:mm]`</i>, where <i>`Z`</i> is the UTC time zone
1773 or a time zone offset <i>`(+|-)hh:mm]`</i> from UTC is used.
1774
1775 A `time_t` value is considered and represented in UTC by the serializer.
1776
1777 Because the `time_t` value range is restricted to dates after 01/01/1970 and
1778 before 2038 assuming `time_t` is a `long` 32 bit, care must be taken to ensure
1779 the range of <i>`xsd:dateTime`</i> values in XML exchanges do not exceed the `time_t`
1780 range.
1781
1782 This restriction does not hold for `struct tm` (<i>`time.h`</i> library), which we can use
1783 to store and exchange a date and time in UTC without date range restrictions.
1784 The serializer uses the `struct tm` members directly for the XML value space of
1785 <i>`xsd:dateTime`</i>:
1786
1787 ~~~{.cpp}
1788 struct tm
1789 {
1790 int tm_sec; // seconds (0 - 60)
1791 int tm_min; // minutes (0 - 59)
1792 int tm_hour; // hours (0 - 23)
1793 int tm_mday; // day of month (1 - 31)
1794 int tm_mon; // month of year (0 - 11)
1795 int tm_year; // year - 1900
1796 int tm_wday; // day of week (Sunday = 0) (NOT USED)
1797 int tm_yday; // day of year (0 - 365) (NOT USED)
1798 int tm_isdst; // is summer time in effect?
1799 char* tm_zone; // abbreviation of timezone (NOT USED)
1800 };
1801 ~~~
1802
1803 You will lose the day of the week information. It is always Sunday
1804 (`tm_wday=0`) and the day of the year is not set either. The time zone is UTC.
1805
1806 This `struct tm` type is mapped to the built-in <i>`xsd:dateTime`</i> XSD type and
1807 serialized with the custom serializer <i>`gsoap/custom/struct_tm.h`</i> that declares a
1808 `xsd__dateTime` type:
1809
1810 ~~~{.cpp}
1811 #import "custom/struct_tm.h" // import typedef struct tm xsd__dateTime;
1812 ... // use xsd__dateTime
1813 ~~~
1814
1815 Compile and link your code with <i>`gsoap/custom/struct_tm.c`</i>.
1816
1817 The `struct timeval` (<i>`sys/time.h`</i> library) type is mapped to the
1818 built-in <i>`xsd:dateTime`</i> XSD type and serialized with the custom serializer
1819 <i>`gsoap/custom/struct_timeval.h`</i> that declares a `xsd__dateTime` type:
1820
1821 ~~~{.cpp}
1822 #import "custom/struct_timeval.h" // import typedef struct timeval xsd__dateTime;
1823 ... // use xsd__dateTime
1824 ~~~
1825
1826 Compile and link your code with <i>`gsoap/custom/struct_timeval.c`</i>.
1827
1828 The same value range restrictions apply to `struct timeval` as they apply to
1829 `time_t`. The added benefit of `struct timeval` is the addition of a
1830 microsecond-precise clock:
1831
1832 ~~~{.cpp}
1833 struct timeval
1834 {
1835 time_t tv_sec; // seconds since Jan. 1, 1970
1836 suseconds_t tv_usec; // and microseconds
1837 };
1838 ~~~
1839
1840 A C++11 `std::chrono::system_clock::time_point` type is mapped to the built-in
1841 <i>`xsd:dateTime`</i> XSD type and serialized with the custom serializer
1842 <i>`gsoap/custom/chrono_time_point.h`</i> that declares a `xsd__dateTime` type:
1843
1844 ~~~{.cpp}
1845 #import "custom/chrono_time_point.h" // import typedef std::chrono::system_clock::time_point xsd__dateTime;
1846 ... // use xsd__dateTime
1847 ~~~
1848
1849 Compile and link your code with <i>`gsoap/custom/chrono_time_point.cpp`</i>.
1850
1851 The `struct tm` type is mapped to the built-in <i>`xsd:date`</i> XSD type and serialized
1852 with the custom serializer <i>`gsoap/custom/struct_tm_date.h`</i> that declares a
1853 `xsd__date` type:
1854
1855 ~~~{.cpp}
1856 #import "custom/struct_tm_date.h" // import typedef struct tm xsd__date;
1857 ... // use xsd__date
1858 ~~~
1859
1860 Compile and link your code with <i>`gsoap/custom/struct_tm_date.c`</i>.
1861
1862 The XML value space of <i>`xsd:date`</i> are Gregorian calendar dates of the form
1863 <i>`[-]CCYY-MM-DD[Z|(+|-)hh:mm]`</i> with a time zone.
1864
1865 The serializer ignores the time part and the deserializer only populates the
1866 date part of the struct, setting the time to 00:00:00. There is no unreasonable
1867 limit on the date range because the year field is stored as an integer (`int`).
1868
1869 An `unsigned long long` (`ULONG64` or `uint64_t`) type that contains a 24 hour
1870 time in microseconds UTC is mapped to the built-in <i>`xsd:time`</i> XSD type and
1871 serialized with the custom serializer <i>`gsoap/custom/long_time.h`</i> that declares a
1872 `xsd__time` type:
1873
1874 ~~~{.cpp}
1875 #import "custom/long_time.h" // import typedef unsigned long long xsd__time;
1876 ... // use xsd__time
1877 ~~~
1878
1879 Compile and link your code with <i>`gsoap/custom/long_time.c`</i>.
1880
1881 This type represents `00:00:00.000000` to `23:59:59.999999`, from 0 to an
1882 upper bound of 86,399,999,999. A microsecond resolution means that a 1 second
1883 increment requires an increment of 1,000,000 in the integer value.
1884
1885 The XML value space of <i>`xsd:time`</i> are points in time recurring each day of the
1886 form <i>`hh:mm:ss.sss[Z|(+|-)hh:mm]`</i>, where <i>`Z`</i> is the UTC time zone or a time
1887 zone offset from UTC is used. The `xsd__time` value is always considered and
1888 represented in UTC by the serializer.
1889
1890 To convert date and/or time values to a string, we use the auto-generated
1891 function for type `T`:
1892
1893 ~~~{.cpp}
1894 const char *soap_T2s(struct soap*, T val)
1895 ~~~
1896
1897 For date and time types `T`, the string returned is stored in an internal
1898 buffer of the current `soap` context, so you should copy it to keep it from being
1899 overwritten. For example, use `char *soap_strdup(struct soap*, const char*)`.
1900
1901 To convert a string to a date/time value, we use the auto-generated function
1902
1903 ~~~{.cpp}
1904 int soap_s2T(struct soap*, const char *str, T *val)
1905 ~~~
1906
1907 where `T` is for example `dateTime` (for `time_t`), `xsd__dateTime` (for
1908 `struct tm`, `struct timeval`, or `std::chrono::system_clock::time_point`).
1909 The function `soap_s2T` returns `SOAP_OK` on success or an error when the value
1910 is not a date/time.
1911
1912 🔝 [Back to table of contents](#)
1913
1914 Time duration types {#toxsd8}
1915 -------------------
1916
1917 The XML value space of <i>`xsd:duration`</i> are values of the form <i>`PnYnMnDTnHnMnS`</i>
1918 where the capital letters are delimiters. Delimiters may be omitted when the
1919 corresponding member is not used.
1920
1921 A `long long` (`LONG64` or `int64_t`) type that contains a duration (time
1922 lapse) in milliseconds is mapped to the built-in <i>`xsd:duration`</i> XSD type and
1923 serialized with the custom serializer <i>`gsoap/custom/duration.h`</i> that declares a
1924 `xsd__duration` type:
1925
1926 ~~~{.cpp}
1927 #import "custom/duration.h" // import typedef long long xsd__duration;
1928 ... // use xsd__duration
1929 ~~~
1930
1931 Compile and link your code with <i>`gsoap/custom/duration.c`</i>.
1932
1933 The duration type `xsd__duration` can represent 106,751,991,167 days forward
1934 and backward with millisecond precision.
1935
1936 Durations that exceed a month are always output in days, rather than months to
1937 avoid days-per-month conversion inacurracies.
1938
1939 Durations that are received in years and months instead of total number of days
1940 from a reference point are not well defined, since there is no accepted
1941 reference time point (it may or may not be the current time). The decoder
1942 simple assumes that there are 30 days per month. For example, conversion of
1943 "P4M" gives 120 days. Therefore, the durations "P4M" and "P120D" are assumed
1944 to be identical, which is not necessarily true depending on the reference point
1945 in time.
1946
1947 Rescaling of the duration value by may be needed when adding the duration value
1948 to a `time_t` value, because `time_t` may or may not have a seconds resolution,
1949 depending on the platform and possible changes to `time_t`.
1950
1951 Rescaling is done automatically when you add a C++11 `std::chrono::nanoseconds`
1952 value to a `std::chrono::system_clock::time_point` value. To use
1953 `std::chrono::nanoseconds` as <i>`xsd:duration`</i>:
1954
1955 ~~~{.cpp}
1956 #import "custom/chrono_duration.h" // import typedef std::chrono::duration xsd__duration;
1957 ... // use xsd__duration
1958 ~~~
1959
1960 Compile and link your code with <i>`gsoap/custom/chrono_duration.cpp`</i>.
1961
1962 This type can represent 384,307,168 days (2^63 nanoseconds) forwards and
1963 backwards in time in increments of 1 ns (1/1000000000 second).
1964
1965 The same observations with respect to receiving durations in years and months
1966 apply to this serializer's decoder.
1967
1968 To convert duration values to a string, we use the auto-generated function
1969
1970 ~~~{.cpp}
1971 const char *soap_xsd__duration2s(struct soap*, xsd__duration val)
1972 ~~~
1973
1974 The string returned is stored in an internal buffer, so you should copy it to
1975 keep it from being overwritten, Use `soap_strdup(struct soap*, const char*)`
1976 for example to copy this string.
1977
1978 To convert a string to a duration value, we use the auto-generated function
1979
1980 ~~~{.cpp}
1981 int soap_s2xsd__dateTime(struct soap*, const char *str, xsd__dateTime *val)
1982 ~~~
1983
1984 The function returns `SOAP_OK` on success or an error when the value is not a
1985 duration.
1986
1987 🔝 [Back to table of contents](#)
1988
1989 Classes and structs {#toxsd9}
1990 -------------------
1991
1992 Classes and structs are mapped to XSD complexTypes. The XML value space
1993 consists of XML elements with attributes and subelements, possibly constrained
1994 by XML schema validation rules that enforce element and attribute occurrence
1995 contraints, numerical value range constraints, and string length and pattern
1996 constraints.
1997
1998 Classes that are declared with the gSOAP tools are limited to single
1999 inheritence only. The soapcpp2 tool does not allow structs to be inherited.
2000
2001 The class and struct name is bound to an XML namespace by means of the prefix
2002 naming convention or by using [colon notation](#toxsd1):
2003
2004 ~~~{.cpp}
2005 //gsoap ns schema namespace: urn:types
2006 class ns__record
2007 {
2008 public:
2009 std::string name;
2010 uint64_t SSN;
2011 ns__record *spouse;
2012 ns__record();
2013 ~ns__record();
2014 protected:
2015 struct soap *soap;
2016 };
2017 ~~~
2018
2019 In the example above, we also added a context pointer to the `soap` context that
2020 manages this instance. It is set when the instance is created in the engine's
2021 context, for example when deserialized and populated by the engine.
2022
2023 The class maps to a complexType in the soapcpp2-generated XML schema:
2024
2025 <div class="alt">
2026 ~~~{.xml}
2027 <complexType name="record">
2028 <sequence>
2029 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
2030 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
2031 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/>
2032 </sequence>
2033 </complexType>
2034 ~~~
2035 </div>
2036
2037 The following sections apply to both structs and classes. Structs require the
2038 use of the `struct` keyword with the struct name, otherwise soapcpp2 will throw
2039 a syntax error. As is often done in C, use a `typedef` to declare a `struct`
2040 that can be used without the `struct` keyword.
2041
2042 🔝 [Back to table of contents](#)
2043
2044 ### Serializable versus transient types and data members {#toxsd9-1}
2045
2046 Public data members of a class or struct are serializable when their types are
2047 serializable. Private and protected members are transient and not
2048 serializable.
2049
2050 Also `const` and `static` members are not serializable, with the exception of
2051 `const char*` and `const wchar_t*`. Types and specific class/struct members
2052 can be made transient with the `extern` qualifier for types and by marking
2053 members with `[` and `]`:
2054
2055 ~~~{.cpp}
2056 extern class std::ostream; // declare std::ostream transient
2057 class ns__record
2058 {
2059 public:
2060 [ int num; ] // not serialized: member is marked transient with [ ]
2061 std::ostream out; // not serialized: std:ostream is transient
2062 static const int MAX = 1024; // not serialized: static const member
2063 private:
2064 std::string id; // not serialized: private member
2065 };
2066 ~~~
2067
2068 By declaring `std::ostream` transient with `extern` you can use this type
2069 wherever you need it without soapcpp2 complaining that this class and any other
2070 class or type declared as `extern` is not defined. Do not use `extern` with
2071 `typedef`, because this declares a custom serializer, see
2072 [adding custom serializers](#custom).
2073
2074 Marking members transient with `[` and `]` makes them transient (and visually
2075 makes them stand out). This has otherwise no effect on the generated code for
2076 the class or struct to be used in your application code.
2077
2078 🔝 [Back to table of contents](#)
2079
2080 ### Derived types in C++ {#toxsd9-1-1}
2081
2082 Extensible and restricted types in XML schemas are derived types from single
2083 simple and complex base types. XML schema derived types are naturally
2084 represented by C++ derived classes using single inheritance. Besides the
2085 concept of extensions versus restrictions, there are two kinds of derived
2086 types: complexTypes with simpleContent, meaning types with XML CDATA values,
2087 and complexTypes with complexContent, meaning types with sub-elements. Both
2088 are permitted to have one or more XML attributes.
2089
2090 A complexType with simpleContent is defined as a wrapper to contain XML CDATA
2091 values and any number of attributes, see
2092 [wrapper class/struct with simpleContent](#toxsd10-4).
2093 Wrapper class/struct types can form a hierarchy of derived types in C++ using
2094 inheritance. For example:
2095
2096 ~~~{.cpp}
2097 class xsd__anyType
2098 {
2099 public:
2100 std::string __item; // string to hold any simpleContent
2101 };
2102 class ns__data : public xsd__anyType
2103 {
2104 public:
2105 @ std::string value 1; // extends xsd:anyType with a required attribute
2106 };
2107 ~~~
2108
2109 The `ns__data` class maps to a complexType in the soapcpp2-generated XML schema:
2110
2111 <div class="alt">
2112 ~~~{.xml}
2113 <complexType name="string">
2114 <simpleContent>
2115 <extension base="xsd:string">
2116 <attribute name="value" type="xsd:string" use="required"/>
2117 </extension>
2118 </simpleContent>
2119 </complexType>
2120 ~~~
2121 </div>
2122
2123 The XML value space consists of an element with the string contents an optional
2124 attribute:
2125
2126 <div class="alt">
2127 ~~~{.xml}
2128 <ns:data value="abc">xyz</ns:data>
2129 ~~~
2130 </div>
2131
2132 By contrast, a complexType with complexContent typically extends a given base
2133 complexType. For example:
2134
2135 ~~~{.cpp}
2136 class ns__base
2137 {
2138 public:
2139 std::string name 1;
2140 int number 1;
2141 };
2142 class ns__derived : public ns__base
2143 {
2144 public:
2145 @ std::string value 1; // extends ns:base with an attribute
2146 std::string text 1; // extends ns:base with an element
2147 };
2148 ~~~
2149
2150 The `ns__base` and `ns__derived` classes maps to complexTypes in the soapcpp2-generated XML schema:
2151
2152 <div class="alt">
2153 ~~~{.xml}
2154 <complexType name="base">
2155 <sequence>
2156 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
2157 <element name="number" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2158 </sequence>
2159 </complexType>
2160 <complexType name="derived">
2161 <complexContent>
2162 <extension base="ns:base">
2163 <sequence>
2164 <element name="text" type="xsd:string" minOccurs="1" maxOccurs="1"/>
2165 </sequence>
2166 </extension>
2167 </complexContent>
2168 <attribute name="value" type="xsd:string" use="required"/>
2169 </complexType>
2170 ~~~
2171 </div>
2172
2173 The XML value space of `ns__derived` consists of three requires child elements
2174 and an optional attribute:
2175
2176 <div class="alt">
2177 ~~~{.xml}
2178 <ns:derived value="abc">
2179 <name>def</name>
2180 <number>123</number>
2181 <text>xyz</text>
2182 </ns:derived>
2183 ~~~
2184 </div>
2185
2186 Derived types can be used for two main purposes in XML schema by extending or
2187 restricting base types. One purpose is to reuse a base type when defining a
2188 derived type, such that common parts do not need to be replicated. The second
2189 purpose is to be able to use a derived type in place of a base type in XML, which
2190 is indicated by an <i>`xsi:type`</i> attribute with the qualified name of the
2191 derived type. Consider for example the following class that uses the
2192 previously declared base types `xsd__anyType` and `ns__base`:
2193
2194 ~~~{.cpp}
2195 class ns__record
2196 {
2197 public:
2198 xsd__anyType *base1 1; // required element
2199 ns__base *base2 1; // required element
2200 };
2201 ~~~
2202
2203 We can assign base type values to the `ns_record` members:
2204
2205 ~~~{.cpp}
2206 ns__record record;
2207 record.base1 = soap_new_xsd__anyType(soap);
2208 record.base2 = soap_new_ns__base(soap);
2209 soap_write_ns__record(soap, &record);
2210 ~~~
2211
2212 This produces the following XML fragment populated with default values (empty
2213 text for strings and zeros for numbers), where element <i>`base1`</i> has a
2214 simpleContent value and element <i>`base2`</i> has two child elements:
2215
2216 <div class="alt">
2217 ~~~{.xml}
2218 <ns:record>
2219 <base1></base1>
2220 <base2>
2221 <name></name>
2222 <number>0</number>
2223 </base2>
2224 </ns:record>
2225 ~~~
2226 </div>
2227
2228 We can also assign derived type values to the `ns_record` members:
2229
2230 ~~~{.cpp}
2231 ns__record record;
2232 record.base1 = soap_new_ns__data(soap);
2233 record.base2 = soap_new_ns__derived(soap);
2234 soap_write_ns__record(soap, &record);
2235 ~~~
2236
2237 This produces the following XML fragment populated with default values (empty
2238 text for strings and zeros for numbers), where element <i>`base1`</i> has
2239 schema type <i>`ns:data`</i> with simpleContent and an attribute, and
2240 <i>`base2`</i> has schema type <i>`ns:derived`</i> with three child elements
2241 and an attribute:
2242
2243 <div class="alt">
2244 ~~~{.xml}
2245 <ns:record>
2246 <base1 xsi:type="ns:data" value=""></base1>
2247 <base2 xsi:type="ns:derived" value="">
2248 <name></name>
2249 <number>0</number>
2250 <text></text>
2251 </base2>
2252 </ns:record>
2253 ~~~
2254 </div>
2255
2256 Deserialization automatically allocates and assigns a `ns__base` class instance to a
2257 `ns__base` pointer when deserializing the <i>`ns:base`</i> schema type and allocates and
2258 assigns a `ns__derived` class instance to a `ns__base` pointer when deserializing the
2259 <i>`ns:derived`</i> type when an element with <i>`xsi:type="ns:derived"`</i> is
2260 parsed. All classes are extended by soapcpp2 by a `soap_type()` method that
2261 returns the unique `SOAP_TYPE_T` value of the class `T`. This makes it easy to
2262 check whether the deserialized data contains a derived type to implement
2263 type-safe code, for example:
2264
2265 ~~~{.cpp}
2266 ns__record record;
2267 soap_read_ns__record(soap, &record);
2268 if (record.base1->soap_type() == SOAP_TYPE_ns__data)
2269 std::cout << "Derived ns:data "
2270 << dynamic_cast<ns__data*>(record.base1)->value
2271 << std::endl;
2272 else
2273 std::cout << "Base xsd:anyType" << std::endl;
2274 if (record.base2->soap_type() == SOAP_TYPE_ns__derived)
2275 std::cout << "Derived ns:derived "
2276 << dynamic_cast<ns__derived*>(record.base2)->value
2277 << std::endl;
2278 else
2279 std::cout << "Base ns:base" << std::endl;
2280 ~~~
2281
2282 This example should use the `SOAP_XML_STRICT` mode flag to initialize the
2283 `soap` context to ensure that all required values are present in the
2284 deserialized structures.
2285
2286 🔝 [Back to table of contents](#)
2287
2288 ### Derived types in C {#toxsd9-1-2}
2289
2290 While single inheritance works well in C++ to represent derived types as we
2291 discussed in the previous section, this will obviously not work in C. Two
2292 methods to serialize derived types in C are presented here. The first method
2293 uses `void*` to serialize anything. The second method is more accurate and is
2294 relatively new in gSOAP.
2295
2296 To serialize any type is possible with [tagged void pointer members](#toxsd9-12) to
2297 serialize data pointed to by a `void*` member, which can be any serializable
2298 type, such as derived types. For `void*` deserialization to work the XML
2299 parsed must contain an <i>`xsi:type`</i> attribute with a schema type. Only
2300 then can the deserializer instantiate the corresponding serializable C/C++
2301 type. Base types serialized do not require an <i>`xsi:type`</i> to indicate
2302 the base schema type, so this approach is not guaranteed to work and requires a
2303 workaround with an anonymous wrapper struct/class that contains both the base
2304 type and a `void*`. For example:
2305
2306 ~~~{.cpp}
2307 struct ns__base // a base type
2308 {
2309 char *name 1;
2310 int number 1;
2311 };
2312 struct ns__derived // extends ns__base with two additional members
2313 {
2314 char *name 1;
2315 int number 1;
2316 char *text 1;
2317 @ char *value 1;
2318 };
2319 struct __ns__base // a wrapper, not visible in XML
2320 {
2321 int __type; // the SOAP_TYPE_T pointed to by __self
2322 void *__self; // points to any type
2323 struct ns__base *__self; // wraps ns__base for the current element tag
2324 }
2325 class ns__record
2326 {
2327 struct __ns__base base;
2328 };
2329 ~~~
2330
2331 The `__ns__base` wrapper wraps the `ns__base` type to (de)serialize the
2332 <i>`base`</i> element that has no <i>`xsi:type`</i> attribute and uses `void*`
2333 to (de)serialize the <i>`base`</i> element that has <i>`xsi:type`</i>
2334 attribute. This works fine at the XML parsing level, but the generated
2335 XML schema components do not accurately represent the derived type, because it
2336 lacks the extension/restriction of the derived type (and the `__ns__base`
2337 wrapper is invisible).
2338
2339 Using `void*` to represent derived types in a base type wrapper is not very
2340 accurate because we can serialize anything, not just derived types of a given
2341 base type. The wrapper may also hold two values: the base type value and a
2342 derived type value. Furthermore, using arrays or containers that hold base and
2343 derived types becomes quite tricky because an array item could hold both the
2344 base and derived type.
2345
2346 As of gSOAP version 2.8.75, `wsdl2h -F` option `-F` generates base type structs
2347 extended with transient pointer members to its derived types. To serialize the
2348 base type itself, all of the pointer members are NULL. If one of the pointer
2349 members points to a derived type the derived type is serialized instead.
2350 Deserialization is automatic, in that the base type is deserialized if the
2351 element has no <i>`xsi:type`</i> attribute or the attribute is the base schema
2352 type, and a derived type is deserialized if the element has an
2353 <i>`xsi>type`</i> attribute with the derived schema type.
2354
2355 This method is fully automated for the wsdl2h tool to generate an interface
2356 header file for soapcpp2 with the type derivations in C. To use this method to
2357 generate code from WSDLs and XSDs, use `wsdl2h -F` option `-F`. This also
2358 works in C++ if desired, but C++ inheritance works fine without this method.
2359
2360 Using this method with soapcpp2 alone using a manually-specified interface
2361 header file produces the specified type inheritance in the soapcpp2-generated
2362 WSDL and XML schema files as complexType extensions.
2363
2364 The soapcpp2 tool warns if a derived type has multiple base types. At most one
2365 base type for a derived type may be specified.
2366
2367 This method with transient pointers to derived types makes it easy to use base
2368 and derived types in C:
2369
2370 ~~~{.cpp}
2371 struct ns__base // a base type
2372 {
2373 char *name 1;
2374 int number 1;
2375 [ struct ns__derived *ns__derived; ] // points to derived type if non-NULL
2376 };
2377 struct ns__derived // extends ns__base with two additional members
2378 {
2379 char *name 1;
2380 int number 1;
2381 char *text 1;
2382 @ char *value 1;
2383 };
2384 struct ns__record
2385 {
2386 struct ns__base base; // contains base type or derived type value
2387 };
2388 ~~~
2389
2390 The `ns__base` struct includes the special member `ns__derived` that points to
2391 a `ns__derived` value. This special member must be:
2392
2393 - a transient member (i.e. non-serializable) by placing the declaration within
2394 `[` and `]`, and
2395 - the member name must match the type name (to be more precise, at least the
2396 initial part of the member name must match the type name as in the example
2397 `ns__derived_` works too).
2398
2399 To serialize the `ns__base` value requires the `ns__derived` member to be NULL.
2400 To serialize the `ns__derived` value requires the `ns__derived` member to point
2401 to the `ns__derived` value to serialize and the `ns__base` members are
2402 irrelevant.
2403
2404 We can assign the base type value to the `ns_record::base` member:
2405
2406 ~~~{.cpp}
2407 struct ns__record record;
2408 soap_default_ns__record(soap, &record);
2409 soap_write_ns__record(soap, &record);
2410 ~~~
2411
2412 This produces the following XML fragment populated with default values (empty
2413 text for strings and zeros for numbers), where element <i>`base`</i> has two
2414 child elements:
2415
2416 <div class="alt">
2417 ~~~{.xml}
2418 <ns:record>
2419 <base>
2420 <name></name>
2421 <number>0</number>
2422 </base>
2423 </ns:record>
2424 ~~~
2425 </div>
2426
2427 We can also assign the derived type value to the `ns_record::base` member:
2428
2429 ~~~{.cpp}
2430 struct ns__record record;
2431 soap_default_ns__record(soap, &record);
2432 record.base.ns__derived = soap_new_ns__derived(soap, -1);
2433 soap_write_ns__record(soap, &record);
2434 ~~~
2435
2436 This produces the following XML fragment populated with default values (empty
2437 text for strings and zeros for numbers), where element <i>`base`</i> has schema
2438 type <i>`ns:derived`</i> with three child elements and an attribute:
2439
2440 <div class="alt">
2441 ~~~{.xml}
2442 <ns:record>
2443 <base xsi:type="ns:derived" value="">
2444 <name></name>
2445 <number>0</number>
2446 <text></text>
2447 </base>
2448 </ns:record>
2449 ~~~
2450 </div>
2451
2452 Deserialization automatically assigns values to the base members for the
2453 `ns__base` type and populates the `ns__derived` member when a derived type with
2454 <i>`xsi:type="ns:derived"`</i> is parsed. This makes it easy to decompose the
2455 deserialized data:
2456
2457 ~~~{.cpp}
2458 struct ns__record record;
2459 soap_read_ns__record(soap, &record);
2460 if (record.ns__derived)
2461 printf("Derived type with name=%s number=%d text=%s value=%s\n",
2462 record.ns__derived->name,
2463 record.ns__derived->number,
2464 record.ns__derived->text,
2465 record.ns__derived->value);
2466 else
2467 printf("Base type with name=%s number=%d\n",
2468 record.name,
2469 record.number);
2470 ~~~
2471
2472 This example requires the `SOAP_XML_STRICT` mode flag to initialize the `soap`
2473 context to ensure that all required values are present in the deserialized
2474 structures, otherwise the `char*` strings may be NULL since XML validation
2475 constraints are not enforced on the XML input.
2476
2477 Deeper levels of simulated inheritance are possible, for example:
2478
2479 ~~~{.cpp}
2480 struct ns__base // a base type
2481 {
2482 char *name 1;
2483 int number 1;
2484 [ struct ns__derived *ns__derived; ] // points to derived type if non-NULL
2485 };
2486 struct ns__derived // extends ns__base with two additional members
2487 {
2488 char *name 1;
2489 int number 1;
2490 char *text 1;
2491 @ char *value 1;
2492 [ struct ns__derived_derived *ns__derived_derived; ] // points to derived_derived type if non-NULL
2493 };
2494 struct ns__derived_derived // extends ns__derived with an additional member
2495 {
2496 char *name 1;
2497 int number 1;
2498 char *text 1;
2499 @ char *value 1;
2500 @ char *type 1;
2501 };
2502 ~~~
2503
2504 This requires two pointer traversals from the base type `ns__base` via
2505 `ns__derived` to reach `ns__derived_derived`.
2506
2507 🔝 [Back to table of contents](#)
2508
2509 ### Volatile classes and structs {#toxsd9-2}
2510
2511 Classes and structs can be declared `volatile` in the interface header file for
2512 soapcpp2, which only has meaning for the gSOAP tools. This annotation means
2513 that these types are already declared elsewhere in your project's source code
2514 and you do not want soapcpp2 to generate code with a second declaration of
2515 these types.
2516
2517 For example, `struct tm` is declared in the <i>`time.h`</i> library. You can
2518 make it serializable and include a partial list of data members that you want
2519 to serialize:
2520
2521 ~~~{.cpp}
2522 volatile struct tm
2523 {
2524 int tm_sec; // seconds (0 - 60)
2525 int tm_min; // minutes (0 - 59)
2526 int tm_hour; // hours (0 - 23)
2527 int tm_mday; // day of month (1 - 31)
2528 int tm_mon; // month of year (0 - 11)
2529 int tm_year; // year - 1900
2530 };
2531 ~~~
2532
2533 You can declare classes and structs `volatile` for any such types you want to
2534 serialize by only providing the public data members you want to serialize.
2535
2536 In addition, [colon notation](#toxsd2) is a simple and effective way to bind an
2537 existing class or struct to a schema. For example, you can change the `tm` name
2538 as follows without affecting the code that uses `struct tm` generated by
2539 soapcpp2:
2540
2541 ~~~{.cpp}
2542 volatile struct ns:tm { ... }
2543 ~~~
2544
2545 This struct maps to a complexType in the soapcpp2-generated XML schema:
2546
2547 <div class="alt">
2548 ~~~{.xml}
2549 <complexType name="tm">
2550 <sequence>
2551 <element name="tm-sec" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2552 <element name="tm-min" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2553 <element name="tm-hour" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2554 <element name="tm-mday" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2555 <element name="tm-mon" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2556 <element name="tm-year" type="xsd:int" minOccurs="1" maxOccurs="1"/>
2557 </sequence>
2558 </complexType>
2559 ~~~
2560 </div>
2561
2562 🔝 [Back to table of contents](#)
2563
2564 ### Mutable classes and structs {#toxsd9-3}
2565
2566 Classes and structs can be declared `mutable` with the gSOAP tools. This means
2567 that their definition can be spread out over the source code. This promotes the
2568 concept of a class or struct as a *row of named values*, also known as a *named
2569 tuple*, that can be extended at compile time in your source code with additional
2570 members. Because these types differ from the traditional object-oriented
2571 principles and design concepts of classes and objects, constructors and
2572 destructors cannot be defined (also because we cannot guarantee merging these
2573 into one such that all members will be initialized). A default constructor,
2574 copy constructor, assignment operation, and destructor will be assigned
2575 automatically by soapcpp2.
2576
2577 ~~~{.cpp}
2578 mutable struct ns__tuple
2579 {
2580 @ std::string id;
2581 };
2582
2583 mutable struct ns__tuple
2584 {
2585 std::string name;
2586 std::string value;
2587 };
2588 ~~~
2589
2590 The members are collected into one definition generated by soapcpp2. Members
2591 may be repeated from one definition to another, but only if their associated
2592 types are identical. So, for example, a third extension with a `value` member
2593 with a different type fails:
2594
2595 ~~~{.cpp}
2596 mutable struct ns__tuple
2597 {
2598 float value; // BAD: value is already declared std::string
2599 };
2600 ~~~
2601
2602 The `mutable` concept has proven to be very useful when declaring and
2603 collecting SOAP Headers for multiple services, which are collected into one
2604 `struct SOAP_ENV__Header` by the soapcpp2 tool.
2605
2606 🔝 [Back to table of contents](#)
2607
2608 ### Default and fixed member values {#toxsd9-4}
2609
2610 Class and struct data members in C and C++ may be declared with an optional
2611 default initialization value that is provided "inline" with the declaration of
2612 the member:
2613
2614 ~~~{.cpp}
2615 class ns__record
2616 {
2617 public:
2618 std::string name = "Joe";
2619 ...
2620 };
2621 ~~~
2622
2623 Alternatively, you can use C++11 default initialization syntax:
2624
2625 ~~~{.cpp}
2626 class ns__record
2627 {
2628 public:
2629 std::string name { "Joe" };
2630 ...
2631 };
2632 ~~~
2633
2634 Any member with a primitive type can be initialized in this way.
2635
2636 These initializations are performed by the default constructor that is added by
2637 soapcpp2 to each class and struct (in C++ only). A constructor is only added
2638 when a default constructor is not already defined with the class declaration.
2639
2640 You can explicitly (re)initialize an object with these initial values by using
2641 the soapcpp2 auto-generated functions:
2642
2643 - `void T::soap_default(struct soap*)` for `class T` (C++ only)
2644
2645 - `void soap_default_T(struct soap*, T*)` for `struct T` (C and C++).
2646
2647 If `T` is a struct or class that has a `soap` pointer member to a `::soap`
2648 context then this pointer member will be set to the first argument passed to
2649 these functions to initialize their `soap` pointer member.
2650
2651 Default value initializations can be provided for members that have primitive
2652 types (`bool`, `enum`, `time_t`, numeric and string types).
2653
2654 Default value initializations of pointer members is permitted, but the effect
2655 is different. To conform to XML schema validation, an attribute member that is
2656 a pointer to a primitive type will be assigned the default value when parsed
2657 from XML. An element member that is a pointer to a primitive type will be
2658 assigned when the element is empty when parsed from XML.
2659
2660 As of gSOAP 2.8.48 and greater, a fixed value can be assigned with a `==`. A
2661 fixed value is also verified by the parser's validator.
2662
2663 Default and fixed values for members with or without pointers are best
2664 explained with the following two example fragments.
2665
2666 A record class with default values for `std::string` (or `std::wstring`)
2667 attributes and elements is declared as follows:
2668
2669 ~~~{.cpp}
2670 class ns__record_with_default
2671 {
2672 public:
2673 @ std::string a = "A"; // optional XML attribute with default value "A"
2674 @ std::string b 1 = "B"; // required XML attribute with default value "B"
2675 @ std::string *c = "C"; // optional XML attribute with default value "C"
2676 std::string d 0 = "D"; // optional XML element with default value "D"
2677 std::string e = "E"; // required XML element with default value "E"
2678 std::string *f = "F"; // optional XML element with default value "F"
2679 ...
2680 };
2681 ~~~
2682
2683 Also `std::unique_ptr` and `std::shared_ptr` may be used instead of a regular
2684 pointer to strings.
2685
2686 With C `char*` (or `const char*`, `const wchar_t*`) strings in a struct, this
2687 becomes:
2688
2689 ~~~{.cpp}
2690 struct ns__record_with_default
2691 {
2692 @ char* a = "A"; // optional XML attribute with default value "A"
2693 @ char* b 1 = "B"; // required XML attribute with default value "B"
2694 char* e 1 = "E"; // required XML element with default value "E"
2695 char* f = "F"; // optional XML element with default value "F"
2696 ...
2697 };
2698 ~~~
2699
2700 By contrast to `std::string e`, `char* e` must be marked `1` to make it
2701 required, because pointer members are optional by default.
2702
2703 Attributes are considered optional by default, unless marked as required with
2704 the occurrence constraint `1`. Elements are considered required unless the
2705 member type is a pointer or if the member is marked optional with occurrence
2706 constraint `0`.
2707
2708 Instead of default values, fixed values indicate that the attribute or element
2709 must contain that value, and only that value, when provided in XML. A fixed
2710 value is specified with a `==`.
2711
2712 Attributes with default or fixed values may be omitted in XML. When absent,
2713 the default/fixed value is used at the receiving side, i.e. the deserializer
2714 assigns the default/fixed value when the attribute is absent. Therefore, there
2715 is no need to make attributes with default/fixed values pointer based, because
2716 there is no way to distinguish an omitted attribute from a populated attribute
2717 on the receiving side. The `c` member in the example above can be a
2718 non-pointer for this reason. The wsdl2h tool does not generate pointers for
2719 attributes with default/fixed values.
2720
2721 Elements with default or fixed values may be optional and the use of
2722 default/fixed values with elements differs from attributes. The default/fixed
2723 value of an element is only used for elements that are empty in the XML payload
2724 received. Omitted optional elements in the XML payload received are simply
2725 absent; no default/fixed value is assigned.
2726
2727 @note gSOAP 2.8.106 and greater treat `char*` and `wchar_t*` with explicit
2728 default and fixed values differently than previous versions. Versions prior to
2729 2.8.106 assign the default value when the corresponding XML element is absent,
2730 whereas 2.8.106 and greater assign NULL when the XML element is absent, exactly
2731 as documented in this updated version of this document. To revert to the old
2732 behavior, use <b>`soapcpp2 -z4`</b> option <b>`-z4`</b>. The change affects
2733 members `char* f` and `char* l` (see below).
2734
2735 A record class (can be a struct in C) with fixed values for attributes and
2736 elements is declared as follows:
2737
2738 ~~~{.cpp}
2739 class ns__record_with_fixed
2740 {
2741 public:
2742 @ std::string g == "G"; // optional XML attribute with fixed value "G"
2743 @ std::string h 1 == "H"; // required XML attribute with fixed value "H"
2744 @ std::string *i == "I"; // optional XML attribute with fixed value "I"
2745 std::string j 0 == "J"; // optional XML element with fixed value "J"
2746 std::string k == "K"; // required XML element with fixed value "K"
2747 std::string *l == "L"; // optional XML element with fixed value "L"
2748 ...
2749 };
2750 ~~~
2751
2752 With C `char*` (or `const char*`, `const wchar_t*`) strings in a struct, this
2753 becomes:
2754
2755 ~~~{.cpp}
2756 struct ns__record_with_fixed
2757 {
2758 @ char* g == "G"; // optional XML attribute with fixed value "G"
2759 @ char* h 1 == "H"; // required XML attribute with fixed value "H"
2760 char* k 1 == "K"; // required XML element with fixed value "K"
2761 char* l == "L"; // optional XML element with fixed value "L"
2762 ...
2763 };
2764 ~~~
2765
2766 The XML schema validation rules for the examples above are as follows:
2767
2768 Member | Notes
2769 ------ | ---------------------------------------------------------------------
2770 `a` | attribute may appear once; if it does not appear its value is "A", otherwise its value is that given (also note: instantiating `ns__record_with_default` assigns the default value "A")
2771 `b` | has no effect when parsing XML (but note: instantiating `ns__record_with_default` assigns the default value "B")
2772 `c` | attribute may appear once; if it does not appear its value is "C", otherwise its value is that given (also note: instantiating `ns__record_with_default` assigns NULL)
2773 `d` | element may appear once; if it does not appear or if it is empty, its value is "D"; otherwise its value is that given (also note: instantiating `ns__record_with_default` assigns the default value "D")
2774 `e` | has no effect when parsing XML (but note: instantiating `ns__record_with_default` assigns the default value "E")
2775 `f` | element may appear once; if it does not appear it is not provided; if it does appear and it is empty, its value is "F"; otherwise its value is that given (also note: instantiating `ns__record_with_default` assigns NULL)
2776 `g` | attribute may appear once; if it does not appear its value is "G", if it does not appear its value is "G" (also note: instantiating `ns__record_with_fixed` assigns the fixed value "G")
2777 `h` | attribute must appear once, its value must be "H" (also note: instantiating `ns__record_with_fixed` assigns the fixed value "H")
2778 `i` | attribute may appear once; if it does not appear its value is "I", if it does not appear its value is "I" (also note: instantiating `ns__record_with_fixed` assigns NULL)
2779 `j` | element may appear once, if it does not appear it is not provided; if it does appear and it is empty, its value is "J"; if it does appear and it is not empty, its value must be "J" (also note: instantiating `ns__record_with_fixed` assigns the fixed value "J")
2780 `k` | element must appear once, its value must be "K" (also note: instantiating `ns__record_with_fixed` assigns the fixed value "K")
2781 `l` | element may appear once, if it does not appear it is not provided; if it does appear and it is empty, its value is "J"; if it does appear and it is not empty, its value must be "J" (also note: instantiating `ns__record_with_fixed` assigns NULL)
2782
2783 Members of type `char[N]` (fixed length string) can have default and fixed
2784 values, when <b>`soapcpp2 -b`</b> option <b>`-b`</b> is used. Also `char**`
2785 (pointer to a string) members can have default and fixed values. However,
2786 members of this type will be initialized to NULL. The default/fixed values
2787 will be assigned with the same rules as for `char*` when deserialized from XML.
2788
2789 @see Section [operations on classes and structs](#toxsd9-14).
2790
2791 🔝 [Back to table of contents](#)
2792
2793 ### Attribute members {#toxsd9-5}
2794
2795 Class and struct data members are declared as XML attributes by annotating
2796 their type with a `@` qualifier:
2797
2798 ~~~{.cpp}
2799 class ns__record
2800 {
2801 public:
2802 @ std::string name; // required (non-pointer means required)
2803 @ uint64_t SSN; // required (non-pointer means required)
2804 ns__record *spouse; // optional (pointer means minOccurs=0)
2805 };
2806 ~~~
2807
2808 This class maps to a complexType in the soapcpp2-generated XML schema:
2809
2810 <div class="alt">
2811 ~~~{.xml}
2812 <complexType name="record">
2813 <sequence>
2814 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/>
2815 </sequence>
2816 <attribute name="name" type="xsd:string" use="required"/>
2817 <attribute name="SSN" type="xsd:unsignedLong" use="required"/>
2818 </complexType>
2819 ~~~
2820 </div>
2821
2822 An example XML instance of `ns__record` is:
2823
2824 <div class="alt">
2825 ~~~{.xml}
2826 <ns:record xmlns:ns="urn:types" name="Joe" SSN="1234567890">
2827 <spouse name="Jane" SSN="1987654320">
2828 </spouse>
2829 </ns:record>
2830 ~~~
2831 </div>
2832
2833 Attribute data members are restricted to primitive types (`bool`, `enum`,
2834 `time_t`, numeric and string types), `xsd__hexBinary`, `xsd__base64Binary`, and
2835 custom serializers, such as `xsd__dateTime`. Custom serializers for types that
2836 may be used as attributes should define `soap_s2T` and `soap_T2s` functions that
2837 convert values of type `T` to strings and back.
2838
2839 Attribute data members can be pointers and smart pointers to these types, which
2840 permits attributes to be optional.
2841
2842 🔝 [Back to table of contents](#)
2843
2844 ### Backtick XML tags {#toxsd9-5-1}
2845
2846 The XML tag name of a class/struct member is the name of the member with the
2847 usual XML tag translation, see [colon notation](#toxsd2).
2848
2849 To override the standard translation of identifier names to XML tag names of
2850 attributes and elements, add the XML tag name in backticks (requires gSOAP
2851 2.8.30 or greater):
2852
2853 ~~~{.cpp}
2854 class ns__record
2855 {
2856 public:
2857 @ std::string name `full-name`;
2858 @ uint64_t SSN `tax-id`;
2859 ns__record *spouse `married-to`;
2860 };
2861 ~~~
2862
2863 This class maps to a complexType in the soapcpp2-generated XML schema:
2864
2865 <div class="alt">
2866 ~~~{.xml}
2867 <complexType name="record">
2868 <sequence>
2869 <element name="married-to" type="ns:record" minOccurs="0" maxOccurs="1"/>
2870 </sequence>
2871 <attribute name="full-name" type="xsd:string" use="required"/>
2872 <attribute name="tax-id" type="xsd:unsignedLong" use="required"/>
2873 </complexType>
2874 ~~~
2875 </div>
2876
2877 An example XML instance of `ns__record` is:
2878
2879 <div class="alt">
2880 ~~~{.xml}
2881 <ns:record xmlns:ns="urn:types" full-name="Joe" tax-id="1234567890">
2882 <married-to full-name="Jane" tax-id="1987654320">
2883 </married-to>
2884 </ns:record>
2885 ~~~
2886 </div>
2887
2888 A backtick XML tag name may contain any non-empty sequence of ASCII and UTF-8
2889 characters except white space and the backtick character. A backtick tag can
2890 be combined with member constraints and default member initializers:
2891
2892 ~~~{.cpp}
2893 @ uint64_t SSN `tax-id` 0:1 = 999;
2894 ~~~
2895
2896 🔝 [Back to table of contents](#)
2897
2898 ### Qualified and unqualified members {#toxsd9-6}
2899
2900 Class, struct, and union data members are mapped to namespace qualified or
2901 unqualified tag names of local elements and attributes. If a data member has
2902 no prefix then the default form of qualification is applied based on the
2903 element/attribute form that is declared with the XML schema of the class, struct,
2904 or union type. If the member name has a namespace prefix by colon notation,
2905 then the prefix overrules the default (un)qualified form. Therefore,
2906 [colon notation](#toxsd2) is an effective mechanism to control qualification of
2907 tag names of individual members of classes, structs, and unions.
2908
2909 The XML schema elementFormDefault and attributeFormDefault declarations control
2910 the tag name qualification of local elements and attributes, respectively.
2911
2912 - "unqualified" indicates that local elements/attributes are not qualified with
2913 the namespace prefix.
2914
2915 - "qualified" indicates that local elements/attributes must be qualified with
2916 the namespace prefix.
2917
2918 Individual schema declarations of local elements and attributes may overrule
2919 this by using the form declaration in an XML schema and by using colon notation
2920 to add namespace prefixes to class, struct, and union members in the header
2921 file for soapcpp2.
2922
2923 Consider for example an `ns__record` class in the `ns` namespace in which local
2924 elements are qualified and local attributes are unqualified by default:
2925
2926 ~~~{.cpp}
2927 //gsoap ns schema namespace: urn:types
2928 //gsoap ns schema elementForm: qualified
2929 //gsoap ns schema attributeForm: unqualified
2930 class ns__record
2931 {
2932 public:
2933 @ std::string name;
2934 @ uint64_t SSN;
2935 ns__record *spouse;
2936 };
2937 ~~~
2938
2939 This class maps to a complexType in the soapcpp2-generated XML schema with
2940 targetNamespace "urn:types", elementFormDefault qualified and
2941 attributeFormDefault unqualified:
2942
2943 <div class="alt">
2944 ~~~{.xml}
2945 <schema targetNamespace="urn:types"
2946 ...
2947 elementFormDefault="qualified"
2948 attributeFormDefault="unqualified"
2949 ... >
2950 <complexType name="record">
2951 <sequence>
2952 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1"/>
2953 </sequence>
2954 <attribute name="name" type="xsd:string" use="required"/>
2955 <attribute name="SSN" type="xsd:unsignedLong" use="required"/>
2956 </complexType>
2957 </schema>
2958 ~~~
2959 </div>
2960
2961 An example XML instance of `ns__record` is:
2962
2963 <div class="alt">
2964 ~~~{.xml}
2965 <ns:record xmlns:ns="urn:types" name="Joe" SSN="1234567890">
2966 <ns:spouse> name="Jane" SSN="1987654320">
2967 </ns:spouse>
2968 </ns:record>
2969 ~~~
2970 </div>
2971
2972 Here the root element <i>`<ns:record>`</i> is qualified because it is a root
2973 element of the XML schema with target namespace "urn:types". Its local element
2974 <i>`<ns:spouse>`</i> is namespace qualified because the elementFormDefault of
2975 local elements is qualified. Attributes are unqualified.
2976
2977 The default namespace (un)qualification of local elements and attributes can be
2978 overruled by adding a prefix to the member name by using colon notation:
2979
2980 ~~~{.cpp}
2981 //gsoap ns schema namespace: urn:types
2982 //gsoap ns schema elementForm: qualified
2983 //gsoap ns schema attributeForm: unqualified
2984 class ns__record
2985 {
2986 public:
2987 @ std::string ns:name; // 'ns:' qualified
2988 @ uint64_t SSN;
2989 ns__record *:spouse; // ':' unqualified (no prefix)
2990 };
2991 ~~~
2992
2993 The colon notation for member <i>`ns:name`</i> forces qualification of its attribute
2994 tag in XML. The colon notation for member <i>`:spouse`</i> removes qualification from
2995 its local element tag:
2996
2997 <div class="alt">
2998 ~~~{.xml}
2999 <schema targetNamespace="urn:types"
3000 ...
3001 elementFormDefault="unqualified"
3002 attributeFormDefault="unqualified"
3003 ... >
3004 <complexType name="record">
3005 <sequence>
3006 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" form="unqualified"/>
3007 </sequence>
3008 <attribute name="name" type="xsd:string" use="required" form="qualified"/>
3009 <attribute name="SSN" type="xsd:unsignedLong" use="required"/>
3010 </complexType>
3011 </schema>
3012 ~~~
3013 </div>
3014
3015 XML instances of `ns__record` have unqualified spouse elements and qualified
3016 ns:name attributes:
3017
3018 <div class="alt">
3019 ~~~{.xml}
3020 <ns:record xmlns:ns="urn:types" ns:name="Joe" SSN="1234567890">
3021 <spouse> ns:name="Jane" SSN="1987654320">
3022 </spouse>
3023 </ns:record>
3024 ~~~
3025 </div>
3026
3027 Members of a class or struct can also be prefixed using the `prefix__name`
3028 convention or using colon notation `prefix:name`. However, this has a
3029 different effect by referring to global (root) elements and attributes, see
3030 [document root element definitions](#toxsd9-7).
3031
3032 [Backtick XML tags](#toxsd9-5-1) can be used in place of the member name
3033 annotations and will achieve the same effect as described when these tag names
3034 are (un)qualified (requires gSOAP 2.8.30 or greater).
3035
3036 @note You must declare a target namespace with a `//gsoap ns schema namespace:`
3037 directive to enable the `elementForm` and `attributeForm` directives in order
3038 to generate valid XML schemas with soapcpp2. See [directives](#directives) for
3039 more details.
3040
3041 🔝 [Back to table of contents](#)
3042
3043 ### Defining document root elements {#toxsd9-7}
3044
3045 To define and reference XML document root elements we use type names that start
3046 with an underscore:
3047
3048 ~~~{.cpp}
3049 class _ns__record
3050 ~~~
3051
3052 Alternatively, we can use a `typedef` to define a document root element with a
3053 given type:
3054
3055 ~~~{.cpp}
3056 typedef ns__record _ns__record;
3057 ~~~
3058
3059 This `typedef` maps to a global root element that is added to the
3060 soapcpp2-generated XML schema:
3061
3062 <div class="alt">
3063 ~~~{.xml}
3064 <element name="record" type="ns:record"/>
3065 ~~~
3066 </div>
3067
3068 An example XML instance of `_ns__record` is:
3069
3070 <div class="alt">
3071 ~~~{.xml}
3072 <ns:record xmlns:ns="urn:types">
3073 <name>Joe</name>
3074 <SSN>1234567890</SSN>
3075 <spouse>
3076 <name>Jane</name>
3077 <SSN>1987654320</SSN>
3078 </spouse>
3079 </ns:record>
3080 ~~~
3081 </div>
3082
3083 Global-level element/attribute definitions are also referenced and/or added to
3084 the generated XML schema when serializable data members reference these by
3085 their qualified name:
3086
3087 ~~~{.cpp}
3088 typedef std::string _ns__name 1 : 100;
3089 class _ns__record
3090 {
3091 public:
3092 @ _QName xsi__type; // built-in XSD attribute xsi:type
3093 _ns__name ns__name; // ref to global ns:name element
3094 uint64_t SSN;
3095 _ns__record *spouse;
3096 };
3097 ~~~
3098
3099 These types map to the following comonents in the soapcpp2-generated XML
3100 schema:
3101
3102 <div class="alt">
3103 ~~~{.xml}
3104 <simpleType name="name">
3105 <restriction base="xsd:string">
3106 <minLength value="1"/>
3107 <maxLength value="100"/>
3108 </restriction>
3109 </simpleType>
3110 <element name="name" type="ns:name"/>
3111 <complexType name="record">
3112 <sequence>
3113 <element ref="ns:name" minOccurs="1" maxOccurs="1"/>
3114 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3115 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1"/>
3116 </sequence>
3117 <attribute ref="xsi:type" use="optional"/>
3118 </complexType>
3119 <element name="record" type="ns:record"/>
3120 ~~~
3121 </div>
3122
3123 Use only use qualified member names when their types match the global-level
3124 element types that they refer to. For example:
3125
3126 ~~~{.cpp}
3127 typedef std::string _ns__name; // global element ns:name of type xsd:string
3128 class _ns__record
3129 {
3130 public:
3131 int ns__name; // BAD: global element ns:name is NOT type int
3132 _ns__record ns__record; // OK: ns:record is a global-level root element
3133 ...
3134 };
3135 ~~~
3136
3137 Therefore, we recommend to use qualified member names only when necessary to
3138 refer to standard XSD elements and attributes, such as `xsi__type`, and
3139 `xsd__lang`.
3140
3141 By contrast, colon notation has the desired effect to (un)qualify local tag
3142 names by overruling the default element/attribute namespace qualification, see
3143 [qualified and unqualified members](#toxsd9-6).
3144
3145 As an alternative to prefixing member names, use the backtick tag (requires
3146 gSOAP 2.8.30 or greater):
3147
3148 ~~~{.cpp}
3149 typedef std::string _ns__name 1 : 100;
3150 class _ns__record
3151 {
3152 public:
3153 @ _QName t <i>`xsi:type`</i>; // built-in XSD attribute xsi:type
3154 _ns__name s <i>`ns:name`</i>; // ref to global ns:name element
3155 uint64_t SSN;
3156 _ns__record *spouse;
3157 };
3158 ~~~
3159
3160 🔝 [Back to table of contents](#)
3161
3162 ### (Smart) pointer members and their occurrence constraints {#toxsd9-8}
3163
3164 A public pointer-typed data member is serialized by following its (smart)
3165 pointer(s) to the value pointed to. To serialize pointers to dynamic arrays of
3166 data, please see the next section on
3167 [container and array members and their occurrence constraints](#toxsd9-9).
3168
3169 Pointers that are NULL and smart pointers that are empty are serialized to
3170 produce omitted element and attribute values, unless an element is required
3171 and is nillable (struct/class members marked with `nullptr`) in which case the
3172 element is rendered as an empty element with <i>`xsi:nil="true"`</i>.
3173
3174 To control the occurrence requirements of pointer-based data members,
3175 occurrence constraints are associated with data members in the form of a range
3176 `minOccurs : maxOccurs`. For non-repeatable (meaning, not a container or array)
3177 data members, there are only three reasonable occurrence constraints:
3178
3179 - `0:0` means that this element or attribute is prohibited.
3180
3181 - `0:1` means that this element or attribute is optional.
3182
3183 - `1:1` means that this element or attribute is required.
3184
3185 Pointer-based data members have a default `0:1` occurrence constraint, making
3186 them optional, and their XML schema local element/attribute definition is
3187 marked as nillable. Non-pointer data members have a default `1:1` occurence
3188 constraint, making them required.
3189
3190 A `nullptr` occurrence constraint may be applicable to required elements that
3191 are nillable pointer types, thus `nullptr 1:1`. This indicates that the
3192 element is nillable (can be `NULL` or `nullptr`). A pointer data member that
3193 is explicitly marked as required and nillable with `nullptr 1:1` will be
3194 serialized as an element with an <i>`xsi:nil`</i> attribute, thus effectively
3195 revealing the NULL property of its value.
3196
3197 A non-pointer data member that is explicitly marked as optional with `0:1` will
3198 be set to its default value when no XML value is presented to the deserializer.
3199 A default value can be assigned to a data member that has a primitive type or
3200 is a (smart) pointer to primitive type.
3201
3202 Consider for example:
3203
3204 ~~~{.cpp}
3205 class ns__record
3206 {
3207 public:
3208 std::shared_ptr<std::string> name; // optional (pointer means minOccurs=0)
3209 uint64_t SSN 0:1 = 999; // force optional with default 999
3210 ns__record *spouse nullptr 1:1; // force required and nillabe when absent
3211 };
3212 ~~~
3213
3214 This class maps to a complexType in the soapcpp2-generated XML schema:
3215
3216 <div class="alt">
3217 ~~~{.xml}
3218 <complexType name="record">
3219 <sequence>
3220 <element name="name" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
3221 <element name="SSN" type="xsd:unsignedLong" minOccurs="0" maxOccurs="1" default="999"/>
3222 <element name="spouse" type="ns:record" minOccurs="1" maxOccurs="1" nillable="true"/>
3223 </sequence>
3224 </complexType>
3225 ~~~
3226 </div>
3227
3228 An example XML instance of `ns__record` with its `name` string value set to
3229 `Joe`, `SSN` set to its default, and `spouse` set to NULL:
3230
3231 <div class="alt">
3232 ~~~{.xml}
3233 <ns:record xmlns:ns="urn:types" ...>
3234 <name>Joe</name>
3235 <SSN>999</SSN>
3236 <spouse xsi:nil="true"/>
3237 </ns:record>
3238 ~~~
3239 </div>
3240
3241 @note In general, a smart pointer is simply declared as a `volatile` template
3242 in a interface header file for soapcpp2:
3243 ~~~{.cpp}
3244 volatile template <class T> class NAMESPACE::shared_ptr;
3245 ~~~
3246
3247 @note The soapcpp2 tool generates code that uses `NAMESPACE::shared_ptr` and
3248 `NAMESPACE::make_shared` to create shared pointers to objects, where
3249 `NAMESPACE` is any valid C++ namespace such as `std` and `boost` if you have
3250 Boost installed.
3251
3252 🔝 [Back to table of contents](#)
3253
3254 ### Container and array members and their occurrence constraints {#toxsd9-9}
3255
3256 Class and struct data member types that are containers `std::deque`,
3257 `std::list`, `std::vector` and `std::set` are serialized as a collection of
3258 the values they contain. You can also serialize dynamic arrays, which is the
3259 alternative for C to store collections of data. Let's start with containers.
3260
3261 You can use `std::deque`, `std::list`, `std::vector`, and `std::set` containers
3262 by importing:
3263
3264 ~~~{.cpp}
3265 #import "import/stl.h" // import all containers
3266 #import "import/stldeque.h" // import deque
3267 #import "import/stllist.h" // import list
3268 #import "import/stlvector.h" // import vector
3269 #import "import/stlset.h" // import set
3270 ~~~
3271
3272 For example, to use a vector data mamber to store names in a record:
3273
3274 ~~~{.cpp}
3275 #import "import/stlvector.h"
3276 class ns__record
3277 {
3278 public:
3279 std::vector<std::string> names;
3280 uint64_t SSN;
3281 };
3282 ~~~
3283
3284 To limit the number of names in the vector within reasonable bounds, occurrence
3285 constraints are associated with the container. Occurrence constraints are of
3286 the form `minOccurs : maxOccurs`:
3287
3288 ~~~{.cpp}
3289 #import "import/stlvector.h"
3290 class ns__record
3291 {
3292 public:
3293 std::vector<std::string> names 1:10;
3294 uint64_t SSN;
3295 };
3296 ~~~
3297
3298 This class maps to a complexType in the soapcpp2-generated XML schema:
3299
3300 <div class="alt">
3301 ~~~{.xml}
3302 <complexType name="record">
3303 <sequence>
3304 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="10"/>
3305 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3306 </sequence>
3307 </complexType>
3308 ~~~
3309 </div>
3310
3311 @note In general, a container is simply declared as a template in an interface
3312 header file for soapcpp2. All class templates are considered containers
3313 (except when declared `volatile`, see smart pointers). For example,
3314 `std::vector` is declared in <i>`gsoap/import/stlvector.h`</i> as:
3315 ~~~{.cpp}
3316 template <class T> class std::vector;
3317 ~~~
3318
3319 @note You can define and use your own containers. The soapcpp2 tool generates
3320 code that uses the following members of the `template <typename T> class C`
3321 container:
3322 ~~~{.cpp}
3323 void C::clear()
3324 C::iterator C::begin()
3325 C::const_iterator C::begin() const
3326 C::iterator C::end()
3327 C::const_iterator C::end() const
3328 size_t C::size() const
3329 C::iterator C::insert(C::iterator pos, const T& val)
3330 ~~~
3331
3332 @note For more details see the example `simple_vector` container with
3333 documentation in the package under <i>`gsoap/samples/template`</i>.
3334
3335 Because C does not support a container template library, we can use a
3336 dynamically-sized array of values. This array is declared as a size-pointer
3337 pair of members within a struct or class. The array size information is stored
3338 in a special size tag member with the name `__size` or `__sizeX`, where `X` can
3339 be any name, or by an `$int` member to identify the member as a special size
3340 tag:
3341
3342 ~~~{.cpp}
3343 struct ns__record
3344 {
3345 $ int sizeofnames; // array size
3346 char* *names; // array of char* names
3347 uint64_t SSN;
3348 };
3349 ~~~
3350
3351 This struct maps to a complexType in the soapcpp2-generated XML schema:
3352
3353 <div class="alt">
3354 ~~~{.xml}
3355 <complexType name="record">
3356 <sequence>
3357 <element name="name" type="xsd:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
3358 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3359 </sequence>
3360 </complexType>
3361 ~~~
3362 </div>
3363
3364 To limit the number of names in the array within reasonable bounds, occurrence
3365 constraints are associated with the array size member. Occurrence constraints
3366 are of the form `minOccurs : maxOccurs`:
3367
3368 ~~~{.cpp}
3369 struct ns__record
3370 {
3371 $ int sizeofnames 1:10; // array size 1..10
3372 char* *names; // array of one to ten char* names
3373 uint64_t SSN;
3374 };
3375 ~~~
3376
3377 This struct maps to a complexType in the soapcpp2-generated XML schema:
3378
3379 <div class="alt">
3380 ~~~{.xml}
3381 <complexType name="record">
3382 <sequence>
3383 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="10" nillable="true"/>
3384 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3385 </sequence>
3386 </complexType>
3387 ~~~
3388 </div>
3389
3390 Arrays can also be declared as nested elements, similar to SOAP-encoded dynamic arrays, and these arrays can be used with or without SOAP applications. This requires a separate struct or class with the name of the SOAP array, which should not be qualified with a namespace prefix:
3391
3392 ~~~{.cpp}
3393 struct ArrayOfstring
3394 {
3395 char* *__ptr 1:100; // array of 1..100 strings
3396 int __size; // array size
3397 };
3398 struct ns__record
3399 {
3400 struct ArrayOfstring names; // array of char* names
3401 uint64_t SSN;
3402 };
3403 ~~~
3404
3405 The `ns__record` struct maps to a complexType that references the <i>`ArrayOfstring`</i> complexType with an sequence of 1 to 100 <i>`item`</i> elements:
3406
3407 <div class="alt">
3408 ~~~{.xml}
3409 <complexType name="ArrayOfstring">
3410 <sequence>
3411 <element name="item" type="xsd:string" minOccurs="1" maxOccurs="100"/>
3412 </sequence>
3413 </complexType>
3414 <complexType name="record">
3415 <sequence>
3416 <element name="names" type="ns:ArrayOfstring" minOccurs="1" maxOccurs="1"/>
3417 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3418 </sequence>
3419 </complexType>
3420 ~~~
3421 </div>
3422
3423 To change the <i>`item`</i> element name in the WSDL, XML schema, and XML messages, use `__ptrName` where `Name` is the name you want to use.
3424
3425 @note When <b>`soapcpp2 -e`</b> option <b>`-e`</b> is used, the <i>`ArrayOfstring`</i> becomes a SOAP-encoded array for SOAP 1.1/1.2 RPC encoded messaging:
3426 <div class="alt">
3427 ~~~{.xml}
3428 <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
3429 <complexType name="ArrayOfstring">
3430 <complexContent>
3431 <restriction base="SOAP-ENC:Array">
3432 <sequence>
3433 <element name="item" type="xsd:string" minOccurs="1" maxOccurs="100"/>
3434 </sequence>
3435 <attribute ref="SOAP-ENC:arrayType" WSDL:arrayType="xsd:string[]"/>
3436 </restriction>
3437 </complexContent>
3438 </complexType>
3439 <complexType name="record">
3440 <sequence>
3441 <element name="names" type="ns:ArrayOfstring" minOccurs="1" maxOccurs="1"/>
3442 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3443 </sequence>
3444 </complexType>
3445 ~~~
3446 </div>
3447
3448 Fixed-size arrays can be used to store a fixed number of values:
3449
3450 ~~~{.cpp}
3451 struct ns__record
3452 {
3453 char* names[10]; // array of 10 char* names
3454 uint64_t SSN;
3455 };
3456 ~~~
3457
3458 The fixed-size array is similar to a SOAP-encoded array, which can be used with or without SOAP applications. This struct maps to a complexType that references a <i>`Array10Ofstring`</i> complexType with ten <i>`item`</i> elements:
3459
3460 <div class="alt">
3461 ~~~{.xml}
3462 <complexType name="record">
3463 <sequence>
3464 <element name="names" type="ns:Array10Ofstring" minOccurs="1" maxOccurs="1"/>
3465 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3466 </sequence>
3467 </complexType>
3468 <complexType name="Array10Ofstring">
3469 <sequence>
3470 <element name="item" type="xsd:string" minOccurs="0" maxOccurs="10"/>
3471 </sequence>
3472 </complexType>
3473 ~~~
3474 </div>
3475
3476 @note When <b>`soapcpp2 -e`</b> option <b>`-e`</b> is used, the <i>`Array10Ofstring`</i> becomes a SOAP-encoded array for SOAP 1.1/1.2 RPC encoded messaging, see previous note.
3477
3478 🔝 [Back to table of contents](#)
3479
3480 ### Sequencing with hidden members {#toxsd9-10}
3481
3482 A member becomes a hidden XML element, i.e. not visibly rendered in XML, when
3483 its name starts with a double underscore. This makes it possible to sequence a
3484 collection of data members, basically by forming a sequence of elements that
3485 can be optional or repeated together.
3486
3487 To create a sequence of members that are optional, use a pointer-based hidden
3488 member that is a struct with the collection of members to sequence:
3489
3490 ~~~{.cpp}
3491 struct ns__record
3492 {
3493 std::string name; // required name
3494 struct __ns__optional
3495 {
3496 uint64_t SSN; // SSN in optional group
3497 std::string phone; // phone number in optional group
3498 } *__optional; // optional group
3499 };
3500 ~~~
3501
3502 Here we used a hidden struct type `__ns__optional` which starts with a double
3503 underscore, because we do not want to define a new global type for the XML
3504 schema we generate. We just need a unique name for a structure that sequences
3505 the two members.
3506
3507 This struct maps to a complexType in the soapcpp2-generated XML schema:
3508
3509 <div class="alt">
3510 ~~~{.xml}
3511 <complexType name="record">
3512 <sequence>
3513 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
3514 <sequence minOccurs="0" maxOccurs="1">
3515 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3516 <element name="phone" type="xsd:string" minOccurs="1" maxOccurs="1"/>
3517 </sequence>
3518 </sequence>
3519 </complexType>
3520 ~~~
3521 </div>
3522
3523 The `name` member is a required element of the <i>`ns:record`</i> complexType.
3524 The <i>`ns:record`</i> complexType has an optional sequence of `SSN` and
3525 `phone` elements.
3526
3527 To create repetitions of a sequence of members, use an array as follows:
3528
3529 ~~~{.cpp}
3530 struct ns__record
3531 {
3532 std::string name; // required name
3533 $ int sizeofarray; // size of group array
3534 struct __ns__array
3535 {
3536 uint64_t SSN; // SSN in group
3537 std::string phone; // phone number in group
3538 } *__array; // group array
3539 };
3540 ~~~
3541
3542 This struct maps to a complexType in the soapcpp2-generated XML schema:
3543
3544 <div class="alt">
3545 ~~~{.xml}
3546 <complexType name="record">
3547 <sequence>
3548 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
3549 <sequence minOccurs="0" maxOccurs="unbounded">
3550 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
3551 <element name="phone" type="xsd:string" minOccurs="1" maxOccurs="1"/>
3552 </sequence>
3553 </sequence>
3554 </complexType>
3555 ~~~
3556 </div>
3557
3558 The `name` member is a required element of the <i>`ns:record`</i> complexType.
3559 The <i>`ns:record`</i> complexType has a potentially unbounded sequence of
3560 `SSN` and `phone` elements. You can specify array bounds instead of zero to
3561 unbounded, see [container and array members and their occurrence constraints](#toxsd9-9).
3562
3563 The XML value space consists of a sequence of SSN and phone elements:
3564
3565 <div class="alt">
3566 ~~~{.xml}
3567 <ns:record>
3568 <name>numbers</name>
3569 <SSN>1234567890</SSN>
3570 <phone>555-123-4567</phone>
3571 <SSN>1987654320</SSN>
3572 <phone>555-789-1234</phone>
3573 <SSN>2345678901</SSN>
3574 <phone>555-987-6543</phone>
3575 </ns:record>
3576 ~~~
3577 </div>
3578
3579 🔝 [Back to table of contents](#)
3580
3581 ### Tagged union members {#toxsd9-11}
3582
3583 A union member in a class or in a struct cannot be serialized unless a
3584 discriminating *variant selector* member is provided that tells the serializer
3585 which union field to serialize. This effectively creates a *tagged union*.
3586
3587 The variant selector is associated with the union as a selector-union pair of members.
3588 The variant selector is a member with the name `__union` or `__unionX`, where
3589 `X` can be any name, or by an `$int` member to identify the member as a variant
3590 selector tag:
3591
3592 ~~~{.cpp}
3593 class ns__record
3594 {
3595 public:
3596 $ int xORnORs; // variant selector with values SOAP_UNION_fieldname
3597 union ns__choice
3598 {
3599 float x;
3600 int n;
3601 char *s;
3602 } u;
3603 std::string name;
3604 };
3605 ~~~
3606
3607 The variant selector values are auto-generated based on the union name `choice`
3608 and the names of its members `x`, `n`, and `s`:
3609
3610 - `xORnORs = SOAP_UNION_ns__choice_x` when `u.x` is valid.
3611
3612 - `xORnORs = SOAP_UNION_ns__choice_n` when `u.n` is valid.
3613
3614 - `xORnORs = SOAP_UNION_ns__choice_s` when `u.s` is valid.
3615
3616 - `xORnORs = 0` when none are valid (should only be used with great care,
3617 because XSD validation may fail when content is required but absent).
3618
3619 This class maps to a complexType with a sequence and choice in the
3620 soapcpp2-generated XML schema:
3621
3622 <div class="alt">
3623 ~~~{.xml}
3624 <complexType name="record">
3625 <sequence>
3626 <choice>
3627 <element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/>
3628 <element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/>
3629 <element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
3630 </choice>
3631 <element name="names" type="xsd:string" minOccurs="1" maxOccurs="1" nillable="true"/>
3632 </sequence>
3633 </complexType>
3634 ~~~
3635 </div>
3636
3637 A container or dynamic array of a union requires wrapping the variant selector
3638 and union member in a struct:
3639
3640 ~~~{.cpp}
3641 class ns__record
3642 {
3643 public:
3644 std::vector<
3645 struct ns__data // data with a choice of x, n, or s
3646 {
3647 $ int xORnORs; // variant selector with values SOAP_UNION_fieldname
3648 union ns__choice
3649 {
3650 float x;
3651 int n;
3652 char *s;
3653 } u;
3654 }> data; // vector with data
3655 };
3656 ~~~
3657
3658 and an equivalent definition with a dynamic array instead of a `std::vector`
3659 (you can use this in C with structs):
3660
3661 ~~~{.cpp}
3662 class ns__record
3663 {
3664 public:
3665 $ int sizeOfdata; // size of dynamic array
3666 struct ns__data // data with a choice of x, n, or s
3667 {
3668 $ int xORnORs; // variant selector with values SOAP_UNION_fieldname
3669 union ns__choice
3670 {
3671 float x;
3672 int n;
3673 char *s;
3674 } u;
3675 } *data; // points to the data array of length sizeOfdata
3676 };
3677 ~~~
3678
3679 This maps to two complexTypes in the soapcpp2-generated XML schema:
3680
3681 <div class="alt">
3682 ~~~{.xml}
3683 <complexType name="data">
3684 <choice>
3685 <element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/>
3686 <element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/>
3687 <element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
3688 </choice>
3689 </complexType>
3690 <complexType name="record">
3691 <sequence>
3692 <element name="data" type="ns:data" minOccurs="0" maxOccurs="unbounded"/>
3693 </sequence>
3694 </complexType>
3695 ~~~
3696 </div>
3697
3698 The XML value space consists of a sequence of item elements each wrapped in an
3699 data element:
3700
3701 <div class="alt">
3702 ~~~{.xml}
3703 <ns:record xmlns:ns="urn:types" ...>
3704 <data>
3705 <n>123</n>
3706 </data>
3707 <data>
3708 <x>3.1</x>
3709 </data>
3710 <data>
3711 <s>hello</s>
3712 </data>
3713 <data>
3714 <s>world</s>
3715 </data>
3716 </ns:record>
3717 ~~~
3718 </div>
3719
3720 To remove the wrapping data element, simply rename the wrapping struct to
3721 `__ns__data` and the member to `__data` to make this member invisible to the
3722 serializer. The double underscore prefix naming convention is used for the
3723 struct name and member name. Also use a dynamic array instead of a container
3724 (so you can also use this approach in C with structs):
3725
3726 ~~~{.cpp}
3727 class ns__record
3728 {
3729 public:
3730 $ int sizeOfdata; // size of dynamic array
3731 struct __ns__data // contains choice of x, n, or s
3732 {
3733 $ int xORnORs; // variant selector with values SOAP_UNION_fieldname
3734 union ns__choice
3735 {
3736 float x;
3737 int n;
3738 char *s;
3739 } u;
3740 } *__data; // points to the data array of length sizeOfdata
3741 };
3742 ~~~
3743
3744 This maps to a complexType in the soapcpp2-generated XML schema:
3745
3746 <div class="alt">
3747 ~~~{.xml}
3748 <complexType name="record">
3749 <sequence minOccurs="0" maxOccurs="unbounded">
3750 <choice>
3751 <element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/>
3752 <element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/>
3753 <element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
3754 </choice>
3755 </sequence>
3756 </complexType>
3757 ~~~
3758 </div>
3759
3760 The XML value space consists of a sequence of <i>`<x>`</i>, <i>`<n>`</i>, and/or <i>`<s>`</i>
3761 elements:
3762
3763 <div class="alt">
3764 ~~~{.xml}
3765 <ns:record xmlns:ns="urn:types" ...>
3766 <n>123</n>
3767 <x>3.1</x>
3768 <s>hello</s>
3769 <s>world</s>
3770 </ns:record>
3771 ~~~
3772 </div>
3773
3774 Please note that structs, classes, and unions are unnested by soapcpp2 (as in
3775 the C standard of nested structs and unions). Therefore, the `ns__choice`
3776 union in the `ns__record` class is redeclared at the top level despite its
3777 nesting within the `ns__record` class. This means that you will have to choose
3778 a unique name for each nested struct, class, and union.
3779
3780 🔝 [Back to table of contents](#)
3781
3782 ### Tagged void pointer members {#toxsd9-12}
3783
3784 To serialize data pointed to by `void*` requires run-time type information that
3785 tells the serializer what type of data to serialize by means of a *tagged void
3786 pointer*. This type information is stored in a special type tag member of a
3787 struct/class with the name `__type` or `__typeX`, where `X` can be any name, or
3788 alternatively by an `$int` special member of any name as a type tag:
3789
3790 ~~~{.cpp}
3791 class ns__record
3792 {
3793 public:
3794 $ int typeOfdata; // type tag with values SOAP_TYPE_T
3795 void *data; // points to some data of type T
3796 };
3797 ~~~
3798
3799 A type tag member has nonzero values `SOAP_TYPE_T` where `T` is the name of a
3800 struct/class or the name of a primitive type, such as `int`, `std__string` (for
3801 `std::string`), `string` (for `char*`).
3802
3803 This class maps to a complexType with a sequence in the soapcpp2-generated
3804 XML schema:
3805
3806 <div class="alt">
3807 ~~~{.xml}
3808 <complexType name="record">
3809 <sequence>
3810 <element name="data" type="xsd:anyType" minOccurs="0" maxOccurs="1"/>
3811 </sequence>
3812 </complexType>
3813 ~~~
3814 </div>
3815
3816 The XML value space consists of the XML value space of the type with the
3817 addition of an <i>`xsi:type`</i> attribute to the enveloping element:
3818
3819 <div class="alt">
3820 ~~~{.xml}
3821 <ns:record xmlns:ns="urn:types" ...>
3822 <data xsi:type="xsd:int">123</data>
3823 </ns:record>
3824 ~~~
3825 </div>
3826
3827 This <i>`xsi:type`</i> attribute is important for the receiving end to distinguish
3828 the type of data to instantiate. The receiver cannot deserialize the data
3829 without an <i>`xsd:type`</i> attribute.
3830
3831 You can find the `SOAP_TYPE_T` name of each serializable type in the
3832 auto-generated <i>`soapStub.h`</i> file.
3833
3834 Also all serializable C++ classes have a virtual `int T::soap_type()` member
3835 that returns their `SOAP_TYPE_T` value that you can use.
3836
3837 When the `void*` pointer is NULL or when `typeOfdata` is zero, the data is not
3838 serialized.
3839
3840 A container or dynamic array of `void*` pointers to <i>`xsd:anyType`</i> data
3841 requires wrapping the type tag and `void*` members in a struct:
3842
3843 ~~~{.cpp}
3844 class ns__record
3845 {
3846 public:
3847 std::vector<
3848 struct ns__data // data with an xsd:anyType item
3849 {
3850 $ int typeOfitem; // type tag with values SOAP_TYPE_T
3851 void *item; // points to some item of type T
3852 }> data; // vector with data
3853 };
3854 ~~~
3855
3856 and an equivalent definition with a dynamic array instead of a `std::vector`
3857 (you can use this in C with structs):
3858
3859 ~~~{.cpp}
3860 class ns__record
3861 {
3862 public:
3863 $ int sizeOfdata; // size of dynamic array
3864 struct ns__data // data with an xsd:anyType item
3865 {
3866 $ int typeOfitem; // type tag with values SOAP_TYPE_T
3867 void *item; // points to some item of type T
3868 } *data; // points to the data array of length sizeOfdata
3869 };
3870 ~~~
3871
3872 This maps to two complexTypes in the soapcpp2-generated XML schema:
3873
3874 <div class="alt">
3875 ~~~{.xml}
3876 <complexType name="data">
3877 <sequence>
3878 <element name="item" type="xsd:anyType" minOccurs="1" maxOccurs="1" nillable="true"/>
3879 </sequence>
3880 </complexType>
3881 <complexType name="record">
3882 <sequence>
3883 <element name="data" type="ns:data" minOccurs="0" maxOccurs="unbounded"/>
3884 </sequence>
3885 </complexType>
3886 ~~~
3887 </div>
3888
3889 The XML value space consists of a sequence of item elements each wrapped in a
3890 data element:
3891
3892 <div class="alt">
3893 ~~~{.xml}
3894 <ns:record xmlns:ns="urn:types" ...>
3895 <data>
3896 <item xsi:type="xsd:int">123</item>
3897 </data>
3898 <data>
3899 <item xsi:type="xsd:double">3.1</item>
3900 </data>
3901 <data>
3902 <item xsi:type="xsd:string">abc</item>
3903 </data>
3904 </ns:record>
3905 ~~~
3906 </div>
3907
3908 To remove the wrapping data elements, simply rename the wrapping struct and
3909 member to `__data` to make this member invisible to the serializer with the
3910 double underscore prefix naming convention. Also use a dynamic array instead
3911 of a container (you can use this in C with structs):
3912
3913 ~~~{.cpp}
3914 class ns__record
3915 {
3916 public:
3917 $ int sizeOfdata; // size of dynamic array
3918 struct __data // contains xsd:anyType item
3919 {
3920 $ int typeOfitem; // type tag with values SOAP_TYPE_T
3921 void *item; // points to some item of type T
3922 } *__data; // points to the data array of length sizeOfdata
3923 };
3924 ~~~
3925
3926 This maps to a complexType in the soapcpp2-generated XML schema:
3927
3928 <div class="alt">
3929 ~~~{.xml}
3930 <complexType name="record">
3931 <sequence minOccurs="0" maxOccurs="unbounded">
3932 <element name="item" type="xsd:anyType" minOccurs="1" maxOccurs="1"/>
3933 </sequence>
3934 </complexType>
3935 ~~~
3936 </div>
3937
3938 The XML value space consists of a sequence of data elements:
3939
3940 <div class="alt">
3941 ~~~{.xml}
3942 <ns:record xmlns:ns="urn:types" ...>
3943 <item xsi:type="xsd:int">123</item>
3944 <item xsi:type="xsd:double">3.1</item>
3945 <item xsi:type="xsd:string">abc</item>
3946 </ns:record>
3947 ~~~
3948 </div>
3949
3950 Again, please note that structs, classes, and unions are unnested by soapcpp2
3951 (as in the C standard of nested structs and unions). Therefore, the `__data`
3952 struct in the `ns__record` class is redeclared at the top level despite its
3953 nesting within the `ns__record` class. This means that you will have to choose
3954 a unique name for each nested struct, class, and union.
3955
3956 @see Section [XSD type bindings](#typemap2).
3957
3958 🔝 [Back to table of contents](#)
3959
3960 ### Adding get and set methods {#toxsd9-13}
3961
3962 A public `get` method may be added to a class or struct, which will be
3963 triggered by the deserializer. This method will be invoked right after the
3964 instance is populated by the deserializer. The `get` method can be used to
3965 update or verify deserialized content. It should return `SOAP_OK` or set
3966 `soap::error` to a nonzero error code and return it.
3967
3968 A public `set` method may be added to a class or struct, which will be
3969 triggered by the serializer. The method will be invoked just before the
3970 instance is serialized. Likewise, the `set` method should return `SOAP_OK` or
3971 set set `soap::error` to a nonzero error code and return it.
3972
3973 For example, adding a `set` and `get` method to a class declaration:
3974
3975 ~~~{.cpp}
3976 class ns__record
3977 {
3978 public:
3979 int set(struct soap*); // triggered before serialization
3980 int get(struct soap*); // triggered after deserialization
3981 ...
3982 };
3983 ~~~
3984
3985 To add these and othe rmethods to classes and structs with wsdl2h and
3986 <i>`typemap.dat`</i>, please see [class/struct member additions](#typemap3).
3987
3988 🔝 [Back to table of contents](#)
3989
3990 ### Operations on classes and structs {#toxsd9-14}
3991
3992 The following functions/macros are generated by soapcpp2 for each type `T`,
3993 which should make it easier to send, receive, and copy XML data in C and in
3994 C++:
3995
3996 - `int soap_write_T(struct soap*, T*)` writes an instance of `T` to a file via
3997 file descriptor `int soap::sendfd)` or to a stream via `std::ostream
3998 *soap::os` (C++ only) or saves into a NUL-terminated string by setting
3999 `const char **soap::os` to a string pointer to be set (C only). Returns
4000 `SOAP_OK` on success or an error code, also stored in `soap::error`.
4001
4002 - `int soap_read_T(struct soap*, T*)` reads an instance of `T` from a file via
4003 file descriptor `int soap::recvfd)` or from a stream via `std::istream
4004 *soap::is` (C++ only) or reads from a NUL-termianted string `const char
4005 *soap::is` (C only). Returns `SOAP_OK` on success or an error code, also
4006 stored in `soap::error`.
4007
4008 - `void soap_default_T(struct soap*, T*)` sets an instance `T` to its default
4009 value, resetting members of a struct to their initial values (for classes we
4010 use method `T::soap_default`, see below). If `T` is a struct that has a
4011 `soap` pointer member to a `::soap` context then this pointer member will be
4012 set to the first argument passed to this function to initialize its `soap`
4013 pointer member.
4014
4015 - `T * soap_dup_T(struct soap*, T *dst, const T *src)` (requires <b>`soapcpp2 -Ec`</b>)
4016 deep copy `src` into `dst`, replicating all deep cycles and shared pointers
4017 when a managing `soap` context is provided as argument. When `dst` is NULL,
4018 allocates space for `dst` and returns a pointer to the allocated copy. Deep
4019 copy results in a tree when the `soap` context is NULL, but the presence of
4020 deep cycles will lead to non-termination. Use flag `SOAP_XML_TREE` with
4021 managing context to copy into a tree without cycles and pointers to shared
4022 objects. Returns `dst` or allocated copy when `dst` is NULL.
4023
4024 - `void soap_del_T(const T*)` (requires <b>`soapcpp2 -Ed`</b>) deletes all
4025 heap-allocated members of this object by deep deletion ONLY IF this object
4026 and all of its (deep) members are not managed by a `soap` context AND the deep
4027 structure is a tree (no cycles and co-referenced objects by way of multiple
4028 (non-smart) pointers pointing to the same data). Can be safely used after
4029 `T * soap_dup_T(NULL, NULL, const T*)` to delete the deep copy returned.
4030 Does not delete the object itself.
4031
4032 When in C++ mode, soapcpp2 tool adds several methods to classes in addition to
4033 adding a default constructor and destructor (when these were not explicitly
4034 declared).
4035
4036 The public methods added to a class `T`:
4037
4038 - `virtual int T::soap_type(void)` returns a unique type ID (`SOAP_TYPE_T`).
4039 This numeric ID can be used to distinguish base from derived instances.
4040
4041 - `virtual void T::soap_default(struct soap*)` sets all data members to
4042 default values. If class `T` has a `soap` pointer member to a `::soap`
4043 context then this pointer member will be set to the argument passed to this
4044 function to initialize its `soap` pointer member.
4045
4046 - `virtual void T::soap_serialize(struct soap*) const` serializes object to
4047 prepare for SOAP 1.1/1.2 encoded output (or with `SOAP_XML_GRAPH`) by
4048 analyzing its (cyclic) structures.
4049
4050 - `virtual int T::soap_put(struct soap*, const char *tag, const char *type) const`
4051 emits object in XML, compliant with SOAP 1.1 encoding style, return error
4052 code or `SOAP_OK`. Requires `soap_begin_send(soap)` and
4053 `soap_end_send(soap)`.
4054
4055 - `virtual int T::soap_out(struct soap*, const char *tag, int id, const char *type) const`
4056 emits object in XML, with tag and optional id attribute and <i>`xsi:type`</i>,
4057 return error code or `SOAP_OK`. Requires `soap_begin_send(soap)` and
4058 `soap_end_send(soap)`.
4059
4060 - `virtual void * T::soap_get(struct soap*, const char *tag, const char *type)`
4061 Get object from XML, compliant with SOAP 1.1 encoding style, return pointer
4062 to object or NULL on error. Requires `soap_begin_recv(soap)` and
4063 `soap_end_recv(soap)`.
4064
4065 - `virtual void *soap_in(struct soap*, const char *tag, const char *type)`
4066 Get object from XML, with matching tag and type (NULL matches any tag and
4067 type), return pointer to object or NULL on error. Requires
4068 `soap_begin_recv(soap)` and `soap_end_recv(soap)`
4069
4070 - `virtual T * T::soap_alloc(void) const` returns a new object of type `T`,
4071 default initialized and not managed by a `soap` context.
4072
4073 - `virtual T * T::soap_dup(struct soap*) const` (requires <b>`soapcpp2 -Ec`</b>)
4074 returns a duplicate of this object by deep copying, replicating all deep
4075 cycles and shared pointers when a managing `soap` context is provided as
4076 argument. Deep copy is a tree when argument is NULL, but the presence of
4077 deep cycles will lead to non-termination. Use flag `SOAP_XML_TREE` with the
4078 managing context to copy into a tree without cycles and pointers to shared
4079 objects.
4080
4081 - `virtual void T::soap_del() const` (rquires <b>`soapcpp2 -Ed`</b>) deletes all
4082 heap-allocated members of this object by deep deletion ONLY IF this object
4083 and all of its (deep) members are not managed by a `soap` context AND the deep
4084 structure is a tree (no cycles and co-referenced objects by way of multiple
4085 (non-smart) pointers pointing to the same data). Can be safely used after
4086 `soap_dup(NULL)` to delete the deep copy. Does not delete the object itself.
4087
4088 Also, there are four variations of `soap_new_T` for
4089 class/struct/template type `T` that soapcpp2 auto-generates to create instances
4090 on a context-managed heap:
4091
4092 - `T * soap_new_T(struct soap*)` returns a new instance of `T` with default data
4093 member initializations that are set with the soapcpp2 auto-generated `void
4094 T::soap_default(struct soap*)` method), but ONLY IF the soapcpp2
4095 auto-generated default constructor is used that invokes `soap_default()` and
4096 was not replaced by a user-defined default constructor.
4097
4098 - `T * soap_new_T(struct soap*, int n)` returns an array of `n` new instances of
4099 `T`. Similar to the above, instances are initialized.
4100
4101 - `T * soap_new_req_T(struct soap*, ...)` returns a new instance of `T` and sets
4102 the required data members to the values specified in `...`. The required data
4103 members are those with nonzero minOccurs, see the subsections on
4104 [(smart) pointer members and their occurrence constraints](#toxsd9-8) and
4105 [container and array members and their occurrence constraints](#toxsd9-9).
4106
4107 - `T * soap_new_set_T(struct soap*, ...)` returns a new instance of `T` and sets
4108 the public/serializable data members to the values specified in `...`.
4109
4110 The above functions can be invoked with a NULL `soap` context, but we will be
4111 responsible to use `delete T` to remove this instance from the unmanaged heap.
4112
4113 The allocation functions return NULL when memory allocation failed.
4114
4115 🔝 [Back to table of contents](#)
4116
4117 Special classes and structs {#toxsd10}
4118 ---------------------------
4119
4120 The following applies to both structs and classes. The examples show classes
4121 in C++. For C, use structs and omit the C++ features. Structs also require
4122 the use of the `struct` keyword, otherwise soapcpp2 will throw a syntax error.
4123
4124 ### SOAP-encoded arrays {#toxsd10-1}
4125
4126 A class or struct with the following layout is a one-dimensional SOAP-encoded
4127 array type:
4128
4129 ~~~{.cpp}
4130 class ArrayOfT
4131 {
4132 public:
4133 T *__ptr; // array pointer
4134 int __size; // array size
4135 };
4136 ~~~
4137
4138 where `T` is the array element type. A multidimensional SOAP Array is:
4139
4140 ~~~{.cpp}
4141 class ArrayOfT
4142 {
4143 public:
4144 T *__ptr; // array pointer
4145 int __size[N]; // array size of each dimension
4146 };
4147 ~~~
4148
4149 where `N` is the constant number of dimensions. The pointer points to an array
4150 of `__size[0]*__size[1]* ... * __size[N-1]` elements.
4151
4152 This maps to a complexType restriction of SOAP-ENC:Array in the
4153 soapcpp2-generated XML schema:
4154
4155 <div class="alt">
4156 ~~~{.xml}
4157 <complexType name="ArrayOfT">
4158 <complexContent>
4159 <restriction base="SOAP-ENC:Array">
4160 <sequence>
4161 <element name="item" type="T" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
4162 </sequence>
4163 <attribute ref="SOAP-ENC:arrayType" WSDL:arrayType="ArrayOfT[]"/>
4164 </restriction>
4165 </complexContent>
4166 </complexType>
4167 ~~~
4168 </div>
4169
4170 The name of the class can be arbitrary. We often use `ArrayOfT` without a
4171 prefix to distinguish arrays from other classes and structs.
4172
4173 With SOAP 1.1 encoding, an optional offset member can be added that controls
4174 the start of the index range for each dimension:
4175
4176 ~~~{.cpp}
4177 class ArrayOfT
4178 {
4179 public:
4180 T *__ptr; // array pointer
4181 int __size[N]; // array size of each dimension
4182 int __offset[N]; // array offsets to start each dimension
4183 };
4184 ~~~
4185
4186 For example, we can define a matrix of floats as follows:
4187
4188 ~~~{.cpp}
4189 class Matrix
4190 {
4191 public:
4192 double *__ptr;
4193 int __size[2];
4194 };
4195 ~~~
4196
4197 The following code populates the matrix and serializes it in XML:
4198
4199 ~~~{.cpp}
4200 soap *soap = soap_new1(SOAP_XML_INDENT);
4201 Matrix A;
4202 double a[6] = { 1, 2, 3, 4, 5, 6 };
4203 A.__ptr = a;
4204 A.__size[0] = 2;
4205 A.__size[1] = 3;
4206 soap_write_Matrix(soap, &A);
4207 ~~~
4208
4209 Matrix A is serialized as an array with 2x3 values:
4210
4211 <div class="alt">
4212 ~~~{.xml}
4213 <SOAP-ENC:Array SOAP-ENC:arrayType="xsd:double[2,3]" ...>
4214 <item>1</item>
4215 <item>2</item>
4216 <item>3</item>
4217 <item>4</item>
4218 <item>5</item>
4219 <item>6</item>
4220 </SOAP-ENC:Array>
4221 ~~~
4222 </div>
4223
4224 🔝 [Back to table of contents](#)
4225
4226 ### XSD hexBinary and base64Binary types {#toxsd10-2}
4227
4228 A special case of a one-dimensional array is used to define <i>`xsd:hexBinary`</i> and
4229 <i>`xsd:base64Binary`</i> types when the pointer type is `unsigned char`:
4230
4231 ~~~{.cpp}
4232 class xsd__hexBinary
4233 {
4234 public:
4235 unsigned char *__ptr; // points to raw binary data
4236 int __size; // size of data
4237 };
4238 ~~~
4239
4240 and
4241
4242 ~~~{.cpp}
4243 class xsd__base64Binary
4244 {
4245 public:
4246 unsigned char *__ptr; // points to raw binary data
4247 int __size; // size of data
4248 };
4249 ~~~
4250
4251 To create a new binary type, use either one of the following three forms that
4252 declare a new `ns__binary` type that is a <i>`simpleType`</i> restriction of
4253 <i>`xsd:base64Binary`</i>:
4254
4255 ~~~{.cpp}
4256 typedef xsd__base64Binary ns__binary;
4257 ~~~
4258 ~~~{.cpp}
4259 class ns__binary : public xsd__base64Binary
4260 {
4261 ... // attribute members (@) and class methods
4262 };
4263 ~~~
4264 ~~~{.cpp}
4265 class ns__binary
4266 {
4267 public:
4268 unsigned char *__ptr; // points to raw binary data
4269 int __size; // size of data
4270 ... // attribute members (@) and class methods (optional)
4271 };
4272 ~~~
4273
4274 Here, `xsd__base64Binary` is reused in the first two cases, where
4275 `xsd__base64Binary` is declared as shown above.
4276
4277 @see [DIME/MIME/MTOM attachment binary types](#toxsd10-3)
4278
4279 🔝 [Back to table of contents](#)
4280
4281 ### DIME/MIME/MTOM attachment binary types {#toxsd10-3}
4282
4283 A class or struct with a binary content layout can be extended to support
4284 attachments. The following struct or class type can be used as DIME, MIME, and
4285 MTOM attachment and also be used for <i>`xsd:base64Binary`</i> type values:
4286
4287 ~~~{.cpp}
4288 class xsd__base64Binary
4289 {
4290 public:
4291 unsigned char *__ptr; // points to raw binary data
4292 int __size; // size of data
4293 char *id; // NULL to generate an id, or set to a unique UUID
4294 char *type; // MIME type of the data
4295 char *options; // optional description of MIME attachment
4296 };
4297 ~~~
4298
4299 When the `id`, `type`, or `options` members are non-NULL, an attachment will be
4300 used instead of base64 XML content. DIME attachments are the default. To
4301 switch to MIME use the `SOAP_ENC_MIME` context flag. To switch to MTOM use the
4302 `SOAP_ENC_MTOM` context flag.
4303
4304 MTOM is typically used with XOP <i>`<xop:Include>`</i> elements, which is
4305 preferred and declared as follows:
4306
4307 ~~~{.cpp}
4308 //gsoap xop schema import: http://www.w3.org/2004/08/xop/include
4309 class _xop__Include
4310 {
4311 public:
4312 unsigned char *__ptr; // points to raw binary data
4313 int __size; // size of data
4314 char *id; // NULL to generate an id, or set to a unique UUID
4315 char *type; // MIME type of the data
4316 char *options; // optional description of MIME attachment
4317 };
4318 ~~~
4319
4320 Attachments are beyond the scope of this article. See the
4321 [gSOAP user guide.](../../guide/html/index.html) for more details.
4322
4323 🔝 [Back to table of contents](#)
4324
4325 ### Wrapper class/struct with simpleContent {#toxsd10-4}
4326
4327 A class or struct with the following layout is a complexType that wraps
4328 simpleContent:
4329
4330 ~~~{.cpp}
4331 class ns__simple
4332 {
4333 public:
4334 T __item; // primitive type for the simpleContent
4335 ... // attribute members (@) and class methods (optional)
4336 };
4337 ~~~
4338
4339 The type `T` is a primitive type (`bool`, `enum`, `time_t`, numeric and string
4340 types), `xsd__hexBinary`, `xsd__base64Binary`, and custom serializers, such as
4341 `xsd__dateTime`.
4342
4343 This maps to a complexType with simpleContent in the soapcpp2-generated XML
4344 schema:
4345
4346 <div class="alt">
4347 ~~~{.xml}
4348 <complexType name="simple">
4349 <simpleContent>
4350 <extension base="T"/>
4351 </simpleContent>
4352 </complexType>
4353 ~~~
4354 </div>
4355
4356 A wrapper class/struct may include any number of members that are declared as
4357 attributes with `@`, which should be placed after the `__item` member.
4358
4359 🔝 [Back to table of contents](#)
4360
4361 ### DOM anyType and anyAttribute {#toxsd10-5}
4362
4363 Use of a DOM is optional and enabled by `#import "dom.h"` to use the DOM
4364 `xsd__anyType` element node and `xsd__anyAttribute` attribute node:
4365
4366 ~~~{.cpp}
4367 #import "dom.h"
4368
4369 class ns__record
4370 {
4371 public:
4372 @ xsd__anyAttribute attributes; // optional DOM attributes
4373 xsd__anyType *name; // optional DOM element (pointer means minOccurs=0)
4374 xsd__anyType address; // required DOM element (minOccurs=1)
4375 xsd__anyType email 0; // optional DOM element (minOccurs=0)
4376 ... // other members
4377 };
4378 ~~~
4379
4380 where `name` contains XML stored in a DOM node set and `attributes` is a list
4381 of all visibly rendered attributes. The name `attributes` is arbitrary and any
4382 name will suffice.
4383
4384 You should place the `xsd__anyType` members at the end of the struct or class.
4385 This ensures that the DOM members are populated last as a "catch all". A
4386 member name starting with double underscore is a wildcard member. These
4387 members are placed at the end of a struct or class automatically by soapcpp2.
4388
4389 An `#import "dom.h"` import is automatically added by <b>`wsdl2h -d`</b> with
4390 option <b>`-d`</b> to bind <i>`xsd:anyType`</i> to DOM nodes, and also to
4391 populate <i>`xsd:any`</i>, <i>`xsd:anyAttribute`</i> and <i>`xsd:mixed`</i> XML
4392 content:
4393
4394 ~~~{.cpp}
4395 #import "dom.h"
4396
4397 class ns__record
4398 {
4399 public:
4400 @ xsd__anyAttribute __anyAttribute; // optional DOM attributes
4401 std::vector<xsd__anyType> __any 0; // optional DOM elements (minOccurs=0)
4402 xsd__anyType __mixed 0; // optional mixed content (minOccurs=0)
4403 ... // other members
4404 };
4405 ~~~
4406
4407 where the members prefixed with `__` are "invisible" to the XML parser, meaning
4408 that these members are not bound to XML tag names.
4409
4410 In C you can use a dynamic arrary instead of `std::vector`:
4411
4412 ~~~{.cpp}
4413 #import "dom.h"
4414
4415 struct ns__record
4416 {
4417 @ xsd__anyAttribute __anyAttribute; // optional DOM attributes
4418 $ int __sizeOfany; // size of the array
4419 xsd__anyType *__any; // optional DOM elements (pointer means minOccurs=0)
4420 xsd__anyType __mixed 0; // optional mixed content (minOccurs=0)
4421 ... // other members
4422 };
4423 ~~~
4424
4425 Classes can inherit DOM, which enables full use of polymorphism with one base
4426 DOM class:
4427
4428 ~~~{.cpp}
4429 #import "dom.h"
4430
4431 class ns__record : public xsd__anyType
4432 {
4433 public:
4434 std::vector<xsd__anyType*> array; // array of objects of any class
4435 ... // other members
4436 };
4437 ~~~
4438
4439 This permits an `xsd__anyType` pointer to refer to a derived class such as
4440 `ns__record`, which will be serialized with an <i>`xsi:type`</i> attribute that is
4441 set to "ns:record". The <i>`xsi:type`</i> attributes add the necessary type information
4442 to distinguish the XML content from the DOM base type. This is important for
4443 the receiving end: without <i>`xsd:type`</i> attributes with type names, only base DOM
4444 objects are recognized and instantiated.
4445
4446 Because C lacks object-oriented programming concepts such as class inheritance
4447 and polymorphism, you should consider using [derived types in C and C++](#toxsd9-1-1).
4448
4449 An alternative is to use the special [tagged void pointer members](#toxsd9-12)
4450 to serialize data pointed to by a `void*` member, which can be any serializable
4451 type, such as derived types. This approach uses <i>`xsi:type`</i> attributes
4452 to identify the type of value serialized.
4453
4454 To ensure that wsdl2h generates pointer-based `xsd__anyType` DOM nodes with
4455 <b>`wsdl2h -d`</b> using option <b>`-d`</b> for <i>`xsd:any`</i>, add the
4456 following line to <i>`typemap.dat`</i>:
4457
4458 xsd__any = | xsd__anyType*
4459
4460 This lets wsdl2h produce class/struct members and containers with
4461 `xsd__anyType*` for <i>`xsd:any`</i> instead of `xsd__anyType`. To just force all
4462 <i>`xsd:anyType`</i> uses to be pointer-based, declare in <i>`typemap.dat`</i>:
4463
4464 xsd__anyType = | xsd__anyType*
4465
4466 If you use <b>`wsdl2h -d -p`</b> using options <b>`-d`</b> and <b>`-p`</b> then
4467 every class will inherit DOM as shown above. Without option `-d`, an
4468 `xsd__anyType` type is generated to serve as the root type in the type
4469 hierarchy:
4470
4471 ~~~{.cpp}
4472 class xsd__anyType { _XML __item; struct soap *soap; };
4473
4474 class ns__record : public xsd__anyType
4475 {
4476 ...
4477 };
4478 ~~~
4479
4480 where the `_XML __item` member holds any XML content as a literal XML string.
4481
4482 To use the DOM API, compile <i>`dom.c`</i> (or <i>`dom.cpp`</i> for C++), or
4483 link the gSOAP library with <b>`-lgsoapssl`</b> (or <b>`-lgsoapssl++`</b> for C++).
4484
4485 @see Documentation of [XML DOM and XPath](http://www.genivia.com/doc/dom/html)
4486 for more details.
4487
4488 🔝 [Back to table of contents](#)
4489
4490 Directives {#directives}
4491 ==========
4492
4493 You can use `//gsoap` directives in the interface header file with the data
4494 binding interface for soapcpp2. These directives are used to configure the
4495 code generated by soapcpp2 by declaring various. properties of Web services
4496 and XML schemas. When using the wsdl2h tool, you will notice that wsdl2h
4497 generates directives automatically based on the WSDL and XSD input.
4498
4499 Service directives are applicable to service and operations described by WSDL.
4500 Schema directives are applicable to types, elements, and attributes defined by
4501 XML schemas.
4502
4503 🔝 [Back to table of contents](#)
4504
4505 Service directives {#directives-1}
4506 ------------------
4507
4508 A service directive must start at a new line and is of the form:
4509
4510 ~~~{.cpp}
4511 //gsoap <prefix> service <property>: <value>
4512 ~~~
4513
4514 where `<prefix>` is the XML namespace prefix of a service binding. The
4515 `<property>` and `<value>` fields are one of the following:
4516
4517 property | value
4518 --------------- | -----
4519 `name` | name of the service, optionally followed by text describing the service
4520 `namespace` | URI of the WSDL targetNamespace
4521 `documentation` | text describing the service (see also the `name` property), multiple permitted
4522 `doc` | an alias for the `documentation` property
4523 `style` | `document` (default) SOAP messaging style or `rpc` for SOAP RPC
4524 `encoding` | `literal` (default), `encoded` for SOAP encoding, or a custom URI
4525 `protocol` | specifies SOAP or REST, see below
4526 `port` | URL of the service endpoint, usually an http or https address, to use in the WSDL definitions/service/port/address/\@location
4527 `location` | an alias for the `port` property
4528 `endpoint` | an alias for the `port` property
4529 `transport` | URI declaration of the transport, usually `http://schemas.xmlsoap.org/soap/http`
4530 `definitions` | name of the WSDL definitions/\@name
4531 `type` | name of the WSDL definitions/portType/\@name (WSDL2.0 interface/\@name)
4532 `portType` | an alias for the `type` property (`portType` follows SOAP 1.1 naming conventions)
4533 `interface` | an alias for the `type` property (`interface` follows SOAP 1.2 naming conventions)
4534 `binding` | name of the WSDL definitions/binding/\@name
4535 `portName` | name of the WSDL definitions/service/port/\@name
4536 `executable` | name of the "executable" to use in the WSDL definitions/service/port/address/\@location
4537
4538 The service `name` and `namespace` properties are required in order to generate
4539 a valid WSDL with soapcpp2. The other properties are optional.
4540
4541 The `style` and `encoding` property defaults are changed with
4542 <b>`soapcpp2 -e`</b> option <b>`-e`</b> to `rpc` and `encoded`, respectively.
4543
4544 The `protocol` property is `SOAP` by default (SOAP 1.1). Protocol property
4545 values are:
4546
4547 protocol value | description
4548 -------------- | -----------
4549 `SOAP` | SOAP transport, supporting both SOAP 1.1 and 1.2
4550 `SOAP1.1` | SOAP 1.1 transport (same as `soapcpp2 -1`)
4551 `SOAP1.2` | SOAP 1.2 transport (same as `soapcpp2 -2`)
4552 `SOAP-GET` | one-way SOAP 1.1 or 1.2 with HTTP GET
4553 `SOAP1.1-GET` | one-way SOAP 1.1 with HTTP GET
4554 `SOAP1.2-GET` | one-way SOAP 1.2 with HTTP GET
4555 `HTTP` | non-SOAP REST protocol with HTTP POST
4556 `POST` | non-SOAP REST protocol with HTTP POST
4557 `GET` | non-SOAP REST protocol with HTTP GET
4558 `PUT` | non-SOAP REST protocol with HTTP PUT
4559 `DELETE` | non-SOAP REST protocol with HTTP DELETE
4560
4561 You can bind service operations to the WSDL namespace of a service by using the
4562 namespace prefix as part of the identifier name of the function that defines
4563 the service operation:
4564
4565 ~~~{.cpp}
4566 int prefix__func(arg1, arg2, ..., argn, result);
4567 ~~~
4568
4569 You can override the `port` endpoint URL at runtime in the auto-generated
4570 `soap_call_prefix__func` service call (C/C++ client side) and in the C++ proxy
4571 class service call.
4572
4573 🔝 [Back to table of contents](#)
4574
4575 Service method directives {#directives-2}
4576 -------------------------
4577
4578 Service properties are applicable to a service and to all of its operations.
4579 Service method directives are specifically applicable to a service operation.
4580
4581 A service method directive is of the form:
4582
4583 ~~~{.cpp}
4584 //gsoap <prefix> service method-<property>: <method> <value>
4585 ~~~
4586
4587 where `<prefix>` is the XML namespace prefix of a service binding and
4588 `<method>` is the unqualified name of a service operation. The `<property>`
4589 and `<value>` fields are one of the following:
4590
4591 method property | value
4592 --------------------------- | -----
4593 `method-documentation` | text describing the service operation
4594 `method` | an alias for the `method-documentation` property
4595 `method-action` | `""` or URI SOAPAction HTTP header, or URL query string for REST protocols
4596 `method-input-action` | `""` or URI SOAPAction HTTP header of service request messages
4597 `method-output-action` | `""` or URI SOAPAction HTTP header of service response messages
4598 `method-fault-action` | `""` or URI SOAPAction HTTP header of service fault messages
4599 `method-header-part` | member name of the `SOAP_ENV__Header` struct used in SOAP Header
4600 `method-input-header-part` | member name of the `SOAP_ENV__Header` struct used in SOAP Headers of requests
4601 `method-output-header-part` | member name of the `SOAP_ENV__Header` struct used in SOAP Headers of responses
4602 `method-fault` | type name of a struct or class member used in `SOAP_ENV__Details` struct
4603 `method-mime-type` | REST content type or SOAP MIME attachment content type(s)
4604 `method-input-mime-type` | REST content type or SOAP MIME attachment content type(s) of request message
4605 `method-output-mime-type` | REST content type or SOAP MIME attachment content type(s) of response message
4606 `method-style` | `document` or `rpc`
4607 `method-encoding` | `literal`, `encoded`, or a custom URI for encodingStyle of messages
4608 `method-response-encoding` | `literal`, `encoded`, or a custom URI for encodingStyle of response messages
4609 `method-protocol` | SOAP or REST, see [service directives](#directives-1)
4610
4611 The `method-header-part` properties can be repeated for a service operation to
4612 declare multiple SOAP Header parts that the service operation requires. You
4613 can use `method-input-header-part` and `method-output-header-part` to
4614 differentiate between request and response messages.
4615
4616 The `method-fault` property can be repeated for a service operation to declare
4617 multiple faults that the service operation may return.
4618
4619 The `method-action` property serves two purposes:
4620
4621 -# To set the SOAPAction header for SOAP protocols, i.e. sets the
4622 definitions/binding/operation/SOAP:operation/\@soapAction.
4623
4624 -# To set the URL query string for endpoints with REST protocols, i.e. sets the
4625 definitions/binding/operation/HTTP:operation/\@location, which specifies
4626 a URL query string (starts with a `?`) to complete the service endpoint URL
4627 or extends the endpoint URL with a local path (starts with a `/`).
4628
4629 Use `method-input-action` and `method-output-action` to differentiate the
4630 SOAPAction between SOAP request and response messages.
4631
4632 You can always override the port endpoint URL and action values at runtime in
4633 the auto-generated `soap_call_prefix__func` service call (C/C++ client side)
4634 and in the auto-generated C++ proxy class service calls. A runtime NULL
4635 endpoint URL and/or action uses the defaults set by these directives.
4636
4637 The `method-mime-type` property serves two purposes:
4638
4639 -# To set the type of MIME/MTOM attachments used with SOAP protocols. Multiple
4640 attachment types can be declared for a SOAP service operation, i.e. adds
4641 definitions/binding/operation/input/MIME:multipartRelated/MIME:part/MIME:content/\@type
4642 for each type specified.
4643
4644 -# To set the MIME type of a REST operation. This replaces XML declared in
4645 WSDL by definitions/binding/operation/(input|output)/MIME:mimeXml with
4646 MIME:content/\@type. Use `application/x-www-form-urlencoded` with REST POST
4647 and PUT protocols to send encoded form data automatically instead of XML.
4648 Only primitive type values can be transmitted with form data, such as
4649 numbers and strings, i.e. only types that are legal to use as
4650 [attributes members](#toxsd9-5).
4651
4652 Use `method-input-mime-type` and `method-output-mime-type` to differentiate the
4653 attachment types between request and response messages.
4654
4655 🔝 [Back to table of contents](#)
4656
4657 Schema directives {#directives-3}
4658 -----------------
4659
4660 A schema directive is of the form:
4661
4662 ~~~{.cpp}
4663 //gsoap <prefix> schema <property>: <value>
4664 ~~~
4665
4666 where `<prefix>` is the XML namespace prefix of a schema. The `<property>` and
4667 `<value>` fields are one of the following:
4668
4669 property | value
4670 --------------- | -----
4671 `namespace` | URI of the XSD targetNamespace
4672 `namespace2` | alternate URI pattern for the XSD namespace (i.e. URI is also accepted by the XML parser)
4673 `import` | URI of an imported namespace, as an alternative or in addition to `namespace`, adds `xsd:import` to the generated WSDL and XSD files
4674 `form` | `unqualified` (default) or `qualified` local element and attribute form defaults
4675 `elementForm` | `unqualified` (default) or `qualified` local element form default
4676 `attributeForm` | `unqualified` (default) or `qualified` local attribute form default
4677 `typed` | `no` (default) or `yes` for serializers to add `xsi:type` attributes to XML
4678
4679 To learn more about the local form defaults, see [qualified and unqualified members.](#toxsd9-6)
4680
4681 The `namespace2` URI is a pattern with `*` matching any sequence of characters
4682 and `-` matching any character. This pattern instructs the XML parser and validator
4683 to also accept the URI pattern as a valid namespace for the specified `<prefix>`.
4684
4685 The `typed` property is implicitly `yes` when <b>`soapcpp2 -t`</b> option <b>`-t`</b> is used.
4686
4687 🔝 [Back to table of contents](#)
4688
4689 Schema type directives {#directives-4}
4690 ----------------------
4691
4692 A schema type directive is of the form:
4693
4694 ~~~{.cpp}
4695 //gsoap <prefix> schema type-<property>: <name> <value>
4696 //gsoap <prefix> schema type-<property>: <name>::<member> <value>
4697 ~~~
4698
4699 where `<prefix>` is the XML namespace prefix of a schema and `<name>` is an
4700 unqualified name of a C/C++ type, and the optional `<member>` is a class/struct
4701 members or enum constant.
4702
4703 You can describe a type with one of the following:
4704
4705 type property | value
4706 -------------------- | -----
4707 `type-documentation` | text describing the schema type
4708 `type` | an alias for the `type-documentation` property
4709
4710 For example, you can add a description to an enumeration:
4711
4712 ~~~{.cpp}
4713 //gsoap ns schema type: Vowels The letters A, E, I, O, U, and sometimes Y
4714 //gsoap ns schema type: Vowels::Y A vowel, sometimes
4715 enum class ns__Vowels : char { A = 'A', E = 'E', I = 'I', O = 'O', U = 'U', Y = 'Y' };
4716 ~~~
4717
4718 This documented enumeration maps to a simpleType restriction of <i>`xsd:string`</i> in
4719 the soapcpp2-generated schema:
4720
4721 <div class="alt">
4722 ~~~{.xml}
4723 <simpleType name="Vowels">
4724 <annotation>
4725 <documentation>The letters A, E, I, O, U, and sometimes Y</documentation>
4726 </annotation>
4727 <restriction base="xsd:string">
4728 <enumeration value="A"/>
4729 <enumeration value="E"/>
4730 <enumeration value="I"/>
4731 <enumeration value="O"/>
4732 <enumeration value="U"/>
4733 <enumeration value="Y">
4734 <annotation>
4735 <documentation>A vowel, sometimes</documentation>
4736 </annotation>
4737 <enumeration/>
4738 </restriction>
4739 </simpleType>
4740 ~~~
4741 </div>
4742
4743 🔝 [Back to table of contents](#)
4744
4745 Serialization rules {#rules}
4746 ===================
4747
4748 A presentation on XML data bindings is not complete without discussing the
4749 serialization rules and options that put your data in XML on the wire or store
4750 it a file or buffer.
4751
4752 There are several options to choose from to serialize data in XML. The choice
4753 depends on the use of the SOAP protocol or if SOAP is not required. The wsdl2h
4754 tool automates this for you by taking the WSDL transport bindings into account
4755 when generating the service functions in C and C++ that use SOAP or REST.
4756
4757 The gSOAP tools are not limited to SOAP. The tools implement generic XML data
4758 bindings for SOAP, REST, and other uses of XML. So you can read and write XML
4759 using the serializing [operations on classes and structs](#toxsd9-14).
4760
4761 The following sections briefly explain the serialization rules with respect to
4762 the SOAP protocol for XML Web services. A basic understanding of the SOAP
4763 protocol is useful when developing client and server applications that must
4764 interoperate with other SOAP applications.
4765
4766 SOAP/REST Web service client and service operations are represented as
4767 functions in your interface header file with the data binding interface for
4768 soapcpp2. The soapcpp2 tool will translate these function to client-side
4769 service invocation calls and server-side service operation dispatchers.
4770
4771 A discussion of SOAP clients and servers is beyond the scope of this article.
4772 However, the SOAP options discussed here also apply to SOAP client and server
4773 development.
4774
4775 🔝 [Back to table of contents](#)
4776
4777 SOAP document versus rpc style {#doc-rpc}
4778 ------------------------------
4779
4780 The `wsdl:binding/soap:binding/@style` attribute in the <i>`<wsdl:binding>`</i>
4781 section of a WSDL is either "document" or "rpc". The "rpc" style refers to
4782 SOAP RPC (Remote Procedure Call), which is more restrictive than the "document"
4783 style by requiring one XML element in the SOAP Body to act as the procedure
4784 name with XML subelements as its parameters.
4785
4786 For example, the following directives in the interface header file for soapcpp2
4787 declare that `DBupdate` is a SOAP RPC encoding service method:
4788
4789 ~~~{.cpp}
4790 //gsoap ns service namespace: urn:DB
4791 //gsoap ns service method-protocol: DBupdate SOAP
4792 //gsoap ns service method-style: DBupdate rpc
4793 int ns__DBupdate(...);
4794 ~~~
4795
4796 The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with
4797 one element representing the operation with the parameters as subelements:
4798
4799 <div class="alt">
4800 ~~~{.xml}
4801 <SOAP-ENV:Envelope
4802 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
4803 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
4804 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4805 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4806 xmlsn:ns="urn:DB">
4807 <SOAP-ENV:Body>
4808 <ns:DBupdate>
4809 ...
4810 </ns:DBupdate>
4811 </SOAP-ENV:Body>
4812 </SOAP-ENV:Envelope>
4813 ~~~
4814 </div>
4815
4816 The "document" style puts no restrictions on the SOAP Body content. However, we
4817 recommend that the first element's tag name in the SOAP Body should be unique
4818 to each type of operation, so that the receiver can dispatch the operation
4819 based on this element's tag name. Alternatively, the HTTP URL path can be used
4820 to specify the operation, or the HTTP action header can be used to dispatch
4821 operations automatically on the server side (soapcpp2 options -a and -A).
4822
4823 🔝 [Back to table of contents](#)
4824
4825 SOAP literal versus encoding {#lit-enc}
4826 ----------------------------
4827
4828 The `wsdl:operation/soap:body/@use` attribute in the <i>`<wsdl:binding>`</i> section
4829 of a WSDL is either "literal" or "encoded". The "encoded" use refers to the
4830 SOAP encoding rules that support id-ref multi-referenced elements to serialize
4831 data as graphs.
4832
4833 SOAP encoding is very useful if the data internally forms a graph (including
4834 cycles) and we want the graph to be serialized in XML in a format that ensures
4835 that its structure is preserved. In that case, SOAP 1.2 encoding is the best
4836 option.
4837
4838 SOAP encoding also adds encoding rules for [SOAP arrays](#toxsd10) to serialize
4839 multi-dimensional arrays. The use of XML attributes to exchange XML data in
4840 SOAP encoding is not permitted. The only attributes permitted are the standard
4841 XSD attributes, SOAP encoding attributes (such as for arrays), and id-ref.
4842
4843 For example, the following directives in the interface header file for soapcpp2
4844 declare that `DBupdate` is a SOAP RPC encoding service method:
4845
4846 ~~~{.cpp}
4847 //gsoap ns service namespace: urn:DB
4848 //gsoap ns service method-protocol: DBupdate SOAP
4849 //gsoap ns service method-style: DBupdate rpc
4850 //gsoap ns service method-encoding: DBupdate encoded
4851 int ns__DBupdate(...);
4852 ~~~
4853
4854 The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with
4855 an encodingStyle attribute for SOAP 1.1 encoding and an element representing the
4856 operation with parameters that are SOAP 1.1 encoded:
4857
4858 <div class="alt">
4859 ~~~{.xml}
4860 <SOAP-ENV:Envelope
4861 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
4862 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
4863 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4864 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4865 xmlsn:ns="urn:DB">
4866 <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
4867 <ns:DBupdate>
4868 <records SOAP-ENC:arrayType="ns:record[3]">
4869 <item>
4870 <name href="#_1"/>
4871 <SSN>1234567890</SSN>
4872 </item>
4873 <item>
4874 <name>Jane</name>
4875 <SSN>1987654320</SSN>
4876 </item>
4877 <item>
4878 <name href="#_1"/>
4879 <SSN>2345678901</SSN>
4880 </item>
4881 </records>
4882 </ns:DBupdate>
4883 <id id="_1" xsi:type="xsd:string">Joe</id>
4884 </SOAP-ENV:Body>
4885 </SOAP-ENV:Envelope>
4886 ~~~
4887 </div>
4888
4889 In the XML fragment shown above the name "Joe" is shared by two records and the
4890 string is referenced by SOAP 1.1 href and id attributes.
4891
4892 While the soapcpp-generated serializers only introduce multi-referenced
4893 elements in the payload when they are actually multi-referenced in the data
4894 graph, other SOAP applications may render multi-referenced elements more
4895 aggressively. The example could also be rendered as:
4896
4897 <div class="alt">
4898 ~~~{.xml}
4899 <SOAP-ENV:Envelope
4900 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
4901 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
4902 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4903 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4904 xmlsn:ns="urn:DB">
4905 <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
4906 <ns:DBupdate>
4907 <records SOAP-ENC:arrayType="ns:record[3]">
4908 <item href="#id1"/>
4909 <item href="#id2"/>
4910 <item href="#id3"/>
4911 </records>
4912 </ns:DBupdate>
4913 <id id="id1" xsi:type="ns:record">
4914 <name href="#id4"/>
4915 <SSN>1234567890</SSN>
4916 </id>
4917 <id id="id2" xsi:type="ns:record">
4918 <name href="#id5"/>
4919 <SSN>1987654320</SSN>
4920 </id>
4921 <id id="id3" xsi:type="ns:record">
4922 <name href="#id4"/>
4923 <SSN>2345678901</SSN>
4924 </id>
4925 <id id="id4" xsi:type="xsd:string">Joe</id>
4926 <id id="id5" xsi:type="xsd:string">Jane</id>
4927 </SOAP-ENV:Body>
4928 </SOAP-ENV:Envelope>
4929 ~~~
4930 </div>
4931
4932 SOAP 1.2 encoding is cleaner and produces more accurate XML encodings of data
4933 graphs by setting the id attribute on the element that is referenced:
4934
4935 <div class="alt">
4936 ~~~{.xml}
4937 <SOAP-ENV:Envelope
4938 xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
4939 xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
4940 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4941 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4942 xmlsn:ns="urn:DB">
4943 <SOAP-ENV:Body>
4944 <ns:DBupdate SOAP-ENV:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
4945 <records SOAP-ENC:itemType="ns:record" SOAP-ENC:arraySize="3">
4946 <item>
4947 <name SOAP-ENC:id="_1">Joe</name>
4948 <SSN>1234567890</SSN>
4949 </item>
4950 <item>
4951 <name>Jane</name>
4952 <SSN>1987654320</SSN>
4953 </item>
4954 <item>
4955 <name SOAP-ENC:ref="_1"/>
4956 <SSN>2345678901</SSN>
4957 </item>
4958 </records>
4959 </ns:DBupdate>
4960 </SOAP-ENV:Body>
4961 </SOAP-ENV:Envelope>
4962 ~~~
4963 </div>
4964
4965 @note Some SOAP 1.2 applications consider the namespace `SOAP-ENC` of
4966 <i>`SOAP-ENC:id`</i> and <i>`SOAP-ENC:ref`</i> optional. With gSOAP, the SOAP
4967 1.2 encoding serialization follows the 2007 standard, while accepting
4968 unqualified id and ref attributes.
4969
4970 To remove all rendered id-ref multi-referenced elements, use the
4971 `SOAP_XML_TREE` flag to initialize the `soap` context.
4972
4973 Some XSD validation rules are turned off with SOAP encoding, because of the
4974 presence of additional attributes, such as id and ref/href, SOAP arrays with
4975 arbitrary element tags for array elements, and the occurrence of additional
4976 multi-ref elements in the SOAP 1.1 Body.
4977
4978 The use of "literal" puts no restrictions on the XML in the SOAP Body. Full
4979 XSD validation is possible, which can be enabled with the `SOAP_XML_STRICT`
4980 flag to initialize the `soap` context. However, data graphs will be
4981 serialized as trees and cycles in the data will be cut from the XML rendition.
4982
4983 🔝 [Back to table of contents](#)
4984
4985 SOAP 1.1 versus SOAP 1.2 {#soap}
4986 ------------------------
4987
4988 There are two SOAP protocol versions: 1.1 and 1.2. The gSOAP tools can switch
4989 between the two versions seamlessly. You can declare the default SOAP version
4990 for a service operation as follows:
4991
4992 ~~~{.cpp}
4993 //gsoap ns service method-protocol: DBupdate SOAP1.2
4994 ~~~
4995
4996 Use `SOAP` (SOAP 1.1), `SOAP1.1`, `SOAP1.2`, and `HTTP` to switch SOAP versions
4997 or enable REST methods with HTTP POST. See [service directives](#directives-1)
4998 and [XML serialization](#non-soap).
4999
5000 The soapcpp2 tool auto-generates client and server code. At the client side,
5001 this operation sends data with SOAP 1.2 but accepts responses also in SOAP 1.1.
5002 At the server side, this operation accepts requests in SOAP 1.1 and 1.2 and
5003 will return responses in the same SOAP version.
5004
5005 As we discussed in the previous section, the SOAP 1.2 protocol has a cleaner
5006 multi-referenced element serialization format that greatly enhances the
5007 accuracy of data graph serialization with SOAP RPC encoding and is therefore
5008 recommended.
5009
5010 The SOAP 1.2 protocol default can also be set by importing and loading
5011 <i>`gsoap/import/soap12.h`</i>:
5012
5013 ~~~{.cpp}
5014 #import "soap12.h"
5015 ~~~
5016
5017 Finally, the soapcpp2 tool has options to force SOAP 1.1, SOAP 1.2, or remove
5018 SOAP altogether with <b>`soapcpp2 -1`</b> (SOAP 1.1), <b>`soapcpp2 -2`</b>
5019 (SOAP 1.2) and <b>`soapcpp2 -0`</b> (plain XML, no SOAP).
5020
5021 🔝 [Back to table of contents](#)
5022
5023 XML serialization {#non-soap}
5024 -----------------
5025
5026 You can serialize data to XML that is stored on the heap, on the stack (locals), and
5027 static data as long as the serializable (i.e. non-transient) values are
5028 properly initialized and pointers in the data structures are either NULL or
5029 point to valid structures.
5030
5031 When XML is deserialized into data, the data is put on the heap and managed by the
5032 `::soap` context, see also [memory management](#memory).
5033
5034 You can read and write XML directly to a file or stream with the serializing
5035 [operations on classes and structs](#toxsd9-14).
5036
5037 To define and use XML Web service client and service operations, we can declare
5038 these operations in your interface header file with the data binding interface
5039 for soapcpp2 as functions. The function are translated by soapcpp2 to
5040 client-side service invocation calls and server-side service operation
5041 dispatchers.
5042
5043 The REST operations POST, GET, and PUT are declared with `//gsoap` directives
5044 in the interface header file for soapcpp2. For example, a REST HTTP POST
5045 operation is declared as follows:
5046
5047 ~~~{.cpp}
5048 //gsoap ns service namespace: urn:DB
5049 //gsoap ns service method-protocol: DBupdate POST
5050 int ns__DBupdate(...);
5051 ~~~
5052
5053 There are no SOAP Envelope and SOAP Body elements in the payload for
5054 `DBupdate`. Also the XML serialization rules are identical to SOAP
5055 document/literal, meaning no SOAP RPC encoding XML structures are implicitly
5056 used. The XML payload only has the operation name as an element with its
5057 parameters serialized as subelements:
5058
5059 <div class="alt">
5060 ~~~{.xml}
5061 <ns:DBupdate xmln:ns="urn:DB" ...>
5062 ...
5063 </ns:DBupdate>
5064 ~~~
5065 </div>
5066
5067 To force id-ref serialization with REST similar to SOAP 1.2 multi-reference
5068 encoding, use the `SOAP_XML_GRAPH` flag to initialize the `soap` context.
5069 The XML serialization includes id and ref attributes for multi-referenced
5070 elements as follows:
5071
5072 <div class="alt">
5073 ~~~{.xml}
5074 <ns:DBupdate xmln:ns="urn:DB" ...>
5075 <records>
5076 <item>
5077 <name id="_1">Joe</name>
5078 <SSN>1234567890</SSN>
5079 </item>
5080 <item>
5081 <name>Jane</name>
5082 <SSN>1987654320</SSN>
5083 </item>
5084 <item>
5085 <name ref="_1"/>
5086 <SSN>2345678901</SSN>
5087 </item>
5088 </records>
5089 </ns:DBupdate>
5090 ~~~
5091 </div>
5092
5093 🔝 [Back to table of contents](#)
5094
5095 Input and output {#io}
5096 ================
5097
5098 Reading and writing XML from/to files, streams and string buffers is done via
5099 the managing `soap` context by setting one of the following context variables that
5100 control IO sources and sinks:
5101
5102 ~~~{.cpp}
5103 soap->recvfd = fd; // an int file descriptor to read from (0 by default)
5104 soap->sendfd = fd; // an int file descriptor to write to (1 by default)
5105 soap->is = &is; // C++ only: a std::istream is object to read from
5106 soap->os = &os; // C++ only: a std::ostream os object to write to
5107 soap->is = cs; // C only: a const char* string to read from (soap->is will advance)
5108 soap->os = &cs; // C only: pointer to a const char*, will be set to point to the string output
5109 ~~~
5110
5111 Normally, all of these context variables are NULL, which is required to send and
5112 receive data over sockets by gSOAP client and server applications. Therefore,
5113 if you set any of these context variables in a client or server application
5114 then you should reset them to NULL to ensure that socket communications are not
5115 blocked.
5116
5117 @note The use of `soap::is` and `soap::os` in C requires gSOAP 2.8.28 or greater.
5118
5119 In the following sections, we present more details on how to read and write to
5120 files and streams, and use string buffers as sources and sinks for XML data.
5121
5122 In addition, you can set IO callback functions to handle IO at a lower level.
5123 For more details on defining your own callback functions, see the
5124 [gSOAP user guide.](../../guide/html/index.html)
5125
5126 🔝 [Back to table of contents](#)
5127
5128 Reading and writing from/to files and streams {#io1}
5129 ---------------------------------------------
5130
5131 The default IO is standard input and output. Other sources and sinks (those
5132 listed above) will be used until you (re)set them. For example with file-based
5133 input and output:
5134
5135 ~~~{.cpp}
5136 FILE *fp = fopen("record.xml", "r");
5137 if (fp != NULL)
5138 {
5139 soap->recvfd = fileno(fp); // get file descriptor of file to read from
5140 if (soap_read_ns__record(soap, &pers1))
5141 ... // handle IO error
5142 fclose(fp);
5143 soap->recvfd = 0; // read from stdin, or -1 to block reading
5144 }
5145
5146 FILE *fp = fopen("record.xml", "w");
5147 if (fp != NULL)
5148 {
5149 soap->sendfd = fileno(fp); // get file descriptor of file to write to
5150 if (soap_write_ns__record(soap, &pers1))
5151 ... // handle IO error
5152 fclose(fp);
5153 soap->sendfd = 1; // write to stdout, or -1 to block writing
5154 }
5155 ~~~
5156
5157 Similar code with streams in C++:
5158
5159 ~~~{.cpp}
5160 #include <fstream>
5161
5162 std::fstream fs;
5163 fs.open("record.xml", std::ios::in);
5164 if (fs)
5165 {
5166 soap->is = &fs;
5167 if (soap_read__ns__record(soap, &pers1))
5168 ... // handle IO error
5169 fs.close();
5170 soap->is = NULL;
5171 }
5172
5173 fs.open("record.xml", std::ios::out);
5174 if (fs)
5175 {
5176 soap->os = &fs;
5177 if (soap_write__ns__record(soap, &pers1))
5178 ... // handle IO error
5179 fs.close();
5180 soap->os = NULL;
5181 }
5182 ~~~
5183
5184 🔝 [Back to table of contents](#)
5185
5186 Reading and writing from/to string buffers {#io2}
5187 ------------------------------------------
5188
5189 For C++ we recommend to use `std::stringstream` objects from the
5190 <i>`sstream`</i> C++ library as illustrated in the following example:
5191
5192 ~~~{.cpp}
5193 #include <sstream>
5194
5195 std::stringstream ss;
5196 ss.str("..."); // XML to parse
5197 soap->is = &ss;
5198 if (soap_read__ns__record(soap, &pers1))
5199 ... // handle IO error
5200 soap->is = NULL;
5201
5202 soap->os = &ss;
5203 if (soap_write__ns__record(soap, &pers1))
5204 ... // handle IO error
5205 soap->os = NULL;
5206 std::string s = ss.str(); // string with XML
5207 ~~~
5208
5209 For C we can use `soap::is` and `soap::os` to point to strings of XML content
5210 as follows (this requires gSOAP 2.8.28 or later):
5211
5212 ~~~{.cpp}
5213 soap->is = "..."; // XML to parse
5214 if (soap_read__ns__record(soap, &pers1))
5215 ... // handle IO error
5216 soap->is = NULL;
5217
5218 const char *cs = NULL;
5219 soap->os = &cs;
5220 if (soap_write__ns__record(soap, &pers1))
5221 ... // handle IO error
5222 soap->os = NULL;
5223 ... = cs; // string with XML (do not free(cs): managed by the context and freed with soap_end())
5224 ~~~
5225
5226 The type of `soap::os` is a pointer to a `const char*` string. The pointer is
5227 set by the managing `soap` context to point to the XML data that is stored on
5228 the context-managed heap.
5229
5230 For earlier gSOAP versions we recommend to use IO callbacks `soap::frecv` and
5231 `soap::fsend`, see the [gSOAP user guide.](../../guide/html/index.html).
5232
5233 🔝 [Back to table of contents](#)
5234
5235 Memory management {#memory}
5236 =================
5237
5238 Memory management with the `soap` context enables us to allocate data in
5239 context-managed heap space that can be collectively deleted. All deserialized
5240 data is placed on the context-managed heap by the `soap` context of the engine.
5241
5242 🔝 [Back to table of contents](#)
5243
5244 Memory management in C {#memory1}
5245 ----------------------
5246
5247 When working with gSOAP in C (i.e. using <b>`wsdl2h -c`</b> option <b>`-c`</b>
5248 or <b>`soapcpp2 -c`</b> option <b>`-c`</b>), data is allocated on the managed heap with:
5249
5250 - `void *soap_malloc(struct soap*, size_t len)`.
5251
5252 This function allocates `len` bytes on the heap managed by the specified
5253 context and returns NULL when allocation failed.
5254
5255 You can also make shallow copies of data with `soap_memdup` that uses
5256 `soap_malloc` and a safe version of `memcpy` to copy a chunk of data `src` with
5257 length `len` to the context-managed heap:
5258
5259 - `void * soap_memdup(struct soap*, const void *src, size_t len)`
5260
5261 This function returns a pointer to the copy. This function requires gSOAP
5262 2.8.27 or later.
5263
5264 In gSOAP 2.8.35 and greater versions, you can use an auto-generated function to
5265 allocate and initialize data of type `T` on the managed heap:
5266
5267 - `T * soap_new_T(struct soap*, int n)`
5268
5269 This function returns an array of length `n` of type `T` data that is default
5270 initialized (by internally calling `soap_malloc(soap, n * sizeof(T))` and then
5271 `soap_default_T(soap, T*)` on each array value). Use a negative value or `n=1`
5272 to allocate and initialize a single value. This function returns NULL when
5273 allocation failed.
5274
5275 The `soap_malloc` function is essentially a wrapper around `malloc`, but
5276 permits the `soap` context to track all heap allocations for collective
5277 deletion with `soap_end(soap)`:
5278
5279 ~~~{.cpp}
5280 #include "soapH.h"
5281 #include "ns.nsmap"
5282 ...
5283 struct soap *soap = soap_new(); // new context
5284 ...
5285 struct ns__record *record = (struct ns__record*)soap_malloc(soap, sizeof(struct ns__record));
5286 soap_default_ns__record(soap, record); // auto-generated struct initializer
5287 ...
5288 soap_destroy(soap); // only for C++, see section on C++ below
5289 soap_end(soap); // delete record and all other heap allocations
5290 soap_free(soap); // delete context
5291 ~~~
5292
5293 All data on the managed heap is mass-deleted with `soap_end(soap)` which must
5294 be called before `soap_done(soap)` or `soap_free(soap)`, which end the use of
5295 the `soap` context and free the context, respectively. Use
5296 `soap_free(soap)` only when the context is allocated with `soap_new()`. Use
5297 `soap_done(soap)` only when the context is stack allocated (so cannot be
5298 deleted from the heap).
5299
5300 The managed heap is checked for memory leaks at run time when the source code
5301 is compiled with option <b>`-DDEBUG`</b>.
5302
5303 The soapcpp2 auto-generated deserializers in C use `soap_malloc` to allocate
5304 and populate deserialized structures, which are managed by the context for
5305 collective deletion.
5306
5307 To make `char*` and `wchar_t*` string copies to the context-managed heap, we
5308 can use the functions:
5309
5310 - `char *soap_strdup(struct soap*, const char *str)` and
5311
5312 - `wchar_t *soap_wstrdup(struct soap*, const wchar_t *wstr)`.
5313
5314 If your C compiler supports `typeof` then you can use the following macro to
5315 simplify the managed heap allocation and initialization of primitive values:
5316
5317 ~~~{.cpp}
5318 #define soap_assign(soap, lhs, rhs) (*(lhs = (typeof(lhs))soap_malloc(soap, sizeof(*lhs))) = rhs)
5319 ~~~
5320
5321 Pointers to primitive values are often used for optional members. For example,
5322 assume we have the following struct:
5323
5324 ~~~{.cpp}
5325 struct ns__record
5326 {
5327 const char *name 1; // required (minOccurs=1)
5328 uint64_t *SSN; // optional (pointer means minOccurs=0)
5329 struct ns__record *spouse; // optional (pointer means minOccurs=0)
5330 };
5331 ~~~
5332
5333 Use `soap_assign` to create a SSN value on the managed heap:
5334
5335 ~~~{.cpp}
5336 struct soap *soap = soap_new(); // new context
5337 ...
5338 struct ns__record *record = (struct ns__record*)soap_malloc(soap, sizeof(struct ns__record));
5339 soap_default_ns__record(soap, record);
5340 record->name = soap_strdup(soap, "Joe");
5341 soap_assign(soap, record->SSN, 1234567890UL);
5342 ...
5343 soap_end(soap); // delete managed soap_malloc'ed heap data
5344 soap_free(soap); // delete context
5345 ~~~
5346
5347 Without the `soap_assign` macro, you will need two lines of code, one to
5348 allocate and one to assign (you should also use this if your system can run out
5349 of memory):
5350
5351 ~~~{.cpp}
5352 assert((record->SSN = (uint64_t*)soap_malloc(soap, sizeof(utint64_t))) != NULL);
5353 *record->SSN = 1234567890UL;
5354 ~~~
5355
5356 The serializer can serialize any heap, stack, or static allocated data. So we
5357 can also create a new record as follows:
5358
5359 ~~~{.cpp}
5360 struct soap *soap = soap_new(); // new context
5361 ...
5362 struct ns__record *record = (struct ns__record*)soap_malloc(soap, sizeof(struct ns__record));
5363 static uint64_t SSN = 1234567890UL;
5364 soap_default_ns__record(soap, record);
5365 record->name = "Joe";
5366 record->SSN = &SSN; // safe to use static values: the value of record->SSN is never changed
5367 ...
5368 soap_end(soap); // delete managed soap_malloc'ed heap data
5369 soap_free(soap); // delete context
5370 ~~~
5371
5372 Use the soapcpp2 auto-generated `soap_dup_T` functions to duplicate data into
5373 another `soap` context (this requires <b>`soapcpp2 -Ec`</b> option <b>`-Ec`</b> to
5374 generate), here shown for C with the second argument `dst` NULL because we want
5375 to allocate a new managed structure:
5376
5377 ~~~{.cpp}
5378 struct soap *other_soap = soap_new(); // another context
5379 struct ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
5380 ...
5381 soap_destroy(other_soap); // only for C++, see section on C++ below
5382 soap_end(other_soap); // delete other_record and all of its deep data
5383 soap_free(other_soap); // delete context
5384 ~~~
5385
5386 The only reason to use another `soap` context and not to use the primary `soap`
5387 context is when the primary context must be destroyed together with all of the
5388 objects it manages while some of the objects must be kept alive. If the
5389 objects that are kept alive contain deep cycles then this is the only option we
5390 have, because deep copy with a managing `soap` context detects and preserves
5391 these cycles unless the `SOAP_XML_TREE` flag is used with the `soap` context:
5392
5393 ~~~{.cpp}
5394 struct soap *other_soap = soap_new1(SOAP_XML_TREE); // another context
5395 struct ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
5396 ~~~
5397
5398 The resulting deep copy will be a full copy of the source data structure as a
5399 tree without co-referenced data (i.e. no digraph) and without cycles. Cycles
5400 are pruned and (one of the) pointers that forms a cycle is repaced by NULL.
5401
5402 You can also deep copy into unmanaged space and use the auto-generated
5403 `soap_del_T()` function (requires <b>`soapcpp2 -Ed`</b> option <b>`-Ed`</b> to generate) to delete
5404 it later:
5405
5406 ~~~{.cpp}
5407 struct ns__record *other_record = soap_dup_ns__record(NULL, NULL, record);
5408 ...
5409 soap_del_ns__record(other_record); // deep delete record data members
5410 free(other_record); // delete the record
5411 ~~~
5412
5413 But you should not do this for any data that has deep cycles in its runtime
5414 data structure. Cycles in the data structure will lead to non-termination when
5415 making unmanaged deep copies. Consider for example:
5416
5417 ~~~{.cpp}
5418 struct ns__record
5419 {
5420 const char *name 1; // required (minOccurs=1)
5421 uint64_t SSN; // required (non-pointer means minOccurs=1)
5422 struct ns__record *spouse; // optional (pointer means minOccurs=0)
5423 };
5424 ~~~
5425
5426 The code to populate a structure with a mutual spouse relationship:
5427
5428 ~~~{.cpp}
5429 struct soap *soap = soap_new();
5430 ...
5431 struct ns__record pers1, pers2;
5432 soap_default_ns__record(soap, &pers1);
5433 soap_default_ns__record(soap, &pers2);
5434 pers1.name = "Joe"; // OK to serialize static data
5435 pers1.SSN = 1234567890;
5436 pers1.spouse = &pers2;
5437 pers2.name = soap_strdup(soap, "Jane"); // allocates and copies a string
5438 pers2.SSN = 1987654320;
5439 pers2.spouse = &pers1;
5440 ...
5441 struct ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
5442 struct ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5443 soap_set_mode(soap, SOAP_XML_TREE);
5444 struct ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5445 ~~~
5446
5447 The bad case is where there is no context used in the first argument. The deep
5448 copy functions use a context to keep track of co-referenced data nodes and
5449 cycles in the data structure copies, to copy co-referenced nodes just once.
5450 Co-references in a data structure are formed by pointers and smart pointers
5451 such as `std::shared_ptr`, such that at least two pointers point to the same
5452 data.
5453
5454 The serializer can serialize any heap, stack, or static allocated data, such as
5455 in the code shown above. So we can serialize the stack-allocated `pers1`
5456 record as follows:
5457
5458 ~~~{.cpp}
5459 FILE *fp = fopen("record.xml", "w");
5460 if (fp != NULL)
5461 {
5462 soap->sendfd = fileno(fp); // file descriptor to write to
5463 soap_set_mode(soap, SOAP_XML_GRAPH); // support id-ref w/o requiring SOAP
5464 soap_clr_mode(soap, SOAP_XML_TREE); // if set, clear
5465 soap_write_ns__record(soap, &pers1);
5466 fclose(fp);
5467 soap->sendfd = -1; // block further writing
5468 }
5469 ~~~
5470
5471 which produces an XML document record.xml that is similar to:
5472
5473 <div class="alt">
5474 ~~~{.xml}
5475 <ns:record xmlns:ns="urn:types" id="Joe">
5476 <name>Joe</name>
5477 <SSN>1234567890</SSN>
5478 <spouse id="Jane">
5479 <name>Jane</name>
5480 <SSN>1987654320</SSN>
5481 <spouse ref="#Joe"/>
5482 </spouse>
5483 </ns:record>
5484 ~~~
5485 </div>
5486
5487 Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph
5488 leads to the same non-termination problem when we later try to copy the data
5489 into unmanaged memory heap space:
5490
5491 ~~~{.cpp}
5492 struct soap *soap = soap_new1(SOAP_XML_GRAPH); // support id-ref w/o SOAP
5493 ...
5494 struct ns__record pers1;
5495 FILE *fp = fopen("record.xml", "r");
5496 if (fp != NULL)
5497 {
5498 soap->recvfd = fileno(fp);
5499 if (soap_read_ns__record(soap, &pers1))
5500 ... // handle IO error
5501 fclose(fp);
5502 soap->recvfd = -1; // blocks further reading
5503 }
5504 ...
5505 struct ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
5506 struct ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5507 soap_set_mode(soap, SOAP_XML_TREE);
5508 struct ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5509 ~~~
5510
5511 Copying data with `soap_dup_T(soap)` into managed heap memory space is always
5512 safe. Copying into unmanaged heap memory space requires diligence. But
5513 deleting unmanaged data is easy with `soap_del_T()`.
5514
5515 You can also use `soap_del_T()` to delete structures that you created in C, but
5516 only if these structures are created with `malloc` and do NOT contain pointers
5517 to stack and static data.
5518
5519 You can unlink one or more allocated objects from the managed heap to allow the
5520 object to live after `soap_end(soap)` by using:
5521
5522 - `void soap_unlink(struct soap *soap, void *ptr)`
5523
5524 The unlinked heap-allocated data pointed to by `ptr` can be accessed after
5525 `soap_end(soap)`. Do not forget to free the data with `free(ptr)`. Be aware
5526 that `soap_unlink(soap, ptr)` does not perform a deep unlinkage. If `ptr` is a
5527 struct, pointer members will become invalid when pointing to objects on the
5528 managed heap. Use `soap_unlink(soap, ptr->member)` to unlink `member` as well.
5529
5530 Finally, when data is allocated in managed memory heap space, either explicitly
5531 with the allocation functions shown above or by the soapcpp2-generated
5532 deserializers, you can delegate the management and deletion of this data to
5533 another `soap` context. That context will be responsible to delete the data
5534 with `soap_end(soap)` later:
5535
5536 - `void delegate_deletion(struct soap *soap_from, struct soap *soap_to)`
5537
5538 This allows the `soap_from` context to be deleted with `soap_free(soap_from)`
5539 (assuming it is allocated with `soap_new()`, use `soap_done(soap_from)` when
5540 `soap_from` is stack-allocated) while the managed data remains intact. You
5541 can use this function any time, to delegate management and deletion to another
5542 context `soap_to` and then continue with the current context. You can also use
5543 different source `soap_from` contexts to delegate management and deletion to
5544 the other `soap_to` context. To mass delete all managed data, use
5545 `soap_end(soap_to)`.
5546
5547 🔝 [Back to table of contents](#)
5548
5549 Memory management in C++ {#memory2}
5550 ------------------------
5551
5552 When working with gSOAP in C++, the engine allocates data on a managed heap
5553 using `soap_new_T(soap)` to allocate a type with type name `T`. Managed heap
5554 allocation is tracked by the `soap` context for collective deletion with
5555 `soap_destroy(soap)` for structs, classes, and templates and with
5556 `soap_end(soap)` for everything else.
5557
5558 You should only use `soap_malloc(struct soap*, size_t len)` to allocate
5559 primitive types because constructors are not invoked. Therefore, `soap_new_T`
5560 is preferred. The auto-generated `T * soap_new_T(struct soap*)` returns data
5561 allocated on the managed heap for type `T`. The data is mass-deleted with
5562 `soap_destroy(soap)` followed by `soap_end(soap)`.
5563
5564 The `soap_new_T` functions return NULL when allocation fails. C++ exceptions
5565 are never raised by the engine and serializers when data is allocated.
5566
5567 There are four variations of `soap_new_T` functions to allocate data of type
5568 `T` that soapcpp2 auto-generates:
5569
5570 - `T * soap_new_T(struct soap*)` returns a new instance of `T` that is default
5571 initialized. For classes, initialization is internally performed using the
5572 soapcpp2 auto-generated `void T::soap_default(struct soap*)` method of the
5573 class, but ONLY IF the soapcpp2 auto-generated default constructor is used
5574 that invokes `soap_default()` and was not replaced by a user-defined default
5575 constructor.
5576
5577 - `T * soap_new_T(struct soap*, int n)` returns an array of `n` new instances of
5578 `T`. The instances in the array are default initialized as described above.
5579
5580 - `T * soap_new_req_T(struct soap*, ...)` (structs and classes only) returns a
5581 new instance of `T` and sets the required data members to the values
5582 specified in `...`. The required data members are those with nonzero
5583 minOccurs, see the subsections on
5584 [(smart) pointer members and their occurrence constraints](#toxsd9-8) and
5585 [container and array members and their occurrence constraints](#toxsd9-9).
5586
5587 - `T * soap_new_set_T(struct soap*, ...)` (structs and classes only) returns a
5588 new instance of `T` and sets the public/serializable data members to the values
5589 specified in `...`.
5590
5591 The above functions can be invoked with a NULL `soap` context, but you are then
5592 responsible to use `delete T` to remove this instance from the unmanaged heap.
5593
5594 For example, to allocate a managed `std::string` you can use:
5595
5596 ~~~{.cpp}
5597 std::string *s = soap_new_std__string(soap);
5598 ~~~
5599
5600 To throw a `std::bad_alloc` exception when memory allocation fails, we can define the
5601 following class and macro:
5602
5603 ~~~{.cpp}
5604 class alloc_check {
5605 public:
5606 template<typename T>
5607 T operator=(T ptr)
5608 {
5609 if (ptr == NULL)
5610 throw std::bad_alloc();
5611 return ptr;
5612 }
5613 };
5614
5615 #define CHECK alloc_check() =
5616 ~~~
5617
5618 And use `CHECK` as follows to throw an exception when memory allocation fails:
5619
5620 ~~~{.cpp}
5621 std::string *s = CHECK soap_new_std__string(soap);
5622 ~~~
5623
5624 To throw a `std::runtime_exception` when memory allocation fails, with file
5625 and line number information where the error occurred, we can define the
5626 following revised version of our exception-throwing macro:
5627
5628 ~~~{.cpp}
5629 class alloc_failure : public std::runtime_error {
5630 public:
5631 alloc_failure(const char *file, size_t line) : std::runtime_error(error(file, line))
5632 { }
5633 private:
5634 std::string error(const char *file, size_t line) const
5635 {
5636 std::stringstream ss;
5637 ss << "Memory allocation failed in " << file << " at line " << line;
5638 return ss.str();
5639 }
5640 };
5641
5642 class alloc_check_with_info {
5643 public:
5644 alloc_check_with_info(const char *file, size_t line) : file(file), line(line)
5645 { }
5646 template<typename T>
5647 T operator=(T ptr) const
5648 {
5649 if (ptr == NULL)
5650 throw alloc_failure(file, line);
5651 return ptr;
5652 }
5653 const char *file;
5654 size_t line;
5655 };
5656
5657 #define CHECK alloc_check_with_info(__FILE__, __LINE__) =
5658 ~~~
5659
5660 And use `CHECK` as follows to throw an exception with the file and line number
5661 of the location where memory allocation failed:
5662
5663 ~~~{.cpp}
5664 std::string *s = CHECK soap_new_std__string(soap);
5665 ~~~
5666
5667 Primitive types and arrays of primitive values may be allocated with
5668 `soap_malloc` (actually, `soap_new_T` calls `soap_malloc` for primitive type
5669 `T`). All primitive types (i.e. no classes, structs, class templates,
5670 containers, and smart pointers) are allocated with `soap_malloc` for reasons of
5671 efficiency.
5672
5673 You can use a C++ template to simplify the managed allocation and initialization
5674 of primitive values as follows (this is for primitive types only):
5675
5676 ~~~{.cpp}
5677 template<class T>
5678 T * soap_make(struct soap *soap, T val)
5679 {
5680 T *p = (T*)soap_malloc(soap, sizeof(T));
5681 if (p == NULL)
5682 throw std::bad_alloc();
5683 *p = val;
5684 return p;
5685 }
5686 ~~~
5687
5688 For example, assuming we have the following class:
5689
5690 ~~~{.cpp}
5691 class ns__record
5692 {
5693 public:
5694 std::string name; // required (non-pointer means minOccurs=1)
5695 uint64_t *SSN; // optional (pointer means minOccurs=0)
5696 ns__record *spouse; // optional (pointer means minOccurs=0)
5697 };
5698 ~~~
5699
5700 You can instantiate a record by using the auto-generated
5701 `soap_new_set_ns__record` and use `soap_make` to create a SSN value on the
5702 managed heap as follows:
5703
5704 ~~~{.cpp}
5705 soap *soap = soap_new(); // new context
5706 ...
5707 ns__record *record = soap_new_set_ns__record(
5708 soap,
5709 "Joe",
5710 soap_make<uint64_t>(soap, 1234567890UL),
5711 NULL);
5712 ...
5713 soap_destroy(soap); // delete record and all other managed instances
5714 soap_end(soap); // delete managed soap_malloc'ed heap data
5715 soap_free(soap); // delete context
5716 ~~~
5717
5718 All data on the managed heap is mass-deleted with `soap_end(soap)` which must
5719 be called before `soap_done(soap)` or `soap_free(soap)`, which end the use of
5720 the `soap` context and free the context, respectively. Use
5721 `soap_free(soap)` only when the context is allocated with `soap_new()`. Use
5722 `soap_done(soap)` only when the context is stack allocated (so cannot be
5723 deleted from the heap).
5724
5725 The managed heap is checked for memory leaks at run time when the source code
5726 is compiled with option <b>`-DDEBUG`</b>.
5727
5728 However, the serializer can serialize any heap, stack, or static allocated
5729 data. So we can also create a new record as follows:
5730
5731 ~~~{.cpp}
5732 uint64_t SSN = 1234567890UL;
5733 ns__record *record = soap_new_set_ns__record(soap, "Joe", &SSN, NULL);
5734 ~~~
5735
5736 which will be fine to serialize this record as long as the local `SSN`
5737 stack-allocated value remains in scope when invoking the serializer and/or
5738 using `record`. It does not matter if `soap_destroy` and `soap_end` are called
5739 beyond the scope of `SSN`.
5740
5741 To facilitate class methods to access the managing context, we can add a soap
5742 context pointer to a class/struct:
5743
5744 ~~~{.cpp}
5745 class ns__record
5746 {
5747 public:
5748 ...
5749 void create_more(); // needs a context to create more internal data
5750 protected:
5751 struct soap *soap; // the context that manages this instance, or NULL
5752 };
5753 ~~~
5754
5755 The `soap` context pointer member of the class is set when invoking
5756 `soap_new_T` (and similar) with a non-NULL context argument that will be
5757 assigned to the `soap` member of the class.
5758
5759 You can also use a template when an array of pointers to values is required.
5760 To create an array of pointers to values, define the following template:
5761
5762 ~~~{.cpp}
5763 template<class T>
5764 T **soap_make_array(struct soap *soap, T* array, int n) throw (std::bad_alloc)
5765 {
5766 T **p = (T**)soap_malloc(soap, n * sizeof(T*));
5767 if (p == NULL)
5768 throw std::bad_alloc();
5769 for (int i = 0; i < n; ++i)
5770 p[i] = &array[i];
5771 return p;
5772 }
5773 ~~~
5774
5775 The `array` parameter is a pointer to an array of `n` values. The template
5776 returns an array of `n` pointers that point to the values in that array:
5777
5778 ~~~{.cpp}
5779 // create an array of 100 pointers to 100 records
5780 int n = 100;
5781 ns__record **precords = soap_make_array(soap, soap_new_ns__record(soap, n), n);
5782 for (int i = 0; i < n; ++i)
5783 {
5784 precords[i]->name = "...";
5785 precords[i]->SSN = soap_make<uint64_t>(1234567890UL + i);
5786 }
5787 ~~~
5788
5789 Note that `soap_new_ns__record(soap, n)` returns a pointer to an array of `n`
5790 records, which is then used to create an array of `n` pointers to these records.
5791
5792 Use the soapcpp2 auto-generated `soap_dup_T` functions to duplicate data into
5793 another `soap` context (this requires <b>`soapcpp2 -Ec`</b> option <b>`-Ec`</b> to generate), here shown
5794 for C++ with the second argument `dst` NULL to allocate a new managed object:
5795
5796 ~~~{.cpp}
5797 soap *other_soap = soap_new(); // another context
5798 ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
5799 ...
5800 soap_destroy(other_soap); // delete record and other managed instances
5801 soap_end(other_soap); // delete other data (the SSNs on the heap)
5802 soap_free(other_soap); // delete context
5803 ~~~
5804
5805 To duplicate base and derived instances when a base class pointer or reference
5806 is provided, use the auto-generated method `T * T::soap_dup(struct soap*)`:
5807
5808 ~~~{.cpp}
5809 soap *other_soap = soap_new(); // another context
5810 ns__record *other_record = record->soap_dup(other_soap);
5811 ...
5812 soap_destroy(other_soap); // delete record and other managed instances
5813 soap_end(other_soap); // delete other data (the SSNs on the heap)
5814 soap_free(other_soap); // delete context
5815 ~~~
5816
5817 The only reason to use another context and not to use the primary `soap`
5818 context is when the primary context must be destroyed together with all of the
5819 objects it manages while some of the objects must be kept alive. If the
5820 objects that are kept alive contain deep cycles then this is the only option we
5821 have, because deep copy with a managing `soap` context detects and preserves
5822 these cycles unless the `SOAP_XML_TREE` flag is used with the context:
5823
5824 ~~~{.cpp}
5825 soap *other_soap = soap_new1(SOAP_XML_TREE); // another context
5826 ns__record *other_record = record->soap_dup(other_soap); // deep tree copy
5827 ~~~
5828
5829 The resulting deep copy will be a full copy of the source data structure as a
5830 tree without co-referenced data (i.e. no digraph) and without cycles. Cycles
5831 are pruned and (one of the) pointers that forms a cycle is repaced by NULL.
5832
5833 You can also deep copy into unmanaged space and use the auto-generated
5834 `soap_del_T()` function or the `T::soap_del()` method (requires
5835 <b>`soapcpp2 -Ec`</b> option <b>`-Ec`</b> to generate) to delete it later,
5836 but we should not do this for any data that has deep cycles in its runtime data
5837 structure graph:
5838
5839 ~~~{.cpp}
5840 ns__record *other_record = record->soap_dup(NULL);
5841 ...
5842 other_record->soap_del(); // deep delete record data members
5843 delete other_record; // delete the record
5844 ~~~
5845
5846 Cycles in the data structure will lead to non-termination when making unmanaged
5847 deep copies. Consider for example:
5848
5849 ~~~{.cpp}
5850 class ns__record
5851 {
5852 public:
5853 const char *name 1; // required (minOccurs=1)
5854 uint64_t SSN; // required (non-pointer means minOccurs=1)
5855 ns__record *spouse; // optional (pointer means minOccurs=1)
5856 };
5857 ~~~
5858
5859 The code to populate a structure with a mutual spouse relationship:
5860
5861 ~~~{.cpp}
5862 soap *soap = soap_new();
5863 ...
5864 ns__record pers1, pers2;
5865 pers1.name = "Joe";
5866 pers1.SSN = 1234567890;
5867 pers1.spouse = &pers2;
5868 pers2.name = "Jane";
5869 pers2.SSN = 1987654320;
5870 pers2.spouse = &pers1;
5871 ...
5872 ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
5873 ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5874 soap_set_mode(soap, SOAP_XML_TREE);
5875 ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5876 ~~~
5877
5878 The serializer can serialize any heap, stack, or static allocated data, such as
5879 shown in the code shown above. So we can serialize the stack-allocated `pers1`
5880 record as follows:
5881
5882 ~~~{.cpp}
5883 FILE *fp = fopen("record.xml", "w");
5884 if (fp != NULL)
5885 {
5886 soap->sendfd = fileno(fp); // file descriptor to write to
5887 soap_set_mode(soap, SOAP_XML_GRAPH); // support id-ref w/o requiring SOAP
5888 soap_clr_mode(soap, SOAP_XML_TREE); // if set, clear
5889 if (soap_write_ns__record(soap, &pers1))
5890 ... // handle IO error
5891 fclose(fp);
5892 soap->sendfd = -1; // block further writing
5893 }
5894 ~~~
5895
5896 which produces an XML document record.xml that is similar to:
5897
5898 <div class="alt">
5899 ~~~{.xml}
5900 <ns:record xmlns:ns="urn:types" id="Joe">
5901 <name>Joe</name>
5902 <SSN>1234567890</SSN>
5903 <spouse id="Jane">
5904 <name>Jane</name>
5905 <SSN>1987654320</SSN>
5906 <spouse ref="#Joe"/>
5907 </spouse>
5908 </ns:record>
5909 ~~~
5910 </div>
5911
5912 Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph
5913 leads to the same non-termination problem when we later try to copy the data
5914 into unmanaged space:
5915
5916 ~~~{.cpp}
5917 soap *soap = soap_new1(SOAP_XML_GRAPH); // support id-ref w/o SOAP
5918 ...
5919 ns__record pers1;
5920 FILE *fp = fopen("record.xml", "r");
5921 if (fp != NULL)
5922 {
5923 soap->recvfd = fileno(fp); // file descriptor to read from
5924 if (soap_read_ns__record(soap, &pers1))
5925 ... // handle IO error
5926 fclose(fp);
5927 soap->recvfd = -1; // block further reading
5928 }
5929 ...
5930 ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
5931 ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5932 soap_set_mode(soap, SOAP_XML_TREE);
5933 ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
5934 ~~~
5935
5936 Copying data with `soap_dup_T(soap)` into managed space is always safe. Copying
5937 into unmanaged space requires diligence. But deleting unmanaged data is easy
5938 with `soap_del_T()`.
5939
5940 You can also use `soap_del_T()` to delete structures in C++, but only if these
5941 structures are created with `new` (and `new []` for arrays when applicable) for
5942 classes, structs, and class templates and with `malloc` for anything else, and
5943 the structures do NOT contain pointers to stack and static data.
5944
5945 You can unlink one or more allocated objects from the managed heap to allow the
5946 object to live after `soap_destroy(soap)` and `soap_end(soap)` by using:
5947
5948 - `void soap_unlink(struct soap *soap, void *ptr)`
5949
5950 The unlinked heap-allocated data pointed to by `ptr` can be accessed after
5951 `soap_destroy(soap)` and `soap_end(soap)`. Do not forget to free the data with
5952 `delete ptr` (C++ class instance only) or with `free(ptr)` (non-class data).
5953 Be aware that `soap_unlink(soap, ptr)` does not perform a deep unlinkage. If
5954 `ptr` is a struct or class, pointer members will become invalid when pointing
5955 to objects on the managed heap. Use `soap_unlink(soap, ptr->member)` to unlink
5956 `member` as well.
5957
5958 Finally, when data is allocated in managed memory heap space, either explicitly
5959 with the allocation functions shown above or by the soapcpp2-generated
5960 deserializers, you can delegate the management and deletion of this data to
5961 another `soap` context. That context will be responsible to delete the data
5962 with `soap_destroy(soap)` and `soap_end(soap)` later:
5963
5964 - `void delegate_deletion(struct soap *soap_from, struct soap *soap_to)`
5965
5966 This allows the `soap_from` context to be deleted with `soap_free(soap_from)`
5967 (assuming it is allocated with `soap_new()`, use `soap_done(soap_from)` when
5968 `soap_from` is stack-allocated) while the managed data remains intact. You
5969 can use this function any time, to delegate management and deletion to another
5970 context `soap_to` and then continue with the current context. You can also use
5971 different source `soap_from` contexts to delegate management and deletion to
5972 the other `soap_to` context. To mass delete all managed data, use
5973 `soap_destroy(soap_to)` followed by `soap_end(soap_to)`.
5974
5975 🔝 [Back to table of contents](#)
5976
5977 Context flags to initialize the soap struct {#flags}
5978 ===========================================
5979
5980 There are several `soap` context initialization flags and context mode flags to
5981 control XML serialization at runtime. The flags are set with `soap_new1()` to
5982 allocate and initialize a new context:
5983
5984 ~~~{.cpp}
5985 struct soap *soap = soap_new1(flag1 | flag2 | ... | flagn);
5986 ...
5987 soap_destroy(soap); // delete objects
5988 soap_end(soap); // delete other data and temp data
5989 soap_free(soap); // free context
5990 ~~~
5991
5992 and with `soap_init1()` for stack-allocated contexts:
5993
5994 ~~~{.cpp}
5995 struct soap soap;
5996 soap_init1(&soap, flag1 | flag2 | ... | flagn);
5997 ...
5998 soap_destroy(&soap); // delete objects
5999 soap_end(&soap); // delete other data and temp data
6000 soap_done(&soap); // clear context
6001 ~~~
6002
6003 where `flag1`, `flag2`, ..., `flagn` is one of:
6004
6005 - `SOAP_C_UTFSTRING`: enables all `std::string` and `char*` strings to
6006 contain UTF-8 content. This option is recommended.
6007
6008 - `SOAP_C_NILSTRING`: treat empty strings as if they were NULL pointers, i.e.
6009 omits elements and attributes when empty.
6010
6011 - `SOAP_XML_STRICT`: strictly validates XML while deserializing. Should not be
6012 used together with SOAP 1.1/1.2 encoding style of messaging. Use
6013 <b>`soapcpp2 -s`</b> option <b>`-s`</b> to hard code `SOAP_XML_STRICT` in the
6014 generated serializers. Not recommended with SOAP 1.1/1.2 encoding style
6015 messaging.
6016
6017 - `SOAP_XML_INDENT`: produces indented XML.
6018
6019 - `SOAP_XML_CANONICAL`: c14n canonocalization, removes unused `xmlns` bindings
6020 and adds them to appropriate places by applying c14n normalization rules.
6021 Should not be used together with SOAP 1.1/1.2 encoding style messaging.
6022
6023 - `SOAP_XML_TREE`: write tree XML without id-ref, while pruning data structure
6024 cycles to prevent nontermination of the serializer for cyclic structures.
6025
6026 - `SOAP_XML_GRAPH`: write graph (digraph and cyclic graphs with shared pointers
6027 to objects) using id-ref attributes. That is, XML with SOAP multi-ref
6028 encoded id-ref elements. This is a structure-preserving serialization format,
6029 because co-referenced data and also cyclic relations are accurately represented.
6030
6031 - `SOAP_XML_DEFAULTNS`: uses xmlns default namespace declarations, assuming
6032 that the schema attribute form is "qualified" by default (be warned if it is
6033 not, since attributes in the null namespace will get bound to namespaces!).
6034
6035 - `SOAP_XML_NIL`: emit empty element with <i>`xsi:nil`</i> for all NULL pointers
6036 serialized.
6037
6038 - `SOAP_XML_IGNORENS`: the XML parser ignores XML namespaces, i.e. element and
6039 attribute tag names match independent of their namespace.
6040
6041 - `SOAP_XML_NOTYPE`: removes all <i>`xsi:type`</i> attribuation. This option is usually
6042 not needed unless the receiver rejects all <i>`xsi:type`</i> attributes. This option
6043 may affect the quality of the deserializer, which relies on <i>`xsi:type`</i>
6044 attributes to distinguish base class instances from derived class instances
6045 transported in the XML payloads.
6046
6047 - `SOAP_IO_CHUNK`: to enable HTTP chunked transfers.
6048
6049 - `SOAP_IO_STORE`: full buffering of outbound messages.
6050
6051 - `SOAP_ENC_ZLIB`: compress messages, requires compiling with option <b>`-DWITH_GZIP`</b> and
6052 linking with zlib using option <b>`-lz`</b>.
6053
6054 - `SOAP_ENC_MIME`: enable MIME attachments, see
6055 [DIME/MIME/MTOM attachment binary types](#toxsd10-3).
6056
6057 - `SOAP_ENC_MTOM`: enable MTOM attachments, see
6058 [DIME/MIME/MTOM attachment binary types](#toxsd10-3).
6059
6060 @note C++ Web service proxy and service classes have their own `soap` context, either
6061 as a base class (with <b>`soapcpp2 -i`</b> option <b>`-i`</b>) or as a pointer member `soap` that points to
6062 a context (with <b>`soapcpp2 -j`</b> option <b>`-j`</b>). These contexts are allocated when the proxy or
6063 service is instantiated with context flags that are passed to the constructor.
6064
6065 🔝 [Back to table of contents](#)
6066
6067 Context parameter settings {#params}
6068 ==========================
6069
6070 After allocation and initializtion of a `soap` context, several context
6071 parameters can be set (some parameters may require 2.8.31 or greater):
6072
6073 - `unsigned int soap::maxlevel` is the maximum XML nesting depth levels that
6074 the parser permits. Default initialized to `SOAP_MAXLEVEL` (10000), which is
6075 a redefinable macro in <i>`gsoap/stdsoap2.h`</i>. Set `soap::maxlevel` to a
6076 lower value to restrict XML parsing nesting depth.
6077
6078 - `long soap::maxlength` is the maximum string content length if not already
6079 constrained by an XML schema validation `maxLength` constraint. Zero means
6080 unlimited string lengths are permitted (unless restricted by XML schema
6081 `maxLength`). Default initialized to `SOAP_MAXLENGTH` (0), which is a
6082 redefinable macro in <i>`gsoap/stdsoap2.h`</i>. Set `soap::maxlength` to a
6083 positive value to restrict the number of (wide) characters in strings parsed,
6084 restrict hexBinary byte length, and restrict base64Binary byte length.
6085
6086 - `size_t soap::maxoccurs` is the maximum number of array or container elements
6087 permitted by the parser. Must be greater than zero (0). Default initialized
6088 to `SOAP_MAXOCCURS` (100000), which is a redefinable macro in
6089 <i>`gsoap/stdsoap2.h`</i>. Set `soap::maxoccurs` to a positive value to
6090 restrict the number of array and container elements that can be parsed.
6091
6092 - `soap::version` is the SOAP version used, with 0 for non-SOAP, 1 for SOAP1.1,
6093 and 2 for SOAP1.2. This value is normally set by web service operations, and
6094 is otherwise 0 (non-SOAP). Use `soap_set_version(struct soap*, short)` to
6095 set the value. This controls XML namespaces and SOAP id-ref serialization
6096 when applicable with an encodingStyle (see below).
6097
6098 - `const char *soap::encodingStyle` is a string that is used with SOAP
6099 encoding, normally NULL for non-SOAP XML. Set this string to "" (empty
6100 string) to enable SOAP encoding style, which supports id-ref graph
6101 serialization (see also the `SOAP_XML_GRAPH` [context flag](#flags)).
6102
6103 - `int soap::recvfd` is the file descriptor to read and parse source data from.
6104 Default initialized to 0 (stdin). See also [input and output](#io).
6105
6106 - `int soap::sendfd` is the file descriptor to write data to. Default
6107 initialized to 1 (stdout). See also [input and output](#io).
6108
6109 - `const char *is` for C: string to read and parse source data from, overriding
6110 the `recvfd` source. Normally NULL. This value must be reset to NULL or
6111 the parser will continue to read from this string content until the NUL
6112 character. See also [input and output](#io).
6113
6114 - `std::istream *is` for C++: an input stream to read and parse source data
6115 from, overriding the `recvfd` source. Normally NULL. This value must be
6116 reset to NULL or the parser will continue to read from this stream until EOF.
6117 See also [input and output](#io).
6118
6119 - `const char **os` for C: points to a string (a `const char *`) that will be
6120 set to point to the string output. Normally NULL. This value must be reset
6121 to NULL or the next output will result in reassigning the pointer to point to
6122 the next string that is output. The strings are automatically deallocated by
6123 `soap_end(soap)`. See also [input and output](#io).
6124
6125 - `std::ostream *os` for C++: an output stream to write output to. Normally
6126 NULL. This value must be reste to NULL or the next output will be send to
6127 this stream. See also [input and output](#io).
6128
6129 🔝 [Back to table of contents](#)
6130
6131 Error handling and reporting {#errors}
6132 ============================
6133
6134 The gSOAP API functions return `SOAP_OK` (zero) or a non-zero error code. The
6135 error code is stored in `int soap::error` of the current `soap` context.
6136 Error messages can be displayed with:
6137
6138 - `void soap_stream_fault(struct soap*, std::ostream &os)` for C++ only, prints
6139 the error message to an output stream.
6140
6141 - `void soap_print_fault(struct soap*, FILE *fd)` prints the error message to a
6142 FILE descriptor.
6143
6144 - `void soap_sprint_fault(struct soap*, char *buf, size_t len)` saves the error
6145 message to a fixed-size buffer allocated with a maximum length.
6146
6147 - `void soap_print_fault_location(struct soap*, FILE *fd)` prints the location
6148 and part of the XML where the parser encountered an error.
6149
6150 C++ exceptions are never raised by the engine or serializers, even when data is
6151 allocated.
6152
6153 A `SOAP_EOM` error code is returned when memory was exhausted during
6154 processing of input and/or output of data.
6155
6156 An EOF (`SOAP_EOF` or -1) error code is returned when the parser has hit EOF
6157 but expected more input, or when socket communications timed out. In addition
6158 to the `SOAP_EOF` error, the `int soap::errnum` of the `soap` context is
6159 set to the `errno` value of the operation that failed. For timeouts, the
6160 `soap::ernum` value is always 0 instead of an `errno` error code.
6161
6162 Use `soap_xml_error_check(soap->error)` to check for XML errors. This returns
6163 true (non-zero) when a parsing and validation error has occurred.
6164
6165 For example:
6166
6167 ~~~{.cpp}
6168 #include <sstream>
6169
6170 struct soap *soap = soap_new1(SOAP_XML_INDENT | SOAP_XML_STRICT | SOAP_XML_TREE);
6171 struct ns__record person;
6172 std::stringstream ss;
6173 ss.str("..."); // XML to parse
6174 soap->is = &ss;
6175 if (soap_read__ns__record(soap, &person))
6176 {
6177 if (soap_xml_error_check(soap->error))
6178 std::cerr << "XML parsing error!" << std::endl;
6179 else
6180 soap_stream_fault(soap, std::cerr);
6181 }
6182 else
6183 {
6184 ... // all OK, use person record
6185 }
6186 soap_destroy(soap); // delete objects
6187 soap_end(soap); // delete other data and temp data
6188 soap_free(soap); // free context
6189 ~~~
6190
6191 When deploying your application on UNIX and Linux systems, UNIX signal handlers
6192 should be added to your code handle signals, in particular `SIGPIPE`:
6193
6194 ~~~{.cpp}
6195 signal(SIGPIPE, sigpipe_handler);
6196 ~~~
6197
6198 where the `sigpipe_handler` is a function:
6199
6200 ~~~{.cpp}
6201 void sigpipe_handler(int x) { }
6202 ~~~
6203
6204 Other UNIX signals may have to be handled as well.
6205
6206 The engine is designed for easy memory cleanup after being interrupted. Use
6207 `soap_destroy(soap)` and `soap_end(soap)`, after which the `soap` context can
6208 be reused.
6209
6210 🔝 [Back to table of contents](#)
6211
6212 Features and limitations {#features}
6213 ========================
6214
6215 In general, to use the generated code:
6216
6217 - Make sure to `#include "soapH.h"` in your code and also define a namespace
6218 table or `#include "ns.nsmap"` with the generated table, where `ns` is the
6219 namespace prefix for services.
6220
6221 - Use <b>`soapcpp2 -j`</b> option <b>`-j`</b> (C++ only) to generate C++ proxy and service objects.
6222 The auto-generated files include documented inferfaces. Compile with
6223 <i>`soapC.cpp`</i> and link with <b>`-lgsoap++`</b>, or alternatively compile
6224 <i>`gsoap/stdsoap2.cpp`</i>.
6225
6226 - Without <b>`soapcpp2 -j`</b> option <b>`-j`</b>: client-side uses the auto-generated
6227 <i>`soapClient.cpp`</i> and <i>`soapC.cpp`</i> (or C versions of those).
6228 Compile and link with <b>`-lgsoap++`</b> (<b>`-lgsoap`</b> for C), or
6229 alternatively compile <i>`gsoap/stdsoap2.cpp`</i> (<i>`gsoap/stdsoap2.c`</i>
6230 for C).
6231
6232 - Without <b>`soapcpp2 -j`</b> option <b>`-j`</b>: server-side uses the
6233 auto-generated <i>`soapServer.cpp`</i> and <i>`soapC.cpp`</i> (or C versions
6234 of those). Compile and link with <b>`-lgsoap++`</b> (<b>`-lgsoap`</b> for
6235 C), or alternatively compile <i>`gsoap/stdsoap2.cpp`</i> (<i>`stdsoap2.c`</i>
6236 for C).
6237
6238 - Use `soap_new()` or `soap_new1(int flags)` to allocate and initialize a
6239 heap-allocated `soap` context with or without flags. Delete this `soap` context with
6240 `soap_free(struct soap*)`, but only after `soap_destroy(struct soap*)` and
6241 `soap_end(struct soap*)`.
6242
6243 - Use `soap_init(struct *soap)` or `soap_init1(struct soap*, int flags)` to
6244 initialize a stack-allocated `soap` context with or without flags. End the use of
6245 this context with `soap_done(struct soap*)`, but only after
6246 `soap_destroy(struct soap*)` and `soap_end(struct soap*)`.
6247
6248 Additional notes with respect to the wsdl2h and soapcpp2 tools:
6249
6250 - Nested classes, structs, and unions in a interface header file are unnested
6251 by soapcpp2.
6252
6253 - Use `#import "file.h"` instead of `#include` to import other header files in
6254 a interface header file for soapcpp2. The `#include`, `#define`, and
6255 `#pragma` are accepted by soapcpp2, but are moved to the very start of the
6256 generated code for the C/C++ compiler to include before all generated
6257 definitions. Often it is useful to add an `#include` with a
6258 [volatile type](#toxsd9-2) that includes the actual type declaration, and to
6259 ensure transient types are declared when these are used in a data binding
6260 interface declared in a interface header file for soapcpp2.
6261
6262 - To remove any SOAP-specific bindings, use <b>`soapcpp2 -0`</b> option <b>`-0`</b>.
6263
6264 - A interface header file for soapcpp2 should not include any code statements,
6265 only data type declarations. This includes constructor initialization lists
6266 that are not permitted. Use member initializations instead.
6267
6268 - C++ namespaces are supported. Use <b>`wsdl2h -qname`</b> option
6269 <b>`-qname`</b> to add C++ namespace `name`. Or add a `namespace name { ... }`
6270 to the header file, but the `{ ... }` must cover the entire
6271 header file content from begin to end.
6272
6273 - Optional XML DOM support can be used to store mixed content or literal XML
6274 content. Otherwise, mixed content may be lost. Use <b>`wsdl2h -d`</b>
6275 option <b>`-d`</b> for XML DOM support and compile and link with
6276 <i>`gsoap/dom.c`</i> or <i>`gsoap/dom.cpp`</i>. For details,
6277 see [XML DOM and XPath](http://www.genivia.com/doc/dom/html).
6278
6279 🔝 [Back to table of contents](#)
6280
6281 Removing SOAP namespaces from XML payloads {#nsmap}
6282 ==========================================
6283
6284 The soapcpp2 tool generates a <i>`.nsmap`</i> file that includes two bindings for SOAP
6285 namespaces. We can remove all SOAP namespaces (and SOAP processing logic) with
6286 <b>`soapcpp2 -0`</b> option <b>`-0`</b> or by simply setting the two entries to NULL:
6287
6288 ~~~{.cpp}
6289 struct Namespace namespaces[] =
6290 {
6291 {"SOAP-ENV", NULL, NULL, NULL},
6292 {"SOAP-ENC", NULL, NULL, NULL},
6293 ...
6294 };
6295 ~~~
6296
6297 Once the <i>`.nsmap`</i> is generated, you can copy-paste the content into your
6298 project code. However, if we rerun wsdl2h on updated WSDL/XSD files or
6299 <i>`typemap.dat`</i> declarations then we need to use the updated table.
6300
6301 In cases that no XML namespaces are used at all, for example with
6302 [XML-RPC](http://www.genivia.com/doc/xml-rpc-json/html), you may use an empty
6303 namespace table:
6304
6305 ~~~{.cpp}
6306 struct Namespace namespaces[] = {{NULL,NULL,NULL,NULL}};
6307 ~~~
6308
6309 However, beware that any built-in xsi attributes that are rendered will lack
6310 the proper namespace binding. At least we suggest to use `SOAP_XML_NOTYPE` for
6311 this reason.
6312
6313 🔝 [Back to table of contents](#)
6314
6315 Examples {#examples}
6316 ========
6317
6318 Select the project files below to peruse the source code examples.
6319
6320 🔝 [Back to table of contents](#)
6321
6322 Source files
6323 ------------
6324
6325 - <i>`address.xsd`</i> Address book schema
6326 - <i>`address.cpp`</i> Address book app (reads/writes address.xml file)
6327 - <i>`addresstypemap.dat`</i> Schema namespace prefix name preference for wsdl2h
6328 - <i>`graph.h`</i> Graph data binding (tree, digraph, cyclic graph)
6329 - <i>`graph.cpp`</i> Test graph serialization as tree, digraph, and cyclic
6330
6331 🔝 [Back to table of contents](#)
6332
6333 Generated files
6334 ---------------
6335
6336 - <i>`address.h`</i> data binding interface generated from address.xsd
6337 - <i>`addressStub.h`</i> C++ data binding definitions
6338 - <i>`addressH.h`</i> Serializers
6339 - <i>`addressC.cpp`</i> Serializers
6340 - <i>`address.xml`</i> Address book data generated by address app
6341 - <i>`graphStub.h`</i> C++ data binding definitions
6342 - <i>`graphH.h`</i> Serializers
6343 - <i>`graphC.cpp`</i> Serializers
6344 - <i>`g.xsd`</i> XSD schema with <i>`g:Graph`</i> complexType
6345 - <i>`g.nsmap`</i> xmlns bindings namespace mapping table
6346
6347 🔝 [Back to table of contents](#)
6348
6349 Build steps
6350 -----------
6351
6352 Building the AddressBook example:
6353
6354 wsdl2h -g -taddresstypemap.dat address.xsd
6355 soapcpp2 -0 -C -S -paddress -I../../import address.h
6356 c++ -I../.. address.cpp addressC.cpp -o address -lgsoap++
6357
6358 Using <b>`wsdl2h -g -taddresstypemap.dat`</b> option <b>`-g`</b> produces
6359 bindings for global (root) elements in addition to types and option
6360 <b>`-taddresstypemap.dat`</b> specifies a mapping file, see further below.
6361
6362 In this case the root element <i>`a:address-book`</i> is bound to `_a__address_book`.
6363 The complexType <i>`a:address`</i> is bound to class `a__address`, which is also the
6364 type of `_a__address_book`. This option is not required, but allows you to use
6365 global element tag names when referring to their serializers, instead of their
6366 type name. Using <b>`soapcpp2 -0 -C -S -paddress`</b> option <b>`-0`</b> removes the
6367 SOAP protocol and the combination of the two options <b>`-C`</b> and
6368 <b>`-S`</b> removes client and server code generation (using option <b>`-C`</b>
6369 alone generates client code and using option <b>`-S`</b> alone generates server
6370 code). Option <b>`-paddress`</b> renames the output <i>`soap`</i>-prefixed files to
6371 <i>`address`</i>-prefixed files.
6372
6373 See the <i>`address.cpp`</i> implementation and [related pages](pages.html).
6374
6375 The <i>`addresstypemap.dat`</i> file specifies the XML namespace prefix for the
6376 bindings:
6377
6378 # Bind the address book schema namespace to prefix 'a'
6379
6380 a = "urn:address-book-example"
6381
6382 # By default the xsd:dateTime schema type is translated to time_t
6383 # To map xsd:dateTime to struct tm, enable the following line:
6384
6385 # xsd__dateTime = #import "../../custom/struct_tm.h"
6386
6387 # ... and compile/link with custom/struct_tm.c
6388
6389 The DOB field is a <i>`xsd:dateTime`</i>, which is bound to `time_t` by default. To
6390 change this to `struct tm`, enable the import of the `xsd__dateTime` custom
6391 serializer by uncommenting the definition of `xsd__dateTime` in
6392 <i>`addresstypemap.dat`</i>. Then change `soap_dateTime2s` to `soap_xsd__dateTime2s`
6393 in the code.
6394
6395 Building the graph serialization example:
6396
6397 soapcpp2 -C -S -pgraph -I../../import graph.h
6398 c++ -I../.. graph.cpp graphC.cpp -o graph -lgsoap++
6399
6400 To compile without using the <b>`-lgsoap++`</b> library: simply compile
6401 <i>`stdsoap2.cpp`</i> together with the above.
6402
6403 🔝 [Back to table of contents](#)
6404
6405 Usage
6406 -----
6407
6408 To execute the AddressBook example:
6409
6410 ./address
6411
6412 To execute the Graph serialization example:
6413
6414 ./graph
6415