![]() |
Home | Documentation |
C and C++ XML Data Bindings
updated Fri Jan 17 2025 by Robert van Engelen
|
This article presents a detailed overview of the gSOAP XML data bindings for C and C++. The XML data bindings for C and C++ are extensively used with gSOAP Web services to serialize C and C++ data in XML as part of the SOAP/XML Web services payloads. Also REST XML with gSOAP relies on XML serialization of C and C++ data via XML data bindings.
The major advantage of XML data bindings is that your application data is always type safe in C and C++ by binding XML schema types to C/C++ types. So integers in XML are bound to C integers, strings in XML are bound to C or C++ strings, complex types in XML are bound to C structs or C++ classes, and so on. The structured data you create and accept will fit the data model and is static type safe. In other words, by leveraging strong typing in C/C++, your XML data meets XML schema validation requirements and satisfies XML interoperability requirements.
In fact, gSOAP data bindings are more powerful than simply representing C/C++ data in XML. The gSOAP tools implement true and tested structure-preserving serialization of C/C++ data in XML, including the serialization of cyclic graph structures with id-ref XML attributes. The gSOAP tools also generate routines for deep copying and deep deletion of C/C++ data structures to simplify memory management. In addition, C/C++ structures are deserialized into managed memory, managed by the gSOAP soap context.
At the end of this article two examples are given to illustrate the application of XML data bindings. The first simple example address.cpp shows how to use wsdl2h to bind an XML schema to C++. The C++ application reads and writes an XML file into and from a C++ "address book" data structure as a simple example. The C++ data structure is a vector of address objects. The second example graph.cpp shows how C++ data can be accurately serialized as a tree, digraph, and cyclic graph in XML. The digraph and cyclic graph serialization rules implement SOAP 1.1/1.2 multi-ref encoding with id-ref attributes to link elements through IDREF XML references, creating a an XML graph with pointers to XML nodes that preserves the structural integrity of the serialized C++ data.
These examples demonstrate XML data bindings only for relatively simple data structures and types. The gSOAP tools support more than just these type of structures to serialize in XML. There are practically no limits to the serialization of C and C++ data types in XML.
Also the support for XML schema (XSD) components is unlimited. The wsdl2h tool maps schemas to C and C++ using built-in intuitive mapping rules, while allowing the mappings to be customized using a typemap.dat file with mapping instructions for wsdl2h.
The information in this article is applicable to gSOAP 2.8.26 and greater that support C++11 features. However, C++11 is not required. The material and the examples in this article use plain C and C++, until the point where we introduce C++11 smart pointers and scoped enumerations. While most of the examples in this article are given in C++, the concepts also apply to C with the exception of containers, smart pointers, classes and their methods. None of these exceptions limit the use of the gSOAP tools for C in any way.
The data binding concepts described in this article were first envisioned in 1999 by Prof. Robert van Engelen at the Florida State University. An implementation was created in 2000, named "stub/skeleton compiler". The first articles on its successor version "gSOAP" appeared in 2002. The principle of mapping XSD components to C/C++ types and vice versa is now widely adopted in systems and programming languages, including Java web services and by C# WCF.
We continue to be committed to our goal to empower C/C++ developers with powerful autocoding tools for XML. Our commitment started in the very early days of SOAP by actively participating in SOAP interoperability testing, participating in the development and testing of the W3C XML Schema Patterns for Databinding Interoperability, and continues by contributing to the development of OASIS open standards in partnership with leading IT companies in the world.
🔝 "" "Back to table of contents"
The typographical conventions used by this document are:
The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC-2119.
🔝 "" "Back to table of contents"
To convert WSDL and XML schemas (XSD files) to code, we use the wsdl2h command on the command line (or command prompt), after opening a terminal. The wsdl2h command generates the data binding interface code that is saved to a special Web services and data bindings interface header file with extension .h that contains the WSDL service declarations and the data binding interface declarations in a familiar C/C++ format:
wsdl2h [options] -o file.h ... XSD and WSDL files ...
This command converts WSDL and XSD files to C++ (or pure C with wsdl2h -c) and saves the data binding interface to a interface header file file.h that uses familiar C/C++ syntax extended with //gsoap directives and annotations. Notational conventions are used in the data binding interface to declare serializable C/C++ types and functions for Web service operations.
The WSDL 1.1/2.0, SOAP 1.1/1.2, and XSD 1.0/1.1 standards are supported by the gSOAP tools. In addition, the most popular WS specifications are also supported, including WS-Addressing, WS-ReliableMessaging, WS-Discovery, WS-Security, WS-Policy, WS-SecurityPolicy, and WS-SecureConversation.
This article focusses mainly on XML data bindings. XML data bindings for C/C++ bind XML schema types to C/C++ types. So integers in XML are bound to C integers, strings in XML are bound to C or C++ strings, complex types in XML are bound to C structs or C++ classes, and so on.
A data binding is dual, meaning supporting a two way direction for development. Either you start with WSDLs and/or XML schemas that are mapped to equivalent C/C++ types, or you start with C/C++ types that are mapped to XSD types. Either way, the end result is that you can serialize C/C++ types in XML such that your XML is an instance of XML schema(s) and is validated against these schema(s).
This covers all of the following standard XSD components with their optional attributes and properties:
XSD component | attributes and properties |
---|---|
schema | targetNamespace, version, elementFormDefault, attributeFormDefault, defaultAttributes |
attribute | name, ref, type, use, default, fixed, form, targetNamespace, wsdl:arrayType |
element | name, ref, type, default, fixed, form, nillable, abstract, substitutionGroup, minOccurs, maxOccurs, targetNamespace |
simpleType | name |
complexType | name, abstract, mixed, defaultAttributesApply |
all | n/a |
choice | minOccurs, maxOccurs |
sequence | minOccurs, maxOccurs |
group | name, ref, minOccurs, maxOccurs |
attributeGroup | name, ref |
any | minOccurs, maxOccurs |
anyAttribute | n/a |
And also the following standard XSD directives are covered:
directive | description |
---|---|
import | Imports a schema into the importing schema for referencing |
include | Include schema component definitions into a schema |
override | Override by replacing schema component definitions |
redefine | Extend or restrict schema component definitions |
annotation | Annotates a component |
The XSD facets and their mappings to C/C++ are:
XSD facet | maps to |
---|---|
enumeration | enum |
simpleContent | class/struct wrapper with __item member |
complexContent | class/struct |
list | enum* bitmask (enum* enumerates a bitmask up to 64 bits) |
extension | class/struct inheritance/extension |
restriction | typedef and class/struct inheritance/redeclaration |
length | typedef with restricted content length annotation |
minLength | typedef with restricted content length annotation |
maxLength | typedef with restricted content length annotation |
minInclusive | typedef with numerical value range restriction annotation |
maxInclusive | typedef with numerical value range restriction annotation |
minExclusive | typedef with numerical value range restriction annotation |
maxExclusive | typedef with numerical value range restriction annotation |
precision | typedef with pattern annotation (pattern used for output, but input is not validated) |
scale | typedef with pattern annotation (pattern used for output, but input is not validated) |
totalDigits | typedef with pattern annotation (pattern used for output, but input is not validated) |
fractionDigits | typedef with pattern annotation (pattern used for output, but input is not validated) |
pattern | typedef with pattern annotation (define soap::fsvalidate callback to validate patterns) |
union | string with union of value |
All primitive XSD types are supported, including but not limited to the following XSD types:
XSD type | maps to |
---|---|
any/anyType | _XML string with literal XML content (or enable DOM with wsdl2h option -d) |
anyURI | string (i.e. char*, wchar_t*, std::string, std::wstring) |
string | string (i.e. char*, wchar_t*, std::string, std::wstring) |
boolean | bool (C++) or enum xsd__boolean (C) |
byte | char (i.e. int8_t) |
short | short (i.e. int16_t) |
int | int (i.e. int32_t) |
long | LONG64 (i.e. long long and int64_t) |
unsignedByte | unsigned char (i.e. uint8_t) |
unsignedShort | unsigned short (i.e. uint16_t) |
unsignedInt | unsigned int (i.e. uint32_t) |
unsignedLong | ULONG64 (i.e. unsigned long long and uint64_t) |
float | float |
double | double |
integer | string or #import "custom/int128.h" to use 128 bit xsd__integer |
decimal | string or #import "custom/long_double.h" to use long double |
precisionDecimal | string |
duration | string or #import "custom/duration.h" to use 64 bit xsd__duration |
dateTime | time_t or #import "custom/struct_tm.h" to use struct tm for xsd__dateTime |
time | string or #import "custom/long_time.h" to use 64 bit xsd__time |
date | string or #import "custom/struct_tm_date.h" to use struct tm for xsd__date |
hexBinary | special class/struct xsd__hexBinary |
base64Binary | special class/struct xsd__base64Binary |
QName | _QName string (URI normalization rules are applied) |
All other primitive XSD types not listed above are mapped to strings, by wsdl2h generating a typedef to string for these types. For example, xsd:token is bound to a C++ or C string:
This associates a compatible value space to the type with the appropriate XSD type name used by the soapcpp2-generated serializers.
It is possible to remap types by adding the appropriate mapping rules to typemap.dat as we will explain in more detail in the next section.
Imported custom serializers are intended to extend the C/C++ type bindings when the default binding to string is not satisfactory to your taste and if the target platform supports these C/C++ types. To add custom serializers to typemap.dat for wsdl2h, see adding custom serializers below.
🔝 "" "Back to table of contents"
Use a typemap.dat file to redefine namespace prefixes and to customize type bindings for the the generated header files produced by the wsdl2h tool. The typemap.dat is the default file processed by wsdl2h. Use wsdl2h -tfile.dat option -tfile.dat to specify a different mapping file file.dat.
Declarations in typemap.dat can be broken up over multiple lines by continuing on the next line by ending each line to be continued with a backslash \. The limit is 4095 characters per line, whether the line is broken up or not.
🔝 "" "Back to table of contents"
The wsdl2h tool generates C/C++ type declarations that use ns1, ns2, etc. as schema-binding URI prefixes. These default prefixes are generated somewhat arbitrarily for each schema targetNamespace URI, meaning that their ordering may change depending on the WSDL and XSD order of processing with wsdl2h.
Therefore, it is strongly recommended to declare your own prefix for each schema URI in typemap.dat to reduce maintaince effort of your code. This is more robust when anticipating possible changes of the schema(s) and/or the binding URI(s) and/or the tooling algorithms.
The first and foremost important thing to do is to define prefix-URI bindings for our C/C++ code by adding the following line(s) to our typemap.dat or make a copy of this file and add the line(s) that bind our choice of prefix name to each URI:
prefix = "URI"
For example, to use g as a prefix for the "urn:graph" XML namespace:
g = "urn:graph"
This produces g__name C/C++ type names that are bound to the "urn:graph" schema by association of g to the generated C/C++ types.
This means that <g:name xmlns:g="urn:graph"> is parsed as an instance of a g__name C/C++ type. Also <x:name xmlns:x="urn:graph"> parses as an instance of g__name, because the prefix x has the same URI value urn:graph. Prefixes in XML have local scopes (like variables in a block).
The first run of wsdl2h will reveal the XML namespace URIs, so you do not need to search WSDLs and XSD files for all of the target namespaces. Just copy them from the generated header file after the first run into typemap.dat for editing.
🔝 "" "Back to table of contents"
Custom C/C++ type bindings can be declared in typemap.dat to associate C/C++ types with specific schema types. These type bindings have four parts:
prefix__type = declaration | use | ptr-use
where
For example, to map xsd:duration to a long long (LONG64) type that holds millisecond duration values, we can use the custom serializer declared in gsoap/custom/duration.h by adding the following line to typemap.dat:
xsd__duration = #import "custom/duration.h"
Here, we omitted the second and third parts, because xsd__duration is the name that wsdl2h uses for this type in our generated code so we should leave the use part unspecified. The third part is omitted to let wsdl2h use xsd__duration * for pointers or std::shared_ptr<xsd__duration> if smart pointers are enabled.
To map xsd:string to wchar_t* wide strings for C source code output:
xsd__string = | wchar_t* | wchar_t*
For C++ we can use the std::wstring wide string:
xsd__string = | std::wstring
Note that the first part is empty, because these types do not require a declaration. A ptr-use part is also defined for wchar_t*, but this is actually needed because the wsdl2h tool recognizes that the use part wchar_t* is already a pointer. By contrast, when using 8-bit strings, it is recommended to use the SOAP_C_UTFSTRING flag to enable UTF-8 formatted strings.
When the ptr-use part is not specified, it will be auto-generated as pointer T* for use type T or std::shared_ptr<T> when the variable $POINTER = std::shared, see the special variables $CONTAINER, $POINTER, $SIZE and $OPTIONAL.
The declaration part need not be empty, for example if a type must be declared. For example:
xsd__string = typedef std::string mystring; | mystring | std::optional<mystring>
When a auto-generated declaration should be preserved but the use or ptr-use parts must be redefined, then we use an ellipsis for the declaration part:
prefix__type = ... | use | ptr-use
The ptr-use part is also useful to map schema polymorphic types to C types for example, where we need to be able to both handle a base type and its extensions as per schema extensibility. Say we have a base type called ns:base that is extended, then we can remap this to a C type that permits referening the extended types via a void* as follows:
ns__base = ... | int __type_base; void*
such that __type_base and void* will be used to (de)serialize any data type, including base and its derived types. The __type_base integer is set to a SOAP_TYPE_T value to indicate what type of data the void* pointer points to.
🔝 "" "Back to table of contents"
In the previous part we saw how a custom serializer is used to bind xsd:duration to a long long (LONG64 or int64_t) type to store millisecond duration values:
xsd__duration = #import "custom/duration.h"
The xsd__duration type is an alias of long long (LONG64 or int64_t).
While wsdl2h will use this binding declared in typemap.dat automatically, you will also need to compile gsoap/custom/duration.c. Each custom serializer has an interface header file to be imported into another interface header file that declares the custom type for soapcpp2 and a serializer implementation file written in C, which should be compiled with the application. You can compile these in C++ (rename files to .cpp if needed).
A custom serializer is declared in an interface header file for soapcpp2 using extern typedef. The typedef name declared is serializable, whereas the type on which it is based is not serializable. This declaration can be combined with volatile when the type should not be redeclared, see volatile classes and structs. For example, the custom serializer for struct tm is the type xsd__datetime declared as follows in gsoap/custom/struct_tm.h:
Another example is xsd__duration as a custom serializer for the C++11 type std::chrono::nanoseconds:
Next, we present all pre-defined custom serializers that are available to you.
🔝 "" "Back to table of contents"
The wsdl2h tool maps xsd:integer to a string by default. To map xsd:integer to the 128 bit big int type __int128_t:
xsd__integer = #import "custom/int128.h"
The xsd__integer type is an alias of __int128_t.
Other XSD integer types that are restrictions of xsd:integer, are xsd:nonNegativeInteger and xsd:nonPositiveInteger, which are further restricted by xsd:positiveInteger and xsd:negativeInteger. To bind these types to __int128_t add the following definitions to typemap.dat:
xsd__nonNegativeInteger = typedef xsd__integer xsd__nonNegativeInteger 0 : ; xsd__nonPositiveInteger = typedef xsd__integer xsd__nonPositiveInteger : 0 ; xsd__positiveInteger = typedef xsd__integer xsd__positiveInteger 1 : ; xsd__negativeInteger = typedef xsd__integer xsd__negativeInteger : -1 ;
Or simply uncomment these definitions in typemap.dat when you are using the latest gSOAP releases.
xsd__integer = typedef LONG64 xsd__integer;
After running wsdl2h and soapcpp2, compile gsoap/custom/int128.c with your project.
🔝 "" "Back to table of contents"
The wsdl2h tool maps xsd:decimal to a string by default. To map xsd:decimal to extended precision floating point:
xsd__decimal = #import "custom/long_double.h" | long double
By contrast to all other custom serializers, this serializer enables long double natively without requiring a new binding name (xsd__decimal is NOT defined).
If your system supports quadmath.h quadruple precision floating point __float128, you can map xsd:decimal to xsd__decimal that is an alias of __float128:
xsd__decimal = #import "custom/float128.h"
In the XML payload the special values INF, -INF, NaN represent plus or minus infinity and not-a-number, respectively.
After running wsdl2h and soapcpp2, compile gsoap/custom/long_double.c with your project.
🔝 "" "Back to table of contents"
The wsdl2h tool maps xsd:dateTime to time_t by default.
The trouble with time_t when represented as 32 bit long integers is that it is limited to dates between 1970 and 2038. A 64 bit time_t is safe to use if the target platform supports it, but lack of 64 bit time_t portability may still cause date range issues.
For this reason struct tm should be used to represent wider date ranges. This custom serializer avoids using date and time information in time_t. You get the raw date and time information. You only lose the day of the week information. It is always Sunday (tm_wday=0).
To map xsd:dateTime to xsd__dateTime which is an alias of struct tm:
xsd__dateTime = #import "custom/struct_tm.h"
If the limited date range of time_t is not a problem but you want to increase the time precision with fractional seconds, then we suggest to map xsd:dateTime to struct timeval:
xsd__dateTime = #import "custom/struct_timeval.h"
If the limited date range of time_t is not a problem but you want to use the C++11 time point type std::chrono::system_clock::time_point (which internally uses time_t):
xsd__dateTime = #import "custom/chrono_time_point.h"
Again, we should make sure that the dates will not exceed the date range when using the default time_t binding for xsd:dateTime or when binding xsd:dateTime to struct timeval or to std::chrono::system_clock::time_point. These are safe to use in applications that use xsd:dateTime to record date stamps within a given window. Otherwise, we recommend the struct tm custom serializer.
After running wsdl2h and soapcpp2, compile gsoap/custom/struct_tm.c with your project.
You could even map xsd:dateTime to a plain string (use char* with C and std::string with C++). For example:
xsd__dateTime = | char*
🔝 "" "Back to table of contents"
The wsdl2h tool maps xsd:date to a string by default. We can map xsd:date to struct tm:
xsd__date = #import "custom/struct_tm_date.h"
The xsd__date type is an alias of struct tm. The serializer ignores the time part and the deserializer only populates the date part of the struct, setting the time to 00:00:00. There is no unreasonable limit on the date range because the year field is stored as an integer (int).
After running wsdl2h and soapcpp2, compile gsoap/custom/struct_tm_date.c with your project.
🔝 "" "Back to table of contents"
The wsdl2h tool maps xsd:time to a string by default. We can map xsd:time to an unsigned long long (ULONG64 or uint64_t) integer with microsecond time precision:
xsd__time = #import "custom/long_time.h"
This type represents 00:00:00.000000 to 23:59:59.999999, from 0 to an upper bound of 86399999999. A microsecond resolution means that a 1 second increment requires an increment of 1000000 in the integer value. The serializer adds a UTC time zone.
After running wsdl2h and soapcpp2, compile gsoap/custom/long_time.c with your project.
🔝 "" "Back to table of contents"
The wsdl2h tool maps xsd:duration to a string by default, unless xsd:duration is mapped to a long long (LONG64 or int64_t) type with with millisecond (ms) time duration precision:
xsd__duration = #import "custom/duration.h"
The xsd__duration type is a 64 bit signed integer that can represent 106,751,991,167 days forwards (positive) and backwards (negative) in time in increments of 1 ms (1/1000 of a second).
Rescaling of the duration value by may be needed when adding the duration value to a time_t value, because time_t may or may not have a seconds resolution, depending on the platform and possible changes to time_t.
Rescaling is done automatically when you add a C++11 std::chrono::nanoseconds value to a std::chrono::system_clock::time_point value. To use std::chrono::nanoseconds as xsd:duration:
xsd__duration = #import "custom/chrono_duration.h"
This type can represent 384,307,168 days (2^63 nanoseconds) forwards and backwards in time in increments of 1 ns (1/1,000,000,000 of a second).
Certain observations with respect to receiving durations in years and months apply to both of these serializer decoders for xsd:duration.
After running wsdl2h and soapcpp2, compile gsoap/custom/duration.c with your project.
🔝 "" "Back to table of contents"
The gSOAP distribution includes several custom serializers for Qt types. Also Qt container classes are supported, see the special variables $CONTAINER, $POINTER, $SIZE and $OPTIONAL.
This feature requires gSOAP 2.8.34 or higher and Qt 4.8 or higher.
Each Qt custom serializer has an interface header file for soapcpp2 and a C++ implementation file to be compiled with your project.
Other Qt primitive types that are Qt typedefs of C/C++ types do not require a custom serializer.
🔝 "" "Back to table of contents"
To use Qt strings instead of C++ strings, add the following definition to typemap.dat:
xsd__string = #import "custom/qstring.h"
After running wsdl2h and soapcpp2, compile gsoap/custom/qstring.cpp with your project.
🔝 "" "Back to table of contents"
To use Qt byte arrays for xsd:base64Binary instead of the xsd__base64Binary class, add the following definition to typemap.dat:
xsd__base64Binary = #import "custom/qbytearray_base64.h"
After running wsdl2h and soapcpp2, compile gsoap/custom/qbytearray_base64.cpp with your project.
🔝 "" "Back to table of contents"
To use Qt byte arrays for xsd:hexBinary instead of the xsd__base64Binary class, add the following definition to typemap.dat:
xsd__hexBinary = #import "custom/qbytearray_hex.h"
After running wsdl2h and soapcpp2, compile gsoap/custom/qbytearray_hex.cpp with your project.
🔝 "" "Back to table of contents"
To use Qt QDateTime for xsd:dateTime, add the following definition to typemap.dat:
xsd__dateTime = #import "custom/datetime.h"
After running wsdl2h and soapcpp2, compile gsoap/custom/qdatetime.cpp with your project.
🔝 "" "Back to table of contents"
To use Qt QDate for xsd:date, add the following definition to typemap.dat:
xsd__date = #import "custom/qdate.h"
After running wsdl2h and soapcpp2, compile gsoap/custom/qdate.cpp with your project.
🔝 "" "Back to table of contents"
To use Qt QDate for xsd:time, add the following definition to typemap.dat:
xsd__time = #import "custom/qtime.h"
After running wsdl2h and soapcpp2, compile gsoap/custom/qtime.cpp with your project.
🔝 "" "Back to table of contents"
All generated classes and structs can be augmented with additional members such as methods, constructors and destructors, and private members:
prefix__type = $ member-declaration
For example, we can add method declarations and private members to a class, say ns__record as follows:
ns__record = $ ns__record(const ns__record &); // copy constructor ns__record = $ void print(); // a print method ns__record = $ private: int status; // a private member
Method declarations cannot include any code, because soapcpp2's input permits only type declarations, not code.
🔝 "" "Back to table of contents"
Type replacements can be given to replace one type entirely with another given type:
prefix__type1 == prefix__type2
This replaces all prefix__type1 by prefix__type2 in the wsdl2h output.
🔝 "" "Back to table of contents"
The typemap.dat $CONTAINER variable defines the container type to use in the wsdl2h-generated declarations for C++, which is std::vector by default. For example, to use std::list as the container in the wsdl2h-generated declarations we add the following line to typemap.dat:
$CONTAINER = std::list
Also a Qt container can be used instead of the default std::vector, for example QVector:
[ #include <QVector> ] $CONTAINER = QVector
To remove containers, use wsdl2h -s. This also removes std::string, but you can re-introduce std::string with xsd__string = | std::string in typemap.dat.
The typemap.dat $POINTER variable defines the smart pointer to use in the wsdl2h-generated declarations for C++, which replaces the use of * pointers. For example:
$POINTER = std::shared_ptr
Not all pointers in the generated output are replaced by smart pointers by wsdl2h, such as pointers as union members and pointers as struct/class members that point to arrays of values.
Alternatives to std::shared_ptr of the form NAMESPACE::shared_ptr can be assigned to $POINTER when the namespace NAMESPACE also implements NAMESPACE::make_shared and when the shared pointer class provides reset() andget() methods and the dereference operator. For example Boost boost::shared_ptr:
[ #include <boost/shared_ptr.hpp> ] $POINTER = boost::shared_ptr
The user-defined content between [ and ] ensures that we include the Boost header files that are needed to support boost::shared_ptr and boost::make_shared.
The variable $SIZE defines the type of array sizes, which is int by default. For example, to change array size types to size_t:
$SIZE = size_t
Permissible types are int and size_t. This variable does not affect the size of dynamic arrays, xsd__hexBinary and xsd__base64Binary types, which is always int.
When C++17 is enabled with wsdl2h and soapcpp2 option -c++17, you can also semi-automatically enable std::optional declarations with optional class and structure member variables. This means that std::optional is used instead of a (smart) pointer to make a member optional.
To enable std::optional with member variables that are primitive types, typedef, and enum automatically:
$OPTIONAL = std::optional
Local unnamed simpleType restrictions may not adopt the specified optional type and still use pointers instead. This limitation may be lifted in a future release.
🔝 "" "Back to table of contents"
Any other content to be generated by wsdl2h can be included in typemap.dat by enclosing it within brackets [ and ] anywhere in the typemap.dat file. Each of the two brackets must appear at the start of a new line.
For example, we can add an #import "wsa5.h" to the wsdl2h-generated output as follows:
[ #import "import/wsa5.h" ]
which emits the #import "import/wsa5.h" literally at the start of the wsdl2h-generated header file.
🔝 "" "Back to table of contents"
The soapcpp2 command generates the data binding implementation code from a data binding interface file.h:
soapcpp2 [options] file.h
where file.h is a interface header file that declares the XML data binding interface. The file.h is typically generated by wsdl2h, but you can also declare one yourself. If so, add //gsoap directives and declare in this file all our C/C++ types you want to serialize in XML.
You can also declare functions that will be converted to Web service operations by soapcpp2. Global function declarations define service operations, which are of the form:
where arg1, arg2, ..., argn are formal argument declarations of the input and result is a formal argument for the output, which must be a pointer or reference to the result object to be populated. More information on declaring and implementing service operation functions can be found in the gSOAP user guide.
🔝 "" "Back to table of contents"
The following C/C++ types are supported by soapcpp2 and mapped to XSD types and constructs. See the subsections below for more details or follow the links.
🔝 "" "Back to table of contents"
Boolean Type | Notes |
---|---|
bool | C++ bool |
enum xsd__boolean | C alternative to C++ bool with false_ and true_ |
🔝 "" "Back to table of contents"
Enumeration Type | Notes |
---|---|
enum | enumeration |
enum class | C++11 scoped enumeration, requires soapcpp2 -c++11 |
enum* | a bitmask that enumerates values 1, 2, 4, 8, ... |
enum* class | C++11 scoped enumeration bitmask, requires soapcpp2 -c++11 |
🔝 "" "Back to table of contents"
Numerical Type | Notes |
---|---|
char | byte |
short | 16 bit integer |
int | 32 bit integer |
long | 32 bit integer |
LONG64 | 64 bit integer |
xsd__integer | 128 bit integer, use #import "custom/int128.h" |
long long | same as LONG64 |
unsigned char | unsigned byte |
unsigned short | unsigned 16 bit integer |
unsigned int | unsigned 32 bit integer |
unsigned long | unsigned 32 bit integer |
ULONG64 | unsigned 64 bit integer |
unsigned long long | same as ULONG64 |
int8_t | same as char |
int16_t | same as short |
int32_t | same as int |
int64_t | same as LONG64 |
uint8_t | same as unsigned char |
uint16_t | same as unsigned short |
uint32_t | same as unsigned int |
uint64_t | same as ULONG64 |
size_t | transient type (not serializable) |
float | 32 bit float |
double | 64 bit float |
long double | extended precision float, use #import "custom/long_double.h" |
xsd__decimal | quadmath.h library 128 bit quadruple precision float, use #import "custom/float128.h" |
typedef | declares a type name, with optional value range and string length bounds |
🔝 "" "Back to table of contents"
String Type | Notes |
---|---|
char* | string (may contain UTF-8 with flag SOAP_C_UTFSTRING) |
wchar_t* | wide string |
std::string | C++ string (may contain UTF-8 with flag SOAP_C_UTFSTRING) |
std::wstring | C++ wide string |
char[N] | fixed-size string, requires soapcpp2 -b |
_QName | normalized QName content |
_XML | literal XML string content with wide characters in UTF-8 |
typedef | declares a new string type name, may restrict string length |
🔝 "" "Back to table of contents"
Date and Time Type | Notes |
---|---|
time_t | date and time point since epoch |
struct tm | date and time point, use #import "custom/struct_tm.h" |
struct tm | date point, use #import "custom/struct_tm_date.h" |
struct timeval | date and time point, use #import "custom/struct_timeval.h" |
unsigned long long | time point in microseconds, use #import "custom/long_time.h" |
std::chrono::system_clock::time_point | date and time point, use #import "custom/chrono_time_point.h" |
🔝 "" "Back to table of contents"
Time Duration Type | Notes |
---|---|
long long | duration in milliseconds, use #import "custom/duration.h" |
std::chrono::nanoseconds | duration in nanoseconds, use #import "custom/chrono_duration.h" |
🔝 "" "Back to table of contents"
Classes, Structs, and Members | Notes |
---|---|
class | C++ class with single inheritance only |
struct | C struct or C++ struct without inheritance |
std::shared_ptr<T> | C++11 smart shared pointer |
std::unique_ptr<T> | C++11 smart pointer |
std::auto_ptr<T> | C++ smart pointer |
std::deque<T> | use #import "import/stldeque.h" |
std::list<T> | use #import "import/stllist.h" |
std::vector<T> | use #import "import/stlvector.h" |
std::set<T> | use #import "import/stlset.h" |
template<T> class | a container with begin(), end(), size(), clear(), and insert() methods |
T* | pointer to data of type T |
T* | as a class or struct member: points to data of type T or array of T with member __size |
T[N] | as a class or struct member: fixed-size array of type T |
union | as a class or struct member: requires a variant selector member __union |
void* | as a class or struct member: requires a __type member to indicate the type of object pointed to |
🔝 "" "Back to table of contents"
Special Classes and Structs | Notes |
---|---|
Special Array class/struct | single and multidimensional SOAP Arrays |
Special Wrapper class/struct | complexTypes with simpleContent, wraps __item member |
xsd__hexBinary | binary content |
xsd__base64Binary | binary content and optional DIME/MIME/MTOM attachments |
xsd__anyType | DOM elements, use #import "dom.h" |
@xsd__anyAttribute | DOM attributes, use #import "dom.h" |
🔝 "" "Back to table of contents"
To bind C/C++ type names to XSD types, a simple form of name prefixing is used by the gSOAP tools by prepending the XML namespace prefix to the C/C++ type name with a pair of undescrores. This also ensures that name clashes cannot occur when multiple WSDL and XSD files are converted to C/C++. Also, C++ namespaces are not sufficiently rich to capture XML schema namespaces accurately, for example when class members are associated with schema elements defined in another XML namespace and thus the XML namespace scope of the member's name is relevant, not just its type.
However, from a C/C++ centric point of view this can be cumbersome. Therefore, colon notation is an alternative to physically augmenting C/C++ names with prefixes.
For example, the following class uses colon notation to bind the record class to the urn:types schema:
The colon notation is stripped away by soapcpp2 when generating the data binding implementation code for our project. So the final code just uses record to identify this class and its constructor/destructor.
When using colon notation make sure to be consistent and not use colon notation mixed with prefixed forms. The qualified name ns:record differs from ns__record, because ns:record is compiled to an unqualified record name in the source code output by the soapcpp2 tool.
Colon notation also facilitates overruling the elementFormDefault and attributeFormDefault declaration that is applied to local elements and attributes, when declared as members of classes, structs, and unions. For more details, see qualified and unqualified members.
A C/C++ identifier name (a type name, member name, function name, or parameter name) is translated to an XML tag name by the following rules:
For example, the C/C++ namespace qualified identifier name s_a__my_way is translated to the XML tag name s-a:my-way by translating the prefix s_a and the local name my_way.
Struct/class member and parameter name translation can be overruled by using backtick XML tags (with gSOAP 2.8.30 and greater).
🔝 "" "Back to table of contents"
The C++ bool type is bound to built-in XSD type xsd:boolean.
The C alternative is to define an enumeration:
or by defining an enumeration in C with pseudo-scoped enumeration constants:
The XML value space of these types is false and true, but also accepted are 0 and 1 values for false and true, respectively.
To prevent name clashes, false_ and true_ have a trailing underscore in their enum symbols. Trailing underscores are removed from the XML value space.
🔝 "" "Back to table of contents"
Enumerations are mapped to XSD simpleType enumeration restrictions of xsd:string, xsd:QName, and xsd:long.
Consider for example:
which maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema:
Enumeration name constants can be pseudo-scoped to prevent name clashes, because enumeration name constants have a global scope in C and C++:
You can also use C++11 scoped enumerations to prevent name clashes:
Here, the enumeration class base type : int is optional. In place of int in the example above, we can also use int8_t, int16_t, int32_t, or int64_t.
The XML value space of the enumertions defined above is RED, WHITE, and BLUE.
Prefix-qualified enumeration name constants are mapped to simpleType restrictions of xsd:QName, for example:
which maps to a simpleType restriction of xsd:QName in the soapcpp2-generated schema:
Enumeration name constants can be pseudo-numeric as follows:
which maps to a simpleType restriction of xsd:long:
The XML value space of this type is 3, 5, 7, and 11.
Besides (pseudo-) scoped enumerations, another way to prevent name clashes accross enumerations is to start an enumeration name constant with one underscore or followed it by any number of underscores, which makes it unique. The leading and trailing underscores are removed from the XML value space.
The gSOAP soapcpp2 tool permits reusing enumeration name constants across (non-scoped) enumerations as long as these values are assigned the same constant. Therefore, the following is permitted:
A bitmask type is an enum* "product enumeration" with a geometric, power-of-two sequence of values assigned to the enumeration constants:
where the product enum assigns 1 to SSL3, 2 to TLS10, 4 to TLS11, 8 to TLS12, and 16 to TLS13, which allows these enumeration constants to be used in composing bitmasks with | (bitwise or) & (bitwise and), and ~ (bitwise not):
The bitmask type maps to a simpleType list restriction of xsd:string in the soapcpp2-generated XML schema:
The XML value space of this type consists of all 16 possible subsets of the four values, represented by an XML string with space-separated values. For example, the bitmask TLS10 | TLS11 | TLS12 equals 14 and is represented by the XML text TLS10 TLS11 TLS12.
You can also use C++11 scoped enumerations with bitmasks using enum* product enumerations:
The base type of a scoped enumeration bitmask, when explicitly given, is ignored. The base type is either int or int64_t, depending on the number of constants enumerated in the bitmask.
To convert enum name constants and bitmasks to a string, we use the auto-generated function for enum T:
The string returned is stored in an internal buffer of the current soap context, so you should copy it to keep it from being overwritten. For example, use char *soap_strdup(struct soap*, const char*).
To convert a string to an enum constant or bitmask, we use the auto-generated function
This function takes the name (or names, space-separated for bitmasks) of the enumeration constant in a string str. Names should be given without the pseudo-scope prefix and without trailing underscores. The function sets val to the corresponding integer enum constant or to a bitmask. The function returns SOAP_OK (zero) on success or an error if the string is not a valid enumeration name.
🔝 "" "Back to table of contents"
Integer and floating point types are mapped to the equivalent built-in XSD types with the same sign and bit width.
The size_t type is transient (not serializable) because its width is platform dependent. We recommend to use uint64_t instead.
The XML value space of integer types are their decimal representations without loss of precision.
The XML value space of floating point types are their decimal representations. The decimal representations are formatted with the printf format string "%.9G" for floats and the printf format string "%.17lG" for double. To change the format strings, we can assign new strings to the following soap context members:
Decimal representations may result in a loss of precision of the least significant decimal. Therefore, the format strings that are used by default are sufficiently precise to avoid loss, but this may result in long decimal fractions in the XML value space.
The long double extended floating point type requires a custom serializer:
You can now use long double, which has a serializer that serializes this type as xsd:decimal. Compile and link your code with the file gsoap/custom/long_double.c.
The value space of floating point values includes the special values INF, -INF, and NaN. You can check a value for plus or minus infinity and not-a-number as follows:
To assign these values, use:
If your system supports __float128 then you can also use this 128 bit floating point type with a custom serializer:
Then use the xsd__decimal alias of __float128, which has a serializer. Do not use __float128 directly, which is transient (not serializable).
To check for INF, -INF, and NaN of a __float128 value use:
The range of a typedef-defined numerical type can be restricted using the range : operator with inclusive lower and upper bounds. For example:
This maps to a simpleType restriction of xsd:int in the soapcpp2-generated schema:
The lower and upper bound of a range are optional. When omitted, values are not bound from below or from above, respectively.
The range of a floating point typedef-defined type can be restricted within floating point constant bounds.
Also with a floating point typedef a printf-format pattern can be given of the form "%[width][.precision]f" to format decimal values using the given width and precision fields:
This maps to a simpleType restriction of xsd:float in the soapcpp2-generated schema:
For exclusive bounds, we use the < operator instead of the : range operator:
Values eps of ns__epsilon are restricted between 0.0 < eps < 1.0.
This maps to a simpleType restriction of xsd:float in the soapcpp2-generated schema:
To make just one of the bounds exclusive, while keeping the other bound inclusive, we add a < on the left or on the right side of the range ':' operator. For example:
It is valid to make both left and right side exclusive with < : < which is in fact identical to the exlusive range < operator:
It helps to think of the : as a placeholder of the value between the two bounds, which is easier to memorize than the shorthand forms of bounds from which the : is removed:
bounds | validation check | shorthand |
---|---|---|
1 : | 1 <= x | 1 |
1 : 10 | 1 <= x <= 10 | |
: 10 | x <= 10 | |
1 < : < 10 | 1 < x < 10 | 1 < 10 |
1 : < 10 | 1 <= x < 10 | |
: < 10 | x < 10 | < 10 |
1 < : | 1 < x | 1 < |
1 < : 10 | 1 < x <= 10 |
Besides float, also double and long double values can be restricted. For example, consider a nonzero probability extended floating point precision type:
Value range restrictions are validated by the parser for all inbound XML data. A type fault SOAP_TYPE will be thrown by the deserializer if the value is out of range.
Finally, if your system supports __int128_t then you can also use this 128 bit integer type with a custom serializer:
Use the xsd__integer alias of __int128_t, which has a serializer. Do not use __int128_t directly, which is transient (not serializable).
To convert numeric values to a string, we use the auto-generated function for numeric type T:
For numeric types T, the string returned is stored in an internal buffer of the current soap context, so you should copy it to keep it from being overwritten. For example, use char *soap_strdup(struct soap*, const char*).
To convert a string to a numeric value, we use the auto-generated function
where T is for example int, LONG64, float, decimal (the custom serializer name of long double) or xsd__integer (the custom serializer name of __int128_t). The function soap_s2T returns SOAP_OK on success or an error when the value is not numeric. For floating point types, "INF", "-INF" and "NaN" are valid strings to convert to numbers.
🔝 "" "Back to table of contents"
String types are mapped to the built-in xsd:string and xsd:QName XSD types.
The wide strings wchar_t* and std::wstring may contain Unicode that is preserved in the XML value space.
Strings char* and std::string can only contain extended Latin, but we can store UTF-8 content that is preserved in the XML value space when the soap context is initialized with the flag SOAP_C_UTFSTRING.
The length of a string of a typedef-defined string type can be restricted:
which maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema:
String length restrictions are validated by the parser for inbound XML data. A value length fault SOAP_LENGTH will be thrown by the deserializer if the string is too long or too short.
In addition, an XSD regex pattern restriction can be associated with a string typedef:
which maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema:
Pattern restrictions are validated by the parser for inbound XML data only if the soap::fsvalidate and soap::fwvalidate callbacks are defined.
Exclusive length bounds can be used with strings:
Fixed-size strings (char[N]) are rare occurrences in the wild, but apparently still used in some projects to store strings. To facilitate fixed-size string serialization, use soapcpp2 -b option -b. For example:
which maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema:
Fixed-size strings must contain NUL-terminated text and should not contain raw binary data. Also, the length limitation is more restrictive for UTF-8 content (enabled with the SOAP_C_UTFSTRING) that requires multibyte character encodings. As a consequence, UTF-8 content may be truncated to fit.
Raw binary data can be stored in a xsd__base64Binary or xsd__hexBinary structure, or transmitted as a MIME attachment.
The built-in _QName type is a regular C string type (char*) that maps to xsd:QName but has the added advantage that it holds normalized qualified names. There are actually two forms of normalized QName content, to ensure any QName is represented accurately:
The first form of string is used when the prefix (and the binding URI) is defined in the namespace table and is bound to a URI (see the .nsmap file). The second form is used when the URI is not defined in the namespace table and therefore no prefix is available to bind and normalize the URI to.
A _QName string may contain a sequence of space-separated QName values, not just one, and all QName values are normalized to the format shown above.
To define a std::string base type for xsd:QName, we use a typedef:
The xsd__QName string content is normalized, just as with the _QName normalization.
To serialize strings that contain literal XML content to be reproduced in the XML value space, use the built-in _XML string type, which is a regular C string type (char*) that maps to plain XML CDATA.
To define a std::string base type for literal XML content, use a typedef:
Strings can hold any of the values of the XSD built-in primitive types. We can use a string typedef to declare the use of the string type as a XSD built-in type:
You must ensure that the string values we populate in this type conform to the XML standard, which in case of xsd:token is the lexical and value spaces of xsd:token are the sets of all strings after whitespace replacement of any occurrence of #x9, #xA , and #xD by #x20 and collapsing.
As of version 2.8.49, the gSOAP parser will automatically collapse or replace the white space content when receiving data for XSD types that require white space collapsed or replaced. This normalization is applied to strings directly. The decision to collapse or replace is based on the typedef name corresponding to the built-in string-based XSD type.
To copy char* or wchar_t* strings with a context that manages the allocated memory, use functions
To convert a wide string to a UTF-8 encoded string, use function
The function allocates and returns a string, with its memory being managed by the context.
To convert a UTF-8 encoded string to a wide string, use function
where to is set to point to an allocated wchar_t* string. Pass -1 for minlen and maxlen to ignore length constraints on the target string. The function returns SOAP_OK or an error when the length constraints are not met.
🔝 "" "Back to table of contents"
The C/C++ time_t type is mapped to the built-in xsd:dateTime XSD type that represents a date and time within a time zone (typically UTC).
The XML value space contains ISO 8601 Gregorian time instances of the form [-]CCYY-MM-DDThh:mm:ss.sss[Z|(+|-)hh:mm], where Z is the UTC time zone or a time zone offset (+|-)hh:mm] from UTC is used.
A time_t value is considered and represented in UTC by the serializer.
Because the time_t value range is restricted to dates after 01/01/1970 and before 2038 assuming time_t is a long 32 bit, care must be taken to ensure the range of xsd:dateTime values in XML exchanges do not exceed the time_t range.
This restriction does not hold for struct tm (time.h library), which we can use to store and exchange a date and time in UTC without date range restrictions. The serializer uses the struct tm members directly for the XML value space of xsd:dateTime:
You will lose the day of the week information. It is always Sunday (tm_wday=0) and the day of the year is not set either. The time zone is UTC.
This struct tm type is mapped to the built-in xsd:dateTime XSD type and serialized with the custom serializer gsoap/custom/struct_tm.h that declares a xsd__dateTime type:
Compile and link your code with gsoap/custom/struct_tm.c.
The struct timeval (sys/time.h library) type is mapped to the built-in xsd:dateTime XSD type and serialized with the custom serializer gsoap/custom/struct_timeval.h that declares a xsd__dateTime type:
Compile and link your code with gsoap/custom/struct_timeval.c.
The same value range restrictions apply to struct timeval as they apply to time_t. The added benefit of struct timeval is the addition of a microsecond-precise clock:
A C++11 std::chrono::system_clock::time_point type is mapped to the built-in xsd:dateTime XSD type and serialized with the custom serializer gsoap/custom/chrono_time_point.h that declares a xsd__dateTime type:
Compile and link your code with gsoap/custom/chrono_time_point.cpp.
The struct tm type is mapped to the built-in xsd:date XSD type and serialized with the custom serializer gsoap/custom/struct_tm_date.h that declares a xsd__date type:
Compile and link your code with gsoap/custom/struct_tm_date.c.
The XML value space of xsd:date are Gregorian calendar dates of the form [-]CCYY-MM-DD[Z|(+|-)hh:mm] with a time zone.
The serializer ignores the time part and the deserializer only populates the date part of the struct, setting the time to 00:00:00. There is no unreasonable limit on the date range because the year field is stored as an integer (int).
An unsigned long long (ULONG64 or uint64_t) type that contains a 24 hour time in microseconds UTC is mapped to the built-in xsd:time XSD type and serialized with the custom serializer gsoap/custom/long_time.h that declares a xsd__time type:
Compile and link your code with gsoap/custom/long_time.c.
This type represents 00:00:00.000000 to 23:59:59.999999, from 0 to an upper bound of 86,399,999,999. A microsecond resolution means that a 1 second increment requires an increment of 1,000,000 in the integer value.
The XML value space of xsd:time are points in time recurring each day of the form hh:mm:ss.sss[Z|(+|-)hh:mm], where Z is the UTC time zone or a time zone offset from UTC is used. The xsd__time value is always considered and represented in UTC by the serializer.
To convert date and/or time values to a string, we use the auto-generated function for type T:
For date and time types T, the string returned is stored in an internal buffer of the current soap context, so you should copy it to keep it from being overwritten. For example, use char *soap_strdup(struct soap*, const char*).
To convert a string to a date/time value, we use the auto-generated function
where T is for example dateTime (for time_t), xsd__dateTime (for struct tm, struct timeval, or std::chrono::system_clock::time_point). The function soap_s2T returns SOAP_OK on success or an error when the value is not a date/time.
🔝 "" "Back to table of contents"
The XML value space of xsd:duration are values of the form PnYnMnDTnHnMnS where the capital letters are delimiters. Delimiters may be omitted when the corresponding member is not used.
A long long (LONG64 or int64_t) type that contains a duration (time lapse) in milliseconds is mapped to the built-in xsd:duration XSD type and serialized with the custom serializer gsoap/custom/duration.h that declares a xsd__duration type:
Compile and link your code with gsoap/custom/duration.c.
The duration type xsd__duration can represent 106,751,991,167 days forward and backward with millisecond precision.
Durations that exceed a month are always output in days, rather than months to avoid days-per-month conversion inacurracies.
Durations that are received in years and months instead of total number of days from a reference point are not well defined, since there is no accepted reference time point (it may or may not be the current time). The decoder simple assumes that there are 30 days per month. For example, conversion of "P4M" gives 120 days. Therefore, the durations "P4M" and "P120D" are assumed to be identical, which is not necessarily true depending on the reference point in time.
Rescaling of the duration value by may be needed when adding the duration value to a time_t value, because time_t may or may not have a seconds resolution, depending on the platform and possible changes to time_t.
Rescaling is done automatically when you add a C++11 std::chrono::nanoseconds value to a std::chrono::system_clock::time_point value. To use std::chrono::nanoseconds as xsd:duration:
Compile and link your code with gsoap/custom/chrono_duration.cpp.
This type can represent 384,307,168 days (2^63 nanoseconds) forwards and backwards in time in increments of 1 ns (1/1000000000 second).
The same observations with respect to receiving durations in years and months apply to this serializer's decoder.
To convert duration values to a string, we use the auto-generated function
The string returned is stored in an internal buffer, so you should copy it to keep it from being overwritten, Use soap_strdup(struct soap*, const char*) for example to copy this string.
To convert a string to a duration value, we use the auto-generated function
The function returns SOAP_OK on success or an error when the value is not a duration.
🔝 "" "Back to table of contents"
Classes and structs are mapped to XSD complexTypes. The XML value space consists of XML elements with attributes and subelements, possibly constrained by XML schema validation rules that enforce element and attribute occurrence contraints, numerical value range constraints, and string length and pattern constraints.
Classes that are declared with the gSOAP tools are limited to single inheritence only. The soapcpp2 tool does not allow structs to be inherited.
The class and struct name is bound to an XML namespace by means of the prefix naming convention or by using colon notation:
In the example above, we also added a context pointer to the soap context that manages this instance. It is set when the instance is created in the engine's context, for example when deserialized and populated by the engine.
The class maps to a complexType in the soapcpp2-generated XML schema:
The following sections apply to both structs and classes. Structs require the use of the struct keyword with the struct name, otherwise soapcpp2 will throw a syntax error. As is often done in C, use a typedef to declare a struct that can be used without the struct keyword.
🔝 "" "Back to table of contents"
Public data members of a class or struct are serializable when their types are serializable. Private and protected members are transient and not serializable.
Also const and static members are not serializable, with the exception of const char* and const wchar_t*. Types and specific class/struct members can be made transient with the extern qualifier for types and by marking members with [ and ]:
By declaring std::ostream transient with extern you can use this type wherever you need it without soapcpp2 complaining that this class and any other class or type declared as extern is not defined. Do not use extern with typedef, because this declares a custom serializer, see adding custom serializers.
Marking members transient with [ and ] makes them transient (and visually makes them stand out). This has otherwise no effect on the generated code for the class or struct to be used in your application code.
🔝 "" "Back to table of contents"
Extensible and restricted types in XML schemas are derived types from single simple and complex base types. XML schema derived types are naturally represented by C++ derived classes using single inheritance. Besides the concept of extensions versus restrictions, there are two kinds of derived types: complexTypes with simpleContent, meaning types with XML CDATA values, and complexTypes with complexContent, meaning types with sub-elements. Both are permitted to have one or more XML attributes.
A complexType with simpleContent is defined as a wrapper to contain XML CDATA values and any number of attributes, see wrapper class/struct with simpleContent. Wrapper class/struct types can form a hierarchy of derived types in C++ using inheritance. For example:
The ns__data class maps to a complexType in the soapcpp2-generated XML schema:
The XML value space consists of an element with the string contents an optional attribute:
By contrast, a complexType with complexContent typically extends a given base complexType. For example:
The ns__base and ns__derived classes maps to complexTypes in the soapcpp2-generated XML schema:
The XML value space of ns__derived consists of three requires child elements and an optional attribute:
Derived types can be used for two main purposes in XML schema by extending or restricting base types. One purpose is to reuse a base type when defining a derived type, such that common parts do not need to be replicated. The second purpose is to be able to use a derived type in place of a base type in XML, which is indicated by an xsi:type attribute with the qualified name of the derived type. Consider for example the following class that uses the previously declared base types xsd__anyType and ns__base:
We can assign base type values to the ns_record members:
This produces the following XML fragment populated with default values (empty text for strings and zeros for numbers), where element base1 has a simpleContent value and element base2 has two child elements:
We can also assign derived type values to the ns_record members:
This produces the following XML fragment populated with default values (empty text for strings and zeros for numbers), where element base1 has schema type ns:data with simpleContent and an attribute, and base2 has schema type ns:derived with three child elements and an attribute:
Deserialization automatically allocates and assigns a ns__base class instance to a ns__base pointer when deserializing the ns:base schema type and allocates and assigns a ns__derived class instance to a ns__base pointer when deserializing the ns:derived type when an element with xsi:type="ns:derived" is parsed. All classes are extended by soapcpp2 by a soap_type() method that returns the unique SOAP_TYPE_T value of the class T. This makes it easy to check whether the deserialized data contains a derived type to implement type-safe code, for example:
This example should use the SOAP_XML_STRICT mode flag to initialize the soap context to ensure that all required values are present in the deserialized structures.
🔝 "" "Back to table of contents"
While single inheritance works well in C++ to represent derived types as we discussed in the previous section, this will obviously not work in C. Two methods to serialize derived types in C are presented here. The first method uses void* to serialize anything. The second method is more accurate and is relatively new in gSOAP.
To serialize any type is possible with tagged void pointer members to serialize data pointed to by a void* member, which can be any serializable type, such as derived types. For void* deserialization to work the XML parsed must contain an xsi:type attribute with a schema type. Only then can the deserializer instantiate the corresponding serializable C/C++ type. Base types serialized do not require an xsi:type to indicate the base schema type, so this approach is not guaranteed to work and requires a workaround with an anonymous wrapper struct/class that contains both the base type and a void*. For example:
The __ns__base wrapper wraps the ns__base type to (de)serialize the base element that has no xsi:type attribute and uses void* to (de)serialize the base element that has xsi:type attribute. This works fine at the XML parsing level, but the generated XML schema components do not accurately represent the derived type, because it lacks the extension/restriction of the derived type (and the __ns__base wrapper is invisible).
Using void* to represent derived types in a base type wrapper is not very accurate because we can serialize anything, not just derived types of a given base type. The wrapper may also hold two values: the base type value and a derived type value. Furthermore, using arrays or containers that hold base and derived types becomes quite tricky because an array item could hold both the base and derived type.
As of gSOAP version 2.8.75, wsdl2h -F option -F generates base type structs extended with transient pointer members to its derived types. To serialize the base type itself, all of the pointer members are NULL. If one of the pointer members points to a derived type the derived type is serialized instead. Deserialization is automatic, in that the base type is deserialized if the element has no xsi:type attribute or the attribute is the base schema type, and a derived type is deserialized if the element has an xsi>type attribute with the derived schema type.
This method is fully automated for the wsdl2h tool to generate an interface header file for soapcpp2 with the type derivations in C. To use this method to generate code from WSDLs and XSDs, use wsdl2h -F option -F. This also works in C++ if desired, but C++ inheritance works fine without this method.
Using this method with soapcpp2 alone using a manually-specified interface header file produces the specified type inheritance in the soapcpp2-generated WSDL and XML schema files as complexType extensions.
The soapcpp2 tool warns if a derived type has multiple base types. At most one base type for a derived type may be specified.
This method with transient pointers to derived types makes it easy to use base and derived types in C:
The ns__base struct includes the special member ns__derived that points to a ns__derived value. This special member must be:
To serialize the ns__base value requires the ns__derived member to be NULL. To serialize the ns__derived value requires the ns__derived member to point to the ns__derived value to serialize and the ns__base members are irrelevant.
We can assign the base type value to the ns_record::base member:
This produces the following XML fragment populated with default values (empty text for strings and zeros for numbers), where element base has two child elements:
We can also assign the derived type value to the ns_record::base member:
This produces the following XML fragment populated with default values (empty text for strings and zeros for numbers), where element base has schema type ns:derived with three child elements and an attribute:
Deserialization automatically assigns values to the base members for the ns__base type and populates the ns__derived member when a derived type with xsi:type="ns:derived" is parsed. This makes it easy to decompose the deserialized data:
This example requires the SOAP_XML_STRICT mode flag to initialize the soap context to ensure that all required values are present in the deserialized structures, otherwise the char* strings may be NULL since XML validation constraints are not enforced on the XML input.
Deeper levels of simulated inheritance are possible, for example:
This requires two pointer traversals from the base type ns__base via ns__derived to reach ns__derived_derived.
🔝 "" "Back to table of contents"
Classes and structs can be declared volatile in the interface header file for soapcpp2, which only has meaning for the gSOAP tools. This annotation means that these types are already declared elsewhere in your project's source code and you do not want soapcpp2 to generate code with a second declaration of these types.
For example, struct tm is declared in the time.h library. You can make it serializable and include a partial list of data members that you want to serialize:
You can declare classes and structs volatile for any such types you want to serialize by only providing the public data members you want to serialize.
In addition, colon notation is a simple and effective way to bind an existing class or struct to a schema. For example, you can change the tm name as follows without affecting the code that uses struct tm generated by soapcpp2:
This struct maps to a complexType in the soapcpp2-generated XML schema:
🔝 "" "Back to table of contents"
Classes and structs can be declared mutable with the gSOAP tools. This means that their definition can be spread out over the source code. This promotes the concept of a class or struct as a row of named values, also known as a named tuple, that can be extended at compile time in your source code with additional members. Because these types differ from the traditional object-oriented principles and design concepts of classes and objects, constructors and destructors cannot be defined (also because we cannot guarantee merging these into one such that all members will be initialized). A default constructor, copy constructor, assignment operation, and destructor will be assigned automatically by soapcpp2.
The members are collected into one definition generated by soapcpp2. Members may be repeated from one definition to another, but only if their associated types are identical. So, for example, a third extension with a value member with a different type fails:
The mutable concept has proven to be very useful when declaring and collecting SOAP Headers for multiple services, which are collected into one struct SOAP_ENV__Header by the soapcpp2 tool.
🔝 "" "Back to table of contents"
Class and struct data members in C and C++ may be declared with an optional default initialization value that is provided "inline" with the declaration of the member:
Alternatively, you can use C++11 default initialization syntax:
Any member with a primitive type can be initialized in this way.
These initializations are performed by the default constructor that is added by soapcpp2 to each class and struct (in C++ only). A constructor is only added when a default constructor is not already defined with the class declaration.
You can explicitly (re)initialize an object with these initial values by using the soapcpp2 auto-generated functions:
If T is a struct or class that has a soap pointer member to a soap context then this pointer member will be set to the first argument passed to these functions to initialize their soap pointer member.
Default value initializations can be provided for members that have primitive types (bool, enum, time_t, numeric and string types).
Default value initializations of pointer members is permitted, but the effect is different. To conform to XML schema validation, an attribute member that is a pointer to a primitive type will be assigned the default value when parsed from XML. An element member that is a pointer to a primitive type will be assigned when the element is empty when parsed from XML.
As of gSOAP 2.8.48 and greater, a fixed value can be assigned with a ==. A fixed value is also verified by the parser's validator.
Default and fixed values for members with or without pointers are best explained with the following two example fragments.
A record class with default values for std::string (or std::wstring) attributes and elements is declared as follows:
Also std::unique_ptr and std::shared_ptr may be used instead of a regular pointer to strings.
With C char* (or const char*, const wchar_t*) strings in a struct, this becomes:
By contrast to std::string e, char* e must be marked 1 to make it required, because pointer members are optional by default.
Attributes are considered optional by default, unless marked as required with the occurrence constraint 1. Elements are considered required unless the member type is a pointer or if the member is marked optional with occurrence constraint 0.
Instead of default values, fixed values indicate that the attribute or element must contain that value, and only that value, when provided in XML. A fixed value is specified with a ==.
Attributes with default or fixed values may be omitted in XML. When absent, the default/fixed value is used at the receiving side, i.e. the deserializer assigns the default/fixed value when the attribute is absent. Therefore, there is no need to make attributes with default/fixed values pointer based, because there is no way to distinguish an omitted attribute from a populated attribute on the receiving side. The c member in the example above can be a non-pointer for this reason. The wsdl2h tool does not generate pointers for attributes with default/fixed values.
Elements with default or fixed values may be optional and the use of default/fixed values with elements differs from attributes. The default/fixed value of an element is only used for elements that are empty in the XML payload received. Omitted optional elements in the XML payload received are simply absent; no default/fixed value is assigned.
A record class (can be a struct in C) with fixed values for attributes and elements is declared as follows:
With C char* (or const char*, const wchar_t*) strings in a struct, this becomes:
The XML schema validation rules for the examples above are as follows:
Member | Notes |
---|---|
a | attribute may appear once; if it does not appear its value is "A", otherwise its value is that given (also note: instantiating ns__record_with_default assigns the default value "A") |
b | has no effect when parsing XML (but note: instantiating ns__record_with_default assigns the default value "B") |
c | attribute may appear once; if it does not appear its value is "C", otherwise its value is that given (also note: instantiating ns__record_with_default assigns NULL) |
d | element may appear once; if it does not appear or if it is empty, its value is "D"; otherwise its value is that given (also note: instantiating ns__record_with_default assigns the default value "D") |
e | has no effect when parsing XML (but note: instantiating ns__record_with_default assigns the default value "E") |
f | element may appear once; if it does not appear it is not provided; if it does appear and it is empty, its value is "F"; otherwise its value is that given (also note: instantiating ns__record_with_default assigns NULL) |
g | attribute may appear once; if it does not appear its value is "G", if it does not appear its value is "G" (also note: instantiating ns__record_with_fixed assigns the fixed value "G") |
h | attribute must appear once, its value must be "H" (also note: instantiating ns__record_with_fixed assigns the fixed value "H") |
i | attribute may appear once; if it does not appear its value is "I", if it does not appear its value is "I" (also note: instantiating ns__record_with_fixed assigns NULL) |
j | element may appear once, if it does not appear it is not provided; if it does appear and it is empty, its value is "J"; if it does appear and it is not empty, its value must be "J" (also note: instantiating ns__record_with_fixed assigns the fixed value "J") |
k | element must appear once, its value must be "K" (also note: instantiating ns__record_with_fixed assigns the fixed value "K") |
l | element may appear once, if it does not appear it is not provided; if it does appear and it is empty, its value is "J"; if it does appear and it is not empty, its value must be "J" (also note: instantiating ns__record_with_fixed assigns NULL) |
Members of type char[N] (fixed length string) can have default and fixed values, when soapcpp2 -b option -b is used. Also char** (pointer to a string) members can have default and fixed values. However, members of this type will be initialized to NULL. The default/fixed values will be assigned with the same rules as for char* when deserialized from XML.
🔝 "" "Back to table of contents"
Class and struct data members are declared as XML attributes by annotating their type with a @ qualifier:
This class maps to a complexType in the soapcpp2-generated XML schema:
An example XML instance of ns__record is:
Attribute data members are restricted to primitive types (bool, enum, time_t, numeric and string types), xsd__hexBinary, xsd__base64Binary, and custom serializers, such as xsd__dateTime. Custom serializers for types that may be used as attributes should define soap_s2T and soap_T2s functions that convert values of type T to strings and back.
Attribute data members can be pointers and smart pointers to these types, which permits attributes to be optional.
🔝 "" "Back to table of contents"
The XML tag name of a class/struct member is the name of the member with the usual XML tag translation, see colon notation.
To override the standard translation of identifier names to XML tag names of attributes and elements, add the XML tag name in backticks (requires gSOAP 2.8.30 or greater):
This class maps to a complexType in the soapcpp2-generated XML schema:
An example XML instance of ns__record is:
A backtick XML tag name may contain any non-empty sequence of ASCII and UTF-8 characters except white space and the backtick character. A backtick tag can be combined with member constraints and default member initializers:
🔝 "" "Back to table of contents"
Class, struct, and union data members are mapped to namespace qualified or unqualified tag names of local elements and attributes. If a data member has no prefix then the default form of qualification is applied based on the element/attribute form that is declared with the XML schema of the class, struct, or union type. If the member name has a namespace prefix by colon notation, then the prefix overrules the default (un)qualified form. Therefore, colon notation is an effective mechanism to control qualification of tag names of individual members of classes, structs, and unions.
The XML schema elementFormDefault and attributeFormDefault declarations control the tag name qualification of local elements and attributes, respectively.
Individual schema declarations of local elements and attributes may overrule this by using the form declaration in an XML schema and by using colon notation to add namespace prefixes to class, struct, and union members in the header file for soapcpp2.
Consider for example an ns__record class in the ns namespace in which local elements are qualified and local attributes are unqualified by default:
This class maps to a complexType in the soapcpp2-generated XML schema with targetNamespace "urn:types", elementFormDefault qualified and attributeFormDefault unqualified:
An example XML instance of ns__record is:
Here the root element <ns:record> is qualified because it is a root element of the XML schema with target namespace "urn:types". Its local element <ns:spouse> is namespace qualified because the elementFormDefault of local elements is qualified. Attributes are unqualified.
The default namespace (un)qualification of local elements and attributes can be overruled by adding a prefix to the member name by using colon notation:
The colon notation for member ns:name forces qualification of its attribute tag in XML. The colon notation for member :spouse removes qualification from its local element tag:
XML instances of ns__record have unqualified spouse elements and qualified ns:name attributes:
Members of a class or struct can also be prefixed using the prefix__name convention or using colon notation prefix:name. However, this has a different effect by referring to global (root) elements and attributes, see document root element definitions.
Backtick XML tags can be used in place of the member name annotations and will achieve the same effect as described when these tag names are (un)qualified (requires gSOAP 2.8.30 or greater).
🔝 "" "Back to table of contents"
To define and reference XML document root elements we use type names that start with an underscore:
Alternatively, we can use a typedef to define a document root element with a given type:
This typedef maps to a global root element that is added to the soapcpp2-generated XML schema:
An example XML instance of _ns__record is:
Global-level element/attribute definitions are also referenced and/or added to the generated XML schema when serializable data members reference these by their qualified name:
These types map to the following comonents in the soapcpp2-generated XML schema:
Use only use qualified member names when their types match the global-level element types that they refer to. For example:
Therefore, we recommend to use qualified member names only when necessary to refer to standard XSD elements and attributes, such as xsi__type, and xsd__lang.
By contrast, colon notation has the desired effect to (un)qualify local tag names by overruling the default element/attribute namespace qualification, see qualified and unqualified members.
As an alternative to prefixing member names, use the backtick tag (requires gSOAP 2.8.30 or greater):
🔝 "" "Back to table of contents"
A public pointer-typed data member is serialized by following its (smart) pointer(s) to the value pointed to. To serialize pointers to dynamic arrays of data, please see the next section on container and array members and their occurrence constraints.
Pointers that are NULL and smart pointers that are empty are serialized to produce omitted element and attribute values, unless an element is required and is nillable (struct/class members marked with nullptr) in which case the element is rendered as an empty element with xsi:nil="true".
To control the occurrence requirements of pointer-based data members, occurrence constraints are associated with data members in the form of a range minOccurs : maxOccurs. For non-repeatable (meaning, not a container or array) data members, there are only three reasonable occurrence constraints:
Pointer-based data members have a default 0:1 occurrence constraint, making them optional, and their XML schema local element/attribute definition is marked as nillable. Non-pointer data members have a default 1:1 occurence constraint, making them required.
A nullptr occurrence constraint may be applicable to required elements that are nillable pointer types, thus nullptr 1:1. This indicates that the element is nillable (can be NULL or nullptr). A pointer data member that is explicitly marked as required and nillable with nullptr 1:1 will be serialized as an element with an xsi:nil attribute, thus effectively revealing the NULL property of its value.
A non-pointer data member that is explicitly marked as optional with 0:1 will be set to its default value when no XML value is presented to the deserializer. A default value can be assigned to a data member that has a primitive type or is a (smart) pointer to primitive type.
Consider for example:
This class maps to a complexType in the soapcpp2-generated XML schema:
An example XML instance of ns__record with its name string value set to Joe, SSN set to its default, and spouse set to NULL:
🔝 "" "Back to table of contents"
Class and struct data member types that are containers std::deque, std::list, std::vector and std::set are serialized as a collection of the values they contain. You can also serialize dynamic arrays, which is the alternative for C to store collections of data. Let's start with containers.
You can use std::deque, std::list, std::vector, and std::set containers by importing:
For example, to use a vector data mamber to store names in a record:
To limit the number of names in the vector within reasonable bounds, occurrence constraints are associated with the container. Occurrence constraints are of the form minOccurs : maxOccurs:
This class maps to a complexType in the soapcpp2-generated XML schema:
Because C does not support a container template library, we can use a dynamically-sized array of values. This array is declared as a size-pointer pair of members within a struct or class. The array size information is stored in a special size tag member with the name __size or __sizeX, where X can be any name, or by an $int member to identify the member as a special size tag:
This struct maps to a complexType in the soapcpp2-generated XML schema:
To limit the number of names in the array within reasonable bounds, occurrence constraints are associated with the array size member. Occurrence constraints are of the form minOccurs : maxOccurs:
This struct maps to a complexType in the soapcpp2-generated XML schema:
Arrays can also be declared as nested elements, similar to SOAP-encoded dynamic arrays, and these arrays can be used with or without SOAP applications. This requires a separate struct or class with the name of the SOAP array, which should not be qualified with a namespace prefix:
The ns__record struct maps to a complexType that references the ArrayOfstring complexType with an sequence of 1 to 100 item elements:
To change the item element name in the WSDL, XML schema, and XML messages, use __ptrName where Name is the name you want to use.
Fixed-size arrays can be used to store a fixed number of values:
The fixed-size array is similar to a SOAP-encoded array, which can be used with or without SOAP applications. This struct maps to a complexType that references a Array10Ofstring complexType with ten item elements:
🔝 "" "Back to table of contents"
A member becomes a hidden XML element, i.e. not visibly rendered in XML, when its name starts with a double underscore. This makes it possible to sequence a collection of data members, basically by forming a sequence of elements that can be optional or repeated together.
To create a sequence of members that are optional, use a pointer-based hidden member that is a struct with the collection of members to sequence:
Here we used a hidden struct type __ns__optional which starts with a double underscore, because we do not want to define a new global type for the XML schema we generate. We just need a unique name for a structure that sequences the two members.
This struct maps to a complexType in the soapcpp2-generated XML schema:
The name member is a required element of the ns:record complexType. The ns:record complexType has an optional sequence of SSN and phone elements.
To create repetitions of a sequence of members, use an array as follows:
This struct maps to a complexType in the soapcpp2-generated XML schema:
The name member is a required element of the ns:record complexType. The ns:record complexType has a potentially unbounded sequence of SSN and phone elements. You can specify array bounds instead of zero to unbounded, see container and array members and their occurrence constraints.
The XML value space consists of a sequence of SSN and phone elements:
🔝 "" "Back to table of contents"
A union member in a class or in a struct cannot be serialized unless a discriminating variant selector member is provided that tells the serializer which union field to serialize. This effectively creates a tagged union.
The variant selector is associated with the union as a selector-union pair of members. The variant selector is a member with the name __union or __unionX, where X can be any name, or by an $int member to identify the member as a variant selector tag:
The variant selector values are auto-generated based on the union name choice and the names of its members x, n, and s:
This class maps to a complexType with a sequence and choice in the soapcpp2-generated XML schema:
A container or dynamic array of a union requires wrapping the variant selector and union member in a struct:
and an equivalent definition with a dynamic array instead of a std::vector (you can use this in C with structs):
This maps to two complexTypes in the soapcpp2-generated XML schema:
The XML value space consists of a sequence of item elements each wrapped in an data element:
To remove the wrapping data element, simply rename the wrapping struct to __ns__data and the member to __data to make this member invisible to the serializer. The double underscore prefix naming convention is used for the struct name and member name. Also use a dynamic array instead of a container (so you can also use this approach in C with structs):
This maps to a complexType in the soapcpp2-generated XML schema:
The XML value space consists of a sequence of <x>, <n>, and/or <s> elements:
Please note that structs, classes, and unions are unnested by soapcpp2 (as in the C standard of nested structs and unions). Therefore, the ns__choice union in the ns__record class is redeclared at the top level despite its nesting within the ns__record class. This means that you will have to choose a unique name for each nested struct, class, and union.
🔝 "" "Back to table of contents"
To serialize data pointed to by void* requires run-time type information that tells the serializer what type of data to serialize by means of a tagged void pointer. This type information is stored in a special type tag member of a struct/class with the name __type or __typeX, where X can be any name, or alternatively by an $int special member of any name as a type tag:
A type tag member has nonzero values SOAP_TYPE_T where T is the name of a struct/class or the name of a primitive type, such as int, std__string (for std::string), string (for char*).
This class maps to a complexType with a sequence in the soapcpp2-generated XML schema:
The XML value space consists of the XML value space of the type with the addition of an xsi:type attribute to the enveloping element:
This xsi:type attribute is important for the receiving end to distinguish the type of data to instantiate. The receiver cannot deserialize the data without an xsd:type attribute.
You can find the SOAP_TYPE_T name of each serializable type in the auto-generated soapStub.h file.
Also all serializable C++ classes have a virtual int T::soap_type() member that returns their SOAP_TYPE_T value that you can use.
When the void* pointer is NULL or when typeOfdata is zero, the data is not serialized.
A container or dynamic array of void* pointers to xsd:anyType data requires wrapping the type tag and void* members in a struct:
and an equivalent definition with a dynamic array instead of a std::vector (you can use this in C with structs):
This maps to two complexTypes in the soapcpp2-generated XML schema:
The XML value space consists of a sequence of item elements each wrapped in a data element:
To remove the wrapping data elements, simply rename the wrapping struct and member to __data to make this member invisible to the serializer with the double underscore prefix naming convention. Also use a dynamic array instead of a container (you can use this in C with structs):
This maps to a complexType in the soapcpp2-generated XML schema:
The XML value space consists of a sequence of data elements:
Again, please note that structs, classes, and unions are unnested by soapcpp2 (as in the C standard of nested structs and unions). Therefore, the __data struct in the ns__record class is redeclared at the top level despite its nesting within the ns__record class. This means that you will have to choose a unique name for each nested struct, class, and union.
🔝 "" "Back to table of contents"
A public get method may be added to a class or struct, which will be triggered by the deserializer. This method will be invoked right after the instance is populated by the deserializer. The get method can be used to update or verify deserialized content. It should return SOAP_OK or set soap::error to a nonzero error code and return it.
A public set method may be added to a class or struct, which will be triggered by the serializer. The method will be invoked just before the instance is serialized. Likewise, the set method should return SOAP_OK or set set soap::error to a nonzero error code and return it.
For example, adding a set and get method to a class declaration:
To add these and othe rmethods to classes and structs with wsdl2h and typemap.dat, please see class/struct member additions.
🔝 "" "Back to table of contents"
The following functions/macros are generated by soapcpp2 for each type T, which should make it easier to send, receive, and copy XML data in C and in C++:
When in C++ mode, soapcpp2 tool adds several methods to classes in addition to adding a default constructor and destructor (when these were not explicitly declared).
The public methods added to a class T:
Also, there are four variations of soap_new_T for class/struct/template type T that soapcpp2 auto-generates to create instances on a context-managed heap:
The above functions can be invoked with a NULL soap context, but we will be responsible to use delete T to remove this instance from the unmanaged heap.
The allocation functions return NULL when memory allocation failed.
🔝 "" "Back to table of contents"
The following applies to both structs and classes. The examples show classes in C++. For C, use structs and omit the C++ features. Structs also require the use of the struct keyword, otherwise soapcpp2 will throw a syntax error.
A class or struct with the following layout is a one-dimensional SOAP-encoded array type:
where T is the array element type. A multidimensional SOAP Array is:
where N is the constant number of dimensions. The pointer points to an array of __size[0]*__size[1]* ... * __size[N-1] elements.
This maps to a complexType restriction of SOAP-ENC:Array in the soapcpp2-generated XML schema:
The name of the class can be arbitrary. We often use ArrayOfT without a prefix to distinguish arrays from other classes and structs.
With SOAP 1.1 encoding, an optional offset member can be added that controls the start of the index range for each dimension:
For example, we can define a matrix of floats as follows:
The following code populates the matrix and serializes it in XML:
Matrix A is serialized as an array with 2x3 values:
🔝 "" "Back to table of contents"
A special case of a one-dimensional array is used to define xsd:hexBinary and xsd:base64Binary types when the pointer type is unsigned char:
and
To create a new binary type, use either one of the following three forms that declare a new ns__binary type that is a simpleType restriction of xsd:base64Binary:
Here, xsd__base64Binary is reused in the first two cases, where xsd__base64Binary is declared as shown above.
🔝 "" "Back to table of contents"
A class or struct with a binary content layout can be extended to support attachments. The following struct or class type can be used as DIME, MIME, and MTOM attachment and also be used for xsd:base64Binary type values:
When the id, type, or options members are non-NULL, an attachment will be used instead of base64 XML content. DIME attachments are the default. To switch to MIME use the SOAP_ENC_MIME context flag. To switch to MTOM use the SOAP_ENC_MTOM context flag.
MTOM is typically used with XOP <xop:Include> elements, which is preferred and declared as follows:
Attachments are beyond the scope of this article. See the gSOAP user guide. for more details.
🔝 "" "Back to table of contents"
A class or struct with the following layout is a complexType that wraps simpleContent:
The type T is a primitive type (bool, enum, time_t, numeric and string types), xsd__hexBinary, xsd__base64Binary, and custom serializers, such as xsd__dateTime.
This maps to a complexType with simpleContent in the soapcpp2-generated XML schema:
A wrapper class/struct may include any number of members that are declared as attributes with @, which should be placed after the __item member.
🔝 "" "Back to table of contents"
Use of a DOM is optional and enabled by #import "dom.h" to use the DOM xsd__anyType element node and xsd__anyAttribute attribute node:
where name contains XML stored in a DOM node set and attributes is a list of all visibly rendered attributes. The name attributes is arbitrary and any name will suffice.
You should place the xsd__anyType members at the end of the struct or class. This ensures that the DOM members are populated last as a "catch all". A member name starting with double underscore is a wildcard member. These members are placed at the end of a struct or class automatically by soapcpp2.
An #import "dom.h" import is automatically added by wsdl2h -d with option -d to bind xsd:anyType to DOM nodes, and also to populate xsd:any, xsd:anyAttribute and xsd:mixed XML content:
where the members prefixed with __ are "invisible" to the XML parser, meaning that these members are not bound to XML tag names.
In C you can use a dynamic arrary instead of std::vector:
Classes can inherit DOM, which enables full use of polymorphism with one base DOM class:
This permits an xsd__anyType pointer to refer to a derived class such as ns__record, which will be serialized with an xsi:type attribute that is set to "ns:record". The xsi:type attributes add the necessary type information to distinguish the XML content from the DOM base type. This is important for the receiving end: without xsd:type attributes with type names, only base DOM objects are recognized and instantiated.
Because C lacks object-oriented programming concepts such as class inheritance and polymorphism, you should consider using derived types in C and C++.
An alternative is to use the special tagged void pointer members to serialize data pointed to by a void* member, which can be any serializable type, such as derived types. This approach uses xsi:type attributes to identify the type of value serialized.
To ensure that wsdl2h generates pointer-based xsd__anyType DOM nodes with wsdl2h -d using option -d for xsd:any, add the following line to typemap.dat:
xsd__any = | xsd__anyType*
This lets wsdl2h produce class/struct members and containers with xsd__anyType* for xsd:any instead of xsd__anyType. To just force all xsd:anyType uses to be pointer-based, declare in typemap.dat:
xsd__anyType = | xsd__anyType*
If you use wsdl2h -d -p using options -d and -p then every class will inherit DOM as shown above. Without option -d, an xsd__anyType type is generated to serve as the root type in the type hierarchy:
where the _XML __item member holds any XML content as a literal XML string.
To use the DOM API, compile dom.c (or dom.cpp for C++), or link the gSOAP library with -lgsoapssl (or -lgsoapssl++ for C++).
🔝 "" "Back to table of contents"
You can use //gsoap directives in the interface header file with the data binding interface for soapcpp2. These directives are used to configure the code generated by soapcpp2 by declaring various. properties of Web services and XML schemas. When using the wsdl2h tool, you will notice that wsdl2h generates directives automatically based on the WSDL and XSD input.
Service directives are applicable to service and operations described by WSDL. Schema directives are applicable to types, elements, and attributes defined by XML schemas.
🔝 "" "Back to table of contents"
A service directive must start at a new line and is of the form:
where <prefix> is the XML namespace prefix of a service binding. The <property> and <value> fields are one of the following:
property | value |
---|---|
name | name of the service, optionally followed by text describing the service |
namespace | URI of the WSDL targetNamespace |
documentation | text describing the service (see also the name property), multiple permitted |
doc | an alias for the documentation property |
style | document (default) SOAP messaging style or rpc for SOAP RPC |
encoding | literal (default), encoded for SOAP encoding, or a custom URI |
protocol | specifies SOAP or REST, see below |
port | URL of the service endpoint, usually an http or https address, to use in the WSDL definitions/service/port/address/@location |
location | an alias for the port property |
endpoint | an alias for the port property |
transport | URI declaration of the transport, usually http://schemas.xmlsoap.org/soap/http |
definitions | name of the WSDL definitions/@name |
type | name of the WSDL definitions/portType/@name (WSDL2.0 interface/@name) |
portType | an alias for the type property (portType follows SOAP 1.1 naming conventions) |
interface | an alias for the type property (interface follows SOAP 1.2 naming conventions) |
binding | name of the WSDL definitions/binding/@name |
portName | name of the WSDL definitions/service/port/@name |
executable | name of the "executable" to use in the WSDL definitions/service/port/address/@location |
The service name and namespace properties are required in order to generate a valid WSDL with soapcpp2. The other properties are optional.
The style and encoding property defaults are changed with soapcpp2 -e option -e to rpc and encoded, respectively.
The protocol property is SOAP by default (SOAP 1.1). Protocol property values are:
protocol value | description |
---|---|
SOAP | SOAP transport, supporting both SOAP 1.1 and 1.2 |
SOAP1.1 | SOAP 1.1 transport (same as soapcpp2 -1) |
SOAP1.2 | SOAP 1.2 transport (same as soapcpp2 -2) |
SOAP-GET | one-way SOAP 1.1 or 1.2 with HTTP GET |
SOAP1.1-GET | one-way SOAP 1.1 with HTTP GET |
SOAP1.2-GET | one-way SOAP 1.2 with HTTP GET |
HTTP | non-SOAP REST protocol with HTTP POST |
POST | non-SOAP REST protocol with HTTP POST |
GET | non-SOAP REST protocol with HTTP GET |
PUT | non-SOAP REST protocol with HTTP PUT |
DELETE | non-SOAP REST protocol with HTTP DELETE |
You can bind service operations to the WSDL namespace of a service by using the namespace prefix as part of the identifier name of the function that defines the service operation:
You can override the port endpoint URL at runtime in the auto-generated soap_call_prefix__func service call (C/C++ client side) and in the C++ proxy class service call.
🔝 "" "Back to table of contents"
Service properties are applicable to a service and to all of its operations. Service method directives are specifically applicable to a service operation.
A service method directive is of the form:
where <prefix> is the XML namespace prefix of a service binding and <method> is the unqualified name of a service operation. The <property> and <value> fields are one of the following:
method property | value |
---|---|
method-documentation | text describing the service operation |
method | an alias for the method-documentation property |
method-action | "" or URI SOAPAction HTTP header, or URL query string for REST protocols |
method-input-action | "" or URI SOAPAction HTTP header of service request messages |
method-output-action | "" or URI SOAPAction HTTP header of service response messages |
method-fault-action | "" or URI SOAPAction HTTP header of service fault messages |
method-header-part | member name of the SOAP_ENV__Header struct used in SOAP Header |
method-input-header-part | member name of the SOAP_ENV__Header struct used in SOAP Headers of requests |
method-output-header-part | member name of the SOAP_ENV__Header struct used in SOAP Headers of responses |
method-fault | type name of a struct or class member used in SOAP_ENV__Details struct |
method-mime-type | REST content type or SOAP MIME attachment content type(s) |
method-input-mime-type | REST content type or SOAP MIME attachment content type(s) of request message |
method-output-mime-type | REST content type or SOAP MIME attachment content type(s) of response message |
method-style | document or rpc |
method-encoding | literal, encoded, or a custom URI for encodingStyle of messages |
method-response-encoding | literal, encoded, or a custom URI for encodingStyle of response messages |
method-protocol | SOAP or REST, see service directives |
The method-header-part properties can be repeated for a service operation to declare multiple SOAP Header parts that the service operation requires. You can use method-input-header-part and method-output-header-part to differentiate between request and response messages.
The method-fault property can be repeated for a service operation to declare multiple faults that the service operation may return.
The method-action property serves two purposes:
Use method-input-action and method-output-action to differentiate the SOAPAction between SOAP request and response messages.
You can always override the port endpoint URL and action values at runtime in the auto-generated soap_call_prefix__func service call (C/C++ client side) and in the auto-generated C++ proxy class service calls. A runtime NULL endpoint URL and/or action uses the defaults set by these directives.
The method-mime-type property serves two purposes:
Use method-input-mime-type and method-output-mime-type to differentiate the attachment types between request and response messages.
🔝 "" "Back to table of contents"
A schema directive is of the form:
where <prefix> is the XML namespace prefix of a schema. The <property> and <value> fields are one of the following:
property | value |
---|---|
namespace | URI of the XSD targetNamespace |
namespace2 | alternate URI pattern for the XSD namespace (i.e. URI is also accepted by the XML parser) |
import | URI of an imported namespace, as an alternative or in addition to namespace, adds xsd:import to the generated WSDL and XSD files |
form | unqualified (default) or qualified local element and attribute form defaults |
elementForm | unqualified (default) or qualified local element form default |
attributeForm | unqualified (default) or qualified local attribute form default |
typed | no (default) or yes for serializers to add xsi:type attributes to XML |
To learn more about the local form defaults, see qualified and unqualified members.
The namespace2 URI is a pattern with * matching any sequence of characters and - matching any character. This pattern instructs the XML parser and validator to also accept the URI pattern as a valid namespace for the specified <prefix>.
The typed property is implicitly yes when soapcpp2 -t option -t is used.
🔝 "" "Back to table of contents"
A schema type directive is of the form:
where <prefix> is the XML namespace prefix of a schema and <name> is an unqualified name of a C/C++ type, and the optional <member> is a class/struct members or enum constant.
You can describe a type with one of the following:
type property | value |
---|---|
type-documentation | text describing the schema type |
type | an alias for the type-documentation property |
For example, you can add a description to an enumeration:
This documented enumeration maps to a simpleType restriction of xsd:string in the soapcpp2-generated schema:
🔝 "" "Back to table of contents"
A presentation on XML data bindings is not complete without discussing the serialization rules and options that put your data in XML on the wire or store it a file or buffer.
There are several options to choose from to serialize data in XML. The choice depends on the use of the SOAP protocol or if SOAP is not required. The wsdl2h tool automates this for you by taking the WSDL transport bindings into account when generating the service functions in C and C++ that use SOAP or REST.
The gSOAP tools are not limited to SOAP. The tools implement generic XML data bindings for SOAP, REST, and other uses of XML. So you can read and write XML using the serializing operations on classes and structs.
The following sections briefly explain the serialization rules with respect to the SOAP protocol for XML Web services. A basic understanding of the SOAP protocol is useful when developing client and server applications that must interoperate with other SOAP applications.
SOAP/REST Web service client and service operations are represented as functions in your interface header file with the data binding interface for soapcpp2. The soapcpp2 tool will translate these function to client-side service invocation calls and server-side service operation dispatchers.
A discussion of SOAP clients and servers is beyond the scope of this article. However, the SOAP options discussed here also apply to SOAP client and server development.
🔝 "" "Back to table of contents"
The wsdl:binding/soap:binding/@style attribute in the <wsdl:binding> section of a WSDL is either "document" or "rpc". The "rpc" style refers to SOAP RPC (Remote Procedure Call), which is more restrictive than the "document" style by requiring one XML element in the SOAP Body to act as the procedure name with XML subelements as its parameters.
For example, the following directives in the interface header file for soapcpp2 declare that DBupdate is a SOAP RPC encoding service method:
The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with one element representing the operation with the parameters as subelements:
The "document" style puts no restrictions on the SOAP Body content. However, we recommend that the first element's tag name in the SOAP Body should be unique to each type of operation, so that the receiver can dispatch the operation based on this element's tag name. Alternatively, the HTTP URL path can be used to specify the operation, or the HTTP action header can be used to dispatch operations automatically on the server side (soapcpp2 options -a and -A).
🔝 "" "Back to table of contents"
The wsdl:operation/soap:body/@use attribute in the <wsdl:binding> section of a WSDL is either "literal" or "encoded". The "encoded" use refers to the SOAP encoding rules that support id-ref multi-referenced elements to serialize data as graphs.
SOAP encoding is very useful if the data internally forms a graph (including cycles) and we want the graph to be serialized in XML in a format that ensures that its structure is preserved. In that case, SOAP 1.2 encoding is the best option.
SOAP encoding also adds encoding rules for SOAP arrays to serialize multi-dimensional arrays. The use of XML attributes to exchange XML data in SOAP encoding is not permitted. The only attributes permitted are the standard XSD attributes, SOAP encoding attributes (such as for arrays), and id-ref.
For example, the following directives in the interface header file for soapcpp2 declare that DBupdate is a SOAP RPC encoding service method:
The XML payload has a SOAP envelope, optional SOAP header, and a SOAP body with an encodingStyle attribute for SOAP 1.1 encoding and an element representing the operation with parameters that are SOAP 1.1 encoded:
In the XML fragment shown above the name "Joe" is shared by two records and the string is referenced by SOAP 1.1 href and id attributes.
While the soapcpp-generated serializers only introduce multi-referenced elements in the payload when they are actually multi-referenced in the data graph, other SOAP applications may render multi-referenced elements more aggressively. The example could also be rendered as:
SOAP 1.2 encoding is cleaner and produces more accurate XML encodings of data graphs by setting the id attribute on the element that is referenced:
To remove all rendered id-ref multi-referenced elements, use the SOAP_XML_TREE flag to initialize the soap context.
Some XSD validation rules are turned off with SOAP encoding, because of the presence of additional attributes, such as id and ref/href, SOAP arrays with arbitrary element tags for array elements, and the occurrence of additional multi-ref elements in the SOAP 1.1 Body.
The use of "literal" puts no restrictions on the XML in the SOAP Body. Full XSD validation is possible, which can be enabled with the SOAP_XML_STRICT flag to initialize the soap context. However, data graphs will be serialized as trees and cycles in the data will be cut from the XML rendition.
🔝 "" "Back to table of contents"
There are two SOAP protocol versions: 1.1 and 1.2. The gSOAP tools can switch between the two versions seamlessly. You can declare the default SOAP version for a service operation as follows:
Use SOAP (SOAP 1.1), SOAP1.1, SOAP1.2, and HTTP to switch SOAP versions or enable REST methods with HTTP POST. See service directives and XML serialization.
The soapcpp2 tool auto-generates client and server code. At the client side, this operation sends data with SOAP 1.2 but accepts responses also in SOAP 1.1. At the server side, this operation accepts requests in SOAP 1.1 and 1.2 and will return responses in the same SOAP version.
As we discussed in the previous section, the SOAP 1.2 protocol has a cleaner multi-referenced element serialization format that greatly enhances the accuracy of data graph serialization with SOAP RPC encoding and is therefore recommended.
The SOAP 1.2 protocol default can also be set by importing and loading gsoap/import/soap12.h:
Finally, the soapcpp2 tool has options to force SOAP 1.1, SOAP 1.2, or remove SOAP altogether with soapcpp2 -1 (SOAP 1.1), soapcpp2 -2 (SOAP 1.2) and soapcpp2 -0 (plain XML, no SOAP).
🔝 "" "Back to table of contents"
You can serialize data to XML that is stored on the heap, on the stack (locals), and static data as long as the serializable (i.e. non-transient) values are properly initialized and pointers in the data structures are either NULL or point to valid structures.
When XML is deserialized into data, the data is put on the heap and managed by the soap context, see also memory management.
You can read and write XML directly to a file or stream with the serializing operations on classes and structs.
To define and use XML Web service client and service operations, we can declare these operations in your interface header file with the data binding interface for soapcpp2 as functions. The function are translated by soapcpp2 to client-side service invocation calls and server-side service operation dispatchers.
The REST operations POST, GET, and PUT are declared with //gsoap directives in the interface header file for soapcpp2. For example, a REST HTTP POST operation is declared as follows:
There are no SOAP Envelope and SOAP Body elements in the payload for DBupdate. Also the XML serialization rules are identical to SOAP document/literal, meaning no SOAP RPC encoding XML structures are implicitly used. The XML payload only has the operation name as an element with its parameters serialized as subelements:
To force id-ref serialization with REST similar to SOAP 1.2 multi-reference encoding, use the SOAP_XML_GRAPH flag to initialize the soap context. The XML serialization includes id and ref attributes for multi-referenced elements as follows:
🔝 "" "Back to table of contents"
Reading and writing XML from/to files, streams and string buffers is done via the managing soap context by setting one of the following context variables that control IO sources and sinks:
Normally, all of these context variables are NULL, which is required to send and receive data over sockets by gSOAP client and server applications. Therefore, if you set any of these context variables in a client or server application then you should reset them to NULL to ensure that socket communications are not blocked.
In the following sections, we present more details on how to read and write to files and streams, and use string buffers as sources and sinks for XML data.
In addition, you can set IO callback functions to handle IO at a lower level. For more details on defining your own callback functions, see the gSOAP user guide.
🔝 "" "Back to table of contents"
The default IO is standard input and output. Other sources and sinks (those listed above) will be used until you (re)set them. For example with file-based input and output:
Similar code with streams in C++:
🔝 "" "Back to table of contents"
For C++ we recommend to use std::stringstream objects from the sstream C++ library as illustrated in the following example:
For C we can use soap::is and soap::os to point to strings of XML content as follows (this requires gSOAP 2.8.28 or later):
The type of soap::os is a pointer to a const char* string. The pointer is set by the managing soap context to point to the XML data that is stored on the context-managed heap.
For earlier gSOAP versions we recommend to use IO callbacks soap::frecv and soap::fsend, see the gSOAP user guide..
🔝 "" "Back to table of contents"
Memory management with the soap context enables us to allocate data in context-managed heap space that can be collectively deleted. All deserialized data is placed on the context-managed heap by the soap context of the engine.
🔝 "" "Back to table of contents"
When working with gSOAP in C (i.e. using wsdl2h -c option -c or soapcpp2 -c option -c), data is allocated on the managed heap with:
This function allocates len bytes on the heap managed by the specified context and returns NULL when allocation failed.
You can also make shallow copies of data with soap_memdup that uses soap_malloc and a safe version of memcpy to copy a chunk of data src with length len to the context-managed heap:
This function returns a pointer to the copy. This function requires gSOAP 2.8.27 or later.
In gSOAP 2.8.35 and greater versions, you can use an auto-generated function to allocate and initialize data of type T on the managed heap:
This function returns an array of length n of type T data that is default initialized (by internally calling soap_malloc(soap, n * sizeof(T)) and then soap_default_T(soap, T*) on each array value). Use a negative value or n=1 to allocate and initialize a single value. This function returns NULL when allocation failed.
The soap_malloc function is essentially a wrapper around malloc, but permits the soap context to track all heap allocations for collective deletion with soap_end(soap):
All data on the managed heap is mass-deleted with soap_end(soap) which must be called before soap_done(soap) or soap_free(soap), which end the use of the soap context and free the context, respectively. Use soap_free(soap) only when the context is allocated with soap_new(). Use soap_done(soap) only when the context is stack allocated (so cannot be deleted from the heap).
The managed heap is checked for memory leaks at run time when the source code is compiled with option -DDEBUG.
The soapcpp2 auto-generated deserializers in C use soap_malloc to allocate and populate deserialized structures, which are managed by the context for collective deletion.
To make char* and wchar_t* string copies to the context-managed heap, we can use the functions:
If your C compiler supports typeof then you can use the following macro to simplify the managed heap allocation and initialization of primitive values:
Pointers to primitive values are often used for optional members. For example, assume we have the following struct:
Use soap_assign to create a SSN value on the managed heap:
Without the soap_assign macro, you will need two lines of code, one to allocate and one to assign (you should also use this if your system can run out of memory):
The serializer can serialize any heap, stack, or static allocated data. So we can also create a new record as follows:
Use the soapcpp2 auto-generated soap_dup_T functions to duplicate data into another soap context (this requires soapcpp2 -Ec option -Ec to generate), here shown for C with the second argument dst NULL because we want to allocate a new managed structure:
The only reason to use another soap context and not to use the primary soap context is when the primary context must be destroyed together with all of the objects it manages while some of the objects must be kept alive. If the objects that are kept alive contain deep cycles then this is the only option we have, because deep copy with a managing soap context detects and preserves these cycles unless the SOAP_XML_TREE flag is used with the soap context:
The resulting deep copy will be a full copy of the source data structure as a tree without co-referenced data (i.e. no digraph) and without cycles. Cycles are pruned and (one of the) pointers that forms a cycle is repaced by NULL.
You can also deep copy into unmanaged space and use the auto-generated soap_del_T() function (requires soapcpp2 -Ed option -Ed to generate) to delete it later:
But you should not do this for any data that has deep cycles in its runtime data structure. Cycles in the data structure will lead to non-termination when making unmanaged deep copies. Consider for example:
The code to populate a structure with a mutual spouse relationship:
The bad case is where there is no context used in the first argument. The deep copy functions use a context to keep track of co-referenced data nodes and cycles in the data structure copies, to copy co-referenced nodes just once. Co-references in a data structure are formed by pointers and smart pointers such as std::shared_ptr, such that at least two pointers point to the same data.
The serializer can serialize any heap, stack, or static allocated data, such as in the code shown above. So we can serialize the stack-allocated pers1 record as follows:
which produces an XML document record.xml that is similar to:
Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph leads to the same non-termination problem when we later try to copy the data into unmanaged memory heap space:
Copying data with soap_dup_T(soap) into managed heap memory space is always safe. Copying into unmanaged heap memory space requires diligence. But deleting unmanaged data is easy with soap_del_T().
You can also use soap_del_T() to delete structures that you created in C, but only if these structures are created with malloc and do NOT contain pointers to stack and static data.
You can unlink one or more allocated objects from the managed heap to allow the object to live after soap_end(soap) by using:
The unlinked heap-allocated data pointed to by ptr can be accessed after soap_end(soap). Do not forget to free the data with free(ptr). Be aware that soap_unlink(soap, ptr) does not perform a deep unlinkage. If ptr is a struct, pointer members will become invalid when pointing to objects on the managed heap. Use soap_unlink(soap, ptr->member) to unlink member as well.
Finally, when data is allocated in managed memory heap space, either explicitly with the allocation functions shown above or by the soapcpp2-generated deserializers, you can delegate the management and deletion of this data to another soap context. That context will be responsible to delete the data with soap_end(soap) later:
This allows the soap_from context to be deleted with soap_free(soap_from) (assuming it is allocated with soap_new(), use soap_done(soap_from) when soap_from is stack-allocated) while the managed data remains intact. You can use this function any time, to delegate management and deletion to another context soap_to and then continue with the current context. You can also use different source soap_from contexts to delegate management and deletion to the other soap_to context. To mass delete all managed data, use soap_end(soap_to).
🔝 "" "Back to table of contents"
When working with gSOAP in C++, the engine allocates data on a managed heap using soap_new_T(soap) to allocate a type with type name T. Managed heap allocation is tracked by the soap context for collective deletion with soap_destroy(soap) for structs, classes, and templates and with soap_end(soap) for everything else.
You should only use soap_malloc(struct soap*, size_t len) to allocate primitive types because constructors are not invoked. Therefore, soap_new_T is preferred. The auto-generated T * soap_new_T(struct soap*) returns data allocated on the managed heap for type T. The data is mass-deleted with soap_destroy(soap) followed by soap_end(soap).
The soap_new_T functions return NULL when allocation fails. C++ exceptions are never raised by the engine and serializers when data is allocated.
There are four variations of soap_new_T functions to allocate data of type T that soapcpp2 auto-generates:
The above functions can be invoked with a NULL soap context, but you are then responsible to use delete T to remove this instance from the unmanaged heap.
For example, to allocate a managed std::string you can use:
To throw a std::bad_alloc exception when memory allocation fails, we can define the following class and macro:
And use CHECK as follows to throw an exception when memory allocation fails:
To throw a std::runtime_exception when memory allocation fails, with file and line number information where the error occurred, we can define the following revised version of our exception-throwing macro:
And use CHECK as follows to throw an exception with the file and line number of the location where memory allocation failed:
Primitive types and arrays of primitive values may be allocated with soap_malloc (actually, soap_new_T calls soap_malloc for primitive type T). All primitive types (i.e. no classes, structs, class templates, containers, and smart pointers) are allocated with soap_malloc for reasons of efficiency.
You can use a C++ template to simplify the managed allocation and initialization of primitive values as follows (this is for primitive types only):
For example, assuming we have the following class:
You can instantiate a record by using the auto-generated soap_new_set_ns__record and use soap_make to create a SSN value on the managed heap as follows:
All data on the managed heap is mass-deleted with soap_end(soap) which must be called before soap_done(soap) or soap_free(soap), which end the use of the soap context and free the context, respectively. Use soap_free(soap) only when the context is allocated with soap_new(). Use soap_done(soap) only when the context is stack allocated (so cannot be deleted from the heap).
The managed heap is checked for memory leaks at run time when the source code is compiled with option -DDEBUG.
However, the serializer can serialize any heap, stack, or static allocated data. So we can also create a new record as follows:
which will be fine to serialize this record as long as the local SSN stack-allocated value remains in scope when invoking the serializer and/or using record. It does not matter if soap_destroy and soap_end are called beyond the scope of SSN.
To facilitate class methods to access the managing context, we can add a soap context pointer to a class/struct:
The soap context pointer member of the class is set when invoking soap_new_T (and similar) with a non-NULL context argument that will be assigned to the soap member of the class.
You can also use a template when an array of pointers to values is required. To create an array of pointers to values, define the following template:
The array parameter is a pointer to an array of n values. The template returns an array of n pointers that point to the values in that array:
Note that soap_new_ns__record(soap, n) returns a pointer to an array of n records, which is then used to create an array of n pointers to these records.
Use the soapcpp2 auto-generated soap_dup_T functions to duplicate data into another soap context (this requires soapcpp2 -Ec option -Ec to generate), here shown for C++ with the second argument dst NULL to allocate a new managed object:
To duplicate base and derived instances when a base class pointer or reference is provided, use the auto-generated method T * T::soap_dup(struct soap*):
The only reason to use another context and not to use the primary soap context is when the primary context must be destroyed together with all of the objects it manages while some of the objects must be kept alive. If the objects that are kept alive contain deep cycles then this is the only option we have, because deep copy with a managing soap context detects and preserves these cycles unless the SOAP_XML_TREE flag is used with the context:
The resulting deep copy will be a full copy of the source data structure as a tree without co-referenced data (i.e. no digraph) and without cycles. Cycles are pruned and (one of the) pointers that forms a cycle is repaced by NULL.
You can also deep copy into unmanaged space and use the auto-generated soap_del_T() function or the T::soap_del() method (requires soapcpp2 -Ec option -Ec to generate) to delete it later, but we should not do this for any data that has deep cycles in its runtime data structure graph:
Cycles in the data structure will lead to non-termination when making unmanaged deep copies. Consider for example:
The code to populate a structure with a mutual spouse relationship:
The serializer can serialize any heap, stack, or static allocated data, such as shown in the code shown above. So we can serialize the stack-allocated pers1 record as follows:
which produces an XML document record.xml that is similar to:
Deserialization of an XML document with a SOAP 1.1/1.2 encoded id-ref graph leads to the same non-termination problem when we later try to copy the data into unmanaged space:
Copying data with soap_dup_T(soap) into managed space is always safe. Copying into unmanaged space requires diligence. But deleting unmanaged data is easy with soap_del_T().
You can also use soap_del_T() to delete structures in C++, but only if these structures are created with new (and new [] for arrays when applicable) for classes, structs, and class templates and with malloc for anything else, and the structures do NOT contain pointers to stack and static data.
You can unlink one or more allocated objects from the managed heap to allow the object to live after soap_destroy(soap) and soap_end(soap) by using:
The unlinked heap-allocated data pointed to by ptr can be accessed after soap_destroy(soap) and soap_end(soap). Do not forget to free the data with delete ptr (C++ class instance only) or with free(ptr) (non-class data). Be aware that soap_unlink(soap, ptr) does not perform a deep unlinkage. If ptr is a struct or class, pointer members will become invalid when pointing to objects on the managed heap. Use soap_unlink(soap, ptr->member) to unlink member as well.
Finally, when data is allocated in managed memory heap space, either explicitly with the allocation functions shown above or by the soapcpp2-generated deserializers, you can delegate the management and deletion of this data to another soap context. That context will be responsible to delete the data with soap_destroy(soap) and soap_end(soap) later:
This allows the soap_from context to be deleted with soap_free(soap_from) (assuming it is allocated with soap_new(), use soap_done(soap_from) when soap_from is stack-allocated) while the managed data remains intact. You can use this function any time, to delegate management and deletion to another context soap_to and then continue with the current context. You can also use different source soap_from contexts to delegate management and deletion to the other soap_to context. To mass delete all managed data, use soap_destroy(soap_to) followed by soap_end(soap_to).
🔝 "" "Back to table of contents"
There are several soap context initialization flags and context mode flags to control XML serialization at runtime. The flags are set with soap_new1() to allocate and initialize a new context:
and with soap_init1() for stack-allocated contexts:
where flag1, flag2, ..., flagn is one of:
🔝 "" "Back to table of contents"
After allocation and initializtion of a soap context, several context parameters can be set (some parameters may require 2.8.31 or greater):
🔝 "" "Back to table of contents"
The gSOAP API functions return SOAP_OK (zero) or a non-zero error code. The error code is stored in int soap::error of the current soap context. Error messages can be displayed with:
C++ exceptions are never raised by the engine or serializers, even when data is allocated.
A SOAP_EOM error code is returned when memory was exhausted during processing of input and/or output of data.
An EOF (SOAP_EOF or -1) error code is returned when the parser has hit EOF but expected more input, or when socket communications timed out. In addition to the SOAP_EOF error, the int soap::errnum of the soap context is set to the errno value of the operation that failed. For timeouts, the soap::ernum value is always 0 instead of an errno error code.
Use soap_xml_error_check(soap->error) to check for XML errors. This returns true (non-zero) when a parsing and validation error has occurred.
For example:
When deploying your application on UNIX and Linux systems, UNIX signal handlers should be added to your code handle signals, in particular SIGPIPE:
where the sigpipe_handler is a function:
Other UNIX signals may have to be handled as well.
The engine is designed for easy memory cleanup after being interrupted. Use soap_destroy(soap) and soap_end(soap), after which the soap context can be reused.
🔝 "" "Back to table of contents"
In general, to use the generated code:
Additional notes with respect to the wsdl2h and soapcpp2 tools:
🔝 "" "Back to table of contents"
The soapcpp2 tool generates a .nsmap file that includes two bindings for SOAP namespaces. We can remove all SOAP namespaces (and SOAP processing logic) with soapcpp2 -0 option -0 or by simply setting the two entries to NULL:
Once the .nsmap is generated, you can copy-paste the content into your project code. However, if we rerun wsdl2h on updated WSDL/XSD files or typemap.dat declarations then we need to use the updated table.
In cases that no XML namespaces are used at all, for example with XML-RPC, you may use an empty namespace table:
However, beware that any built-in xsi attributes that are rendered will lack the proper namespace binding. At least we suggest to use SOAP_XML_NOTYPE for this reason.
🔝 "" "Back to table of contents"
Select the project files below to peruse the source code examples.
🔝 "" "Back to table of contents"
🔝 "" "Back to table of contents"
🔝 "" "Back to table of contents"
Building the AddressBook example:
wsdl2h -g -taddresstypemap.dat address.xsd soapcpp2 -0 -C -S -paddress -I../../import address.h c++ -I../.. address.cpp addressC.cpp -o address -lgsoap++
Using wsdl2h -g -taddresstypemap.dat option -g produces bindings for global (root) elements in addition to types and option -taddresstypemap.dat specifies a mapping file, see further below.
In this case the root element a:address-book is bound to _a__address_book. The complexType a:address is bound to class a__address, which is also the type of _a__address_book. This option is not required, but allows you to use global element tag names when referring to their serializers, instead of their type name. Using soapcpp2 -0 -C -S -paddress option -0 removes the SOAP protocol and the combination of the two options -C and -S removes client and server code generation (using option -C alone generates client code and using option -S alone generates server code). Option -paddress renames the output soap-prefixed files to address-prefixed files.
See the address.cpp implementation and related pages.
The addresstypemap.dat file specifies the XML namespace prefix for the bindings:
# Bind the address book schema namespace to prefix 'a' a = "urn:address-book-example" # By default the xsd:dateTime schema type is translated to time_t # To map xsd:dateTime to struct tm, enable the following line: # xsd__dateTime = #import "../../custom/struct_tm.h" # ... and compile/link with custom/struct_tm.c
The DOB field is a xsd:dateTime, which is bound to time_t by default. To change this to struct tm, enable the import of the xsd__dateTime custom serializer by uncommenting the definition of xsd__dateTime in addresstypemap.dat. Then change soap_dateTime2s to soap_xsd__dateTime2s in the code.
Building the graph serialization example:
soapcpp2 -C -S -pgraph -I../../import graph.h c++ -I../.. graph.cpp graphC.cpp -o graph -lgsoap++
To compile without using the -lgsoap++ library: simply compile stdsoap2.cpp together with the above.
🔝 "" "Back to table of contents"
To execute the AddressBook example:
./address
To execute the Graph serialization example:
./graph