From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- examples/cxx/tree/binary/README | 16 ++ examples/cxx/tree/binary/boost/README | 49 ++++++ .../tree/binary/boost/boost-archive-extraction.hxx | 188 +++++++++++++++++++++ .../tree/binary/boost/boost-archive-insertion.hxx | 177 +++++++++++++++++++ examples/cxx/tree/binary/boost/driver.cxx | 73 ++++++++ .../cxx/tree/binary/boost/library-prologue.hxx | 9 + examples/cxx/tree/binary/boost/library.xml | 53 ++++++ examples/cxx/tree/binary/boost/library.xsd | 76 +++++++++ examples/cxx/tree/binary/boost/makefile | 79 +++++++++ examples/cxx/tree/binary/cdr/README | 36 ++++ examples/cxx/tree/binary/cdr/driver.cxx | 88 ++++++++++ examples/cxx/tree/binary/cdr/library.xml | 53 ++++++ examples/cxx/tree/binary/cdr/library.xsd | 76 +++++++++ examples/cxx/tree/binary/cdr/makefile | 78 +++++++++ examples/cxx/tree/binary/makefile | 30 ++++ examples/cxx/tree/binary/xdr/README | 34 ++++ examples/cxx/tree/binary/xdr/driver.cxx | 149 ++++++++++++++++ examples/cxx/tree/binary/xdr/library.xml | 53 ++++++ examples/cxx/tree/binary/xdr/library.xsd | 76 +++++++++ examples/cxx/tree/binary/xdr/makefile | 78 +++++++++ 20 files changed, 1471 insertions(+) create mode 100644 examples/cxx/tree/binary/README create mode 100644 examples/cxx/tree/binary/boost/README create mode 100644 examples/cxx/tree/binary/boost/boost-archive-extraction.hxx create mode 100644 examples/cxx/tree/binary/boost/boost-archive-insertion.hxx create mode 100644 examples/cxx/tree/binary/boost/driver.cxx create mode 100644 examples/cxx/tree/binary/boost/library-prologue.hxx create mode 100644 examples/cxx/tree/binary/boost/library.xml create mode 100644 examples/cxx/tree/binary/boost/library.xsd create mode 100644 examples/cxx/tree/binary/boost/makefile create mode 100644 examples/cxx/tree/binary/cdr/README create mode 100644 examples/cxx/tree/binary/cdr/driver.cxx create mode 100644 examples/cxx/tree/binary/cdr/library.xml create mode 100644 examples/cxx/tree/binary/cdr/library.xsd create mode 100644 examples/cxx/tree/binary/cdr/makefile create mode 100644 examples/cxx/tree/binary/makefile create mode 100644 examples/cxx/tree/binary/xdr/README create mode 100644 examples/cxx/tree/binary/xdr/driver.cxx create mode 100644 examples/cxx/tree/binary/xdr/library.xml create mode 100644 examples/cxx/tree/binary/xdr/library.xsd create mode 100644 examples/cxx/tree/binary/xdr/makefile (limited to 'examples/cxx/tree/binary') diff --git a/examples/cxx/tree/binary/README b/examples/cxx/tree/binary/README new file mode 100644 index 0000000..365551e --- /dev/null +++ b/examples/cxx/tree/binary/README @@ -0,0 +1,16 @@ +This directory contains a number of examples that show how to serialize +the object model into a number of predefined and custom binary formats. +The following list gives an overview of each example: + +boost + Shows how to save/load the object model to/from a custom format + using the Boost serialization library as an example. + +cdr + Shows how to save/load the object model to/from CDR (Common Data + Representation) binary format using ACE CDR streams. + +xdr + Shows how to save/load the object model to/from XDR (eXternal Data + Representation) binary format using the XDR API provided as part of + Sun RPC. diff --git a/examples/cxx/tree/binary/boost/README b/examples/cxx/tree/binary/boost/README new file mode 100644 index 0000000..6cdd2dd --- /dev/null +++ b/examples/cxx/tree/binary/boost/README @@ -0,0 +1,49 @@ +This example shows how to save/load the object model to/from a custom +format using the Boost serialization library as an example. You will +need the Boost serialization library[1] installed in order to build +and run this example. + +[1] http://www.boost.org + +The example consists of the following files: + +library.xsd + XML Schema which describes a library of books. + +library.xml + Sample XML instance document. + +boost-archive-extraction.hxx +boost-archive-insertion.hxx + Boost archive insertion and extraction operators for fundamental + types. You will need to provide a similar set of operators for + your own stream types. + +library-prologue.hxx + Contains a number of #include directives that are inserted into + the generated code by the XSD compiler. The included files are: + boost/archive/text_oarchive.hpp, boost/archive/text_oarchive.hpp, + boost-archive-insertion.hxx, and boost-archive-insertion.hxx. + +library.hxx +library.cxx + C++ types that represent the given vocabulary as well as Boost + archive insertion and extraction operations. These are generated + by the XSD compiler from library.xsd. The --hxx-prologue-file + option is used to insert the contents of the library-prologue.hxx + file into the generated header file. The --generate-insertion and + --generate-extraction options are used to generate the insertion + and extraction operations for text_oarchive and text_iarchive + types. + +driver.cxx + Driver for the example. It first calls one of the parsing functions + that constructs the object model from the input XML file. It then + saves the object model to text_oarchive and loads it back from + text_iarchive. Additionally, it prints the resulting text + representation as well as the content of the object model before + saving it to text_oarchive and after loading it from text_iarchive. + +To run the example on the sample XML instance document simply execute: + +$ ./driver library.xml diff --git a/examples/cxx/tree/binary/boost/boost-archive-extraction.hxx b/examples/cxx/tree/binary/boost/boost-archive-extraction.hxx new file mode 100644 index 0000000..7711b28 --- /dev/null +++ b/examples/cxx/tree/binary/boost/boost-archive-extraction.hxx @@ -0,0 +1,188 @@ +// file : examples/cxx/tree/binary/boost/boost-archive-insertion.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef BOOST_ARCHIVE_EXTRACTION_HXX +#define BOOST_ARCHIVE_EXTRACTION_HXX + +#include // std::size_t +#include + +#include +#include + +#include + +namespace xsd +{ + namespace cxx + { + namespace tree + { + // as_size + // + template + inline istream& + operator>> (istream& s, istream_common::as_size& x) + { + std::size_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + // 8-bit + // + template + inline istream& + operator>> (istream& s, istream_common::as_int8& x) + { + boost::int8_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + template + inline istream& + operator>> (istream& s, istream_common::as_uint8& x) + { + boost::uint8_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + + // 16-bit + // + template + inline istream& + operator>> (istream& s, istream_common::as_int16& x) + { + boost::int16_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + template + inline istream& + operator>> (istream& s, istream_common::as_uint16& x) + { + boost::uint16_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + + // 32-bit + // + template + inline istream& + operator>> (istream& s, istream_common::as_int32& x) + { + boost::int32_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + template + inline istream& + operator>> (istream& s, istream_common::as_uint32& x) + { + boost::uint32_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + + // 64-bit + // + template + inline istream& + operator>> (istream& s, istream_common::as_int64& x) + { + boost::int64_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + template + inline istream& + operator>> (istream& s, istream_common::as_uint64& x) + { + boost::uint64_t r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + + // Boolean + // + template + inline istream& + operator>> (istream& s, istream_common::as_bool& x) + { + bool r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + + // Floating-point + // + template + inline istream& + operator>> (istream& s, istream_common::as_float32& x) + { + float r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + template + inline istream& + operator>> (istream& s, istream_common::as_float64& x) + { + double r; + s.impl () >> r; + x.x_ = static_cast (r); + return s; + } + + // Extraction of std::basic_string. + // + + template + inline istream& + operator>> (istream& s, std::basic_string& x) + { + s.impl () >> x; + return s; + } + + + // Extraction of a binary buffer. + // + template + istream& + operator>> (istream& s, buffer& x) + { + std::size_t size; + s.impl () >> size; + x.size (size); + s.impl ().load_binary (x.data (), size); + } + } + } +} + +#endif // BOOST_ARCHIVE_EXTRACTION_HXX diff --git a/examples/cxx/tree/binary/boost/boost-archive-insertion.hxx b/examples/cxx/tree/binary/boost/boost-archive-insertion.hxx new file mode 100644 index 0000000..7a00ef8 --- /dev/null +++ b/examples/cxx/tree/binary/boost/boost-archive-insertion.hxx @@ -0,0 +1,177 @@ +// file : examples/cxx/tree/binary/boost/boost-archive-insertion.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef BOOST_ARCHIVE_INSERTION_HXX +#define BOOST_ARCHIVE_INSERTION_HXX + +#include // std::size_t +#include + +#include +#include + +#include + +namespace xsd +{ + namespace cxx + { + namespace tree + { + // as_size + // + template + inline ostream& + operator<< (ostream& s, ostream_common::as_size x) + { + std::size_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + // 8-bit + // + template + inline ostream& + operator<< (ostream& s, ostream_common::as_int8 x) + { + boost::int8_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream_common::as_uint8 x) + { + boost::uint8_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + + // 16-bit + // + template + inline ostream& + operator<< (ostream& s, ostream_common::as_int16 x) + { + boost::int16_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream_common::as_uint16 x) + { + boost::uint16_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + + // 32-bit + // + template + inline ostream& + operator<< (ostream& s, ostream_common::as_int32 x) + { + boost::int32_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream_common::as_uint32 x) + { + boost::uint32_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + + // 64-bit + // + template + inline ostream& + operator<< (ostream& s, ostream_common::as_int64 x) + { + boost::int64_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream_common::as_uint64 x) + { + boost::uint64_t v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + + // Boolean + // + template + inline ostream& + operator<< (ostream& s, ostream_common::as_bool x) + { + bool v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + + // Floating-point + // + template + inline ostream& + operator<< (ostream& s, ostream_common::as_float32 x) + { + float v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream_common::as_float64 x) + { + double v (static_cast (x.x_)); + s.impl () << v; + return s; + } + + + // Insertion of std::basic_string. + // + template + inline ostream& + operator<< (ostream& s, const std::basic_string& x) + { + s.impl () << x; + return s; + } + + + // Insertion of a binary buffer. + // + template + ostream& + operator<< (ostream& s, const buffer& x) + { + // Boost.Serialization needs an lvalue. + // + std::size_t size (x.size()); + s.impl () << size; + s.impl ().save_binary (x.data (), x.size ()); + } + } + } +} + +#endif // BOOST_ARCHIVE_INSERTION_HXX diff --git a/examples/cxx/tree/binary/boost/driver.cxx b/examples/cxx/tree/binary/boost/driver.cxx new file mode 100644 index 0000000..9490d13 --- /dev/null +++ b/examples/cxx/tree/binary/boost/driver.cxx @@ -0,0 +1,73 @@ +// file : examples/cxx/tree/binary/boost/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include // std::memcpy +#include +#include + +// You can generate insertion/extraction code for other archive +// types (for example, binary, XML, etc). +// +#include +#include + +#include "library.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " library.xml" << endl; + return 1; + } + + try + { + using namespace library; + using boost::archive::text_oarchive; + using boost::archive::text_iarchive; + + // Read in the file. + // + std::auto_ptr c (catalog_ (argv[1])); + + cerr << *c << endl; + + // Save into a text archive. + // + std::ostringstream ostr; + text_oarchive oa (ostr); + xml_schema::ostream os (oa); + + os << *c; + + // Print the text representation. + // + std::string str (ostr.str ()); + + cerr << endl + << "text representation: " << endl + << str << endl; + + // Load from a text archive. + // + std::istringstream istr (str); + text_iarchive ia (istr); + xml_schema::istream is (ia); + + std::auto_ptr copy (new catalog (is)); + + cerr << *copy << endl; + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/examples/cxx/tree/binary/boost/library-prologue.hxx b/examples/cxx/tree/binary/boost/library-prologue.hxx new file mode 100644 index 0000000..ba0d35f --- /dev/null +++ b/examples/cxx/tree/binary/boost/library-prologue.hxx @@ -0,0 +1,9 @@ +// Include declarations for the archive types. +// +#include +#include + +// Include insertion/extraction operators for fundamental types. +// +#include "boost-archive-insertion.hxx" +#include "boost-archive-extraction.hxx" diff --git a/examples/cxx/tree/binary/boost/library.xml b/examples/cxx/tree/binary/boost/library.xml new file mode 100644 index 0000000..da2bee6 --- /dev/null +++ b/examples/cxx/tree/binary/boost/library.xml @@ -0,0 +1,53 @@ + + + + + + + + 0679760806 + The Master and Margarita + fiction + + + Mikhail Bulgakov + 1891-05-15 + 1940-03-10 + + + + + + 0679600841 + War and Peace + history + + + Leo Tolstoy + 1828-09-09 + 1910-11-20 + + + + + + 0679420290 + Crime and Punishment + philosophy + + + Fyodor Dostoevsky + 1821-11-11 + 1881-02-09 + + + + diff --git a/examples/cxx/tree/binary/boost/library.xsd b/examples/cxx/tree/binary/boost/library.xsd new file mode 100644 index 0000000..9f35d4a --- /dev/null +++ b/examples/cxx/tree/binary/boost/library.xsd @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/binary/boost/makefile b/examples/cxx/tree/binary/boost/makefile new file mode 100644 index 0000000..f40c6d6 --- /dev/null +++ b/examples/cxx/tree/binary/boost/makefile @@ -0,0 +1,79 @@ +# file : examples/cxx/tree/binary/boost/makefile +# author : Boris Kolpackov +# 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)) +dep := $(obj:.o=.o.d) + +driver := $(out_base)/driver +clean := $(out_base)/.clean + + +# Import. +# +$(call import,\ + $(scf_root)/import/libxerces-c/stub.make,\ + l: xerces_c.l,cpp-options: xerces_c.l.cpp-options) + +$(call import,\ + $(scf_root)/import/libboost/serialization/stub.make,\ + l: boost_serialization.l,cpp-options: boost_serialization.l.cpp-options) + + +# Build. +# +$(driver): $(obj) $(xerces_c.l) $(boost_serialization.l) + +$(obj) $(dep): cpp_options := -I$(src_root)/libxsd -I$(src_root) +$(obj) $(dep): $(xerces_c.l.cpp-options) $(boost_serialization.l.cpp-options) + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := \ +--generate-ostream \ +--hxx-prologue-file $(src_base)/library-prologue.hxx \ +--generate-insertion boost::archive::text_oarchive \ +--generate-extraction boost::archive::text_iarchive + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): $(out_root)/xsd/xsd + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean)) + + +# 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,$(scf_root)/xsd/tree/xsd-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) diff --git a/examples/cxx/tree/binary/cdr/README b/examples/cxx/tree/binary/cdr/README new file mode 100644 index 0000000..914d27c --- /dev/null +++ b/examples/cxx/tree/binary/cdr/README @@ -0,0 +1,36 @@ +This example shows how to save/load the object model to/from CDR +(Common Data Representation) binary format using ACE CDR streams. +Support for other data representation streams can be easily added. You +will need the ACE library[1] installed in order to build and run this +example. + +[1] 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 + C++ types that represent the given vocabulary as well as data + representation stream insertion and extraction operations. These + are generated by XSD from library.xsd. Note that the + --generate-insertion and --generate-extraction options are used + to generate the insertion and extraction operations for ACE CDR + stream. + +driver.cxx + Driver for the example. It first calls one of the parsing functions + that constructs the object model from the input XML file. It then + saves the object model to ACE_OuputCDR and loads it back from + ACE_InputCDR. Additionally, it prints the resulting binary + representation as well as the content of the object model before + saving it to the CDR stream and after loading it from the CDR stream. + +To run the example on the sample XML instance document simply execute: + +$ ./driver library.xml diff --git a/examples/cxx/tree/binary/cdr/driver.cxx b/examples/cxx/tree/binary/cdr/driver.cxx new file mode 100644 index 0000000..08ec0d5 --- /dev/null +++ b/examples/cxx/tree/binary/cdr/driver.cxx @@ -0,0 +1,88 @@ +// file : examples/cxx/tree/binary/cdr/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include // std::memcpy +#include + +#include // ACE_HEX_DUMP +#include + +// The following two headers define XSD-specific insertion/extraction +// operations for ACE CDR streams. You can use the content of these +// headers as a guide to implementing insertion/extraction to/from +// your own data representation streams: +// +// xsd/cxx/tree/ace-cdr-stream-insertion.hxx +// xsd/cxx/tree/ace-cdr-stream-extraction.hxx + +#include "library.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " library.xml" << endl; + return 1; + } + + try + { + using namespace library; + + // Read in the file. + // + std::auto_ptr c (catalog_ (argv[1])); + + cerr << *c << endl; + + // Save to a CDR stream. + // + ACE_OutputCDR ace_ocdr; + xml_schema::ostream ocdr (ace_ocdr); + + ocdr << *c; + + // Print the binary representation and at the same time save + // it into a continuous buffer. + // + cerr << endl + << "binary representation size: " << ace_ocdr.total_length () << 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 ()) + { + std::memcpy (data, mb->rd_ptr (), mb->length ()); + data += mb->length (); + + ACE_HEX_DUMP ((LM_DEBUG, mb->rd_ptr (), mb->length ())); + } + + // Load 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::istream icdr (ace_icdr); + + std::auto_ptr copy (new catalog (icdr)); + + cerr << *copy << endl; + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } + + return 0; // ACE makes our main() an ordinary function. +} diff --git a/examples/cxx/tree/binary/cdr/library.xml b/examples/cxx/tree/binary/cdr/library.xml new file mode 100644 index 0000000..e7ccb3f --- /dev/null +++ b/examples/cxx/tree/binary/cdr/library.xml @@ -0,0 +1,53 @@ + + + + + + + + 0679760806 + The Master and Margarita + fiction + + + Mikhail Bulgakov + 1891-05-15 + 1940-03-10 + + + + + + 0679600841 + War and Peace + history + + + Leo Tolstoy + 1828-09-09 + 1910-11-20 + + + + + + 0679420290 + Crime and Punishment + philosophy + + + Fyodor Dostoevsky + 1821-11-11 + 1881-02-09 + + + + diff --git a/examples/cxx/tree/binary/cdr/library.xsd b/examples/cxx/tree/binary/cdr/library.xsd new file mode 100644 index 0000000..92d44d1 --- /dev/null +++ b/examples/cxx/tree/binary/cdr/library.xsd @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/binary/cdr/makefile b/examples/cxx/tree/binary/cdr/makefile new file mode 100644 index 0000000..e811695 --- /dev/null +++ b/examples/cxx/tree/binary/cdr/makefile @@ -0,0 +1,78 @@ +# file : examples/cxx/tree/binary/cdr/makefile +# author : Boris Kolpackov +# 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)) +dep := $(obj:.o=.o.d) + +driver := $(out_base)/driver +clean := $(out_base)/.clean + + +# Import. +# +$(call import,\ + $(scf_root)/import/libxerces-c/stub.make,\ + l: xerces_c.l,cpp-options: xerces_c.l.cpp-options) + +$(call import,\ + $(scf_root)/import/libace/stub.make,\ + l: ace.l,cpp-options: ace.l.cpp-options) + + +# Build. +# +$(driver): $(obj) $(xerces_c.l) $(ace.l) + +$(obj) $(dep): cpp_options := -I$(src_root)/libxsd +$(obj) $(dep): $(xerces_c.l.cpp-options) $(ace.l.cpp-options) + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := \ +--generate-ostream \ +--generate-insertion ACE_OutputCDR \ +--generate-extraction ACE_InputCDR + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): $(out_root)/xsd/xsd + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean)) + + +# 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,$(scf_root)/xsd/tree/xsd-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) diff --git a/examples/cxx/tree/binary/makefile b/examples/cxx/tree/binary/makefile new file mode 100644 index 0000000..f30fc66 --- /dev/null +++ b/examples/cxx/tree/binary/makefile @@ -0,0 +1,30 @@ +# file : examples/cxx/tree/binary/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make + +examples := + +ifeq ($(xsd_with_boost_serialization),y) +examples += boost +endif + +ifeq ($(xsd_with_ace),y) +examples += cdr +endif + +ifeq ($(xsd_with_xdr),y) +examples += xdr +endif + +default := $(out_base)/ +clean := $(out_base)/.clean + +.PHONY: $(default) $(clean) + +$(default): $(addprefix $(out_base)/,$(addsuffix /,$(examples))) +$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(examples))) + +$(foreach e,$(examples),$(call import,$(src_base)/$e/makefile)) diff --git a/examples/cxx/tree/binary/xdr/README b/examples/cxx/tree/binary/xdr/README new file mode 100644 index 0000000..22d5693 --- /dev/null +++ b/examples/cxx/tree/binary/xdr/README @@ -0,0 +1,34 @@ +This example shows how to save/load the object model to/from XDR +(eXternal Data Representation) binary format using XDR streams. +The XDR API is available out of the box on most UNIX and GNU/Linux +systems as part of Sun RPC. On Windows you may need to install a +third-party library which provides the XDR API. + +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 + C++ types that represent the given vocabulary as well as data + representation stream insertion and extraction operations. These + are generated by XSD from library.xsd. Note that the + --generate-insertion and --generate-extraction options are used + to generate the insertion and extraction operations for XDR + stream. + +driver.cxx + Driver for the example. It first calls one of the parsing functions + that constructs the object model from the input XML file. It then + saves the object model to the XDR representation and loads it back. + Additionally, it prints the content of the object model before saving + it to the XDR representation and after loading it from the XDR + representation. + +To run the example on the sample XML instance document simply execute: + +$ ./driver library.xml diff --git a/examples/cxx/tree/binary/xdr/driver.cxx b/examples/cxx/tree/binary/xdr/driver.cxx new file mode 100644 index 0000000..495eafc --- /dev/null +++ b/examples/cxx/tree/binary/xdr/driver.cxx @@ -0,0 +1,149 @@ +// file : examples/cxx/tree/binary/xdr/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include // std::memcpy +#include // std::size_t +#include + +#include +#include + +#include "library.hxx" + +using std::cerr; +using std::endl; +using std::size_t; + +// XDR output functions. Their implementations are provided after main(). +// +struct underflow_info +{ + xml_schema::buffer* buf; + size_t pos; +}; + +extern "C" int +overflow (void* user_data, char* buf, int n); + +extern "C" int +underflow (void* user_data, char* buf, int n); + +// The xdrrec_create function (used below) has slightly different +// prototypes on different platforms. To make this example portable +// we will need to cast the actual function to the following common +// prototype. +// +extern "C" +typedef void (*xdrrec_create_p) ( + XDR*, + unsigned int write_size, + unsigned int read_size, + void* user_data, + int (*read) (void* user_data, char* buf, int n), + int (*write) (void* user_data, char* buf, int n)); + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " library.xml" << endl; + return 1; + } + + try + { + using namespace library; + + xdrrec_create_p xdrrec_create_ = + reinterpret_cast (::xdrrec_create); + + // Read in the file. + // + std::auto_ptr c (catalog_ (argv[1])); + + cerr << *c << endl; + + // Save to an XDR stream. + // + XDR xdr; + xml_schema::buffer buf; + + xdrrec_create_ (&xdr, 0, 0, reinterpret_cast (&buf), 0, &overflow); + xdr.x_op = XDR_ENCODE; + + xml_schema::ostream oxdr (xdr); + + oxdr << *c; + + xdrrec_endofrecord (&xdr, true); // Flush the data. + xdr_destroy (&xdr); + + // The binary representation is now in the memory buffer 'buf'. + // To get to the raw data use buf.data() and buf.size(). + // + cerr << endl + << "binary representation size: " << buf.size () << endl; + + // Load from an XDR stream. + // + underflow_info ui; + ui.buf = &buf; + ui.pos = 0; + + xdrrec_create_ (&xdr, 0, 0, reinterpret_cast (&ui), &underflow, 0); + xdr.x_op = XDR_DECODE; + + xdrrec_skiprecord (&xdr); + + xml_schema::istream ixdr (xdr); + + std::auto_ptr copy (new catalog (ixdr)); + + xdr_destroy (&xdr); + + cerr << *copy << endl; + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} + +extern "C" int +overflow (void* p, char* buf, int n_) +{ + xml_schema::buffer* dst (reinterpret_cast (p)); + size_t n (static_cast (n_)); + + size_t size (dst->size ()); + size_t capacity (dst->capacity ()); + + // Implement exponential growth. + // + if (size + n > capacity && size + n < capacity * 2) + dst->capacity (capacity * 2); + + dst->size (size + n); + std::memcpy (dst->data () + size, buf, n); + + return n; +} + +extern "C" int +underflow (void* p, char* buf, int n_) +{ + underflow_info* ui (reinterpret_cast (p)); + size_t n (static_cast (n_)); + + size_t size (ui->buf->size () - ui->pos); + n = size > n ? n : size; + + std::memcpy (buf, ui->buf->data () + ui->pos, n); + ui->pos += n; + + return n; +} diff --git a/examples/cxx/tree/binary/xdr/library.xml b/examples/cxx/tree/binary/xdr/library.xml new file mode 100644 index 0000000..9ddcd5a --- /dev/null +++ b/examples/cxx/tree/binary/xdr/library.xml @@ -0,0 +1,53 @@ + + + + + + + + 0679760806 + The Master and Margarita + fiction + + + Mikhail Bulgakov + 1891-05-15 + 1940-03-10 + + + + + + 0679600841 + War and Peace + history + + + Leo Tolstoy + 1828-09-09 + 1910-11-20 + + + + + + 0679420290 + Crime and Punishment + philosophy + + + Fyodor Dostoevsky + 1821-11-11 + 1881-02-09 + + + + diff --git a/examples/cxx/tree/binary/xdr/library.xsd b/examples/cxx/tree/binary/xdr/library.xsd new file mode 100644 index 0000000..9999e72 --- /dev/null +++ b/examples/cxx/tree/binary/xdr/library.xsd @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/binary/xdr/makefile b/examples/cxx/tree/binary/xdr/makefile new file mode 100644 index 0000000..6414fe9 --- /dev/null +++ b/examples/cxx/tree/binary/xdr/makefile @@ -0,0 +1,78 @@ +# file : examples/cxx/tree/binary/xdr/makefile +# author : Boris Kolpackov +# 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)) +dep := $(obj:.o=.o.d) + +driver := $(out_base)/driver +clean := $(out_base)/.clean + + +# Import. +# +$(call import,\ + $(scf_root)/import/libxerces-c/stub.make,\ + l: xerces_c.l,cpp-options: xerces_c.l.cpp-options) + +$(call import,\ + $(scf_root)/import/libace/stub.make,\ + l: ace.l,cpp-options: ace.l.cpp-options) + + +# Build. +# +$(driver): $(obj) $(xerces_c.l) $(ace.l) -lnsl + +$(obj) $(dep): cpp_options := -I$(src_root)/libxsd +$(obj) $(dep): $(xerces_c.l.cpp-options) $(ace.l.cpp-options) + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := \ +--generate-ostream \ +--generate-insertion XDR \ +--generate-extraction XDR + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): $(out_root)/xsd/xsd + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean)) + + +# 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,$(scf_root)/xsd/tree/xsd-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) -- cgit v1.1