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