2 XML Data Bindings {#mainpage}
10 This is a detailed overview of the gSOAP XML data bindings concepts and
11 implementation. At the end of
this document two examples are given to
12 illustrate the application of data bindings.
14 The first simple example `address.cpp` shows how to use wsdl2h to bind an XML
15 schema to C++. The C++ application reads and writes an XML file into and from
16 a C++
"address book" data structure. The C++ data structure is an STL vector
19 The second example `graph.cpp` shows how XML is serialized as a tree, digraph,
20 and cyclic graph. The digraph and cyclic graph serialization rules are similar
21 to SOAP 1.1/1.2 encoded multi-ref elements with
id-ref attributes to link
22 elements through IDREF XML references, creating a an XML graph with pointers to
25 These examples demonstrate XML data bindings only
for relatively simple data
26 structures and types. The gSOAP tools support more than just these type of
27 structures, which we will explain in the next sections. Support
for XML schema
28 components is practically unlimited. The wsdl2h tool maps schemas to C and C++
29 using built-in intuitive mapping rules,
while allowing the mappings to be
30 customized
using a `typemap.dat` file with mapping instructions
for wsdl2h.
32 The information in
this document is applicable to gSOAP 2.8.26 and higher, which
33 supports C++11 features. However, C++11 is not required to use
this material
34 and follow the example, unless we need smart pointers and scoped enumerations.
35 While most of the examples in
this document are given in C++, the concepts also
36 apply to C with the exception of containers, smart pointers, classes and their
37 methods. None of these exceptions limit the use of the gSOAP tools
for C in any
40 The data binding concepts described in
this document were first envisioned in
41 1999 by Prof. van Engelen at the Florida State University (the project was
42 named
"stub/skeleton compiler"). The first articles on gSOAP appeared in 2002.
43 The principle of mapping XSD components to C/C++ types and vice versa is now
44 widely adopted in systems and programming languages, including Java web
45 services and by C# WCF.
48 Mapping WSDL and XML schemas to C/C++ {#tocpp}
49 =====================================
51 To convert WSDL and XML schemas (XSD files) to code, we first use the wsdl2h
52 command to generate the data binding
interface code that is saved to a special
55 wsdl2h [options] -o file.h ... XSD and WSDL files ...
57 This command converts WSDL and XSD files to C++ (or pure C with wsdl2h option
58 `-c`) and saves a data binding interface file `file.h` that uses familar C/C++
59 syntax extended with `gsoap` directives and includes notational conventions
60 to declare C/C++ types and functions that are associated with these bindings.
62 The WSDL 1.1/2.0, SOAP 1.1/1.2, and XSD 1.0/1.1 standards are supported by the
63 gSOAP tools. In addition, the most popular WS specifications are also
64 supported, including WS-Addressing, WS-ReliableMessaging, WS-Discovery,
65 WS-Security, WS-Policy, WS-SecurityPolicy, and WS-SecureConversation.
67 This document focusses on XML data bindings and mapping C/C++ to XML 1.0/1.1
68 and XSD 1.0/1.1. This covers all of the following standard XSD components with
69 their optional `[ attributes ]` properties:
71 schema [targetNamespace, version, elementFormDefault,
72 attributeFormDefault, defaultAttributes]
73 attribute [name, ref, type, use, default, fixed, form,
74 targetNamespace, wsdl:arrayType]
75 element [name, ref, type, default, fixed, form, nillable, abstract,
76 substitutionGroup, minOccurs, maxOccurs, targetNamespace]
78 complexType [name, abstract, mixed, defaultAttributesApply]
80 choice [minOccurs, maxOccurs]
81 sequence [minOccurs, maxOccurs]
82 group [name, ref, minOccurs, maxOccurs]
83 attributeGroup [name, ref]
84 any [minOccurs, maxOccurs]
87 And also the following standard XSD components:
89 import imports a schema into the importing schema for referencing
90 include include schema component definitions into a schema
91 override override by replacing schema component definitions
92 redefine extend or restrict schema component definitions
93 annotation annotates a component
95 The XSD facets and their mappings to C/C++ are:
97 enumeration maps to enum
98 simpleContent maps to class/struct wrapper with __item member
99 complexContent maps to class/struct
100 list maps to enum* bitmask (enum* enumerates up to 64 bit masks)
101 extension through inheritance
102 restriction partly through inheritance and redeclaration
103 length restricts content length
104 minLength restricts content length
105 maxLength restricts content length
106 minInclusive restricts numerical value range
107 maxInclusive restricts numerical value range
108 minExclusive restricts numerical value range
109 maxExclusive restricts numerical value range
110 precision maps to float/double but constraint is not validated
111 scale maps to float/double but constraint is not validated
112 totalDigits maps to float/double but constraint is not validated
113 fractionDigits maps to float/double but constraint is not validated
114 pattern must define soap::fsvalidate callback to validate patterns
115 union maps to string of values
117 All primitive XSD types are supported, including but not limited to the
120 anyType maps to
_XML string with literal XML content (or DOM with wsdl2h option -d)
121 anyURI maps to string
122 string maps to string (char*/wchar_t*/std::string/std::wstring)
123 boolean maps to bool (C++) or enum xsd__boolean (C)
124 byte maps to char (int8_t)
125 short maps to short (int16_t)
126 int maps to int (int32_t)
127 long maps to LONG64 (long long and int64_t)
128 unsignedByte maps to unsigned char (uint8_t)
129 unsignedShort maps to unsigned short (uint16_t)
130 unsignedInt maps to unsigned int (uint32_t)
131 unsignedLong maps to ULONG64 (unsigned long long and uint64_t)
133 double maps to double
134 integer maps to string or #import "custom/int128.h"
135 decimal maps to string or #import "custom/long_double.h"
136 precisionDecimal maps to string
137 duration maps to string or #import "custom/duration.h"
138 dateTime maps to time_t or #import "custom/struct_tm.h"
139 time maps to string or #import "custom/long_time.h"
140 date maps to string or #import "custom/struct_tm_date.h"
141 hexBinary maps to class/struct xsd__hexBinary
142 base64Bianry maps to class/struct xsd__base64Binary
143 QName maps to
_QName (URI normalization rules are applied)
145 All other primitive XSD types not listed above are mapped to strings, by
146 generating a typedef. For example, xsd:token is bound to a C++ or C string,
147 which associates a value space to the type with the appropriate XSD type name
148 used by the soapcpp2-generated serializers:
150 typedef std::string xsd__token;
151 typedef char *xsd__token;
153 It is possible to remap types by adding the appropriate mapping rules to
154 `typemap.dat` as we will explain in more detail in the next section.
156 Imported custom serializers are intended to extend the C/C++ type bindings when
157 the default binding to string is not satisfactory to your taste and if the
158 target platform supports these C/C++ types. To add custom serializers to
159 `typemap.dat` for wsdl2h, see [Adding custom serializers](#custom) below.
162 Using typemap.dat to customize data bindings {#typemap}
163 ============================================
165 We use a `typemap.dat` file to redefine
namespace prefixes and to customize
166 type bindings for the the generated header files produced by the wsdl2h tool.
167 The `typemap.dat` is the default file processed by wsdl2h. Use wsdl2h option
168 `-t` to specify a different file.
170 Declarations in `typemap.dat` can be broken up over multiple lines by
171 continuing on the next line by ending each line to be continued with a
172 backslash `\`. The limit is 4095 characters per line, whether the line is
176 XML namespace bindings {#typemap1}
177 ----------------------
179 The wsdl2h tool generates C/C++ type declarations that use `ns1`, `ns2`, etc.
180 as schema-binding URI prefixes. These
default prefixes are generated somewhat
181 arbitrarily
for each schema targetNamespace URI, meaning that their ordering
182 may change depending on the WSDL and XSD order of processing with wsdl2h.
184 Therefore, it is **strongly recommended** to declare your own prefix
for each
185 schema URI in `typemap.dat` to reduce maintaince effort of your code. This
186 is more robust when anticipating possible changes of the schema(s) and/or the
187 binding URI(s) and/or the tooling algorithms.
189 The first and foremost important thing to do is to define prefix-URI bindings
190 for our C/C++ code by adding the following line(s) to our `typemap.dat` or make
191 a copy of this file and add the line(s) that bind our choice of prefix name to
200 This produces `g__name` C/C++ type names that are bound to the "urn:graph"
201 schema by association of `
g` to the generated C/C++ types.
203 This means that `<
g:name xmlns:
g="urn:graph">` is parsed as an instance of a
204 `g__name` C/C++ type. Also `<x:name xmlns:x="urn:graph">` parses as an instance
205 of `g__name`, because the prefix `x` has the same URI value `urn:graph`.
206 Prefixes in XML have local scopes (like variables in a block).
208 The first run of wsdl2h will reveal the URIs, so you do not need to search
209 WSDLs and XSD files for all of the target namespaces. Just copy them from the
210 generated header file after the first run into `typemap.dat` for editing.
213 XSD type bindings {#typemap2}
216 Custom C/C++ type bindings can be declared in `typemap.dat` to associate C/C++
217 types with specific schema types. These type bindings have four parts:
219 prefix__type = declaration | use | ptruse
223 - `prefix__type` is the schema type to be customized (the `prefix__type` name
224 uses the common
double underscore naming convention);
225 - `declaration` declares the C/C++ type in the wsdl2h-generated header file.
226 This part can be empty
if no
explicit declaration is needed;
227 - `use` is an optional part that specifies how the C/C++ type is used in the
228 code. When omitted, it is the same as `prefix__type`;
229 - `ptruse` is an optional part that specifies how the type is used as a
230 pointer type. By
default it is the `use` type name with a `*` or C++11
231 `std::shared_ptr<>` when enabled (see further below).
233 For example, to map xsd:duration to a `
long long` (`LONG64`) type that holds
234 millisecond duration values, we can use the custom serializer declared in
235 `custom/duration.h` by adding the following line to `typemap.dat`:
237 xsd__duration = #
import "custom/duration.h"
239 Here, we omitted the second field, because `xsd__duration` is the name that
240 wsdl2h uses to identify and use
this type
for our code. The third field is
241 omitted to let wsdl2h use `xsd__duration *`
for pointers or
242 `std::shared_ptr<xsd__duration>`
if smart pointers are enabled.
244 To map xsd:
string to `
wchar_t*` wide strings:
246 xsd__string = |
wchar_t* |
wchar_t*
248 Note that the first field is empty, because `
wchar_t` is a C type and does not
249 need to be declared. A `ptruse` field is given so that we
do not end up
250 generating the wrong pointer types, such as `
wchar_t**` and
251 `std::shared_ptr<wchar_t>`.
253 When the
auto-generated declaration should be preserved but the `use` or
254 `ptruse` fields replaced, then we use an ellipsis
for the declaration part:
256 prefix__type = ... | use | ptruse
258 This is useful to map schema polymorphic types to C types
for example, where we
259 need to be able to both handle a base type and its extensions as per schema
260 extensibility. Say we have a base type called ns:base that is extended, then we
261 can remap
this to a C type that permits referening the extended types via a
264 ns__base = ... |
int __type_base;
void*
266 such that `__type_base` and `
void*` are used to (de)serialize any data type,
267 including base and its derived types.
270 Custom serializers
for XSD types {#custom}
271 --------------------------------
273 In the previous part we saw how a custom serializer is used to bind
274 xsd:duration to a `
long long` (`LONG64` or `int64_t`) type to store millisecond
277 xsd__duration = #
import "custom/duration.h"
279 The `xsd__duration` type is an alias of `
long long` (`LONG64` or `int64_t`).
281 While wsdl2h will use
this binding declared in `typemap.dat` automatically, you
282 will also need to compile `custom/duration.c`. Each custom serializer has a
283 header file and an implementation file written in C. You can compile these in
284 C++ (rename files to `.cpp`
if needed).
286 We will discuss the custom serializers that are available to you.
288 ### xsd:integer {#custom-1}
290 The wsdl2h tool maps xsd:integer to a
string by
default. To map xsd:integer to
291 the 128 bit big
int type `__int128_t`:
293 xsd__integer = #
import "custom/int128.h"
295 The `xsd__integer` type is an alias of `__int128_t`.
297 @warning Beware that the xsd:integer value space of integers is in principle
298 unbounded and values can be of arbitrary length. A value range fault
299 `SOAP_TYPE` (value exceeds native representation) or `SOAP_LENGTH` (value
300 exceeds range bounds) will be thrown by the deserializer
if the value is out of
303 Other XSD integer types that are restrictions of xsd:integer, are
304 xsd:nonNegativeInteger and xsd:nonPositiveInteger, which are further restricted
305 by xsd:positiveInteger and xsd:negativeInteger. To bind these types to
306 `__int128_t` we should also add the following definitions to `typemap.dat`:
308 xsd__nonNegativeInteger =
typedef xsd__integer xsd__nonNegativeInteger 0 : ;
309 xsd__nonPositiveInteger =
typedef xsd__integer xsd__nonPositiveInteger : 0 ;
310 xsd__positiveInteger =
typedef xsd__integer xsd__positiveInteger 1 : ;
311 xsd__negativeInteger =
typedef xsd__integer xsd__negativeInteger : -1 ;
313 @note If `__int128_t` 128 bit integers are not supported on your platform and
if it
314 is certain that xsd:integer values are within 64 bit value bounds
for your
315 application
's use, then you can map this type to `LONG64`:
317 xsd__integer = typedef LONG64 xsd__integer;
319 @note Again, a value range fault `SOAP_TYPE` or `SOAP_LENGTH` will be thrown by
320 the deserializer if the value is out of range.
322 @see Section [Numerical types](#toxsd5).
324 ### xsd:decimal {#custom-2}
326 The wsdl2h tool maps xsd:decimal to a string by default. To map xsd:decimal to
327 extended precision floating point:
329 xsd__decimal = #import "custom/long_double.h" | long double
331 By contrast to all other custom serializers, this serializer enables `long
332 double` natively without requiring a new binding name (`xsd__decimal` is NOT
335 If your system supports `<quadmath.h>` quadruple precision floating point
336 `__float128`, you can map xsd:decimal to `xsd__decimal` that is an alias of
339 xsd__decimal = #import "custom/float128.h"
341 @warning Beware that xsd:decimal is in principle a decimal value with arbitraty
342 lengths. A value range fault `SOAP_TYPE` will be thrown by the deserializer if
343 the value is out of range.
345 In the XML payload the special values `INF`, `-INF`, `NaN` represent plus or
346 minus infinity and not-a-number, respectively.
348 @see Section [Numerical types](#toxsd5).
350 ### xsd:dateTime {#custom-3}
352 The wsdl2h tool maps xsd:dateTime to `time_t` by default.
354 The trouble with `time_t` is that it is limited to dates between 1970 and 2038
355 (until its decided by 2038 to widen the bit representation of `time_t`).
357 For this reason `struct tm` should be used to represent wider date ranges. This
358 custom serializer avoids using date and time information in `time_t`. You get
359 the raw date and time information. You only lose the day of the week
360 information. It is always Sunday (`tm_wday=0`).
362 To map xsd:dateTime to `xsd__dateTime` which is an alias of `struct tm`:
364 xsd__dateTime = #import "custom/struct_tm.h"
366 If the limited date range of `time_t` is not a problem but you want to increase
367 the time precision with fractional seconds, then we suggest to map xsd:dateTime
370 xsd__dateTime = #import "custom/struct_timeval.h"
372 If the limited date range of `time_t` is not a problem but you want to use the
373 C++11 time point type `std::chrono::system_clock::time_point` (which internally
376 xsd__dateTime = #import "custom/chrono_time_point.h"
378 Again, we should make sure that the dates will not exceed the date range when
379 using the default `time_t` binding for xsd:dateTime or when binding
380 xsd:dateTime to `struct timeval` or to `std::chrono::system_clock::time_point`.
381 These are safe to use in applications that use xsd:dateTime to record date
382 stamps within a given window. Otherwise, we recommend the `struct tm` custom
383 serializer. You could even map xsd:dateTime to a plain string (use `char*` with
384 C and `std::string` with C++). For example:
386 xsd__dateTime = | char*
388 @see Section [Date and time types](#toxsd7).
390 ### xsd:date {#custom-4}
392 The wsdl2h tool maps xsd:date to a string by default. We can map xsd:date to
395 xsd__date = #import "custom/struct_tm_date.h"
397 The `xsd__date` type is an alias of `struct tm`. The serializer ignores the
398 time part and the deserializer only populates the date part of the struct,
399 setting the time to 00:00:00. There is no unreasonable limit on the date range
400 because the year field is stored as an integer (`int`).
402 @see Section [Date and time types](#toxsd7).
404 ### xsd:time {#custom-5}
406 The wsdl2h tool maps xsd:time to a string by default. We can map xsd:time to
407 an `unsigned long long` (`ULONG64` or `uint64_t`) integer with microsecond time
410 xsd__time = #import "custom/long_time.h"
412 This type represents 00:00:00.000000 to 23:59:59.999999, from `0` to an upper
413 bound of `86399999999`. A microsecond resolution means that a 1 second
414 increment requires an increment of 1000000 in the integer value. The serializer
415 adds a UTC time zone.
417 @see Section [Date and time types](#toxsd7).
419 ### xsd:duration {#custom-6}
421 The wsdl2h tool maps xsd:duration to a string by default, unless xsd:duration
422 is mapped to a `long long` (`LONG64` or `int64_t`) type with with millisecond
423 (ms) time duration precision:
425 xsd__duration = #import "custom/duration.h"
427 The `xsd__duration` type is a 64 bit signed integer that can represent
428 106,751,991,167 days forwards (positive) and backwards (negative) in time in
429 increments of 1 ms (1/1000 of a second).
431 Rescaling of the duration value by may be needed when adding the duration value
432 to a `time_t` value, because `time_t` may or may not have a seconds resolution,
433 depending on the platform and possible changes to `time_t`.
435 Rescaling is done automatically when you add a C++11 `std::chrono::nanoseconds`
436 value to a `std::chrono::system_clock::time_point` value. To use
437 `std::chrono::nanoseconds` as xsd:duration:
439 xsd__duration = #import "custom/chrono_duration.h"
441 This type can represent 384,307,168 days (2^63 nanoseconds) forwards and
442 backwards in time in increments of 1 ns (1/1,000,000,000 of a second).
444 Certain observations with respect to receiving durations in years and months
445 apply to both of these serializer decoders for xsd:duration.
447 @see Section [Time duration types](#toxsd8).
450 Class/struct member additions {#typemap3}
451 -----------------------------
453 All generated classes and structs can be augmented with additional
454 members such as methods, constructors and destructors, and private members:
456 prefix__type = $ member-declaration
458 For example, we can add method declarations and private members to a class, say
459 `ns__record` as follows:
461 ns__record = $ ns__record(const ns__record &); // copy constructor
462 ns__record = $ void print(); // a print method
463 ns__record = $ private: int status; // a private member
465 Note that method declarations cannot include any code, because soapcpp2's input
466 permits only type declarations, not code.
469 Replacing XSD types by equivalent alternatives {#typemap4}
470 ----------------------------------------------
472 Type replacements can be given to replace one type entirely with another given
475 prefix__type1 == prefix__type2
477 This replaces all `prefix__type1` by `prefix__type2` in the wsdl2h output.
479 @warning Do not agressively replace types, because
this can cause XML
480 validation to fail when a value-type mismatch is encountered in the XML input.
481 Therefore, only replace similar types with other similar types that are wider
482 (e.g. `
short` by `
int` and `
float` by `
double`).
485 The built-in typemap.dat variables $CONTAINER and $POINTER {#typemap5}
486 ----------------------------------------------------------
488 The `typemap.dat` `$CONTAINER` variable defines the container to emit in the
489 generated declarations, which is `std::vector` by
default. For example, to emit
490 `std::list` as the container in the wsdl2h-generated declarations:
492 $CONTAINER = std::list
494 The `typemap.dat` `$POINTER` variable defines the smart pointer to emit in the
495 generated declarations, which replaces the use of `*` pointers. For example:
497 $POINTER = std::shared_ptr
499 Not all pointers in the generated output can be replaced by smart pointers.
500 Regular pointers are still used as
union members and for pointers to arrays of
503 @note The standard smart pointer `std::shared_ptr` is generally safe to use.
504 Other smart pointers such as `std::unique_ptr` and `std::auto_ptr` may cause
505 compile-time errors when classes have smart pointer members but no copy
506 constructor (a default copy constructor). A copy constructor is required for
507 non-shared smart pointer copying or swapping.
509 Alternatives to `std::shared_ptr` of the form `NAMESPACE::shared_ptr` can be
510 assigned to `$POINTER` when the namespace `NAMESPACE` also implements
511 `NAMESPACE::make_shared` and when the shared pointer class provides `reset()`
512 and`get()` methods and the dereference operator. For example Boost
516 #include <boost/shared_ptr.hpp>
518 $POINTER = boost::shared_ptr
520 The user-defined content between `[` and `]` ensures that we include the Boost
521 header files that are needed to support `boost::shared_ptr` and
522 `boost::make_shared`.
525 User-defined content {#typemap6}
528 Any other content to be generated by wsdl2h can be included in `typemap.dat` by
529 enclosing it within brackets `[` and `]` anywhere in the `typemap.dat` file.
530 Each of the two brackets MUST appear at the start of a
new line.
532 For example, we can add an `#
import "wsa5.h"` directive to the wsdl2h-generated
536 #import "import/wsa5.h"
539 which emits the `#
import "import/wsa5.h"` literally at the start of the
540 wsdl2h-generated header file.
543 Mapping C/C++ to XML schema {#toxsd}
544 ===========================
546 The soapcpp2 command generates the data binding implementation code from a data
547 binding
interface `file.h`:
549 soapcpp2 [options] file.h
551 where `file.h` is a gSOAP header file that declares the XML data binding
552 interface. The `file.h` is typically generated by wsdl2h, but we can also
553 declare one ourself. If so, we add gSOAP directives and declare in this file
554 all our C/C++ types we want to serialize in XML. We can also declare functions
555 that will be converted to service operations by soapcpp2.
557 Global function declarations define service operations, which are of the form:
559 int ns__name(arg1, arg2, ..., argn, result);
561 where `arg1`, `arg2`, ..., `argn` are formal argument declarations of the input
562 and `result` is a formal argument for the output, which must be a pointer or
563 reference to the result object to be populated. More information can be found
564 in the gSOAP user guide.
567 Overview of serializable C/C++ types {#toxsd1}
568 ------------------------------------
570 The following C/C++ types are supported by soapcpp2 and mapped to XSD types
571 and constructs. See the subsections below
for more details or follow the links.
573 ### List of Boolean types
576 enum xsd__boolean C alternative
bool
578 @see Section [C++
bool and C alternative](#toxsd3).
580 ### List of enumeration and bitmask types
583 enum class C++11 scoped enumeration (soapcpp2 -c++11)
584 enum* a bitmask that enumerates values 1, 2, 4, 8, ...
585 enum* class C++11 scoped enumeration bitmask (soapcpp2 -c++11)
587 @see Section [Enumerations and bitmasks](
#toxsd4).
589 ### List of numerical types
595 LONG64 64 bit integer
596 xsd__integer 128 bit __int128_t integer, use #
import "custom/int128.h"
597 long long same as LONG64
598 unsigned char unsigned byte
599 unsigned short unsigned 16 bit integer
600 unsigned int unsigned 32 bit integer
601 unsigned long unsigned 32 bit integer
602 ULONG64
unsigned 64 bit integer
603 unsigned long long same as ULONG64
605 int16_t same as
short
607 int64_t same as LONG64
608 uint8_t same as
unsigned char
609 uint16_t same as
unsigned short
610 uint32_t same as
unsigned int
611 uint64_t same as ULONG64
612 size_t transient type (not serializable)
615 long double extended precision float, use #
import "custom/long_double.h"
616 xsd__decimal <quadmath.h> 128 bit __float128 quadruple precision float, use #
import "custom/float128.h"
617 typedef declares a type name, with optional value range and
string length bounds
619 @see Section [Numerical types](#toxsd5).
621 ### List of
string types.
625 std::string C++
string
626 std::wstring C++ wide
string
627 char[N] fixed-size
string, requires soapcpp2 option -b
628 _QName normalized QName content
629 _XML literal XML
string content
630 typedef declares a type name, may restrict
string length
632 @see Section [String types](#toxsd6).
634 ### List of date and time types
636 time_t date and time point since epoch
637 struct tm date and time point, use #import "custom/struct_tm.h"
638 struct tm date point, use #import "custom/struct_tm_date.h"
639 struct timeval date and time point, use #import "custom/struct_timeval.h"
640 unsigned long long time point in microseconds, use #import "custom/long_time.h"
641 std::chrono::system_clock::time_point
642 date and time point, use #import "custom/chrono_time_point.h"
644 @see Section [Date and time types](#toxsd7).
646 ### List of time duration types
648 long long duration in milliseconds, use #
import "custom/duration.h"
649 std::chrono::nanoseconds duration in nanoseconds, use #
import "custom/chrono_duration.h"
651 @see Section [Time duration types](#toxsd8).
653 ### List of classes and structs
655 class C++ class with single inheritance only
656 struct C struct or C++ struct without inheritance
658 T[N] fixed-size array of type T
659 std::shared_ptr<T> C++11 smart shared pointer
660 std::unique_ptr<T> C++11 smart pointer
661 std::auto_ptr<T> C++ smart pointer
662 std::deque<T> use #
import "import/stldeque.h"
663 std::list<T> use #
import "import/stllist.h"
664 std::vector<T> use #
import "import/stlvector.h"
665 std::set<T> use #
import "import/stlset.h"
666 template<T>
class a container with begin(), end(), size(), clear(), and insert() methods
667 union requires a discriminant member
668 void* requires a __type member to indicate the type of
object pointed to
670 @see Section [Classes and structs](#toxsd9).
672 ### List of special classes and structs
674 Array single and multidimensional SOAP Arrays
675 xsd__hexBinary binary content
676 xsd__base64Binary binary content and optional MIME/MTOM attachments
677 Wrapper complexTypes with simpleContent
679 @see Section [Special classes and structs](#toxsd10).
682 Colon notation versus name prefixing {#toxsd2}
683 ------------------------------------
685 To bind C/C++ type names to XSD types, a simple form of name prefixing is used
686 by the gSOAP tools by prepending the XML
namespace prefix to the C/C++ type
687 name with a pair of undescrores. This also ensures that name clashes cannot
688 occur when multiple WSDL and XSD files are converted to C/C++. Also, C++
689 namespaces are not sufficiently rich to capture XML schema namespaces
690 accurately, for example when class members are associated with schema elements
691 defined in another XML namespace and thus the XML namespace scope of the
692 member's name is relevant, not just its type.
694 However, from a C/C++ centric point of view this can be cumbersome. Therefore,
695 colon notation is an alternative to physically augmenting C/C++ names with
698 For example, the following class uses colon notation to bind the `record` class
699 to the `urn:types` schema:
712 The colon notation is stripped away by soapcpp2 when generating the data
713 binding implementation code
for our project. So the
final code just uses
714 `record` to identify
this class and its constructor/destructor.
716 When using colon notation we have to be consistent and not use colon notation
717 mixed with prefixed forms. The name `ns:record` differs from `ns__record`,
718 because `ns:record` is compiled to an unqualified `record` name.
721 C++ Bool and C alternative {#toxsd3}
722 --------------------------
724 The C++ `
bool` type is bound to built-in XSD type xsd:
boolean.
726 The C alternative is to define an enumeration:
728 enum xsd__boolean { false_, true_ };
730 or by defining an enumeration in C with pseudo-scoped enumeration constants:
732 enum xsd__boolean { xsd__boolean__false, xsd__boolean__true };
734 The XML value space of these types is `
false` and `
true`, but also accepts `0`
737 To prevent name clashes, `false_` and `true_` have an underscore which are
738 removed in the XML value space.
741 Enumerations and bitmasks {#toxsd4}
742 -------------------------
744 Enumerations are mapped to XSD simpleType enumeration restrictions of
745 xsd:string, xsd:QName, and xsd:
long.
747 Consider
for example:
749 enum ns__Color { RED, WHITE, BLUE };
751 which maps to a simpleType restriction of xsd:
string in the soapcpp2-generated
754 <simpleType name=
"Color">
755 <restriction base=
"xsd:string">
756 <enumeration value=
"RED"/>
757 <enumeration value=
"WHITE"/>
758 <enumeration value=
"BLUE"/>
762 Enumeration name constants can be pseudo-scoped to prevent name clashes,
763 because enumeration name constants have a global scope in C and C++:
765 enum ns__Color { ns__Color__RED, ns__Color__WHITE, ns__Color__BLUE };
767 We can also use C++11 scoped enumerations to prevent name clashes:
769 enum class ns__Color : int { RED, WHITE, BLUE };
771 Here, the enumeration
class base type `: int` is optional. In place of `int`
772 in the example above, we can also use `int8_t`, `int16_t`, `int32_t`, or
775 The XML value space of the enumertions defined above is `RED`, `WHITE`, and
778 Prefix-qualified enumeration name constants are mapped to simpleType
779 restrictions of xsd:QName, for example:
781 enum ns__types { xsd__int, xsd__float };
783 which maps to a simpleType restriction of xsd:QName in the soapcpp2-generated
786 <simpleType name=
"types">
787 <restriction base=
"xsd:QName">
788 <enumeration value=
"xsd:int"/>
789 <enumeration value=
"xsd:float"/>
793 Enumeration name constants can be pseudo-numeric as follows:
795 enum ns__Primes { _3 = 3, _5 = 5, _7 = 7, _11 = 11 };
797 which maps to a simpleType restriction of `xsd:
long`:
799 <simpleType name=
"Color">
800 <restriction base=
"xsd:long">
801 <enumeration value=
"3"/>
802 <enumeration value=
"5"/>
803 <enumeration value=
"7"/>
804 <enumeration value=
"11"/>
808 The XML value space of
this type is `3`, `5`, `7`, and `11`.
810 Besides (pseudo-) scoped enumerations, another way to prevent name clashes
811 accross enumerations is to start an enumeration name constant with one
812 underscore or followed it by any number of underscores, which makes it
813 unique. The leading and trailing underscores are removed in the XML value
816 enum ns__ABC { A, B, C };
817 enum ns__BA { B, A };
818 enum ns__BA_ { B_, A_ };
820 The gSOAP soapcpp2 tool permits reusing enumeration name constants across
821 (non-scoped) enumerations as
long as these values are assigned the same
822 constant. Therefore, the following is permitted:
824 enum ns__Primes { _3 = 3, _5 = 5, _7 = 7, _11 = 11 };
825 enum ns__Throws { _1 = 1, _2 = 2, _3 = 3, _4 = 4, _5 = 5, _6 = 6 };
827 A bitmask type is an `
enum*`
"product" enumeration with a geometric,
828 power-of-two sequence of values assigned to the enumeration constants:
830 enum* ns__Options { SSL3, TLS10, TLS11, TLS12 };
832 where the product
enum assigns 1 to `SSL3`, 2 to `TLS10`, 4 to `TLS11`, and 8
833 to `TLS12`, which allows these enumeration constants to be used in composing
834 bitmasks with `|` (bitwise or) `&` (bitwise and), and `~` (bitwise not):
836 enum ns__Options options = (
enum ns__Options)(SSL3 | TLS10 | TLS11 | TLS12);
843 The bitmask type maps to a simpleType list restriction of xsd:
string in the
844 soapcpp2-generated schema:
846 <simpleType name=
"Options">
848 <restriction base=
"xsd:string">
849 <enumeration value=
"SSL3"/>
850 <enumeration value=
"TLS10"/>
851 <enumeration value=
"TLS11"/>
852 <enumeration value=
"TLS12"/>
857 The XML value space of
this type consists of all 16 possible subsets of the
858 four values, represented by an XML
string with space-separated values. For
859 example, the bitmask `TLS10 | TLS11 | TLS12` equals 14 and is represented in by
860 the XML
string `TLS10 TLS11 TLS12`.
862 We can also use C++11 scoped enumerations with bitmasks:
864 enum*
class ns__Options { SSL3, TLS10, TLS11, TLS12 };
866 The base type of a scoped enumeration bitmask, when explicitly given, is
867 ignored. The base type is either `
int` or `int64_t`, depending on the number
868 of constants enumerated in the bitmask.
870 To convert `
enum` name constants and bitmasks to a string, we use the
871 auto-generated
function for enum `T`:
873 const char *soap_T2s(
struct soap*,
enum T val)
875 To convert a
string to an `
enum` constant or bitmask, we use the
auto-generated
878 int soap_s2T(
struct soap*,
const char *str,
enum T *val)
880 This
function takes the name (or names, space-separated
for bitmasks) of
881 the enumeration constant in a
string `str`. Names should be given without the
882 pseudo-scope prefix and without trailing underscores. The
function sets `val`
883 to the corresponding integer
enum constant or to a bitmask. The
function
884 returns `SOAP_OK` (zero) on success or an error
if the
string is not a valid
888 Numerical types {#toxsd5}
891 Integer and floating point types are mapped to the equivalent built-in XSD
892 types with the same sign and bit width.
894 The `
size_t` type is
transient (not serializable) because its width is platform
895 dependent. We recommend to use `uint64_t` instead.
897 The XML value space of integer types are their decimal representations without
900 The XML value space of floating point types are their decimal representations.
901 The decimal representations are formatted with the printf format
string "%.9G"
902 for floats and the printf format
string "%.17lG" for double. To change the
903 format strings, we can assign
new strings to the following `
struct soap`
906 soap.float_format =
"%g";
907 soap.double_format =
"%lg";
908 soap.long_double_format =
"%Lg";
910 Note that decimal representations may result in a loss of precision of the
911 least significant decimal. Therefore, the format strings that are used by
912 default are sufficiently precise to avoid loss, but
this may result in
long
913 decimal fractions in the XML value space.
915 The `
long double` extended floating point type requires a custom serializer:
917 #
import "custom/long_double.h"
918 ... use
long double ...
920 You can now use `
long double`, which has a serializer that serializes
this type
921 as `xsd:decimal`. Compile and link your code with `custom/long_double.c`.
923 The value space of floating point values includes the special values `INF`,
924 `-INF`, and `NaN`. You can check a value
for plus or minus infinity and
925 not-a-number as follows:
927 soap_isinf(x) && x > 0
928 soap_isinf(x) && x < 0
931 To assign these values, use:
934 x = FLT_PINFY; x = DBL_PINFTY;
935 x = FLT_NINFY; x = DBL_NINFTY;
936 x = FLT_NAN; x = DBL_NAN;
938 If your system supports `__float128` then you can also use this 128 bit
939 floating point type with a custom serializer:
941 #import "custom/float128.h"
942 ... use xsd__decimal ...
944 Then use the `xsd__decimal` alias of `__float128`, which has a serializer. Do
945 not use `__float128` directly, which is
transient (not serializable).
947 To check
for `INF`, `-INF`, and `NaN` of a `__float128` value use:
953 The range of a typedef-defined numerical type can be restricted using the range
954 `:` operator with inclusive lower and upper bounds. For example:
956 typedef
int ns__narrow -10 : 10;
958 This maps to a simpleType restriction of xsd:
int in the soapcpp2-generated
961 <simpleType name="narrow">
962 <restriction base="xsd:
int">
963 <minInclusive value="-10"/>
964 <maxInclusive value="10"/>
968 The lower and upper bound of a range are optional. When omitted, values are
969 not bound from below or from above, respectively.
971 The range of a floating point typedef-defined type can be restricted within
972 floating point constant bounds.
974 Also with a floating point typedef a printf format pattern can be given of the
975 form `"%[width][.precision]f"` to format decimal values using the given width
976 and precision fields:
978 typedef
float ns__PH "%5.2f" 0.0 : 14.0;
980 This maps to a simpleType restriction of xsd:
float in the soapcpp2-generated
983 <simpleType name="PH">
984 <restriction base="xsd:
float">
985 <totalDigits value="5"/>
986 <fractionDigits value="2"/>
987 <minInclusive value="0"/>
988 <maxInclusive value="14"/>
992 For exclusive bounds, we use the `<` operator instead of the `:` range
995 typedef
float ns__epsilon 0.0 < 1.0;
997 Values `eps` of `ns__epsilon` are restricted between `0.0 < eps < 1.0`.
999 This maps to a simpleType restriction of xsd:
float in the soapcpp2-generated
1002 <simpleType name="epsilon">
1003 <restriction base="xsd:
float">
1004 <minExclusive value="0"/>
1005 <maxExclusive value="1"/>
1009 To make just one of the bounds exclusive, while keeping the other bound
1010 inclusive, we add a `<` on the left or on the right side of the range ':'
1011 operator. For example:
1013 typedef
float ns__pos 0.0 < : ;
1014 typedef
float ns__neg : < 0.0 ;
1016 It is valid to make both left and right side exclusive with `< : <` which is in
1017 fact identical to the exlusive range `<` operator:
1019 typedef
float ns__epsilon 0.0 < : < 1.0;
1021 It helps to think of the `:` as a placeholder of the value between the two
1022 bounds, which is easier to memorize than the shorthand forms of bounds from
1023 which the `:` is removed:
1025 | Bounds | Validation check | Shorthand |
1026 | ---------- | ---------------- | --------- |
1027 | 1 : | 1 <= x | 1 |
1028 | 1 : 10 | 1 <= x <= 10 | |
1029 | : 10 | x <= 10 | |
1030 | 1 < : < 10 | 1 < x < 10 | 1 < 10 |
1031 | 1 : < 10 | 1 <= x < 10 | |
1032 | : < 10 | x < 10 | < 10 |
1033 | 1 < : | 1 < x | 1 < |
1034 | 1 < : 10 | 1 < x <= 10 | |
1036 Besides `
float`, also `
double` and `
long double` values can be restricted. For
1037 example, consider a nonzero probability extended floating point precision type:
1039 #import "custom/long_double.h"
1040 typedef long double ns__probability
"%16Lg" 0.0 < : 1.0;
1042 Value range restrictions are validated by the parser
for all inbound XML data.
1043 A type fault `SOAP_TYPE` will be thrown by the deserializer
if the value is out
1046 Finally,
if your system supports `__int128_t` then you can also use
this 128
1047 bit integer type with a custom serializer:
1049 #import "custom/int128.h"
1050 ... use xsd__integer ...
1052 We use the `xsd__integer` alias of `__int128_t`, which has a serializer. Do not
1053 use `__int128_t` directly, which is
transient (not serializable).
1055 To convert numeric values to a
string, we use the
auto-generated
function for
1058 const char *soap_T2s(
struct soap*, T val)
1060 For numeric types `T`, the
string returned is stored in an
internal buffer, so
1061 you MUST copy it to keep it from being overwritten. For example, use
1062 `soap_strdup(
struct soap*,
const char*)`.
1064 To convert a
string to a numeric value, we use the
auto-generated
function
1066 int soap_s2T(
struct soap*,
const char *str, T *val)
1068 where `T` is
for example `
int`, `LONG64`, `
float`, `decimal` (the custom
1069 serializer name of `
long double`) or `xsd__integer` (the custom serializer name
1070 of `__int128_t`). The
function `soap_s2T` returns `SOAP_OK` on success or an
1071 error when the value is not numeric. For floating point types,
"INF",
"-INF"
1072 and
"NaN" are valid strings to convert to numbers.
1075 String types {#toxsd6}
1078 String types are mapped to the built-in xsd:
string and xsd:QName XSD types.
1080 The wide strings `
wchar_t*` and `std::wstring` may contain Unicode that is
1081 preserved in the XML value space.
1083 Strings `
char*` and `std::string` can only contain extended Latin, but we can
1084 store UTF-8 content that is preserved in the XML value space when the `
struct
1085 soap` context is initialized with the flag `XML_C_UTFSTRING`.
1087 @warning Beware that many XML 1.0 parsers reject all control characters (those
1088 between `#x1` and `#x1F`) except `#x9`, `#xA`, and `#xD`. With the newer XML
1089 1.1 version parsers (including gSOAP) you should be fine.
1091 The length of a
string of a
typedef-defined
string type can be restricted:
1093 typedef std::string ns__password 6 : 16;
1095 which maps to a simpleType restriction of xsd:
string in the soapcpp2-generated
1098 <simpleType name=
"password">
1099 <restriction base=
"xsd:string">
1100 <minLength value=
"6"/>
1101 <maxLength value=
"16"/>
1105 String length restrictions are validated by the parser
for inbound XML data.
1106 A value length fault `SOAP_LENGTH` will be thrown by the deserializer
if the
1107 string is too
long or too
short.
1109 In addition, an XSD regex pattern restriction can be associated with a
string
1112 typedef std::string ns__password
"([a-zA-Z]|[0-9]|-)+" 6 : 16;
1114 which maps to a simpleType restriction of xsd:
string in the soapcpp2-generated
1117 <simpleType name=
"password">
1118 <restriction base=
"xsd:string">
1119 <pattern value=
"([a-zA-Z0-9]|-)+"/>
1120 <minLength value=
"6"/>
1121 <maxLength value=
"16"/>
1125 Pattern restrictions are validated by the parser
for inbound XML data only
if
1126 the `soap::fsvalidate` and `soap::fwvalidate` callbacks are defined, see the
1127 gSOAP user guide
for more details.
1129 Exclusive length bounds can be used with strings:
1131 typedef std::string ns__string255 : < 256;
1133 Fixed-size strings (`
char[N]`) are rare occurrences in the wild, but apparently
1134 still used in some projects to store strings. To facilitate fixed-size
string
1135 serialization, use soapcpp2 option `-b`. For example:
1137 typedef char ns__buffer[10];
1139 which maps to a simpleType restriction of xsd:
string in the soapcpp2-generated
1142 <simpleType name=
"buffer">
1143 <restriction base=
"xsd:string">
1144 <maxLength value=
"9"/>
1148 Note that fixed-size strings MUST contain NUL-terminated text and SHOULD NOT
1149 contain raw binary data. Also, the length limitation is more restrictive
for
1150 UTF-8 content (enabled with the `SOAP_C_UTFSTRING`) that requires multibyte
1151 character encodings. As a consequence, UTF-8 content may be truncated to fit.
1153 Note that raw binary data can be stored in a `xsd__base64Binary` or
1154 `xsd__hexBinary` structure, or transmitted as a MIME attachment.
1156 The built-in `
_QName` type is a regular C
string type (`
char*`) that maps to
1157 xsd:QName but has the added advantage that it holds normalized qualified names.
1158 There are actually two forms of normalized QName content, to ensure any QName
1159 is represented accurately and uniquely:
1164 where the first form is used when the prefix (and the binding URI) is defined
1165 in the namespace table and is bound to a URI (see the .nsmap file). The second
1166 form is used when the URI is not defined in the namespace table and therefore
1167 no prefix is available to bind and normalize the URI to.
1169 A `
_QName`
string may contain a sequence of space-separated QName values, not
1170 just one, and all QName values are normalized to the format shown above.
1172 To define a `std::
string` base type for xsd:QName, we use a typedef:
1174 typedef std::
string xsd__QName;
1176 The `xsd__QName`
string content is normalized, just as with the `
_QName`
1179 To serialize strings that contain literal XML content to be reproduced in the
1180 XML value space, use the built-in `
_XML`
string type, which is a regular C
1181 string type (`
char*`) that maps to plain XML CDATA.
1183 To define a `std::
string` base type for literal XML content, use a typedef:
1185 typedef std::
string XML;
1187 Strings can hold any of the values of the XSD built-in primitive types. We can
1188 use a
string typedef to declare the use of the
string type as a XSD built-in
1191 typedef std::
string xsd__token;
1193 We MUST ensure that the
string values we populate in this type conform to the
1194 XML standard, which in case of xsd:token is: the lexical and value spaces of
1195 xsd:token are the sets of all strings after whitespace replacement of any
1196 occurrence of `
#x9`, `#xA` , and `#xD` by `#x20` and collapsing.
1198 To copy `
char*` or `
wchar_t*` strings with a context that manages the allocated
1199 memory, use functions
1201 char *soap_strdup(
struct soap*,
const char*)
1202 wchar_t *soap_wstrdup(struct soap*, const
wchar_t*)
1204 To convert a wide
string to a UTF-8 encoded
string, use function
1206 const
char* SOAP_FMAC2 soap_wchar2s(struct soap*, const
wchar_t *s)
1208 The function allocates and returns a
string, with its memory being managed by
1211 To convert a UTF-8 encoded
string to a wide
string, use function
1213 int soap_s2wchar(struct soap*, const
char *from,
wchar_t **to,
long minlen,
long maxlen)
1215 where `to` is set to point to an allocated `
wchar_t*`
string. Pass `-1` for
1216 `minlen` and `maxlen` to ignore length constraints on the target
string. The
1217 function returns `SOAP_OK` or an error when the length constraints are not met.
1220 Date and time types {#toxsd7}
1223 The C/C++ `time_t` type is mapped to the built-in xsd:dateTime XSD type that
1224 represents a date and time within a time zone (typically UTC).
1226 The XML value space contains ISO 8601 Gregorian time instances of the form
1227 `[-]CCYY-MM-DDThh:mm:ss.sss[Z|(+|-)hh:mm]`, where `Z` is the UTC time zone or a
1228 time zone offset `(+|-)hh:mm]` from UTC is used.
1230 A `time_t` value is considered and represented in UTC by the serializer.
1232 Because the `time_t` value range is restricted to dates after 01/01/1970, care
1233 must be taken to ensure the range of xsd:dateTime values in XML exchanges
do
1234 not exceed the `time_t` range.
1236 This restriction does not hold
for `
struct tm` (`<time.h>`), which we can use
1237 to store and exchange a date and time in UTC without date range restrictions.
1238 The serializer uses the `struct tm` members directly for the XML value space of
1255 You will lose the day of the week information. It is always Sunday
1256 (`tm_wday=0`) and the day of the year is not set either. The time zone is UTC.
1258 This `struct tm` type is mapped to the built-in xsd:dateTime XSD type and
1259 serialized with the custom serializer `custom/struct_tm.h` that declares a
1260 `xsd__dateTime` type:
1262 #import "custom/struct_tm.h" // import typedef struct tm xsd__dateTime;
1263 ... use xsd__dateTime ...
1265 Compile and link your code with `custom/struct_tm.c`.
1267 The `
struct timeval` (`<sys/time.h>`) type is mapped to the built-in
1268 xsd:dateTime XSD type and serialized with the custom serializer
1269 `custom/struct_timeval.h` that declares a `xsd__dateTime` type:
1271 #import "custom/struct_timeval.h" // import typedef struct timeval xsd__dateTime;
1272 ... use xsd__dateTime ...
1274 Compile and link your code with `custom/struct_timeval.c`.
1276 Note that the same value range restrictions apply to `struct timeval` as they
1277 apply to `time_t`. The added benefit of `struct timeval` is the addition of
1278 a microsecond-precise clock:
1283 suseconds_t tv_usec;
1286 A C++11 `std::chrono::system_clock::time_point` type is mapped to the built-in
1287 xsd:dateTime XSD type and serialized with the custom serializer
1288 `custom/chrono_time_point.h` that declares a `xsd__dateTime` type:
1290 #import "custom/chrono_time_point.h"
1291 ... use xsd__dateTime ...
1293 Compile and link your code with `custom/chrono_time_point.cpp`.
1295 The `
struct tm` type is mapped to the built-in xsd:date XSD type and serialized
1296 with the custom serializer `custom/struct_tm_date.h` that declares a
1299 #import "custom/struct_tm_date.h" // import typedef struct tm xsd__date;
1300 ... use xsd__date ...
1302 Compile and link your code with `custom/struct_tm_date.c`.
1304 The XML value space of xsd:date are Gregorian calendar dates of the form
1305 `[-]CCYY-MM-DD[Z|(+|-)hh:mm]` with a time zone.
1307 The serializer ignores the time part and the deserializer only populates the
1308 date part of the struct, setting the time to 00:00:00. There is no unreasonable
1309 limit on the date range because the year field is stored as an integer (`int`).
1311 An `unsigned long long` (`ULONG64` or `uint64_t`) type that contains a 24 hour
1312 time in microseconds UTC is mapped to the built-in xsd:time XSD type and
1313 serialized with the custom serializer `custom/long_time.h` that declares a
1316 #import "custom/long_time.h" // import typedef unsigned long long xsd__time;
1317 ... use xsd__time ...
1319 Compile and link your code with `custom/long_time.c`.
1321 This type represents 00:00:00.000000 to 23:59:59.999999, from `0` to an upper
1322 bound of `86399999999`. A microsecond resolution means that a 1 second
1323 increment requires an increment of 1000000 in the integer value.
1325 The XML value space of xsd:time are points in time recurring each day of the
1326 form `hh:mm:ss.sss[Z|(+|-)hh:mm]`, where `Z` is the UTC time zone or a time
1327 zone offset from UTC is used. The `xsd__time` value is always considered and
1328 represented in UTC by the serializer.
1330 To convert date and/or time values to a string, we use the auto-generated
1331 function for type `T`:
1333 const char *soap_T2s(struct soap*, T val)
1335 For date and time types `T`, the string returned is stored in an internal
1336 buffer, so you MUST copy it to keep it from being overwritten. For example,
1337 use `soap_strdup(struct soap*, const char*)`.
1339 To convert a string to a date/time value, we use the auto-generated function
1341 int soap_s2T(struct soap*, const char *str, T *val)
1343 where `T` is for example `dateTime` (for `time_t`), `xsd__dateTime` (for
1344 `struct tm`, `struct timeval`, or `std::chrono::system_clock::time_point`).
1345 The function `soap_s2T` returns `SOAP_OK` on success or an error when the value
1349 Time duration types {#toxsd8}
1352 The XML value space of xsd:duration are values of the form `PnYnMnDTnHnMnS`
1353 where the capital letters are delimiters. Delimiters may be omitted when the
1354 corresponding member is not used.
1356 A `
long long` (`LONG64` or `int64_t`) type that contains a duration (time
1357 lapse) in milliseconds is mapped to the built-in xsd:duration XSD type and
1358 serialized with the custom serializer `custom/duration.h` that declares a
1359 `xsd__duration` type:
1361 #import "custom/duration.h"
1362 ... use xsd__duration ...
1364 Compile and link your code with `custom/duration.c`.
1366 The duration type `xsd__duration` can represent 106,751,991,167 days forward
1367 and backward with millisecond precision.
1369 Durations that exceed a month are always output in days, rather than months to
1370 avoid days-per-month conversion inacurracies.
1372 Durations that are received in years and months instead of total number of days
1373 from a reference point are not well defined, since there is no accepted
1374 reference time point (it may or may not be the current time). The decoder
1375 simple assumes that there are 30 days per month. For example, conversion of
1376 "P4M" gives 120 days. Therefore, the durations
"P4M" and
"P120D" are assumed
1377 to be identical, which is not necessarily
true depending on the reference point
1380 Rescaling of the duration value by may be needed when adding the duration value
1381 to a `time_t` value, because `time_t` may or may not have a seconds resolution,
1382 depending on the platform and possible changes to `time_t`.
1384 Rescaling is done automatically when you add a C++11 `std::chrono::nanoseconds`
1385 value to a `std::chrono::system_clock::time_point` value. To use
1386 `std::chrono::nanoseconds` as xsd:duration:
1388 #import "custom/chrono_duration.h"
1389 ... use xsd__duration ...
1391 Compile and link your code with `custom/chrono_duration.cpp`.
1393 This type can represent 384,307,168 days (2^63 nanoseconds) forwards and
1394 backwards in time in increments of 1 ns (1/1000000000 second).
1396 The same observations with respect to receiving durations in years and months
1397 apply to this serializer's decoder.
1399 To convert duration values to a
string, we use the auto-generated function
1401 const
char *soap_xsd__duration2s(struct soap*, xsd__duration val)
1403 The
string returned is stored in an internal buffer, so you MUST copy it to
1404 keep it from being overwritten, Use `soap_strdup(struct soap*, const
char*)`
1407 To convert a
string to a duration value, we use the auto-generated function
1409 int soap_s2xsd__dateTime(struct soap*, const
char *str, xsd__dateTime *val)
1411 The function returns `SOAP_OK` on success or an error when the value is not a
1415 Classes and structs {#toxsd9}
1418 Classes and structs are mapped to XSD complexTypes. The XML value space
1419 consists of XML elements with attributes and subelements, possibly constrained
1420 by validation rules that enforce element and attribute occurrence contraints,
1421 numerical value range constraints, and
string length and pattern constraints.
1423 Classes that are declared with the gSOAP tools are limited to single
1424 inheritence only. Structs cannot be inherited.
1426 The
class and struct name is bound to an XML namespace by means of the prefix
1427 naming convention or by using [Colon notation](#toxsd1):
1442 In the example above, we also added a context pointer to the `
struct soap` that
1443 manages this instance. It is set when the instance is created in the engine's
1444 context, for example when deserialized and populated by the engine.
1446 The class maps to a complexType in the soapcpp2-generated schema:
1448 <complexType name="record">
1450 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
1451 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
1452 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/>
1456 ### Serializable versus transient types and members {#toxsd9-1}
1458 Public data members of a class or struct are serialized. Private and
protected
1459 members are transient and not serializable.
1461 Also `const` and `static` members are not serializable, with the exception of
1462 `const char*` and `const wchar_t*`.
1464 Types and specific class/struct members can be made transient by using the
1467 extern class std::ostream;
1473 static const int MAX = 1024;
1476 By declaring `std::ostream`
transient we can use
this type where we need it and
1477 without soapcpp2 complaining that
this class is not defined.
1479 ### Volatile classes and structs {#toxsd9-2}
1481 Classes and structs can be declared `
volatile` with the gSOAP tools. This means
1482 that they are already declared elsewhere in our project
's source code. We do
1483 not want soapcpp2 to generate a second definition for these types.
1485 For example, `struct tm` is declared in `<time.h>`. We want it serializable and
1486 serialize only a selection of its data members:
1490 int tm_sec; // seconds (0 - 60)
1491 int tm_min; // minutes (0 - 59)
1492 int tm_hour; // hours (0 - 23)
1493 int tm_mday; // day of month (1 - 31)
1494 int tm_mon; // month of year (0 - 11)
1495 int tm_year; // year - 1900
1498 We can declare classes and structs `volatile` for any such types we want to
1499 serialize by only providing the public data members we want to serialize.
1501 Colon notation comes in handy to bind an existing class or struct to a schema.
1502 For example, we can change the `tm` name as follows without affecting the code
1503 that uses `struct tm` generated by soapcpp2:
1505 volatile struct ns:tm { ... }
1507 This struct maps to a complexType in the soapcpp2-generated schema:
1509 <complexType name="tm">
1511 <element name="tm-sec" type="xsd:int" minOccurs="1" maxOccurs="1"/>
1512 <element name="tm-min" type="xsd:int" minOccurs="1" maxOccurs="1"/>
1513 <element name="tm-hour" type="xsd:int" minOccurs="1" maxOccurs="1"/>
1514 <element name="tm-mday" type="xsd:int" minOccurs="1" maxOccurs="1"/>
1515 <element name="tm-mon" type="xsd:int" minOccurs="1" maxOccurs="1"/>
1516 <element name="tm-year" type="xsd:int" minOccurs="1" maxOccurs="1"/>
1520 ### Mutable classes and structs {#toxsd9-3}
1522 Classes and structs can be declared `mutable` with the gSOAP tools. This means
1523 that their definition can be spread out over the source code. This promotes the
1524 concept of a class or struct as a *row of named values*, also known as a *named
1525 tuple*, that can be extended at compile time in our source code with additional
1526 members. Because these types differ from the traditional object-oriented
1527 principles and design concepts of classes and objects, constructors and
1528 destructors cannot be defined (also because we cannot guarantee merging these
1529 into one such that all members will be initialized). A default constructor,
1530 copy constructor, assignment operation, and destructor will be assigned
1531 automatically by soapcpp2.
1533 mutable struct ns__tuple
1538 mutable struct ns__tuple
1544 The members are collected into one definition generated by soapcpp2. Members
1545 may be repeated from one definition to another, but only if their associated
1546 types are identical. So, for example, a third extension with a `value` member
1547 with a different type fails:
1549 mutable struct ns__tuple
1551 float value; // BAD: value is already declared std::string
1554 The `mutable` concept has proven to be very useful when declaring and
1555 collecting SOAP Headers for multiple services, which are collected into one
1556 `struct SOAP_ENV__Header` by the soapcpp2 tool.
1558 ### Default member values in C and C++ {#toxsd9-4}
1560 Class and struct data members in C and C++ may be declared with an optional
1561 default initialization value that is provided "inline" with the declaration of
1567 std::string name = "Joe";
1569 These initializations are made by the default constructor that is added by
1570 soapcpp2 to each class and struct. A constructor is only added when a default
1571 constructor is not already defined with the class declaration. You can
1572 explicitly (re)initialize an object with the auto-generated
1573 `soap_default(struct soap*)` method of a class and the auto-generated
1574 `soap_default_T(struct soap*, T*)` function for a struct `T` in C and C++.
1576 Initializations can only be provided for members that have primitive types
1577 (`bool`, `enum`, `time_t`, numeric and string types).
1579 ### Attribute members {#toxsd9-5}
1581 Class and struct data members can be declared as XML attributes by annotating
1582 their type with a `@` with the declaration of the member:
1592 This class maps to a complexType in the soapcpp2-generated schema:
1594 <complexType name="record">
1596 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/>
1598 <attribute name="name" type="xsd:string" use="required"/>
1599 <attribute name="SSN" type="xsd:unsignedLong" use="required"/>
1602 An example XML instance of `ns__record` is:
1604 <ns:record xmlns:ns="urn:types" name="Joe" SSN="1234567890">
1607 <SSN>1987654320</SSN>
1611 Attribute data members are restricted to primitive types (`bool`, `enum`,
1612 `time_t`, numeric and string types), `xsd__hexBinary`, `xsd__base64Binary`, and
1613 custom serializers, such as `xsd__dateTime`. Custom serializers for types that
1614 may be used as attributes MUST define `soap_s2T` and `soap_T2s` functions that
1615 convert values of type `T` to strings and back.
1617 Attribute data members can be pointers and smart pointers to these types, which
1618 permits attributes to be optional.
1620 ### (Smart) pointer members and their occurrence constraints {#toxsd9-6}
1622 A public pointer-typed data member is serialized by following its (smart)
1623 pointer(s) to the value pointed to.
1625 Pointers that are NULL and smart pointers that are empty are serialized to
1626 produce omitted element and attribute values, unless an element is required
1629 To control the occurrence requirements of pointer-based data members,
1630 occurrence constraints are associated with data members in the form of a range
1631 `minOccurs : maxOccurs`. For non-repeatable (meaning, not a container or array)
1632 data members, there are only three reasonable occurrence constraints:
1634 - `0:0` means that this element or attribute is prohibited.
1635 - `0:1` means that this element or attribute is optional.
1636 - `1:1` means that this element or attribute is required.
1638 Pointer-based data members have a default `0:1` occurrence constraint, making
1639 them optional, and their XSD schema local element/attribute definition is
1640 marked as nillable. Non-pointer data members have a default `1:1` occurence
1641 constraint, making them required.
1643 A pointer data member that is explicitly marked as required with `1:1` will be
1644 serialized as an element with an xsi:nil attribute, thus effectively revealing
1645 the NULL property of its value.
1647 A non-pointer data member that is explicitly marked as optional with `0:1` will
1648 be set to its default value when no XML value is presented to the deserializer.
1649 A default value can be assigned to data members that have primitive types.
1651 Consider for example:
1656 std::shared_ptr<std::string> name; // optional (0:1)
1657 uint64_t SSN 0:1 = 999; // forced this to be optional with default 999
1658 ns__record *spouse 1:1; // forced this to be required (only married people)
1661 This class maps to a complexType in the soapcpp2-generated schema:
1663 <complexType name="record">
1665 <element name="name" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
1666 <element name="SSN" type="xsd:unsignedLong" minOccurs="0" maxOccurs="1" default="999"/>
1667 <element name="spouse" type="ns:record" minOccurs="1" maxOccurs="1" nillable="true"/>
1671 An example XML instance of `ns__record` with its `name` string value set to
1672 `Joe`, `SSN` set to its default, and `spouse` set to NULL:
1674 <ns:record xmlns:ns="urn:types">
1677 <spouse xsi:nil="true"/>
1680 @note In general, a smart pointer is simply declared as a `volatile` template
1681 in a gSOAP header file for soapcpp2:
1683 volatile template <class T> class NAMESPACE::shared_ptr;
1685 @note The soapcpp2 tool generates code that uses `NAMESPACE::shared_ptr` and
1686 `NAMESPACE::make_shared` to create shared pointers to objects, where
1687 `NAMESPACE` is any valid C++ namespace such as `std` and `boost` if you have
1690 ### Container members and their occurrence constraints {#toxsd9-7}
1692 Class and struct data member types that are containers `std::deque`,
1693 `std::list`, `std::vector` and `std::set` are serialized as a collections
1696 You can use `std::deque`, `std::list`, `std::vector`, and `std::set` containers by importing:
1698 #import "import/stl.h" // import all containers
1699 #import "import/stldeque.h" // import deque
1700 #import "import/stllist.h" // import list
1701 #import "import/stlvector.h" // import vector
1702 #import "import/stlset.h" // import set
1704 For example, to use a vector data mamber to store names in a record:
1706 #import "import/stlvector.h"
1710 std::vector<std::string> names;
1714 To limit the number of names in the vector within reasonable bounds, occurrence
1715 constraints are associated with the container. Occurrence constraints are of
1716 the form `minOccurs : maxOccurs`:
1718 #import "import/stlvector.h"
1722 std::vector<std::string> names 1:10;
1726 This class maps to a complexType in the soapcpp2-generated schema:
1728 <complexType name="record">
1730 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="10"/>
1731 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1""/>
1735 @note In general, a container is simply declared as a template in a gSOAP
1736 header file for soapcpp2. All class templates are considered containers
1737 (except when declared `volatile`, see smart pointers). For example,
1738 `std::vector` is declared in `gsoap/import/stlvector.h` as:
1740 template <class T> class std::vector;
1742 @note You can define and use your own containers. The soapcpp2 tool generates
1743 code that uses the following members of the `template <typename T> class C`
1747 C::iterator C::begin()
1748 C::const_iterator C::begin() const
1749 C::iterator C::end()
1750 C::const_iterator C::end() const
1751 size_t C::size() const
1752 C::iterator C::insert(C::iterator pos, const T& val)
1754 @note For more details see the example `simple_vector` container with
1755 documentation in the package under `gsoap/samples/template`.
1757 Because C does not support a container template library, we can use a
1758 dynamically-sized array of values. This array is declared as a size-pointer
1763 $int sizeofnames; // array size
1764 char* *names; // array of char* names
1768 where the marker `$` with `int` denotes a special type that is used to store
1769 the array size and to indicate that this is a size-pointer member pair that
1770 declares a dynamically-sized array.
1772 This class maps to a complexType in the soapcpp2-generated schema:
1774 <complexType name="record">
1776 <element name="name" type="xsd:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
1777 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1""/>
1781 To limit the number of names in the array within reasonable bounds, occurrence
1782 constraints are associated with the array size member. Occurrence constraints
1783 are of the form `minOccurs : maxOccurs`:
1787 $int sizeofnames 1:10; // array size 1..10
1788 char* *names; // array of one to ten char* names
1792 This class maps to a complexType in the soapcpp2-generated schema:
1794 <complexType name="record">
1796 <element name="name" type="xsd:string" minOccurs="1" maxOccurs="10" nillable="true"/>
1797 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1""/>
1801 ### Union members {#toxsd9-8}
1803 A union member in a class or in a struct cannot be serialized unless a
1804 discriminating *variant selector* member is provided that tells the serializer
1805 which union field to serialize. This effectively creates a *tagged union*.
1807 The variant selector is associated with the union as a selector-union member
1808 pair, where the variant selector is a special `$int` member:
1813 $int xORnORs; // variant selector
1823 The variant selector values are auto-generated based on the union name `choice`
1824 and the names of its members `x`, `n`, and `s`:
1826 - `xORnORs = SOAP_UNION_choice_x` when `u.x` is valid.
1827 - `xORnORs = SOAP_UNION_choice_n` when `u.n` is valid.
1828 - `xORnORs = SOAP_UNION_choice_s` when `u.s` is valid.
1829 - `xORnORs = 0` when none are valid (should only be used with great care,
1830 because XML content validation may fail when content is required but absent).
1832 This class maps to a complexType with a sequence and choice in the
1833 soapcpp2-generated schema:
1835 <complexType name="record">
1838 <element name="x" type="xsd:float" minOccurs="1" maxOccurs="1"/>
1839 <element name="n" type="xsd:int" minOccurs="1" maxOccurs="1"/>
1840 <element name="s" type="xsd:string" minOccurs="0" maxOccurs="1" nillable="true"/>
1842 <element name="names" type="xsd:string" minOccurs="1" maxOccurs="10" nillable="true"/>
1846 ### Adding get and set methods {#toxsd9-9}
1848 A public `get` method may be added to a class or struct, which will be
1849 triggered by the deserializer. This method will be invoked right after the
1850 instance is populated by the deserializer. The `get` method can be used to
1851 update or verify deserialized content. It should return `SOAP_OK` or set
1852 `soap::error` to a nonzero error code and return it.
1854 A public `set` method may be added to a class or struct, which will be
1855 triggered by the serializer. The method will be invoked just before the
1856 instance is serialized. Likewise, the `set` method should return `SOAP_OK` or
1857 set set `soap::error` to a nonzero error code and return it.
1859 For example, adding a `set` and `get` method to a class declaration:
1864 int set(struct soap*); // triggered before serialization
1865 int get(struct soap*); // triggered after deserialization
1867 To add these and othe rmethods to classes and structs with wsdl2h and
1868 `typemap.dat`, please see [Class/struct member additions](#typemap3).
1870 ### Defining document root elements {#toxsd9-10}
1872 To define and reference XML document root elements we use type names that start
1877 Alternatively, we can use a typedef to define a document root element with a
1880 typedef ns__record _ns__record;
1882 This typedef maps to a global root element that is added to the
1883 soapcpp2-generated schema:
1885 <element name="record" type="ns:record"/>
1887 An example XML instance of `_ns__record` is:
1889 <ns:record xmlns:ns="urn:types">
1891 <SSN>1234567890</SSN>
1894 <SSN>1987654320</SSN>
1898 Global-level element/attribute definitions are also referenced and/or added to
1899 the generated schema when serializable data members reference these by their
1902 typedef std::string _ns__name 1 : 100;
1906 @_QName xsi__type; // built-in XSD attribute xsi:type
1907 _ns__name ns__name; // ref to global ns:name element
1909 _ns__record *spouse;
1912 These types map to the following comonents in the soapcpp2-generated schema:
1914 <simpleType name="name">
1915 <restriction base="xsd:string">
1916 <minLength value="1"/>
1917 <maxLength value="100"/>
1920 <element name="name" type="ns:name"/>
1921 <complexType name="record">
1923 <element ref="ns:name" minOccurs="1" maxOccurs="1"/>
1924 <element name="SSN" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
1925 <element name="spouse" type="ns:record" minOccurs="0" maxOccurs="1" nillable="true"/>
1927 <attribute ref="xsi:type" use="optional"/>
1929 <element name="record" type="ns:record"/>
1931 @warning Use only use qualified member names when their types match the root
1932 element types that they refer to. For example:
1937 int ns__name; // BAD: element ns:name is NOT of an int type
1939 @warning Therefore, we recommend to avoid qualified member names and only use
1940 them when referring to standard XSD elements and attributes, such as
1941 `xsi__type`, and `xsd__lang`. The soapcpp2 tool does not prevent abuse of this
1944 ### Operations on classes and structs {#toxsd9-11}
1946 The following functions/macros are generated by soapcpp2 for each type `T`,
1947 which should make it easier to send, receive, and copy XML data in C and in
1950 - `int soap_write_T(struct soap*, T*)` writes an instance of `T` to a FILE (via
1951 `FILE *soap::sendfd)`) or to a stream (via `std::ostream *soap::os`).
1952 Returns `SOAP_OK` on success or an error code, also stored in `soap->error`.
1954 - `int soap_read_T(struct soap*, T*)` reads an instance of `T` from a FILE (via
1955 `FILE *soap::recvfd)`) or from a stream (via `std::istream *soap::is`).
1956 Returns `SOAP_OK` on success or an error code, also stored in `soap->error`.
1958 - `void soap_default_T(struct soap*, T*)` sets an instance `T` to its default
1959 value, resetting members of a struct to their initial values (for classes we
1960 use method `T::soap_default`, see below).
1962 - `T * soap_dup_T(struct soap*, T *dst, const T *src)` (soapcpp2 option `-Ec`)
1963 deep copy `src` into `dst`, replicating all deep cycles and shared pointers
1964 when a managing soap context is provided as argument. When `dst` is NULL,
1965 allocates space for `dst`. Deep copy is a tree when argument is NULL, but the
1966 presence of deep cycles will lead to non-termination. Use flag
1967 `SOAP_XML_TREE` with managing context to copy into a tree without cycles and
1968 pointers to shared objects. Returns `dst` (or allocated space when `dst` is
1971 - `void soap_del_T(const T*)` (soapcpp2 option `-Ed`) deletes all
1972 heap-allocated members of this object by deep deletion ONLY IF this object
1973 and all of its (deep) members are not managed by a soap context AND the deep
1974 structure is a tree (no cycles and co-referenced objects by way of multiple
1975 (non-smart) pointers pointing to the same data). Can be safely used after
1976 `soap_dup(NULL)` to delete the deep copy. Does not delete the object itself.
1978 When in C++ mode, soapcpp2 tool adds several methods to classes and structs, in
1979 addition to adding a default constructor and destructor (when these were not
1980 explicitly declared).
1982 The public methods added to a class/struct `T`:
1984 - `virtual int T::soap_type(void)` returns a unique type ID (`SOAP_TYPE_T`).
1985 This numeric ID can be used to distinguish base from derived instances.
1987 - `virtual void T::soap_default(struct soap*)` sets all data members to
1990 - `virtual void T::soap_serialize(struct soap*) const` serializes object to
1991 prepare for SOAP 1.1/1.2 encoded output (or with `SOAP_XML_GRAPH`) by
1992 analyzing its (cyclic) structures.
1994 - `virtual int T::soap_put(struct soap*, const char *tag, const char *type) const`
1995 emits object in XML, compliant with SOAP 1.1 encoding style, return error
1996 code or `SOAP_OK`. Requires `soap_begin_send(soap)` and
1997 `soap_end_send(soap)`.
1999 - `virtual int T::soap_out(struct soap*, const char *tag, int id, const char *type) const`
2000 emits object in XML, with tag and optional id attribute and xsi:type, return
2001 error code or `SOAP_OK`. Requires `soap_begin_send(soap)` and
2002 `soap_end_send(soap)`.
2004 - `virtual void * T::soap_get(struct soap*, const char *tag, const char *type)`
2005 Get object from XML, compliant with SOAP 1.1 encoding style, return pointer
2006 to object or NULL on error. Requires `soap_begin_recv(soap)` and
2007 `soap_end_recv(soap)`.
2009 - `virtual void *soap_in(struct soap*, const char *tag, const char *type)`
2010 Get object from XML, with matching tag and type (NULL matches any tag and
2011 type), return pointer to object or NULL on error. Requires
2012 `soap_begin_recv(soap)` and `soap_end_recv(soap)`
2014 - `virtual T * T::soap_alloc(void) const` returns a new object of type `T`,
2015 default initialized and not managed by a soap context.
2017 - `virtual T * T::soap_dup(struct soap*) const` (soapcpp2 option `-Ec`) returns
2018 a duplicate of this object by deep copying, replicating all deep cycles and
2019 shared pointers when a managing soap context is provided as argument. Deep
2020 copy is a tree when argument is NULL, but the presence of deep cycles will
2021 lead to non-termination. Use flag `SOAP_XML_TREE` with the managing context
2022 to copy into a tree without cycles and pointers to shared objects.
2024 - `virtual void T::soap_del() const` (soapcpp2 option `-Ed`) deletes all
2025 heap-allocated members of this object by deep deletion ONLY IF this object
2026 and all of its (deep) members are not managed by a soap context AND the deep
2027 structure is a tree (no cycles and co-referenced objects by way of multiple
2028 (non-smart) pointers pointing to the same data). Can be safely used after
2029 `soap_dup(NULL)` to delete the deep copy. Does not delete the object itself.
2032 Special classes and structs {#toxsd10}
2033 ---------------------------
2035 ### SOAP encoded arrays {#toxsd10-1}
2037 A class or struct with the following layout is a one-dimensional SOAP encoded
2043 T *__ptr; // array pointer
2044 int __size; // array size
2047 where `T` is the array element type. A multidimensional SOAP Array is:
2052 T *__ptr; // array pointer
2053 int __size[N]; // array size of each dimension
2056 where `N` is the constant number of dimensions. The pointer points to an array
2057 of `__size[0]*__size[1]* ... * __size[N-1]` elements.
2059 This maps to a complexType restriction of SOAP-ENC:Array in the
2060 soapcpp2-generated schema:
2062 <complexType name="ArrayOfT">
2064 <restriction base="SOAP-ENC:Array">
2066 <element name="item" type="T" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
2068 <attribute ref="SOAP-ENC:arrayType" WSDL:arrayType="ArrayOfT[]"/>
2073 The name of the class can be arbitrary. We often use `ArrayOfT` without a
2074 prefix to distinguish arrays from other classes and structs.
2076 With SOAP 1.1 encoding, an optional offset member can be added that controls
2077 the start of the index range for each dimension:
2082 T *__ptr; // array pointer
2083 int __size[N]; // array size of each dimension
2084 int __offset[N]; // array offsets to start each dimension
2087 For example, we can define a matrix of floats as follows:
2096 The following code populates the matrix and serializes it in XML:
2098 soap *soap = soap_new1(SOAP_XML_INDENT);
2100 double a[6] = { 1, 2, 3, 4, 5, 6 };
2104 soap_write_Matrix(soap, &A);
2106 Matrix A is serialized as an array with 2x3 values:
2108 <SOAP-ENC:Array SOAP-ENC:arrayType="xsd:double[2,3]" ...>
2117 ### XSD hexBinary and base64Binary types {#toxsd10-2}
2119 A special case of a one-dimensional array is used to define xsd:hexBinary and
2120 xsd:base64Binary types when the pointer type is `unsigned char`:
2122 class xsd__hexBinary
2125 unsigned char *__ptr; // points to raw binary data
2126 int __size; // size of data
2131 class xsd__base64Binary
2134 unsigned char *__ptr; // points to raw binary data
2135 int __size; // size of data
2138 ### MIME/MTOM attachment binary types {#toxsd10-3}
2140 A class or struct with a binary content layout can be extended to support
2141 MIME/MTOM (and older DIME) attachments, such as in xop:Include elements:
2143 //gsoap xop schema import: http://www.w3.org/2004/08/xop/include
2147 unsigned char *__ptr; // points to raw binary data
2148 int __size; // size of data
2149 char *id; // NULL to generate an id, or set to a unique UUID
2150 char *type; // MIME type of the data
2151 char *options; // optional description of MIME attachment
2154 Attachments are beyond the scope of this document and we refer to the gSOAP
2155 user guide for more details.
2157 ### Wrapper class/struct for simpleContent {#toxsd10-4}
2159 A class or struct with the following layout is a complexType that wraps
2168 The type `T` is a primitive type (`bool`, `enum`, `time_t`, numeric and string
2169 types), `xsd__hexBinary`, `xsd__base64Binary`, and custom serializers, such as
2172 This maps to a complexType with simpleContent in the soapcpp2-generated schema:
2174 <complexType name="simple">
2176 <extension base="T"/>
2180 A wrapper class/struct may include any number of attributes declared with `@`.
2183 Serialization rules {#rules}
2186 A presentation on XML data bindings is not complete without discussing the
2187 serialization rules that put your data in XML on the wire.
2189 There are several options to choose from to serialize data in XML. The choice
2190 depends on the use of the SOAP protocol or if SOAP is not required. The wsdl2h
2191 tool automates this for you by taking the WSDL transport bindings into account
2192 when generating the service functions in C and C++ that use SOAP or REST.
2194 The gSOAP tools are not limited to SOAP. The tools implement generic XML data
2195 bindings for SOAP, REST, and other uses of XML. So you can read and write XML
2196 using the serializing [Operations on classes and structs](#toxsd9-11).
2198 The following sections briefly explain the serialization rules with respect to
2199 the SOAP protocol for XML Web services. A basic understanding of the SOAP
2200 protocol is useful when developing client and server applications that must
2201 interoperate with other SOAP applications.
2203 SOAP/REST Web service client and service operations are represented as
2204 functions in our gSOAP header file for soapcpp2. The soapcpp2 tool will
2205 translate these function to client-side service invocation calls and
2206 server-side service operation dispatchers.
2208 A discussion of SOAP clients and servers is beyond the scope of this document.
2209 However, the SOAP options discussed here also apply to SOAP client and server
2213 SOAP document versus rpc style {#doc-rpc}
2214 ------------------------------
2216 The `wsdl:binding/soap:binding/@style` attribute in the wsdl:binding section of
2217 a WSDL is either "document" or "rpc". The "rpc" style refers to SOAP RPC
2218 (Remote Procedure Call), which is more restrictive than the "document" style by
2219 requiring one XML element in the SOAP Body to act as the procedure name with
2220 XML subelements as its parameters.
2222 For example, the following directives in the gSOAP header file for soapcpp2
2223 declare that `DBupdate` is a SOAP RPC encoding service method:
2225 //gsoap ns service namespace: urn:DB
2226 //gsoap ns service method-protocol: DBupdate SOAP
2227 //gsoap ns service method-style: DBupdate rpc
2228 int ns__DBupdate(...);
2230 The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with
2231 one element representing the operation with the parameters as subelements:
2234 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
2235 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
2236 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2237 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
2244 </SOAP-ENV:Envelope>
2246 The "document" style puts no restrictions on the SOAP Body content. However, we
2247 recommend that the first element's tag name in the SOAP Body should be unique
2248 to each type of operation, so that the receiver can dispatch the operation
2249 based on
this element
's tag name. Alternatively, the HTTP URL path can be used
2250 to specify the operation, or the HTTP action header can be used to dispatch
2251 operations automatically on the server side (soapcpp2 options -a and -A).
2254 SOAP literal versus encoding {#lit-enc}
2255 ----------------------------
2257 The `wsdl:operation/soap:body/@use` attribute in the wsdl:binding section of a
2258 WSDL is either "literal" or "encoded". The "encoded" use refers to the SOAP
2259 encoding rules that support id-ref multi-referenced elements to serialize
2262 SOAP encoding is very useful if the data internally forms a graph (including
2263 cycles) and we want the graph to be serialized in XML in a format that ensures
2264 that its structure is preserved. In that case, SOAP 1.2 encoding is the best
2267 SOAP encoding also adds encoding rules for [SOAP arrays](toxsd10) to serialize
2268 multi-dimensional arrays. The use of XML attributes to exchange XML data in
2269 SOAP encoding is not permitted. The only attributes permitted are the standard
2270 XSD attributes, SOAP encoding attributes (such as for arrays), and id-ref.
2272 For example, the following directives in the gSOAP header file for soapcpp2
2273 declare that `DBupdate` is a SOAP RPC encoding service method:
2275 //gsoap ns service namespace: urn:DB
2276 //gsoap ns service method-protocol: DBupdate SOAP
2277 //gsoap ns service method-style: DBupdate rpc
2278 //gsoap ns service method-encoding: DBupdate encoded
2279 int ns__DBupdate(...);
2281 The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with
2282 an encodingStyle attribute for SOAP 1.1 encoding and an element representing the
2283 operation with parameters that are SOAP 1.1 encoded:
2286 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
2287 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
2288 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2289 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
2291 <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
2293 <records SOAP-ENC:arrayType="ns:record[3]">
2296 <SSN>1234567890</SSN>
2300 <SSN>1987654320</SSN>
2304 <SSN>2345678901</SSN>
2308 <id id="_1" xsi:type="xsd:string">Joe</id>
2310 </SOAP-ENV:Envelope>
2312 Note that the name "Joe" is shared by two records and the string is referenced
2313 by SOAP 1.1 href and id attributes.
2315 While gSOAP only introduces multi-referenced elements in the payload when they
2316 are actually multi-referenced in the data graph, other SOAP applications may
2317 render multi-referenced elements more aggressively. The example could also be
2321 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
2322 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
2323 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2324 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
2326 <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
2328 <records SOAP-ENC:arrayType="ns:record[3]">
2334 <id id="id1" xsi:type="ns:record">
2336 <SSN>1234567890</SSN>
2338 <id id="id2" xsi:type="ns:record">
2340 <SSN>1987654320</SSN>
2342 <id id="id3" xsi:type="ns:record">
2344 <SSN>2345678901</SSN>
2346 <id id="id4" xsi:type="xsd:string">Joe</id>
2347 <id id="id5" xsi:type="xsd:string">Jane</id>
2349 </SOAP-ENV:Envelope>
2351 SOAP 1.2 encoding is cleaner and produces more accurate XML encodings of data
2352 graphs by setting the id attribute on the element that is referenced:
2355 xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
2356 xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
2357 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2358 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
2361 <ns:DBupdate SOAP-ENV:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
2362 <records SOAP-ENC:itemType="ns:record" SOAP-ENC:arraySize="3">
2364 <name SOAP-ENC:id="_1">Joe</name>
2365 <SSN>1234567890</SSN>
2369 <SSN>1987654320</SSN>
2372 <name SOAP-ENC:ref="_1"/>
2373 <SSN>2345678901</SSN>
2378 </SOAP-ENV:Envelope>
2380 @note Some SOAP 1.2 applications consider the namespace `SOAP-ENC` of
2381 `SOAP-ENC:id` and `SOAP-ENC:ref` optional. The gSOAP SOAP 1.2 encoding
2382 serialization follows the 2007 standard, while accepting unqualified id and
2385 To remove all rendered id-ref multi-referenced elements in gSOAP, use the
2386 `SOAP_XML_TREE` flag to initialize the gSOAP engine context.
2388 Some XML validation rules are turned off with SOAP encoding, because of the
2389 presence of additional attributes, such as id and ref/href, SOAP arrays with
2390 arbitrary element tags for array elements, and the occurrence of additional
2391 multi-ref elements in the SOAP 1.1 Body.
2393 The use of "literal" puts no restrictions on the XML in the SOAP Body. Full
2394 XML validation is possible, which can be enabled with the `SOAP_XML_STRICT`
2395 flag to initialize the gSOAP engine context. However, data graphs will be
2396 serialized as trees and cycles in the data will be cut from the XML rendition.
2399 SOAP 1.1 versus SOAP 1.2 {#soap}
2400 ------------------------
2402 There are two SOAP protocol versions: 1.1 and 1.2. The gSOAP tools can switch
2403 between the two versions seamlessly. You can declare the default SOAP version
2404 for a service operation as follows:
2406 //gsoap ns service method-protocol: DBupdate SOAP1.2
2408 The gSOAP soapcpp2 auto-generates client and server code. At the client side,
2409 this operation sends data with SOAP 1.2 but accepts responses also in SOAP 1.1.
2410 At the server side, this operation accepts requests in SOAP 1.1 and 1.2 and
2411 will return responses in the same SOAP version.
2413 As we discussed in the previous section, the SOAP 1.2 protocol has a cleaner
2414 multi-referenced element serialization format that greatly enhances the
2415 accuracy of data graph serialization with SOAP RPC encoding and is therefore
2418 The SOAP 1.2 protocol default can also be set by importing and loading
2419 `gsoap/import/soap12.h`:
2424 Non-SOAP XML serialization {#non-soap}
2425 --------------------------
2427 You can serialize data that is stored on the heap, on the stack (locals), and
2428 static data as long as the serializable (i.e. non-transient) members are
2429 properly initialized and pointers in the structures are either NULL or point to
2430 valid structures. Deserialized data is put on the heap and managed by the
2431 gSOAP engine context `struct soap`, see also [Memory management](#memory).
2433 You can read and write XML directly to a file or stream with the serializing
2434 [Operations on classes and structs](#toxsd9-11).
2436 To define and use XML Web service client and service operations, we can declare
2437 these operations in our gSOAP header file for soapcpp2 as functions that
2438 soapcpp2 will translate in client-side service invocation calls and server-side
2439 service operation dispatchers. These functions are auto-generated by wsdl2h
2440 from WSDLs. Note that XSDs do not include service definitions.
2442 The REST operations POST, GET, and PUT are declared with gSOAP directives in
2443 the gSOAP header file for soapcpp2. For example, a REST POST operation is
2444 declared as follows:
2446 //gsoap ns service namespace: urn:DB
2447 //gsoap ns service method-protocol: DBupdate POST
2448 int ns__DBupdate(...);
2450 There is no SOAP Envelope and no SOAP Body in the payload for `DBupdate`. Also
2451 the XML serialization rules are identical to SOAP document/literal. The XML
2452 payload only has the operation name as an element with its parameters
2453 serialized as subelements:
2455 <ns:DBupdate xmln:ns="urn:DB" ...>
2459 To force id-ref serialization with REST similar to SOAP 1.2 multi-reference
2460 encoding, use the `SOAP_XML_GRAPH` flag to initialize the gSOAP engine context.
2461 The XML serialization includes id and ref attributes for multi-referenced
2462 elements as follows:
2464 <ns:DBupdate xmln:ns="urn:DB" ...>
2467 <name id="_1">Joe</name>
2468 <SSN>1234567890</SSN>
2472 <SSN>1987654320</SSN>
2476 <SSN>2345678901</SSN>
2482 Memory management {#memory}
2485 Memory management with the `soap` context enables us to allocate data in
2486 context-managed heap space that can be collectively deleted. All deserialized
2487 data is placed on the context-managed heap by the gSOAP engine.
2490 Memory management in C {#memory1}
2491 ----------------------
2493 In C (wsdl2h option `-c` and soapcpp2 option `-c`), the gSOAP engine allocates
2494 data on a context-managed heap with:
2496 - `void *soap_malloc(struct soap*, size_t len)`.
2498 The `soap_malloc` function is a wrapper around `malloc`, but which also allows
2499 the `struct soap` context to track all heap allocations for collective deletion
2500 with `soap_end(soap)`:
2505 struct soap *soap = soap_new(); // new context
2507 struct ns__record *record = soap_malloc(soap, sizeof(struct ns__record));
2508 soap_default_ns__record(soap, record);
2510 soap_destroy(soap); // only for C++, see section on C++ below
2511 soap_end(soap); // delete record and all other heap allocations
2512 soap_free(soap); // delete context
2514 The soapcpp2 auto-generated deserializers in C use `soap_malloc` to allocate
2515 and populate deserialized structures, which are managed by the context for
2516 collective deletion.
2518 To make `char*` and `wchar_t*` string copies to the context-managed heap, we
2519 can use the functions:
2521 - `char *soap_strdup(struct soap*, const char*)` and
2522 - `wchar_t *soap_wstrdup(struct soap*, const wchar_t*)`.
2524 We use the soapcpp2 auto-generated `soap_dup_T` functions to duplicate data
2525 into another context (this requires soapcpp2 option `-Ec` to generate), here
2526 shown for C with the second argument `dst` NULL because we want to allocate a
2527 new managed structure:
2529 struct soap *other_soap = soap_new(); // another context
2530 struct ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
2532 soap_destroy(other_soap); // only for C++, see section on C++ below
2533 soap_end(other_soap); // delete other_record and all of its deep data
2534 soap_free(other_soap); // delete context
2536 Note that the only reason to use another context and not to use the primary
2537 context is when the primary context must be destroyed together with all of the
2538 objects it manages while some of the objects must be kept alive. If the objects
2539 that are kept alive contain deep cycles then this is the only option we have,
2540 because deep copy with a managing context detects and preserves these
2541 cycles unless the `SOAP_XML_TREE` flag is used with the context:
2543 struct soap *other_soap = soap_new1(SOAP_XML_TREE); // another context
2544 struct ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
2546 The resulting deep copy will be a full copy of the source data structure as a
2547 tree without co-referenced data (i.e. no digraph) and without cycles. Cycles
2548 are pruned and (one of the) pointers that forms a cycle is repaced by NULL.
2550 We can also deep copy into unmanaged space and use the auto-generated
2551 `soap_del_T()` function (requires soapcpp2 option `-Ed` to generate) to delete
2552 it later, but we MUST NOT do this for any data that we suspect has deep cycles:
2554 struct ns__record *other_record = soap_dup_ns__record(NULL, NULL, record);
2556 soap_del_ns__record(other_record); // deep delete record data members
2557 free(other_record); // delete the record
2559 Cycles in the data structure will lead to non-termination when making unmanaged
2560 deep copies. Consider for example:
2569 Our code to populate a structure with a mutual spouse relationship:
2571 struct soap *soap = soap_new();
2573 struct ns__record pers1, pers2;
2574 soap_default_ns__record(soap, &pers1);
2575 soap_default_ns__record(soap, &pers2);
2576 pers1.name = "Joe"; // OK to serialize static data
2577 pers1.SSN = 1234567890;
2578 pers1.spouse = &pers2;
2579 pers2.name = soap_strdup(soap, "Jane"); // allocates and copies a string
2580 pers2.SSN = 1987654320;
2581 pers2.spouse = &pers1;
2583 struct ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
2584 struct ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
2585 soap_set_mode(soap, SOAP_XML_TREE);
2586 struct ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
2588 As we can see, the gSOAP serializer can serialize any heap, stack, or static
2589 allocated data, such as in our code above. So we can serialize the
2590 stack-allocated `pers1` record as follows:
2592 soap->sendfd = fopen("record.xml", "w");
2593 soap_set_mode(soap, SOAP_XML_GRAPH); // support id-ref w/o requiring SOAP
2594 soap_clr_mode(soap, SOAP_XML_TREE); // if set, clear
2595 soap_write_ns__record(soap, &pers1);
2596 fclose(soap->sendfd);
2597 soap->sendfd = NULL;
2599 which produces an XML document record.xml that is similar to:
2601 <ns:record xmlns:ns="urn:types" id="Joe">
2603 <SSN>1234567890</SSN>
2606 <SSN>1987654320</SSN>
2607 <spouse ref="#Joe"/>
2611 Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph
2612 leads to the same non-termination problem when we later try to copy the data
2613 into unmanaged space:
2615 struct soap *soap = soap_new1(SOAP_XML_GRAPH); // support id-ref w/o SOAP
2617 struct ns__record pers1;
2618 soap->recvfd = fopen("record.xml", "r");
2619 soap_read_ns__record(soap, &pers1);
2620 fclose(soap->recvfd);
2621 soap->recvfd = NULL;
2623 struct ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1); // BAD
2624 struct ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1); // OK
2625 soap_set_mode(soap, SOAP_XML_TREE);
2626 struct ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1); // OK
2628 Copying data with `soap_dup_T(soap)` into managed space is always safe. Copying
2629 into unmanaged space requires diligence. But deleting unmanaged data is easy
2630 with `soap_del_T()`.
2632 We can also use `soap_del_T()` to delete structures that we created in C, but
2633 only if these structures are created with `malloc` and do NOT contain pointers
2634 to stack and static data.
2637 Memory management in C++ {#memory2}
2638 ------------------------
2640 In C++, the gSOAP engine allocates data on a managed heap using a combination
2641 of `void *soap_malloc(struct soap*, size_t len)` and `soap_new_T()`, where `T`
2642 is the name of a class, struct, or class template (container or smart pointer).
2643 Heap allocation is tracked by the `struct soap` context for collective
2644 deletion with `soap_destroy(soap)` and `soap_end(soap)`.
2646 Only structs, classes, and class templates are allocated with `new` via
2647 `soap_new_T(struct soap*)` and mass-deleted with `soap_destroy(soap)`.
2649 There are four variations of `soap_new_T` for class/struct/template type `T`
2650 that soapcpp2 auto-generates to create instances on a context-managed heap:
2652 - `T * soap_new_T(struct soap*)` returns a new instance of `T` with default data
2653 member initializations that are set with the soapcpp2 auto-generated `void
2654 T::soap_default(struct soap*)` method), but ONLY IF the soapcpp2
2655 auto-generated default constructor is used that invokes `soap_default()` and
2656 was not replaced by a user-defined default constructor.
2658 - `T * soap_new_T(struct soap*, int n)` returns an array of `n` new instances of
2659 `T`. Similar to the above, instances are initialized.
2661 - `T * soap_new_req_T(struct soap*, ...)` returns a new instance of `T` and sets
2662 the required data members to the values specified in `...`. The required data
2663 members are those with nonzero minOccurs, see the subsections on
2664 [(Smart) pointer members and their occurrence constraints](#toxsd9-6) and
2665 [Container members and their occurrence constraints](#toxsd9-7).
2667 - `T * soap_new_set_T(struct soap*, ...)` returns a new instance of `T` and sets
2668 the public/serializable data members to the values specified in `...`.
2670 The above functions can be invoked with a NULL `soap` context, but we will be
2671 responsible to use `delete T` to remove this instance from the unmanaged heap.
2673 Primitive types and arrays of these are allocated with `soap_malloc` by the
2674 gSOAP engine. As we stated above, all types except for classes, structs, class
2675 templates (containers and smart pointers) are allocated with `soap_malloc` for
2676 reasons of efficiency.
2678 We can use a C++ template to simplify the managed allocation and initialization
2679 of primitive values as follows (this is for primitive types only, because we
2680 should allocate structs and classes with `soap_new_T`):
2683 T * soap_make(struct soap *soap, T val)
2685 T *p = (T*)soap_malloc(soap, sizeof(T));
2686 if (p) // out of memory? Can also guard with assert(p != NULL) or throw an error
2691 For example, assuming we have the following class:
2696 std::string name; // required name
2697 uint64_t *SSN; // optional SSN
2698 ns__record *spouse; // optional spouse
2701 We can instantiate a record by using the auto-generated
2702 `soap_new_set_ns__record` and our `soap_make` to create a SSN value on the
2705 soap *soap = soap_new(); // new context
2707 ns__record *record = soap_new_set_ns__record(
2710 soap_make<uint64_t>(soap, 1234567890LL),
2713 soap_destroy(soap); // delete record and all other managed instances
2714 soap_end(soap); // delete managed soap_malloc'ed heap data
2717 Note however that the gSOAP serializer can serialize any heap, stack, or
static
2718 allocated data. So we can also create a
new record as follows:
2720 uint64_t SSN = 1234567890LL;
2721 ns__record *record = soap_new_set_ns__record(soap,
"Joe", &SSN, NULL);
2723 which will be fine to serialize
this record as
long as the local `SSN`
2724 stack-allocated value remains in scope when invoking the serializer and/or
2725 using `record`. It does not matter
if `soap_destroy` and `soap_end` are called
2726 beyond the scope of `SSN`.
2728 To facilitate our
class methods to access the managing context, we can add a
2729 soap context pointer to a class/struct:
2739 The context is
set when invoking `soap_new_T` (and similar) with a non-NULL
2742 We use the soapcpp2
auto-generated `soap_dup_T` functions to duplicate data
2743 into another context (
this requires soapcpp2 option `-Ec` to generate), here
2744 shown
for C++ with the second argument `dst` NULL because we want to allocate a
2747 soap *other_soap = soap_new();
2748 ns__record *other_record = soap_dup_ns__record(other_soap, NULL, record);
2750 soap_destroy(other_soap);
2751 soap_end(other_soap);
2752 soap_free(other_soap);
2754 To duplicate base and derived instances when a base
class pointer or reference
2755 is provided, use the auto-generated method `T * T::soap_dup(
struct soap*)`:
2757 soap *other_soap = soap_new();
2758 ns__record *other_record = record->soap_dup(other_soap);
2760 soap_destroy(other_soap);
2761 soap_end(other_soap);
2762 soap_free(other_soap);
2764 Note that the only reason to use another context and not to use the primary
2765 context is when the primary context must be destroyed together with all of the
2766 objects it manages
while some of the objects must be kept alive. If the objects
2767 that are kept alive contain deep cycles then
this is the only option we have,
2768 because deep copy with a managing context detects and preserves these
2769 cycles unless the `SOAP_XML_TREE` flag is used with the context:
2771 soap *other_soap = soap_new1(SOAP_XML_TREE);
2772 ns__record *other_record = record->soap_dup(other_soap);
2774 The resulting deep copy will be a full copy of the source data structure as a
2775 tree without co-referenced data (i.e. no digraph) and without cycles. Cycles
2776 are pruned and (one of the) pointers that forms a cycle is repaced by NULL.
2778 We can also deep copy into unmanaged space and use the
auto-generated
2779 `soap_del_T()`
function or the `T::soap_del()` method (requires soapcpp2 option
2780 `-Ed` to generate) to
delete it later, but we MUST NOT
do this for any data
2781 that we suspect has deep cycles:
2783 ns__record *other_record = record->soap_dup(NULL);
2785 other_record->soap_del();
2786 delete other_record;
2788 Cycles in the data structure will lead to non-termination when making unmanaged
2789 deep copies. Consider
for example:
2798 Our code to populate a structure with a mutual spouse relationship:
2800 soap *soap = soap_new();
2802 ns__record pers1, pers2;
2804 pers1.SSN = 1234567890;
2805 pers1.spouse = &pers2;
2806 pers2.name =
"Jane";
2807 pers2.SSN = 1987654320;
2808 pers2.spouse = &pers1;
2810 ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1);
2811 ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1);
2812 soap_set_mode(soap, SOAP_XML_TREE);
2813 ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1);
2815 Note that the gSOAP serializer can serialize any heap, stack, or
static
2816 allocated data, such as in our code above. So we can serialize the
2817 stack-allocated `pers1` record as follows:
2819 soap->sendfd = fopen(
"record.xml",
"w");
2820 soap_set_mode(soap, SOAP_XML_GRAPH);
2821 soap_clr_mode(soap, SOAP_XML_TREE);
2822 soap_write_ns__record(soap, &pers1);
2823 fclose(soap->sendfd);
2824 soap->sendfd = NULL;
2826 which produces an XML document record.xml that is similar to:
2828 <ns:record xmlns:ns=
"urn:types" id=
"Joe">
2830 <SSN>1234567890</SSN>
2833 <SSN>1987654320</SSN>
2834 <spouse ref=
"#Joe"/>
2838 Deserialization of an XML document with a SOAP 1.1/1.2 encoded
id-ref graph
2839 leads to the same non-termination problem when we later
try to copy the data
2840 into unmanaged space:
2842 soap *soap = soap_new1(SOAP_XML_GRAPH);
2845 soap->recvfd = fopen(
"record.xml",
"r");
2846 soap_read_ns__record(soap, &pers1);
2847 fclose(soap->recvfd);
2848 soap->recvfd = NULL;
2850 ns__record *pers3 = soap_dup_ns__record(NULL, NULL, &pers1);
2851 ns__record *pers4 = soap_dup_ns__record(soap, NULL, &pers1);
2852 soap_set_mode(soap, SOAP_XML_TREE);
2853 ns__record *pers5 = soap_dup_ns__record(soap, NULL, &pers1);
2855 Copying data with `soap_dup_T(soap)` into managed space is always safe. Copying
2856 into unmanaged space requires diligence. But deleting unmanaged data is easy
2857 with `soap_del_T()`.
2859 We can also use `soap_del_T()` to
delete structures in C++, but only
if these
2860 structures are created with `
new` (and `
new []`
for arrays when applicable)
for
2861 classes, structs, and
class templates and with `malloc`
for anything
else, and
2862 the structures
do NOT contain pointers to stack and
static data.
2864 Features and limitations {#features}
2865 ========================
2867 In general, to use the generated code:
2869 - Make sure to `#include
"soapH.h"` in your code and also define a
namespace
2870 table or `#include
"ns.nsmap"` with the generated table, where `ns` is the
2871 namespace prefix for services.
2873 - Use soapcpp2 option -j (C++ only) to generate C++ proxy and service objects.
2874 The auto-generated files include documented inferfaces. Compile with
2875 soapC.cpp and link with -lgsoap++, or alternatively compile stdsoap2.cpp.
2877 - Without soapcpp2 option -j: client-side uses the auto-generated
2878 soapClient.cpp and soapC.cpp (or C versions of those). Compile and link with
2879 -lgsoap++ (-lgsoap for C), or alternatively compile stdsoap2.cpp
2882 - Without soapcpp2 option -j: server-side uses the auto-generated
2883 soapServer.cpp and soapC.cpp (or C versions of those). Compile and link with
2884 -lgsoap++ (-lgsoap for C), or alternatively compile stdsoap2.cpp (stdsoap2.c
2887 - Use `soap_new()` or `soap_new1(int flags)` to allocate and initialize a
2888 heap-allocated context with or without flags. Delete this context with
2889 `soap_free(struct soap*)`, but only after `soap_destroy(struct soap*)` and
2890 `soap_end(struct soap*)`.
2892 - Use `soap_init(struct *soap)` or `soap_init1(struct soap*, int flags)` to
2893 initialize a stack-allocated context with or without flags. End the use of
2894 this context with `soap_done(struct soap*)`, but only after
2895 `soap_destroy(struct soap*)` and `soap_end(struct soap*)`.
2897 There are several context initialization flags and context mode flags to
2898 control XML serialization at runtime:
2900 - `SOAP_C_UTFSTRING`: enables all `std::string` and `char*` strings to
2901 contain UTF-8 content. This option is recommended.
2903 - `SOAP_XML_STRICT`: strictly validates XML while deserializing. Should not be
2904 used together with SOAP 1.1/1.2 encoding style of messaging. Use soapcpp2
2905 option `-s` to hard code `SOAP_XML_STRICT` in the generated serializers. Not
2906 recommended with SOAP 1.1/1.2 encoding style messaging.
2908 - `SOAP_XML_INDENT`: produces indented XML.
2910 - `SOAP_XML_CANONICAL`: c14n canonocalization, removes unused `xmlns` bindings
2911 and adds them to appropriate places by applying c14n normalization rules.
2912 Should not be used together with SOAP 1.1/1.2 encoding style messaging.
2914 - `SOAP_XML_TREE`: write tree XML without id-ref, while pruning data structure
2915 cycles to prevent nontermination of the serializer for cyclic structures.
2917 - `SOAP_XML_GRAPH`: write graph (digraph and cyclic graphs with shared pointers
2918 to objects) using id-ref attributes. That is, XML with SOAP multi-ref
2919 encoded id-ref elements. This is a structure-preserving serialization format,
2920 because co-referenced data and also cyclic relations are accurately represented.
2922 - `SOAP_XML_DEFAULTNS`: uses xmlns default bindings, assuming that the schema
2923 element form is "qualified" by default (be warned if it is not!).
2925 - `SOAP_XML_NOTYPE`: removes all xsi:type attribuation. This option is usually
2926 not needed unless the receiver rejects all xs:type attributes. This option
2927 may affect the quality of the deserializer, which relies on xsi:type
2928 attributes to distinguish base class instances from derived class instances
2929 transported in the XML payloads.
2931 Additional notes with respect to the wsdl2h and soapcpp2 tools:
2933 - Nested classes, structs, and unions in a gSOAP header file are unnested by
2936 - Use `#import "file.h"` instead of `#include` to import other header files in
2937 a gSOAP header file for soapcpp2. The `#include` and `#define` directives are
2938 accepted, but are moved to the very start of the generated code for the C/C++
2939 compiler to include before all generated definitions. You should use
2940 `#include` in combinatio with "volatile" types and to ensure transient
2941 (incomplete) types are declared when these are used in the gSOAP header file.
2943 - To remove any SOAP-specific bindings, use soapcpp2 option `-0`.
2945 - A gSOAP header file for soapcpp2 should not include any code statements, only
2946 data type declarations. This includes constructor initialization lists that are
2947 not permitted. Use member initializations instead.
2949 - C++ namespaces are supported. Use wsdl2h option `-qname`. Or add a `namespace
2950 name { ... }` to the header file, but the `{ ... }` MUST cover the entire
2951 header file content from begin to end.
2953 - Optional DOM support can be used to store mixed content or literal XML
2954 content. Otherwise, mixed content may be lost. Use wsdl2h option `-d`
for
2955 DOM support and compile and link with `dom.c` or `dom.cpp`.
2958 Removing SOAP namespaces from XML payloads {#nsmap}
2959 ==========================================
2961 The soapcpp2 tool generates a `.nsmap` file that includes two bindings
for SOAP
2962 namespaces. We can
remove all SOAP namespaces (and SOAP processing logic) with
2963 soapcpp2 option `-0` or by simply setting the two entries to NULL:
2965 struct Namespace namespaces[] =
2967 {
"SOAP-ENV", NULL, NULL, NULL},
2968 {
"SOAP-ENC", NULL, NULL, NULL},
2971 Note that once the `.nsmap` is generated, we can copy-paste the content into
2972 our project code. However,
if we rerun wsdl2h on updated WSDL/XSD files or
2973 `typemap.dat` declarations then we need to use the updated table.
2975 In cases that no XML namespaces are used at all,
for example with
2976 [XML-RPC](www.genivia.com/doc/xml-rpc-json/html), you may use an empty
2979 struct Namespace namespaces[] = {{NULL,NULL,NULL,NULL}};
2981 However, beware that any built-in xsi attributes that are rendered will lack
2982 the proper
namespace binding. At least we suggest to use `SOAP_XML_NOTYPE` for
2985 Examples {#examples}
2988 Select the project files below to peruse the source code examples.
2994 - `address.xsd` Address book schema
2995 - `address.cpp` Address book app (reads/writes address.xml file)
2996 - `addresstypemap.dat` Schema
namespace prefix name preference for wsdl2h
2997 - `graph.h`
Graph data binding (tree, digraph, cyclic graph)
2998 - `graph.cpp` Test graph serialization as tree, digraph, and cyclic
3004 - `address.h` gSOAP-specific data binding definitions from address.xsd
3005 - `addressStub.h` C++ data binding definitions
3006 - `addressH.h` Serializers
3007 - `addressC.cpp` Serializers
3008 - `address.xml` Address book data generated by address app
3009 - `graphStub.h` C++ data binding definitions
3010 - `graphH.h` Serializers
3011 - `graphC.cpp` Serializers
3012 - `
g.xsd` XSD schema with `
g:
Graph` complexType
3013 - `
g.nsmap` xmlns bindings namespace mapping table
3019 Building the AddressBook example:
3021 wsdl2h -
g -t addresstypemap.dat address.xsd
3022 soapcpp2 -0 -CS -I../../import -p address address.h
3023 c++ -I../.. address.cpp addressC.cpp -o address -lgsoap++
3025 Option `-
g` produces bindings for global (root) elements in addition to types.
3026 In this case the root element `a:address-book` is bound to `
_a__address_book`.
3027 The complexType `a:address` is bound to class `
a__address`, which is also the
3028 type of `
_a__address_book`. This option is not required, but allows you to use
3029 global element tag names when referring to their serializers, instead of their
3030 type name. Option `-0` removes the SOAP protocol. Options `-C` and `-S`
3031 removes client and server code generation. Option `-p` renames the output
3032 `soap` files to `address` files.
3034 See the `address.cpp` implementation and [Related Pages](pages.html).
3036 The `addresstypemap.dat` file specifies the XML namespace prefix for the
3039 # Bind the address book schema namespace to prefix 'a'
3041 a = "urn:address-book-example"
3043 # By default the xsd:dateTime schema type is translated to time_t
3044 # To map xsd:dateTime to struct tm, enable the following line:
3046 # xsd__dateTime = #import "../../custom/struct_tm.h"
3048 # ... and compile/link with custom/struct_tm.c
3050 The DOB field is a xsd:dateTime, which is bound to `time_t` by default. To
3051 change this to `struct tm`, enable the import of the `xsd__dateTime` custom
3052 serializer by uncommenting the definition of `xsd__dateTime` in
3053 `addresstypemap.dat`. Then change `soap_dateTime2s` to `soap_xsd__dateTime2s`
3056 Building the graph serialization example:
3058 soapcpp2 -CS -I../../import -p graph graph.h
3059 c++ -I../.. graph.cpp graphC.cpp -o graph -lgsoap++
3061 To compile without using the `libgsoap++` library: simply compile
3062 `stdsoap2.cpp` together with the above.
3068 To execute the AddressBook example:
3072 To execute the
Graph serialization example: