From 0bce70a0e483294b83b8bf9d5468838a63405612 Mon Sep 17 00:00:00 2001
From: Boris Kolpackov
+
+
@@ -398,6 +408,17 @@
that would otherwise not fit into memory.
7 Binary Representation
+
+
+
+
+ 7.1 CDR (Common Data Representation)
+ 7.2 XDR (eXternal Data Representation)
+ 7.3 Custom Representations
Besides reading from and writing to XML, the C++/Hybrid mapping + also supports saving the object model to and loading it from a + number of predefined as well as custom binary formats. Binary + representations contain only the data without any meta information + or markup. Consequently, saving to and loading from a binary + format can be an order of magnitude faster as well as result + in a much smaller footprint compared to parsing and serializing + the same data in XML. Furthermore, the resulting representation + is normally several times smaller than the equivalent XML + representation.
+The Embedded C++/Hybrid mapping was specifically designed and optimized for mobile and embedded systems where hardware constraints require high efficiency and economical use of @@ -5090,6 +5111,274 @@ age: 25 + + +
Besides reading from and writing to XML, the C++/Hybrid mapping + also allows you to save the object model to and load it from a + number of predefined as well as custom data representation + formats. The predefined binary formats are CDR (Common Data + Representation) and XDR (eXternal Data Representation). A + custom format can easily be supported by providing + insertion and extraction operators for basic types.
+ +Binary representations contain only the data without any meta + information or markup. Consequently, saving to and loading + from a binary representation can be an order of magnitude + faster as well as result in a much smaller footprint compared + to parsing and serializing the same data in XML. Furthermore, + the resulting representation is normally several times smaller + than the equivalent XML representation. These properties make a + binary representation ideal for internal data exchange and storage. + A typical application that uses this facility stores the data and + communicates within the system using a binary format and reads/writes + the data in XML when communicating with the outside world.
+ +In order to request the generation of insertion and extraction
+ operators for a specific predefined or custom data representation
+ stream, you will need to use the --generate-insertion
+ and --generate-extraction
compiler options. See the
+ XSD/e
+ Compiler Command Line Manual for more information.
The XSD/e runtime provides implementations of the base insertion
+ and extraction operators for the ACE (Adaptive Communication
+ Environment) CDR streams and the XDR API. The XDR API is available
+ out of the box on most POSIX systems as part of Sun RPC. On other
+ platforms you may need to install a third-party library which
+ provides the XDR API.
+
+ The XSD/e compiler recognizes two special argument values to the
+ --generate-insertion
and --generate-extraction
+ options: CDR
and XDR
. When one of these
+ arguments is specified, the corresponding implementation from the
+ XSD/e runtime is automatically used. The following two sections
+ describe each of these two formats in more detail. It is also
+ possible to add support for saving the object model to and loading
+ it from custom data representation formats as discussed in the
+ last section of this chapter.
The saving of the object model types to a representation stream
+ is implemented with stream insertion operators
+ (operator<<
). Similarly, loading of the object
+ model from a representation stream is implemented with stream
+ extraction operators (operator>>
). The insertion
+ and extraction operators for the built-in XML Schema types as
+ well as the sequence templates are provided by the stream
+ implementation (that is, by the XSD/e runtime in case of CDR and
+ XDR and by you for custom formats). The XSD/e compiler automatically
+ generates insertion and extraction operators for the generated object
+ model types.
When C++ exceptions are enabled (Section 3.3, "C++ + Exceptions"), the signatures of the insertion and extraction + operators are as follows:
+ ++void +operator<< (ostream&, const type&); + +void +operator>> (istream&, type&); ++ +
The insertion and extraction errors are indicated by throwing + stream-specific exceptions. When C++ exceptions are disabled, + the signatures of the insertion and extraction operators are + as follows:
+ ++bool +operator<< (ostream&, const type&); + +bool +operator>> (istream&, type&); ++ +
In this case the insertion and extraction operators return
+ true
if the operation was successful and
+ false
otherwise. The stream object may
+ provide additional error information.
When you request the generation of CDR stream insertion and extraction
+ operators, the ocdrstream
and icdrstream
+ types are defined in the xml_schema
namespace. Additionally,
+ if C++ exceptions are enabled, the cdr_exception
exception
+ is also defined in xml_schema
. The icdrstream
+ and ocdrstream
types are simple wrappers for the
+ ACE_InputCDR and ACE_OutputCDR streams. The following code fragment
+ shows how we can use these types when C++ exceptions are enabled:
+try +{ + const type& x = ... // Object model. + + // Save to a CDR stream. + // + ACE_OutputCDR ace_ocdr; + xml_schema::ocdrstream ocdr (ace_ocdr); + + ocdr << x; + + // Load from a CDR stream. + // + ACE_InputCDR ace_icdr (buf, size); + xml_schema::icdrstream icdr (ace_icdr); + + type copy; + icdr >> copy; +} +catch (const xml_schema::cdr_exception&) +{ + cerr << "CDR operation failed" << endl; +} ++ +
The same code fragment but when C++ exceptions are disabled:
+ ++const type& x = ... // Object model. + +// Save to a CDR stream. +// +ACE_OutputCDR ace_ocdr; +xml_schema::ocdrstream ocdr (ace_ocdr); + +if (!(ocdr << x)) +{ + cerr << "CDR operation failed" << endl; +} + +// Load from a CDR stream. +// +ACE_InputCDR ace_icdr (buf, size); +xml_schema::icdrstream icdr (ace_icdr); + +type copy; + +if (!(icdr >> copy)) +{ + cerr << "CDR operation failed" << endl; +} ++ +
The cdr
example which can be found in the
+ examples/cxx/hybrid/binary/
directory of the XSD/e
+ distribution includes complete source code that shows how to
+ save the object model to and load it from the CDR format.
When you request the generation of XDR stream insertion and extraction
+ operators, the oxdrstream
and xcdrstream
+ types are defined in the xml_schema
namespace. Additionally,
+ if C++ exceptions are enabled, the xdr_exception
exception
+ is also defined in xml_schema
. The ixdrstream
+ and oxdrstream
types are simple wrappers for the XDR
+ API. The following code fragment shows how we can use these types
+ when C++ exceptions are enabled:
+try +{ + const type& x = ... // Object model. + + // Save to a XDR stream. + // + XDR xdr; + xdrrec_create (&xdr, ...); + xml_schema::oxdrstream oxdr (xdr); + + oxdr << x; + + // Load from a XDR stream. + // + xdrrec_create (&xdr, ...); + xml_schema::ixdrstream ixdr (xdr); + + type copy; + ixdr >> copy; +} +catch (const xml_schema::xdr_exception&) +{ + cerr << "XDR operation failed" << endl; +} ++ +
The same code fragment but when C++ exceptions are disabled:
+ ++const type& x = ... // Object model. + +// Save to a XDR stream. +// +XDR xdr; +xdrrec_create (&xdr, ...); +xml_schema::oxdrstream oxdr (xdr); + +if (!(oxdr << x)) +{ + cerr << "XDR operation failed" << endl; +} + +// Load from a XDR stream. +// +xdrrec_create (&xdr, ...); +xml_schema::ixdrstream ixdr (xdr); + +type copy; + +if (!(ixdr >> copy)) +{ + cerr << "XDR operation failed" << endl; +} ++ +
The xdr
example which can be found in the
+ examples/cxx/hybrid/binary/
directory of the XSD/e
+ distribution includes complete source code that shows how to
+ save the object model to and load it from the XDR format.
To add support for saving the object model to and loading it + from a custom format, you will need to perform the following + general steps:
+ +--generate-xml-schema
+ compiler option.--generate-insertion
+ and --generate-extraction
options. The arguments
+ to these options will be your custom output and input stream
+ types, respectively. Use the --hxx-prologue
+ option to include the definitions for these stream types
+ into the generated code. Also use the
+ --extern-xml-schema
option to include the
+ header file obtained in the first step instead of generating
+ the same code directly.The custom
example which can be found in the
+ examples/cxx/hybrid/binary/
directory of the XSD/e
+ distribution includes complete source code that shows how to
+ save the object model to and load it from a custom format using
+ the raw binary representation as an example. You can use the
+ source code from this example as a base to implement support
+ for your own format.
--suppress-serializer-val
--generate-insertion os
os
output stream type. Repeat this
+ option to specify more than one stream type. The special
+ CDR
and XDR
arguments
+ are recognized as ACE CDR and Sun RPC XDR stream types and
+ the corresponding stream wrappers provided by the XSD/e runtime
+ are automatically used. For custom stream types use the
+ --hxx-prologue*
options to include the
+ necessary declarations.--generate-extraction is
is
input stream type. Repeat this
+ option to specify more than one stream type. The special
+ CDR
and XDR
arguments
+ are recognized as ACE CDR and Sun RPC XDR stream types and
+ the corresponding stream wrappers provided by the XSD/e runtime
+ are automatically used. For custom stream types use the
+ --hxx-prologue*
options to include the
+ necessary declarations.--generate-forward