|  | Home | Documentation | README.md Source File updated Fri Jan 12 2018 by Robert van Engelen |

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