diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2009-03-08 17:23:30 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2009-03-08 17:23:30 +0200 |
commit | 0bce70a0e483294b83b8bf9d5468838a63405612 (patch) | |
tree | d11afb4998d6980435c15c4df6e40b1979531672 /examples/cxx/hybrid/binary/cdr | |
parent | 6c63b913179127e09ed7d9da8920493ccceec6ce (diff) |
Add support for binary representations
xsde/cxx/hybrid/insertion-*: insertion operators generator
xsde/cxx/hybrid/extraction-*: extraction operators generator
libxsde/xsde/cxx/hybrid/cdr/: CDR support code
libxsde/xsde/cxx/hybrid/xdr/: XDR support code
tests/cxx/hybrid/binary/: new tests
examples/cxx/hybrid/binary/: new examples
documentation/cxx/hybrid/guide/: new chapter
Diffstat (limited to 'examples/cxx/hybrid/binary/cdr')
-rw-r--r-- | examples/cxx/hybrid/binary/cdr/README | 51 | ||||
-rw-r--r-- | examples/cxx/hybrid/binary/cdr/driver.cxx | 128 | ||||
-rw-r--r-- | examples/cxx/hybrid/binary/cdr/library.xml | 53 | ||||
-rw-r--r-- | examples/cxx/hybrid/binary/cdr/library.xsd | 69 | ||||
-rw-r--r-- | examples/cxx/hybrid/binary/cdr/makefile | 112 |
5 files changed, 413 insertions, 0 deletions
diff --git a/examples/cxx/hybrid/binary/cdr/README b/examples/cxx/hybrid/binary/cdr/README new file mode 100644 index 0000000..52045f0 --- /dev/null +++ b/examples/cxx/hybrid/binary/cdr/README @@ -0,0 +1,51 @@ +This example shows how to save/load the C++/Hybrid object model to/from +CDR (Common Data Representation) binary format using ACE CDR streams. +You will need the ACE library installed in order to build and run this +example: + +http://www.cs.wustl.edu/~schmidt/ACE.html + +The example consists of the following files: + +library.xsd + XML Schema which describes a library of books. + +library.xml + Sample XML instance document. + +library.hxx +library.cxx + +library-pskel.hxx +library-pskel.cxx +library-pimpl.hxx +library-pimpl.cxx + +library-pskel.hxx +library-pskel.cxx +library-pimpl.hxx +library-pimpl.cxx + Object model (the first pair of files), parser skeletons (the + second pair), parser implementations (the third pair), serializer + skeletons (the fourth pair), and serializer implementations (the + fifth pair). These files are generated by the XSD/e compiler from + library.xsd. The --generate-parser, --generate-serializer, and + --generate-aggregate options were used to request the generation + of the parsing and serialization code. The --generate-insertion and + --generate-extraction options were used to generate the insertion + and extraction operations for the CDR streams. + +driver.cxx + Driver for the example. It first calls the parser that constructs + the object model from the input XML file. It then saves the object + model to the CDR representation and loads it back. Finally, the + driver calls the serializer to serialize the loaded object model + back to XML. + +To run the example on the sample XML instance document simply execute: + +$ ./driver library.xml + +The example reads from STDIN if input file is not specified: + +$ ./driver <library.xml diff --git a/examples/cxx/hybrid/binary/cdr/driver.cxx b/examples/cxx/hybrid/binary/cdr/driver.cxx new file mode 100644 index 0000000..18848eb --- /dev/null +++ b/examples/cxx/hybrid/binary/cdr/driver.cxx @@ -0,0 +1,128 @@ +// file : examples/cxx/hybrid/binary/cdr/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include <string.h> // memcpy + +#include <memory> // std::auto_ptr +#include <iostream> + +#include <ace/Log_Msg.h> // ACE_HEX_DUMP +#include <ace/CDR_Stream.h> + +#include "library.hxx" +#include "library-pimpl.hxx" +#include "library-simpl.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + const char* input; + + if (argc < 2) + { + input = "STDIN"; + cerr << "XML file not specified, reading from STDIN" << endl; + } + else + input = argv[1]; + + try + { + using namespace library; + + // Parse. + // + catalog_paggr catalog_p; + + xml_schema::document_pimpl doc_p ( + catalog_p.root_parser (), + catalog_p.root_namespace (), + catalog_p.root_name ()); + + catalog_p.pre (); + + if (argc < 2) + doc_p.parse (std::cin); + else + doc_p.parse (argv[1]); + + std::auto_ptr<catalog> c (catalog_p.post ()); + + // Save the object model to a CDR stream. + // + ACE_OutputCDR ace_ocdr; + xml_schema::ocdrstream ocdr (ace_ocdr); + ocdr << *c; + + // Print the binary representation and at the same time save + // it into a continuous buffer. + // + cerr << "binary representation size: " << ace_ocdr.total_length () << endl + << endl; + + xml_schema::buffer buf (ace_ocdr.total_length ()); + char* data = buf.data (); + + for (const ACE_Message_Block* mb = ace_ocdr.begin (); + mb != 0; + mb = mb->cont ()) + { + memcpy (data, mb->rd_ptr (), mb->length ()); + data += mb->length (); + + ACE_HEX_DUMP ((LM_DEBUG, mb->rd_ptr (), mb->length ())); + } + + // Load the object model from a CDR stream. Note that ACE_InputCDR + // expects the buffer to be properly aligned. Since our buffer is + // dynamically allocated, its alignment should be good enough. + // + ACE_InputCDR ace_icdr (buf.data (), buf.size ()); + xml_schema::icdrstream icdr (ace_icdr); + std::auto_ptr<catalog> copy (new catalog); + icdr >> *copy; + + // Serialize the copy back to XML. + // + catalog_saggr catalog_s; + + xml_schema::document_simpl doc_s ( + catalog_s.root_serializer (), + catalog_s.root_namespace (), + catalog_s.root_name ()); + + doc_s.add_prefix ("lib", "http://www.codesynthesis.com/library"); + doc_s.add_schema ("http://www.codesynthesis.com/library", "library.xsd"); + + catalog_s.pre (*c); + doc_s.serialize (std::cout); + catalog_s.post (); + } + catch (const xml_schema::cdr_exception&) + { + cerr << "CDR operation failed" << endl; + return 1; + } + catch (const xml_schema::parser_exception& e) + { + cerr << input << ":" << e.line () << ":" << e.column () << ": " + << e.text () << endl; + return 1; + } + catch (const xml_schema::serializer_exception& e) + { + cerr << "error: " << e.text () << endl; + return 1; + } + catch (const std::ios_base::failure&) + { + cerr << input << ": unable to open or read/write failure" << endl; + return 1; + } + + return 0; +} diff --git a/examples/cxx/hybrid/binary/cdr/library.xml b/examples/cxx/hybrid/binary/cdr/library.xml new file mode 100644 index 0000000..6bfb4d8 --- /dev/null +++ b/examples/cxx/hybrid/binary/cdr/library.xml @@ -0,0 +1,53 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/hybrid/binary/cdr/library.xml +author : Boris Kolpackov <boris@codesynthesis.com> +copyright : not copyrighted - public domain + +--> + +<lib:catalog xmlns:lib="http://www.codesynthesis.com/library" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.codesynthesis.com/library library.xsd"> + + <book available="false"> + <isbn>0679760806</isbn> + <title>The Master and Margarita</title> + <genre>fiction</genre> + + <author> + <name>Mikhail Bulgakov</name> + <born>1891-05-15</born> + <died>1940-03-10</died> + </author> + </book> + + + <book available="true" > + <isbn>0679600841</isbn> + <title>War and Peace</title> + <genre>history</genre> + + <author> + <name>Leo Tolstoy</name> + <born>1828-09-09</born> + <died>1910-11-20</died> + </author> + </book> + + + <book available="false"> + <isbn>0679420290</isbn> + <title>Crime and Punishment</title> + <genre>philosophy</genre> + + <author> + <name>Fyodor Dostoevsky</name> + <born>1821-11-11</born> + <died>1881-02-09</died> + </author> + </book> + +</lib:catalog> diff --git a/examples/cxx/hybrid/binary/cdr/library.xsd b/examples/cxx/hybrid/binary/cdr/library.xsd new file mode 100644 index 0000000..0b0d9d1 --- /dev/null +++ b/examples/cxx/hybrid/binary/cdr/library.xsd @@ -0,0 +1,69 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/hybrid/binary/cdr/library.xsd +author : Boris Kolpackov <boris@codesynthesis.com> +copyright : not copyrighted - public domain + +--> + +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:lib="http://www.codesynthesis.com/library" + targetNamespace="http://www.codesynthesis.com/library"> + + <xsd:simpleType name="isbn"> + <xsd:restriction base="xsd:unsignedInt"/> + </xsd:simpleType> + + + <xsd:complexType name="title"> + <xsd:simpleContent> + <xsd:extension base="xsd:string"> + <xsd:attribute name="lang" type="xsd:string"/> + </xsd:extension> + </xsd:simpleContent> + </xsd:complexType> + + + <xsd:simpleType name="genre"> + <xsd:restriction base="xsd:string"> + <xsd:enumeration value="romance"/> + <xsd:enumeration value="fiction"/> + <xsd:enumeration value="horror"/> + <xsd:enumeration value="history"/> + <xsd:enumeration value="philosophy"/> + </xsd:restriction> + </xsd:simpleType> + + + <xsd:complexType name="author"> + <xsd:sequence> + <xsd:element name="name" type="xsd:string"/> + <xsd:element name="born" type="xsd:date"/> + <xsd:element name="died" type="xsd:date" minOccurs="0"/> + </xsd:sequence> + </xsd:complexType> + + + <xsd:complexType name="book"> + <xsd:sequence> + <xsd:element name="isbn" type="lib:isbn"/> + <xsd:element name="title" type="lib:title"/> + <xsd:element name="genre" type="lib:genre"/> + <xsd:element name="author" type="lib:author" maxOccurs="unbounded"/> + </xsd:sequence> + <xsd:attribute name="available" type="xsd:boolean" use="required"/> + </xsd:complexType> + + + <xsd:complexType name="catalog"> + <xsd:sequence> + <xsd:element name="book" type="lib:book" maxOccurs="unbounded"/> + </xsd:sequence> + </xsd:complexType> + + + <xsd:element name="catalog" type="lib:catalog"/> + +</xsd:schema> diff --git a/examples/cxx/hybrid/binary/cdr/makefile b/examples/cxx/hybrid/binary/cdr/makefile new file mode 100644 index 0000000..eba1c77 --- /dev/null +++ b/examples/cxx/hybrid/binary/cdr/makefile @@ -0,0 +1,112 @@ +# file : examples/cxx/hybrid/binary/cdr/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../../build/bootstrap.make + +xsd := library.xsd +cxx := driver.cxx + +obj := $(addprefix $(out_base)/,\ +$(cxx:.cxx=.o) \ +$(xsd:.xsd=.o) \ +$(xsd:.xsd=-pskel.o) \ +$(xsd:.xsd=-pimpl.o) \ +$(xsd:.xsd=-sskel.o) \ +$(xsd:.xsd=-simpl.o)) + +dep := $(obj:.o=.o.d) + +xsde.l := $(out_root)/libxsde/xsde/xsde.l +xsde.l.cpp-options := $(out_root)/libxsde/xsde/xsde.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +dist-win := $(out_base)/.dist-win +clean := $(out_base)/.clean + +# Build. +# +$(driver): $(obj) $(xsde.l) + +$(obj) $(dep): $(xsde.l.cpp-options) + +genf := $(xsd:.xsd=.hxx) $(xsd:.xsd=.cxx) \ + $(xsd:.xsd=-pskel.hxx) $(xsd:.xsd=-pskel.cxx) \ + $(xsd:.xsd=-pimpl.hxx) $(xsd:.xsd=-pimpl.cxx) \ + $(xsd:.xsd=-sskel.hxx) $(xsd:.xsd=-sskel.cxx) \ + $(xsd:.xsd=-simpl.hxx) $(xsd:.xsd=-simpl.cxx) + +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(out_root)/xsde/xsde +$(gen): xsde := $(out_root)/xsde/xsde +$(gen): xsde_options += --generate-parser --generate-serializer \ +--generate-aggregate --generate-insertion CDR --generate-extraction CDR + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Dist. +# +dist-common := $(out_base)/.dist-common + +.PHONY: $(dist) $(dist-win) $(dist-common) + +$(dist) $(dist-win) $(dist-common): path := $(subst $(src_root)/,,$(src_base)) + +$(dist-common): + $(call install-data,$(src_base)/driver.cxx,$(dist_prefix)/$(path)/driver.cxx) + $(call install-data,$(src_base)/library.xsd,$(dist_prefix)/$(path)/library.xsd) + $(call install-data,$(src_base)/library.xml,$(dist_prefix)/$(path)/library.xml) + +$(dist): $(dist-common) + $(call install-data,$(src_base)/README,$(dist_prefix)/$(path)/README) + +$(dist-win): $(dist-common) + $(call install-data,$(src_base)/README,$(dist_prefix)/$(path)/README.txt) + $(call message,,unix2dos $(dist_prefix)/$(path)/README.txt) + + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean)) + + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(gen): | $(out_base)/.gitignore +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + + +# How to. +# +$(call include,$(bld_root)/cxx/o-e.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/install.make) +$(call include,$(scf_root)/xsde/hybrid/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsde/makefile) +$(call import,$(src_root)/libxsde/xsde/makefile) |