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