diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2014-05-09 18:33:25 -0700 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2014-05-09 18:33:25 -0700 |
commit | 9cb49187f65bea1a9ec0fd6b35295632d6a7e121 (patch) | |
tree | 3268e2465a0e712ea40622c27f69d914ce425fbd | |
parent | 163fe8b81e7d1f25d745936ca3812e1913d95389 (diff) |
Add 'processing' example that uses higher-level API
-rw-r--r-- | examples/makefile | 2 | ||||
-rw-r--r-- | examples/processing/README | 0 | ||||
-rw-r--r-- | examples/processing/driver.cxx | 145 | ||||
-rw-r--r-- | examples/processing/makefile | 88 | ||||
-rw-r--r-- | examples/processing/position.xml | 7 |
5 files changed, 241 insertions, 1 deletions
diff --git a/examples/makefile b/examples/makefile index 8cd8a21..52808d4 100644 --- a/examples/makefile +++ b/examples/makefile @@ -4,7 +4,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make -examples := roundtrip performance +examples := roundtrip processing performance default := $(out_base)/ test := $(out_base)/.test diff --git a/examples/processing/README b/examples/processing/README new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/examples/processing/README diff --git a/examples/processing/driver.cxx b/examples/processing/driver.cxx new file mode 100644 index 0000000..e0f939d --- /dev/null +++ b/examples/processing/driver.cxx @@ -0,0 +1,145 @@ +// file : examples/processing/driver.cxx +// copyright : not copyrighted - public domain + +#include <string> +#include <fstream> +#include <iostream> + +#include <xml/parser.hxx> +#include <xml/serializer.hxx> +#include <xml/value-traits.hxx> + +using namespace std; +using namespace xml; + +enum object_type {building, mountain}; + +// Specialization of xml::value_traits for object_type. This mechanism is +// used to implements XML-specific value type parsing and serialization. +// By default the parser and serializer fall back onto iostream insertion +// and extraction operators. +// +// This code also shows how we can reuse the parsing exception to implement +// our own diagnostics. +// +namespace xml +{ + template <> + struct value_traits<object_type> + { + static object_type + parse (string s, const parser& p) + { + if (s == "building") + return building; + else if (s == "mountain") + return mountain; + else + throw parsing (p, "invalid object type '" + s + "'"); + } + + static string + serialize (object_type x, const serializer&) + { + if (x == building) + return "building"; + else + return "mountain"; + } + }; +} + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " <xml-file>" << endl; + return 1; + } + + try + { + // Parse the input document and compute the average object position. + // + ifstream ifs (argv[1]); + parser p (ifs, argv[1]); + + p.next_expect (parser::start_element, "object", content::complex); + + unsigned int id (p.attribute<unsigned int> ("id")); + string name (p.element ("name")); + object_type type (p.element<object_type> ("type")); + + float alat (0), alon (0); + unsigned int count (0); + + do + { + p.next_expect (parser::start_element, "position", content::empty); + + float lat (p.attribute<float> ("lat")); + float lon (p.attribute<float> ("lon")); + + p.next_expect (parser::end_element); // position + + alat += lat; + alon += lon; + count++; + + } while (p.peek () == parser::start_element); + + // Here is another example of re-using the parsing exception to + // implement application-level diagnostics. Every object in our + // vocabulary should have at least two position samples. + // + if (count < 2) + throw parsing (p, "at least two position samples required"); + + alat /= count; + alon /= count; + + p.next_expect (parser::end_element); // object + + // Serialize an XML document with simulated positions based on the + // average we just calculated. + // + serializer s (cout, "output"); + s.start_element ("object"); + + s.attribute ("id", id); + s.element ("name", name); + s.element ("type", type); + + for (unsigned int i (0); i < 2; ++i) + { + for (unsigned int j (0); j < 2; ++j) + { + s.start_element ("position"); + + float lat (alat + (i % 2 ? 0.0001F : -0.0001F)); + float lon (alon + (j % 2 ? -0.0001F : 0.0001F)); + + s.attribute ("lat", lat); + s.attribute ("lon", lon); + + s.end_element (); // position + } + } + + s.end_element (); // object + } + catch (const ios_base::failure&) + { + cerr << "io failure" << endl; + return 1; + } + // This handler will handle both parsing (xml::parsing) and serialization + // (xml::serialization) exceptions. + // + catch (const xml::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/examples/processing/makefile b/examples/processing/makefile new file mode 100644 index 0000000..3acc355 --- /dev/null +++ b/examples/processing/makefile @@ -0,0 +1,88 @@ +# file : examples/processing/makefile +# copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC +# license : MIT; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx + +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +studxml.l := $(out_root)/xml/studxml.l +studxml.l.cpp-options := $(out_root)/xml/studxml.l.cpp-options + +driver := $(out_base)/driver +test := $(out_base)/.test +dist := $(out_base)/.dist +clean := $(out_base)/.clean + +# Build. +# +$(driver): $(cxx_obj) $(studxml.l) +$(cxx_obj) $(cxx_od): $(studxml.l.cpp-options) + +$(call include-dep,$(cxx_od)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): name := $(subst $(src_root)/examples/,,$(src_base)) +$(dist): sources := $(cxx_tun) +$(dist): extras := README position.xml +$(dist): export extra_dist := $(extras) $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters \ +$(name)-vc11.vcxproj $(name)-vc11.vcxproj.filters \ +$(name)-vc12.vcxproj $(name)-vc12.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(extras)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + $(call meta-vc11proj,../template/template-vc11.vcxproj,$(name)-vc11.vcxproj) + $(call meta-vc12proj,../template/template-vc12.vcxproj,$(name)-vc12.vcxproj) + +# Test. +# +$(test): $(driver) + $(call message,test $<,$< $(src_base)/position.xml) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) + + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.make) +$(call include,$(bld_root)/meta/vc12proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/o-e.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) + +# Dependencies. +# +$(call import,$(src_root)/xml/makefile) diff --git a/examples/processing/position.xml b/examples/processing/position.xml new file mode 100644 index 0000000..4912ae7 --- /dev/null +++ b/examples/processing/position.xml @@ -0,0 +1,7 @@ +<object id="123"> + <name>Lion's Head</name> + <type>mountain</type> + <position lat="-33.8569" lon="18.5083"/> + <position lat="-33.8568" lon="18.5083"/> + <position lat="-33.8568" lon="18.5082"/> +</object> |