diff options
Diffstat (limited to 'xsd-examples/cxx/tree/binary/boost')
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/.gitignore | 1 | ||||
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/README | 49 | ||||
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/boost-archive-extraction.hxx | 188 | ||||
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/boost-archive-insertion.hxx | 177 | ||||
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/buildfile | 32 | ||||
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/driver.cxx | 72 | ||||
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/library-prologue.hxx | 9 | ||||
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/library.xml | 52 | ||||
-rw-r--r-- | xsd-examples/cxx/tree/binary/boost/library.xsd | 75 |
9 files changed, 655 insertions, 0 deletions
diff --git a/xsd-examples/cxx/tree/binary/boost/.gitignore b/xsd-examples/cxx/tree/binary/boost/.gitignore new file mode 100644 index 0000000..c116ec1 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/boost/.gitignore @@ -0,0 +1 @@ +library.?xx diff --git a/xsd-examples/cxx/tree/binary/boost/README b/xsd-examples/cxx/tree/binary/boost/README new file mode 100644 index 0000000..6cdd2dd --- /dev/null +++ b/xsd-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/xsd-examples/cxx/tree/binary/boost/boost-archive-extraction.hxx b/xsd-examples/cxx/tree/binary/boost/boost-archive-extraction.hxx new file mode 100644 index 0000000..93241da --- /dev/null +++ b/xsd-examples/cxx/tree/binary/boost/boost-archive-extraction.hxx @@ -0,0 +1,188 @@ +// file : cxx/tree/binary/boost/boost-archive-insertion.cxx +// copyright : not copyrighted - public domain + +#ifndef BOOST_ARCHIVE_EXTRACTION_HXX +#define BOOST_ARCHIVE_EXTRACTION_HXX + +#include <cstddef> // std::size_t +#include <string> + +#include <xsd/cxx/tree/buffer.hxx> +#include <xsd/cxx/tree/istream.hxx> + +#include <boost/cstdint.hpp> + +namespace xsd +{ + namespace cxx + { + namespace tree + { + // as_size + // + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_size<T>& x) + { + std::size_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + // 8-bit + // + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_int8<T>& x) + { + boost::int8_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_uint8<T>& x) + { + boost::uint8_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + + // 16-bit + // + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_int16<T>& x) + { + boost::int16_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_uint16<T>& x) + { + boost::uint16_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + + // 32-bit + // + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_int32<T>& x) + { + boost::int32_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_uint32<T>& x) + { + boost::uint32_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + + // 64-bit + // + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_int64<T>& x) + { + boost::int64_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_uint64<T>& x) + { + boost::uint64_t r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + + // Boolean + // + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_bool<T>& x) + { + bool r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + + // Floating-point + // + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_float32<T>& x) + { + float r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + template <typename Archive, typename T> + inline istream<Archive>& + operator>> (istream<Archive>& s, istream_common::as_float64<T>& x) + { + double r; + s.impl () >> r; + x.x_ = static_cast<T> (r); + return s; + } + + // Extraction of std::basic_string. + // + + template <typename Archive, typename C> + inline istream<Archive>& + operator>> (istream<Archive>& s, std::basic_string<C>& x) + { + s.impl () >> x; + return s; + } + + + // Extraction of a binary buffer. + // + template <typename Archive, typename C> + istream<Archive>& + operator>> (istream<Archive>& s, buffer<C>& x) + { + std::size_t size; + s.impl () >> size; + x.size (size); + s.impl ().load_binary (x.data (), size); + return s; + } + } + } +} + +#endif // BOOST_ARCHIVE_EXTRACTION_HXX diff --git a/xsd-examples/cxx/tree/binary/boost/boost-archive-insertion.hxx b/xsd-examples/cxx/tree/binary/boost/boost-archive-insertion.hxx new file mode 100644 index 0000000..b2f7936 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/boost/boost-archive-insertion.hxx @@ -0,0 +1,177 @@ +// file : cxx/tree/binary/boost/boost-archive-insertion.cxx +// copyright : not copyrighted - public domain + +#ifndef BOOST_ARCHIVE_INSERTION_HXX +#define BOOST_ARCHIVE_INSERTION_HXX + +#include <cstddef> // std::size_t +#include <string> + +#include <xsd/cxx/tree/buffer.hxx> +#include <xsd/cxx/tree/ostream.hxx> + +#include <boost/cstdint.hpp> + +namespace xsd +{ + namespace cxx + { + namespace tree + { + // as_size + // + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_size<T> x) + { + std::size_t v (static_cast<std::size_t> (x.x_)); + s.impl () << v; + return s; + } + + // 8-bit + // + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_int8<T> x) + { + boost::int8_t v (static_cast<boost::int8_t> (x.x_)); + s.impl () << v; + return s; + } + + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_uint8<T> x) + { + boost::uint8_t v (static_cast<boost::uint8_t> (x.x_)); + s.impl () << v; + return s; + } + + + // 16-bit + // + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_int16<T> x) + { + boost::int16_t v (static_cast<boost::int16_t> (x.x_)); + s.impl () << v; + return s; + } + + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_uint16<T> x) + { + boost::uint16_t v (static_cast<boost::uint16_t> (x.x_)); + s.impl () << v; + return s; + } + + + // 32-bit + // + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_int32<T> x) + { + boost::int32_t v (static_cast<boost::int32_t> (x.x_)); + s.impl () << v; + return s; + } + + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_uint32<T> x) + { + boost::uint32_t v (static_cast<boost::uint32_t> (x.x_)); + s.impl () << v; + return s; + } + + + // 64-bit + // + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_int64<T> x) + { + boost::int64_t v (static_cast<boost::int64_t> (x.x_)); + s.impl () << v; + return s; + } + + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_uint64<T> x) + { + boost::uint64_t v (static_cast<boost::uint64_t> (x.x_)); + s.impl () << v; + return s; + } + + + // Boolean + // + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_bool<T> x) + { + bool v (static_cast<bool> (x.x_)); + s.impl () << v; + return s; + } + + + // Floating-point + // + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_float32<T> x) + { + float v (static_cast<float> (x.x_)); + s.impl () << v; + return s; + } + + template <typename Archive, typename T> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, ostream_common::as_float64<T> x) + { + double v (static_cast<double> (x.x_)); + s.impl () << v; + return s; + } + + + // Insertion of std::basic_string. + // + template <typename Archive, typename C> + inline ostream<Archive>& + operator<< (ostream<Archive>& s, const std::basic_string<C>& x) + { + s.impl () << x; + return s; + } + + + // Insertion of a binary buffer. + // + template <typename Archive, typename C> + ostream<Archive>& + operator<< (ostream<Archive>& s, const buffer<C>& x) + { + // Boost.Serialization needs an lvalue. + // + std::size_t size (x.size()); + s.impl () << size; + s.impl ().save_binary (x.data (), x.size ()); + return s; + } + } + } +} + +#endif // BOOST_ARCHIVE_INSERTION_HXX diff --git a/xsd-examples/cxx/tree/binary/boost/buildfile b/xsd-examples/cxx/tree/binary/boost/buildfile new file mode 100644 index 0000000..4306273 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/boost/buildfile @@ -0,0 +1,32 @@ +# file : cxx/tree/binary/boost/buildfile +# license : not copyrighted - public domain + +import libs = libxsd%lib{xsd} +import libs += libxerces-c%lib{xerces-c} +import libs += libboost-serialization%lib{boost_serialization} + +./: exe{driver} doc{README} + +exe{driver}: {hxx cxx}{* -library} {hxx ixx cxx}{library} $libs + +exe{driver}: xml{library}: test.input = true + +<{hxx ixx cxx}{library}>: xsd{library} hxx{library-prologue} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-tree --std c++11 \ + --generate-inline \ + --generate-ostream \ + --hxx-prologue-file $path($<[1]) \ + --generate-insertion 'boost::archive::text_oarchive' \ + --generate-extraction 'boost::archive::text_iarchive' \ + --output-dir $out_base \ + $path($<[0]) +}} + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Define XSD_CXX11 since we include libxsd headers directly. +# +cxx.poptions += -DXSD_CXX11 diff --git a/xsd-examples/cxx/tree/binary/boost/driver.cxx b/xsd-examples/cxx/tree/binary/boost/driver.cxx new file mode 100644 index 0000000..d1d08d9 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/boost/driver.cxx @@ -0,0 +1,72 @@ +// file : cxx/tree/binary/boost/driver.cxx +// copyright : not copyrighted - public domain + +#include <memory> // std::unique_ptr +#include <cstring> // std::memcpy +#include <sstream> +#include <iostream> + +// You can generate insertion/extraction code for other archive +// types (for example, binary, XML, etc). +// +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +#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::unique_ptr<catalog> c (catalog_ (argv[1])); + + cerr << *c << endl; + + // Save into a text archive. + // + std::ostringstream ostr; + text_oarchive oa (ostr); + xml_schema::ostream<text_oarchive> 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<text_iarchive> is (ia); + + std::unique_ptr<catalog> copy (new catalog (is)); + + cerr << *copy << endl; + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/xsd-examples/cxx/tree/binary/boost/library-prologue.hxx b/xsd-examples/cxx/tree/binary/boost/library-prologue.hxx new file mode 100644 index 0000000..ba0d35f --- /dev/null +++ b/xsd-examples/cxx/tree/binary/boost/library-prologue.hxx @@ -0,0 +1,9 @@ +// Include declarations for the archive types. +// +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +// Include insertion/extraction operators for fundamental types. +// +#include "boost-archive-insertion.hxx" +#include "boost-archive-extraction.hxx" diff --git a/xsd-examples/cxx/tree/binary/boost/library.xml b/xsd-examples/cxx/tree/binary/boost/library.xml new file mode 100644 index 0000000..6368f44 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/boost/library.xml @@ -0,0 +1,52 @@ +<?xml version="1.0"?> + +<!-- + +file : cxx/tree/binary/boost/library.xml +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 id="MM" available="false"> + <isbn>0679760806</isbn> + <title>The Master and Margarita</title> + <genre>fiction</genre> + + <author recommends="WP"> + <name>Mikhail Bulgakov</name> + <born>1891-05-15</born> + <died>1940-03-10</died> + </author> + </book> + + + <book id="WP"> + <isbn>0679600841</isbn> + <title>War and Peace</title> + <genre>history</genre> + + <author recommends="CP"> + <name>Leo Tolstoy</name> + <born>1828-09-09</born> + <died>1910-11-20</died> + </author> + </book> + + + <book id="CP" 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/xsd-examples/cxx/tree/binary/boost/library.xsd b/xsd-examples/cxx/tree/binary/boost/library.xsd new file mode 100644 index 0000000..3959788 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/boost/library.xsd @@ -0,0 +1,75 @@ +<?xml version="1.0"?> + +<!-- + +file : cxx/tree/binary/boost/library.xsd +copyright : not copyrighted - public domain + +--> + +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xse="http://www.codesynthesis.com/xmlns/xml-schema-extension" + 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:language"/> + </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="person"> + <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="author"> + <xsd:complexContent> + <xsd:extension base="lib:person"> + <xsd:attribute name="recommends" type="xsd:IDREF" xse:refType="lib:book"/> + </xsd:extension> + </xsd:complexContent> + </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" default="true"/> + <xsd:attribute name="id" type="xsd:ID" 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> |