aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/streaming
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cxx/hybrid/streaming')
-rw-r--r--examples/cxx/hybrid/streaming/README71
-rw-r--r--examples/cxx/hybrid/streaming/driver.cxx83
-rw-r--r--examples/cxx/hybrid/streaming/makefile79
-rw-r--r--examples/cxx/hybrid/streaming/object-pimpl.cxx47
-rw-r--r--examples/cxx/hybrid/streaming/object-pimpl.hxx33
-rw-r--r--examples/cxx/hybrid/streaming/object-simpl.cxx69
-rw-r--r--examples/cxx/hybrid/streaming/object-simpl.hxx32
-rw-r--r--examples/cxx/hybrid/streaming/position.xml22
-rw-r--r--examples/cxx/hybrid/streaming/position.xsd28
9 files changed, 464 insertions, 0 deletions
diff --git a/examples/cxx/hybrid/streaming/README b/examples/cxx/hybrid/streaming/README
new file mode 100644
index 0000000..216c370
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/README
@@ -0,0 +1,71 @@
+This example shows how to perform partially event-driven, partially in-
+memory processing using the Embedded C++/Hybrid mapping. With partially
+event-driven parsing and serialization we can process parts of the
+document as they become available as well as handle documents that
+are too large to fit into memory.
+
+This example uses the parser and serializer customization mechanisms
+provided by the C++/Hybrid mapping. For more information, see Section
+4.8, "Customizing the Object Model" and Section 6.1, "Customizing
+Parsers and Serializers" in the Embedded C++/Hybrid Mapping Getting
+Started Guide.
+
+The example consists of the following files:
+
+position.xsd
+ XML Schema which describes a simple object position vocabulary.
+ The position is represented as a potentially large series of
+ latitude/longitude measurements.
+
+position.xml
+ Sample object position document.
+
+position.hxx
+position.cxx
+
+position-pskel.hxx
+position-pskel.cxx
+position-pimpl.hxx
+position-pimpl.cxx
+
+position-pskel.hxx
+position-pskel.cxx
+position-pimpl.hxx
+position-pimpl.cxx
+ Object model (the first pair of files), parser skeletons (the
+ second pair), parser implementations (the third pair), serializer
+ skeletons (the fourth pair), and serializer implementations (the
+ fifth pair). These files are generated by the XSD/e compiler from
+ position.xsd. The --generate-parser, --generate-serializer, and
+ --generate-aggregate options were used to request the generation
+ of the parsing and serialization code. The --custom-parser option
+ was used to customize the object_pimpl parser implementation. The
+ --custom-serializer option was used to customize the object_simpl
+ serializer implementation.
+
+object-pimpl.hxx
+object-pimpl.cxx
+ Custom object parser implementation. It calculates the average
+ latitude and longitude values as position measurements become
+ available. At the end it prints the object position based on
+ these calculations.
+
+object-simpl.hxx
+object-simpl.cxx
+ Custom object serializer implementation. It performs a number of
+ measurements of the object position and serializes them as they
+ become available.
+
+driver.cxx
+ Driver for the example. It first calls the parser that calculates
+ and prints the object position without constructing the object
+ model. It then serializes a new set of measurements which are
+ taken as serialization progresses.
+
+To run the example on the sample XML instance document simply execute:
+
+$ ./driver position.xml
+
+The example reads from STDIN if input file is not specified:
+
+$ ./driver <position.xml
diff --git a/examples/cxx/hybrid/streaming/driver.cxx b/examples/cxx/hybrid/streaming/driver.cxx
new file mode 100644
index 0000000..ab2cd3a
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/driver.cxx
@@ -0,0 +1,83 @@
+// file : examples/cxx/hybrid/streaming/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <iostream>
+
+#include "position.hxx"
+
+#include "position-pimpl.hxx"
+#include "position-simpl.hxx"
+
+using namespace std;
+
+int
+main (int argc, char* argv[])
+{
+ const char* input;
+
+ if (argc < 2)
+ {
+ input = "STDIN";
+ cerr << "XML file not specified, reading from STDIN" << endl;
+ }
+ else
+ input = argv[1];
+
+ try
+ {
+ // Parse.
+ //
+ object_paggr object_p;
+
+ xml_schema::document_pimpl doc_p (
+ object_p.root_parser (),
+ object_p.root_name ());
+
+ object_p.pre ();
+
+ if (argc < 2)
+ doc_p.parse (cin);
+ else
+ doc_p.parse (argv[1]);
+
+ object_p.post (); // Ignore the NULL return value.
+
+ // Serialize.
+ //
+ object_saggr object_s;
+
+ xml_schema::document_simpl doc_s (
+ object_s.root_serializer (),
+ object_s.root_name ());
+
+ // Create an object instance. Its only purpose is to carry the
+ // object id. The position measurements will be supplied directly
+ // by the custom serializer.
+ //
+ object obj;
+ obj.id (456);
+
+ object_s.pre (obj);
+ doc_s.serialize (cout);
+ object_s.post ();
+ }
+ catch (const xml_schema::parser_exception& e)
+ {
+ cerr << input << ":" << e.line () << ":" << e.column () << ": "
+ << e.text () << endl;
+ return 1;
+ }
+ catch (const xml_schema::serializer_exception& e)
+ {
+ cerr << "error: " << e.text () << endl;
+ return 1;
+ }
+ catch (const std::ios_base::failure&)
+ {
+ cerr << input << ": unable to open or read/write failure" << endl;
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/examples/cxx/hybrid/streaming/makefile b/examples/cxx/hybrid/streaming/makefile
new file mode 100644
index 0000000..9fbedb3
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/makefile
@@ -0,0 +1,79 @@
+# file : examples/cxx/hybrid/streaming/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# 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 := position.xsd
+cxx := driver.cxx object-pimpl.cxx object-simpl.cxx
+
+obj := $(addprefix $(out_base)/,\
+$(cxx:.cxx=.o) \
+$(xsd:.xsd=.o) \
+$(xsd:.xsd=-pskel.o) \
+$(xsd:.xsd=-pimpl.o) \
+$(xsd:.xsd=-sskel.o) \
+$(xsd:.xsd=-simpl.o))
+
+dep := $(obj:.o=.o.d)
+
+xsde.l := $(out_root)/libxsde/xsde/xsde.l
+xsde.l.cpp-options := $(out_root)/libxsde/xsde/xsde.l.cpp-options
+
+driver := $(out_base)/driver
+clean := $(out_base)/.clean
+
+# Build.
+#
+$(driver): $(obj) $(xsde.l)
+
+$(obj) $(dep): $(xsde.l.cpp-options)
+
+gen := $(out_base)/$(xsd:.xsd=.hxx) \
+ $(out_base)/$(xsd:.xsd=.cxx) \
+ $(out_base)/$(xsd:.xsd=-pskel.hxx) \
+ $(out_base)/$(xsd:.xsd=-pskel.cxx) \
+ $(out_base)/$(xsd:.xsd=-pimpl.hxx) \
+ $(out_base)/$(xsd:.xsd=-pimpl.cxx) \
+ $(out_base)/$(xsd:.xsd=-sskel.hxx) \
+ $(out_base)/$(xsd:.xsd=-sskel.cxx) \
+ $(out_base)/$(xsd:.xsd=-simpl.hxx) \
+ $(out_base)/$(xsd:.xsd=-simpl.cxx)
+
+$(gen): $(out_root)/xsde/xsde
+$(gen): xsde := $(out_root)/xsde/xsde
+$(gen): xsde_options += --generate-parser --generate-serializer \
+--generate-aggregate --custom-parser object=/object-pimpl.hxx \
+--custom-serializer object=/object-simpl.hxx
+
+$(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)/xsde/hybrid/xsd-cxx.make)
+
+
+# Dependencies.
+#
+$(call import,$(src_root)/xsde/makefile)
+$(call import,$(src_root)/libxsde/xsde/makefile)
diff --git a/examples/cxx/hybrid/streaming/object-pimpl.cxx b/examples/cxx/hybrid/streaming/object-pimpl.cxx
new file mode 100644
index 0000000..3f45c79
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/object-pimpl.cxx
@@ -0,0 +1,47 @@
+// file : examples/cxx/hybrid/streaming/object-pimpl.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <iostream>
+
+// Include position-pimpl.hxx (which includes object-pimpl.hxx)
+// instead of object-pimpl.hxx.
+//
+#include "position-pimpl.hxx"
+
+using namespace std;
+
+void object_pimpl::
+pre ()
+{
+ // Initialize the variables.
+ //
+ count_ = 0;
+ avg_lat_ = 0.0;
+ avg_lon_ = 0.0;
+}
+
+void object_pimpl::
+id (unsigned int id)
+{
+ id_ = id;
+}
+
+void object_pimpl::
+position (const ::position& p)
+{
+ count_++;
+ avg_lat_ += p.lat ();
+ avg_lon_ += p.lon ();
+}
+
+object* object_pimpl::
+post_object ()
+{
+ avg_lat_ /= count_;
+ avg_lon_ /= count_;
+
+ cerr << "object " << id_ << ": " << avg_lat_ << " " << avg_lon_ << endl;
+
+ return 0; // We don't construct the object so return NULL.
+}
diff --git a/examples/cxx/hybrid/streaming/object-pimpl.hxx b/examples/cxx/hybrid/streaming/object-pimpl.hxx
new file mode 100644
index 0000000..8d59063
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/object-pimpl.hxx
@@ -0,0 +1,33 @@
+// file : examples/cxx/hybrid/streaming/object-pimpl.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef OBJECT_PIMPL_HXX
+#define OBJECT_PIMPL_HXX
+
+// Customized object parser implementation.
+//
+class object_pimpl: public object_pskel
+{
+public:
+ virtual void
+ pre ();
+
+ virtual void
+ id (unsigned int);
+
+ virtual void
+ position (const ::position&);
+
+ virtual object*
+ post_object ();
+
+private:
+ unsigned int id_;
+
+ unsigned int count_;
+ float avg_lat_;
+ float avg_lon_;
+};
+
+#endif // OBJECT_PIMPL_HXX
diff --git a/examples/cxx/hybrid/streaming/object-simpl.cxx b/examples/cxx/hybrid/streaming/object-simpl.cxx
new file mode 100644
index 0000000..f23e0d4
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/object-simpl.cxx
@@ -0,0 +1,69 @@
+// file : examples/cxx/hybrid/streaming/object-simpl.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+// Include position-simpl.hxx (which includes object-simpl.hxx)
+// instead of object-simpl.hxx.
+//
+#include "position-simpl.hxx"
+
+// Position measurement instrument interface.
+//
+struct measurements
+{
+ float lat;
+ float lon;
+};
+
+measurements test_measurements [8] =
+{
+ {-33.8569F, 18.5083F},
+ {-33.8568F, 18.5083F},
+ {-33.8568F, 18.5082F},
+ {-33.8570F, 18.5083F},
+ {-33.8569F, 18.5084F},
+ {-33.8570F, 18.5084F},
+ {-33.8570F, 18.5082F},
+ {-33.8569F, 18.5082F}
+};
+
+static void
+measure_position (unsigned int n, float& lat, float& lon)
+{
+ // Call the instrument to measure the position.
+ //
+ lat = test_measurements[n].lat;
+ lon = test_measurements[n].lon;
+}
+
+// Serializer implementation.
+//
+void object_simpl::
+pre (const object& obj)
+{
+ // Cache the object id and determine how many position measuremenets
+ // we need to take.
+ //
+ id_ = obj.id ();
+ count_ = 8;
+}
+
+unsigned int object_simpl::
+id ()
+{
+ return id_;
+}
+
+bool object_simpl::
+position_next ()
+{
+ return count_ > 0;
+}
+
+const position& object_simpl::
+position ()
+{
+ count_--;
+ measure_position (count_, cur_pos_.lat (), cur_pos_.lon ());
+ return cur_pos_;
+}
diff --git a/examples/cxx/hybrid/streaming/object-simpl.hxx b/examples/cxx/hybrid/streaming/object-simpl.hxx
new file mode 100644
index 0000000..6b20399
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/object-simpl.hxx
@@ -0,0 +1,32 @@
+// file : examples/cxx/hybrid/streaming/object-simpl.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef OBJECT_SIMPL_HXX
+#define OBJECT_SIMPL_HXX
+
+// Customized object serializer implementation.
+//
+class object_simpl: public object_sskel
+{
+public:
+ virtual void
+ pre (const object&);
+
+ virtual unsigned int
+ id ();
+
+ virtual bool
+ position_next ();
+
+ virtual const ::position&
+ position ();
+
+private:
+ unsigned int id_;
+
+ unsigned int count_;
+ ::position cur_pos_;
+};
+
+#endif // OBJECT_SIMPL_HXX
diff --git a/examples/cxx/hybrid/streaming/position.xml b/examples/cxx/hybrid/streaming/position.xml
new file mode 100644
index 0000000..0943bb2
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/position.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/hybrid/streaming/position.xml
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<object id="123">
+
+ <position lat="-33.8569" lon="18.5083"/>
+ <position lat="-33.8568" lon="18.5083"/>
+ <position lat="-33.8568" lon="18.5082"/>
+ <position lat="-33.8570" lon="18.5083"/>
+ <position lat="-33.8569" lon="18.5084"/>
+ <position lat="-33.8570" lon="18.5084"/>
+ <position lat="-33.8570" lon="18.5082"/>
+ <position lat="-33.8569" lon="18.5082"/>
+
+</object>
diff --git a/examples/cxx/hybrid/streaming/position.xsd b/examples/cxx/hybrid/streaming/position.xsd
new file mode 100644
index 0000000..70b73f2
--- /dev/null
+++ b/examples/cxx/hybrid/streaming/position.xsd
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/hybrid/streaming/position.xsd
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
+
+ <xsd:complexType name="position">
+ <xsd:attribute name="lat" type="xsd:float" use="required"/>
+ <xsd:attribute name="lon" type="xsd:float" use="required"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="object">
+ <xsd:sequence>
+ <xsd:element name="position" type="position" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="id" type="xsd:unsignedInt" use="required"/>
+ </xsd:complexType>
+
+ <xsd:element name="object" type="object"/>
+
+</xsd:schema>