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/parser/mixin/README | 34 +++++++++++ examples/cxx/parser/mixin/driver.cxx | 104 +++++++++++++++++++++++++++++++++ examples/cxx/parser/mixin/instance.xml | 17 ++++++ examples/cxx/parser/mixin/makefile | 68 +++++++++++++++++++++ examples/cxx/parser/mixin/schema.map | 8 +++ examples/cxx/parser/mixin/schema.xsd | 31 ++++++++++ examples/cxx/parser/mixin/types.hxx | 44 ++++++++++++++ 7 files changed, 306 insertions(+) create mode 100644 examples/cxx/parser/mixin/README create mode 100644 examples/cxx/parser/mixin/driver.cxx create mode 100644 examples/cxx/parser/mixin/instance.xml create mode 100644 examples/cxx/parser/mixin/makefile create mode 100644 examples/cxx/parser/mixin/schema.map create mode 100644 examples/cxx/parser/mixin/schema.xsd create mode 100644 examples/cxx/parser/mixin/types.hxx (limited to 'examples/cxx/parser/mixin') diff --git a/examples/cxx/parser/mixin/README b/examples/cxx/parser/mixin/README new file mode 100644 index 0000000..343e379 --- /dev/null +++ b/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/examples/cxx/parser/mixin/driver.cxx b/examples/cxx/parser/mixin/driver.cxx new file mode 100644 index 0000000..41e5a45 --- /dev/null +++ b/examples/cxx/parser/mixin/driver.cxx @@ -0,0 +1,104 @@ +// file : examples/cxx/parser/mixin/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include +#include + +#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: + auto_ptr 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 (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]); + auto_ptr 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/examples/cxx/parser/mixin/instance.xml b/examples/cxx/parser/mixin/instance.xml new file mode 100644 index 0000000..265271b --- /dev/null +++ b/examples/cxx/parser/mixin/instance.xml @@ -0,0 +1,17 @@ + + + + + + + true + 1 + + diff --git a/examples/cxx/parser/mixin/makefile b/examples/cxx/parser/mixin/makefile new file mode 100644 index 0000000..91030be --- /dev/null +++ b/examples/cxx/parser/mixin/makefile @@ -0,0 +1,68 @@ +# file : examples/cxx/parser/mixin/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 := schema.xsd +cxx := driver.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=-pskel.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) + + +# Build. +# +$(driver): $(obj) $(xerces_c.l) + +$(obj) $(dep): cpp_options := -I$(src_root)/libxsd +$(obj) $(dep): $(xerces_c.l.cpp-options) + +skel := $(out_base)/$(xsd:.xsd=-pskel.hxx) \ + $(out_base)/$(xsd:.xsd=-pskel.ixx) \ + $(out_base)/$(xsd:.xsd=-pskel.cxx) + +$(skel): xsd := $(out_root)/xsd/xsd +$(skel): xsd_options := --type-map $(src_base)/schema.map +$(skel): $(out_root)/xsd/xsd $(src_base)/schema.map + +$(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=-pskel.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/parser/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) diff --git a/examples/cxx/parser/mixin/schema.map b/examples/cxx/parser/mixin/schema.map new file mode 100644 index 0000000..22edb1e --- /dev/null +++ b/examples/cxx/parser/mixin/schema.map @@ -0,0 +1,8 @@ +# file : examples/cxx/parser/mixin/schema.map +# author : Boris Kolpackov +# copyright : not copyrighted - public domain + +include "types.hxx"; + +base ::base*; +derived ::derived*; diff --git a/examples/cxx/parser/mixin/schema.xsd b/examples/cxx/parser/mixin/schema.xsd new file mode 100644 index 0000000..59d8d3b --- /dev/null +++ b/examples/cxx/parser/mixin/schema.xsd @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/parser/mixin/types.hxx b/examples/cxx/parser/mixin/types.hxx new file mode 100644 index 0000000..708dfc3 --- /dev/null +++ b/examples/cxx/parser/mixin/types.hxx @@ -0,0 +1,44 @@ +// file : examples/cxx/parser/mixin/types.hxx +// author : Boris Kolpackov +// 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 -- cgit v1.1