summaryrefslogtreecommitdiff
path: root/libcutl/tests/compiler/traversal/driver.cxx
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2014-07-23 15:25:44 +0200
committerJörg Frings-Fürst <debian@jff-webhosting.net>2014-07-23 15:25:44 +0200
commit8286ac511144e4f17d34eac9affb97e50646344a (patch)
treef1af7320d7b6be6be059216d0ad08ac7b4f73fd0 /libcutl/tests/compiler/traversal/driver.cxx
parenta15cf65c44d5c224169c32ef5495b68c758134b7 (diff)
Imported Upstream version 4.0.0upstream/4.0.0
Diffstat (limited to 'libcutl/tests/compiler/traversal/driver.cxx')
-rw-r--r--libcutl/tests/compiler/traversal/driver.cxx139
1 files changed, 139 insertions, 0 deletions
diff --git a/libcutl/tests/compiler/traversal/driver.cxx b/libcutl/tests/compiler/traversal/driver.cxx
new file mode 100644
index 0000000..b1fea74
--- /dev/null
+++ b/libcutl/tests/compiler/traversal/driver.cxx
@@ -0,0 +1,139 @@
+// file : tests/compiler/traversal/driver.cxx
+// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include <vector>
+#include <iostream>
+
+#include <cutl/shared-ptr.hxx>
+
+#include <cutl/compiler/type-info.hxx>
+#include <cutl/compiler/traversal.hxx>
+
+using namespace std;
+using namespace cutl;
+
+// Data types.
+//
+struct base
+{
+ virtual ~base () {}
+};
+
+struct derived1: base {};
+struct derived2: base {};
+
+typedef vector<shared_ptr<base> > objects;
+
+struct init
+{
+ init ()
+ {
+ using compiler::type_info;
+
+ {
+ type_info ti (typeid (base));
+ insert (ti);
+ }
+
+ {
+ type_info ti (typeid (derived1));
+ ti.add_base (typeid (base));
+ insert (ti);
+ }
+
+ {
+ type_info ti (typeid (derived2));
+ ti.add_base (typeid (base));
+ insert (ti);
+ }
+ }
+} init_;
+
+// Traversers.
+//
+template <typename X>
+struct traverser: compiler::traverser_impl<X, base>,
+ virtual compiler::dispatcher<base>
+{
+ void
+ add_traverser (compiler::traverser_map<base>& m)
+ {
+ compiler::dispatcher<base>::traverser (m);
+ }
+};
+
+typedef traverser<base> base_trav;
+typedef traverser<derived1> derived1_trav;
+typedef traverser<derived2> derived2_trav;
+
+struct base_impl: base_trav
+{
+ virtual void
+ traverse (type&)
+ {
+ cout << "base_impl: base" << endl;
+ }
+};
+
+struct derived1_impl: derived1_trav
+{
+ virtual void
+ traverse (type&)
+ {
+ cout << "derived1_impl: derived1" << endl;
+ }
+};
+
+struct combined_impl: derived1_trav, derived2_trav
+{
+ virtual void
+ traverse (derived1&)
+ {
+ cout << "combined_impl: derived1" << endl;
+ }
+
+ virtual void
+ traverse (derived2&)
+ {
+ cout << "combined_impl: derived2" << endl;
+ }
+};
+
+int
+main ()
+{
+ objects o;
+ o.push_back (shared_ptr<base> (new (shared) base));
+ o.push_back (shared_ptr<base> (new (shared) derived1));
+ o.push_back (shared_ptr<base> (new (shared) derived2));
+
+ base_impl base;
+ derived1_impl derived1;
+ combined_impl combined;
+
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ base.dispatch (**i);
+
+ cout << endl;
+
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ derived1.dispatch (**i);
+
+ cout << endl;
+
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ combined.dispatch (**i);
+
+ cout << endl;
+
+ base.add_traverser (derived1);
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ base.dispatch (**i);
+
+ cout << endl;
+
+ derived1.add_traverser (combined);
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ derived1.dispatch (**i);
+}