diff options
Diffstat (limited to 'xsd-examples/cxx/parser/mixin')
-rw-r--r-- | xsd-examples/cxx/parser/mixin/README | 34 | ||||
-rw-r--r-- | xsd-examples/cxx/parser/mixin/buildfile | 24 | ||||
-rw-r--r-- | xsd-examples/cxx/parser/mixin/driver.cxx | 105 | ||||
-rw-r--r-- | xsd-examples/cxx/parser/mixin/instance.xml | 16 | ||||
-rw-r--r-- | xsd-examples/cxx/parser/mixin/schema.map | 7 | ||||
-rw-r--r-- | xsd-examples/cxx/parser/mixin/schema.xsd | 30 | ||||
-rw-r--r-- | xsd-examples/cxx/parser/mixin/types.hxx | 43 |
7 files changed, 259 insertions, 0 deletions
diff --git a/xsd-examples/cxx/parser/mixin/README b/xsd-examples/cxx/parser/mixin/README new file mode 100644 index 0000000..343e379 --- /dev/null +++ b/xsd-examples/cxx/parser/mixin/README @@ -0,0 +1,34 @@ +This example shows how to reuse implementations of base parsers +in derived parsers using the mixin C++ idiom. + +The example consists of the following files: + +schema.xsd + XML Schema which defined two data types: base and + derived. + +instance.xml + Sample XML instance document. + +types.hxx + C++ classes that correspond to the base and derived + types in schema.xsd. + +schema.map + Type map. It maps XML Schema types defined in schema.xsd + to C++ types defined in types.hxx. + +schema-pskel.hxx +schema-pskel.cxx + Parser skeletons generated by XSD from schema.xsd and + schema.map. + +driver.cxx + Parser implementations and a driver for the example. It + shows how to mix the implementation of the base parser + into the derived parser. + +To run the example on the sample XML instance document simply +execute: + +$ ./driver instance.xml diff --git a/xsd-examples/cxx/parser/mixin/buildfile b/xsd-examples/cxx/parser/mixin/buildfile new file mode 100644 index 0000000..d64d06d --- /dev/null +++ b/xsd-examples/cxx/parser/mixin/buildfile @@ -0,0 +1,24 @@ +# file : cxx/parser/mixin/buildfile +# license : not copyrighted - public domain + +import libs = libxsd%lib{xsd} + +./: exe{driver} doc{README} + +exe{driver}: {hxx cxx}{* -schema-pskel} {hxx ixx cxx}{schema-pskel} $libs + +exe{driver}: xml{instance}: test.input = true + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +<{hxx ixx cxx}{schema-pskel}>: xsd{schema} map{schema} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-parser $xsd_cxx_std \ + --generate-inline \ + --skel-file-suffix -pskel \ + --type-map $path($<[1]) \ + --output-dir $out_base \ + $path($<[0]) +}} diff --git a/xsd-examples/cxx/parser/mixin/driver.cxx b/xsd-examples/cxx/parser/mixin/driver.cxx new file mode 100644 index 0000000..8e74854 --- /dev/null +++ b/xsd-examples/cxx/parser/mixin/driver.cxx @@ -0,0 +1,105 @@ +// file : cxx/parser/mixin/driver.cxx +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr/std::unique_ptr +#include <iostream> + +#include <libxsd/cxx/config.hxx> // XSD_AUTO_PTR + +#include "types.hxx" +#include "schema-pskel.hxx" + +using namespace std; + +struct base_pimpl: virtual base_pskel +{ + virtual void + pre () + { + base_.reset (new ::base); + } + + virtual void + a (bool v) + { + base_->a (v); + } + + virtual base* + post_base () + { + return base_.release (); + } + +protected: + XSD_AUTO_PTR<base> base_; +}; + +// Implement derived parser by mixing-in base's implementation. +// +struct derived_pimpl: derived_pskel, base_pimpl +{ + virtual void + pre () + { + // Override base's pre() with the new implementation that + // instantiates derived instead of base. + // + base_.reset (new ::derived); + } + + virtual void + b (int v) + { + // We could also store a pointer to derived in derived_impl to + // avoid casting. + // + static_cast< ::derived* > (base_.get ())->b (v); + } + + virtual derived* + post_derived () + { + return static_cast<derived*> (base_.release ()); + } +}; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " instance.xml" << endl; + return 1; + } + + try + { + // Construct the parser. + // + xml_schema::boolean_pimpl bool_p; + xml_schema::int_pimpl int_p; + derived_pimpl derived_p; + + derived_p.parsers (bool_p, int_p); + + xml_schema::document doc_p (derived_p, "root"); + + derived_p.pre (); + doc_p.parse (argv[1]); + XSD_AUTO_PTR<derived> d (derived_p.post_derived ()); + + cerr << "a: " << boolalpha << d->a () << endl; + cerr << "b: " << d->b () << endl; + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } + catch (const std::ios_base::failure&) + { + cerr << argv[1] << ": unable to open or read failure" << endl; + return 1; + } +} diff --git a/xsd-examples/cxx/parser/mixin/instance.xml b/xsd-examples/cxx/parser/mixin/instance.xml new file mode 100644 index 0000000..90e2757 --- /dev/null +++ b/xsd-examples/cxx/parser/mixin/instance.xml @@ -0,0 +1,16 @@ +<?xml version="1.0"?> + +<!-- + +file : cxx/parser/mixin/instance.xml +copyright : not copyrighted - public domain + +--> + +<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="schema.xsd"> + + <a>true</a> + <b>1</b> + +</root> diff --git a/xsd-examples/cxx/parser/mixin/schema.map b/xsd-examples/cxx/parser/mixin/schema.map new file mode 100644 index 0000000..a6ffa76 --- /dev/null +++ b/xsd-examples/cxx/parser/mixin/schema.map @@ -0,0 +1,7 @@ +# file : cxx/parser/mixin/schema.map +# copyright : not copyrighted - public domain + +include "types.hxx"; + +base ::base*; +derived ::derived*; diff --git a/xsd-examples/cxx/parser/mixin/schema.xsd b/xsd-examples/cxx/parser/mixin/schema.xsd new file mode 100644 index 0000000..891241e --- /dev/null +++ b/xsd-examples/cxx/parser/mixin/schema.xsd @@ -0,0 +1,30 @@ +<?xml version="1.0"?> + +<!-- + +file : cxx/parser/mixin/schema.xsd +copyright : not copyrighted - public domain + +--> + +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> + + <xsd:complexType name="base"> + <xsd:sequence> + <xsd:element name="a" type="xsd:boolean"/> + </xsd:sequence> + </xsd:complexType> + + <xsd:complexType name="derived"> + <xsd:complexContent> + <xsd:extension base="base"> + <xsd:sequence> + <xsd:element name="b" type="xsd:int"/> + </xsd:sequence> + </xsd:extension> + </xsd:complexContent> + </xsd:complexType> + + <xsd:element name="root" type="derived"/> + +</xsd:schema> diff --git a/xsd-examples/cxx/parser/mixin/types.hxx b/xsd-examples/cxx/parser/mixin/types.hxx new file mode 100644 index 0000000..887b29e --- /dev/null +++ b/xsd-examples/cxx/parser/mixin/types.hxx @@ -0,0 +1,43 @@ +// file : cxx/parser/mixin/types.hxx +// copyright : not copyrighted - public domain + +#ifndef TYPES_HXX +#define TYPES_HXX + +struct base +{ + bool + a () const + { + return a_; + } + + void + a (bool v) + { + a_ = v; + } + +private: + bool a_; +}; + +struct derived: base +{ + int + b () const + { + return b_; + } + + void + b (int v) + { + b_ = v; + } + +private: + int b_; +}; + +#endif // TYPES_HXX |