diff options
Diffstat (limited to 'ACE/contrib/utility/Example/Introspection')
11 files changed, 856 insertions, 0 deletions
diff --git a/ACE/contrib/utility/Example/Introspection/InheritanceTree/Hierarchy.cpp b/ACE/contrib/utility/Example/Introspection/InheritanceTree/Hierarchy.cpp new file mode 100644 index 00000000000..0dbc616483e --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/InheritanceTree/Hierarchy.cpp @@ -0,0 +1,104 @@ +// file : Hierarchy.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#include "Hierarchy.hpp" + +// A +// +// + +using Introspection::Object; +using Introspection::Access; + +namespace +{ + TypeInfo + a_init_ () + { + TypeInfo ti (typeid (A)); + ti.add_base (Access::PUBLIC, true, Object::static_type_info ()); + return ti; + } + + TypeInfo a_ (a_init_ ()); +} + +TypeInfo const& A:: +static_type_info () +{ + return a_; +} + +// B +// +// + +namespace +{ + TypeInfo + b_init_ () + { + TypeInfo ti (typeid (B)); + ti.add_base (Access::PUBLIC, false, A::static_type_info ()); + return ti; + } + + TypeInfo b_ (b_init_ ()); +} + +TypeInfo const& B:: +static_type_info () +{ + return b_; +} + +// C +// +// + +namespace +{ + TypeInfo + c_init_ () + { + TypeInfo ti (typeid (C)); + ti.add_base (Access::PUBLIC, true, A::static_type_info ()); + return ti; + } + + TypeInfo c_ (c_init_ ()); +} + +TypeInfo const& C:: +static_type_info () +{ + return c_; +} + + +// D +// +// + +namespace +{ + TypeInfo + d_init_ () + { + TypeInfo ti (typeid (D)); + ti.add_base (Access::PUBLIC, true, B::static_type_info ()); + ti.add_base (Access::PUBLIC, false, C::static_type_info ()); + return ti; + } + + TypeInfo d_ (d_init_ ()); +} + +TypeInfo const& D:: +static_type_info () +{ + return d_; +} +//$Id$ diff --git a/ACE/contrib/utility/Example/Introspection/InheritanceTree/Hierarchy.hpp b/ACE/contrib/utility/Example/Introspection/InheritanceTree/Hierarchy.hpp new file mode 100644 index 00000000000..213e0593f6b --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/InheritanceTree/Hierarchy.hpp @@ -0,0 +1,61 @@ +// file : Example/Introspection/InheritanceTree/Hierarchy.hpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#ifndef HIERARCHY_HPP +#define HIERARCHY_HPP + +#include "Utility/Introspection/Introspection.hpp" + +namespace Introspection = Utility::Introspection; + +using Introspection::TypeInfo; +using Introspection::TypeId; + +struct A : virtual Introspection::Object +{ + A () + { + type_info (static_type_info ()); + } + + static TypeInfo const& + static_type_info (); +}; + +struct B : virtual A +{ + B () + { + type_info (static_type_info ()); + } + + static TypeInfo const& + static_type_info (); +}; + +struct C : virtual A +{ + C () + { + type_info (static_type_info ()); + } + + static TypeInfo const& + static_type_info (); +}; + +struct D : virtual B, C +{ + D () + { + type_info (static_type_info ()); + } + + static TypeInfo const& + static_type_info (); +}; + +#endif // HIERARCHY_HPP +//$Id$ diff --git a/ACE/contrib/utility/Example/Introspection/InheritanceTree/Makefile b/ACE/contrib/utility/Example/Introspection/InheritanceTree/Makefile new file mode 100644 index 00000000000..9b839c2111f --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/InheritanceTree/Makefile @@ -0,0 +1,22 @@ +# file : Example/Introspection/InheritanceTree/Makefile +# author : Boris Kolpackov <boris@kolpackov.net> +# copyright : Copyright (c) 2002-2003 Boris Kolpackov +# license : http://kolpackov.net/license.html + +root := ../../.. + +include $(root)/BuildRules/Bootstrap.rules + +$(call include, $(root)/BuildRules/Executable.pre.rules) + + +cxx_translation_units := Hierarchy.cpp inheritance_tree.cpp + +module_base := inheritance_tree + +CXX_PREPROCESS_FLAGS += -I $(root) + +CXX_LINK_LIBS += -L$(root)/Utility/Introspection -lIntrospection + +$(call include, $(root)/BuildRules/Executable.post.rules) +# $Id$ diff --git a/ACE/contrib/utility/Example/Introspection/InheritanceTree/inheritance_tree.cpp b/ACE/contrib/utility/Example/Introspection/InheritanceTree/inheritance_tree.cpp new file mode 100644 index 00000000000..97c4c68dda6 --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/InheritanceTree/inheritance_tree.cpp @@ -0,0 +1,64 @@ +// file : Example/Introspection/InheritanceTree/inheritance_tree.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +// Note: This example is by no means complete. In fact properly printing +// arbitrary C++ inheritance tree is a non-trivial task. If you would like +// to improve this example please feel free to send your results back ;-). +// + +#include "Hierarchy.hpp" + +#include <set> +#include <iostream> + +using std::endl; + +typedef +std::set<TypeId> +TypeIdSet; + +void +print_inheritance_tree_core (std::ostream& os, + TypeInfo const& ti, + TypeIdSet& set) +{ + bool nl = false; + + for (TypeInfo::BaseIterator i = ti.begin_base (); + i != ti.end_base (); + i++) + { + TypeId tid (i->type_info ().type_id ()); + + if (set.find (tid) != set.end ()) continue; + + nl = true; + set.insert (tid); + print_inheritance_tree_core (os, i->type_info (), set); + } + + if (nl) os << endl; + + os << ti.type_id () << " "; +} + +void +print_inheritance_tree (std::ostream& os, TypeInfo const& ti) +{ + TypeIdSet set; + print_inheritance_tree_core (os, ti, set); + os << endl; +} + +int +main () +{ + B* b = new D; + + print_inheritance_tree (std::cout, b->type_info ()); + + delete b; +} +//$Id$ diff --git a/ACE/contrib/utility/Example/Introspection/Makefile b/ACE/contrib/utility/Example/Introspection/Makefile new file mode 100644 index 00000000000..2dc0a1f809e --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/Makefile @@ -0,0 +1,16 @@ +# file : Example/Introspection/Makefile +# author : Boris Kolpackov <boris@kolpackov.net> +# copyright : Copyright (c) 2002-2003 Boris Kolpackov +# license : http://kolpackov.net/license.html + +root := ../.. + +include $(root)/BuildRules/Bootstrap.rules + +$(call include, $(root)/BuildRules/Recursion.pre.rules) + +target_makefile_list := +target_directory_list := InheritanceTree Traversal + +$(call include, $(root)/BuildRules/Recursion.post.rules) +# $Id$ diff --git a/ACE/contrib/utility/Example/Introspection/Traversal/Makefile b/ACE/contrib/utility/Example/Introspection/Traversal/Makefile new file mode 100644 index 00000000000..c9b1a8da46a --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/Traversal/Makefile @@ -0,0 +1,22 @@ +# file : Example/Introspection/Traversal/Makefile +# author : Boris Kolpackov <boris@kolpackov.net> +# copyright : Copyright (c) 2002-2003 Boris Kolpackov +# license : http://kolpackov.net/license.html + +root := ../../.. + +include $(root)/BuildRules/Bootstrap.rules + +$(call include, $(root)/BuildRules/Executable.pre.rules) + + +cxx_translation_units := SyntaxTree.cpp Traversal.cpp driver.cpp + +module_base := driver + +CXX_PREPROCESS_FLAGS += -I $(root) + +CXX_LINK_LIBS += -L$(root)/Utility/Introspection -lIntrospection + +$(call include, $(root)/BuildRules/Executable.post.rules) +# $Id$ diff --git a/ACE/contrib/utility/Example/Introspection/Traversal/SyntaxTree.cpp b/ACE/contrib/utility/Example/Introspection/Traversal/SyntaxTree.cpp new file mode 100644 index 00000000000..71115ff2b24 --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/Traversal/SyntaxTree.cpp @@ -0,0 +1,119 @@ +// file : Example/Introspection/Traversal/SyntaxTree.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#include "SyntaxTree.hpp" + +using namespace Utility::Introspection; + +namespace SyntaxTree +{ + + // Node + // + // + + namespace + { + TypeInfo + node_init_ () + { + TypeInfo ti (typeid (Node)); + ti.add_base (Access::PUBLIC, true, Object::static_type_info ()); + return ti; + } + + TypeInfo node_ (node_init_ ()); + } + + TypeInfo const& Node:: + static_type_info () { return node_; } + + + // Declaration + // + // + + namespace + { + TypeInfo + declaration_init_ () + { + TypeInfo ti (typeid (Declaration)); + ti.add_base (Access::PUBLIC, true, Node::static_type_info ()); + return ti; + } + + TypeInfo declaration_ (declaration_init_ ()); + } + + TypeInfo const& Declaration:: + static_type_info () { return declaration_; } + + + // Scope + // + // + + namespace + { + TypeInfo + scope_init_ () + { + TypeInfo ti (typeid (Scope)); + ti.add_base (Access::PUBLIC, true, Declaration::static_type_info ()); + return ti; + } + + TypeInfo scope_ (scope_init_ ()); + } + + TypeInfo const& Scope:: + static_type_info () { return scope_; } + + + // InterfaceDecl + // + // + + namespace + { + TypeInfo + interface_decl_init_ () + { + TypeInfo ti (typeid (InterfaceDecl)); + ti.add_base (Access::PUBLIC, true, Declaration::static_type_info ()); + return ti; + } + + TypeInfo interface_decl_ (interface_decl_init_ ()); + } + + TypeInfo const& InterfaceDecl:: + static_type_info () { return interface_decl_; } + + + // InterfaceDef + // + // + + namespace + { + TypeInfo + interface_def_init_ () + { + TypeInfo ti (typeid (InterfaceDef)); + ti.add_base (Access::PUBLIC, true, InterfaceDecl::static_type_info ()); + ti.add_base (Access::PUBLIC, true, Scope::static_type_info ()); + return ti; + } + + TypeInfo interface_def_ (interface_def_init_ ()); + } + + TypeInfo const& InterfaceDef:: + static_type_info () { return interface_def_; } + +} +//$Id$ diff --git a/ACE/contrib/utility/Example/Introspection/Traversal/SyntaxTree.hpp b/ACE/contrib/utility/Example/Introspection/Traversal/SyntaxTree.hpp new file mode 100644 index 00000000000..7bd824ce683 --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/Traversal/SyntaxTree.hpp @@ -0,0 +1,95 @@ +// file : Example/Introspection/Traversal/SyntaxTree.hpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#ifndef SYNTAX_TREE_HPP +#define SYNTAX_TREE_HPP + +#include <vector> + +#include "Utility/Introspection/Introspection.hpp" + +namespace SyntaxTree +{ + // + // + // + + struct Node : virtual Utility::Introspection::Object + { + Node () + { + type_info (static_type_info ()); + } + + static Utility::Introspection::TypeInfo const& + static_type_info (); + }; + + // + // + // + + struct Declaration : virtual Node + { + Declaration () + { + type_info (static_type_info ()); + } + + static Utility::Introspection::TypeInfo const& + static_type_info (); + }; + + typedef + std::vector<Declaration*> + DeclarationList; + + // + // + // + struct Scope : virtual Declaration + { + Scope () + { + type_info (static_type_info ()); + } + + static Utility::Introspection::TypeInfo const& + static_type_info (); + + DeclarationList content_; + }; + + // + // + // + struct InterfaceDecl : virtual Declaration + { + InterfaceDecl () + { + type_info (static_type_info ()); + } + + static Utility::Introspection::TypeInfo const& + static_type_info (); + }; + + // + // + // + struct InterfaceDef : virtual InterfaceDecl, virtual Scope + { + InterfaceDef () + { + type_info (static_type_info ()); + } + + static Utility::Introspection::TypeInfo const& + static_type_info (); + }; +} + +#endif // SYNTAX_TREE_HPP +//$Id$ diff --git a/ACE/contrib/utility/Example/Introspection/Traversal/Traversal.cpp b/ACE/contrib/utility/Example/Introspection/Traversal/Traversal.cpp new file mode 100644 index 00000000000..9fa94327c2c --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/Traversal/Traversal.cpp @@ -0,0 +1,105 @@ +// file : Example/Introspection/Traversal/Traversal.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#include "Traversal.hpp" + +#include <set> +#include <map> + +using namespace Utility::Introspection; + +namespace Traversal +{ + // Dispatcher + // + // + + struct TypeInfoComparator + { + bool + operator () (TypeInfo const& x, TypeInfo const& y) const + { + return x.type_id () < y.type_id (); + } + }; + + typedef + std::map<TypeInfo, unsigned long, TypeInfoComparator> + LevelMap; + + typedef + std::set<TypeInfo, TypeInfoComparator> + TypeInfoSet; + + unsigned long + compute_levels (TypeInfo const& ti, unsigned long cur, LevelMap& map) + { + unsigned long ret = cur; + + if (map.find (ti) == map.end () || map[ti] < cur) map[ti] = cur; + + for (TypeInfo::BaseIterator i = ti.begin_base (); + i != ti.end_base (); + i++) + { + unsigned long t = compute_levels (i->type_info (), cur + 1, map); + if (t > ret) ret = t; + } + + return ret; + } + + void + flatten_tree (TypeInfo const& ti, TypeInfoSet& set) + { + set.insert (ti); + + for (TypeInfo::BaseIterator i = ti.begin_base (); + i != ti.end_base (); + i++) + { + flatten_tree (i->type_info (), set); + } + } + + void Dispatcher:: + dispatch (SyntaxTree::Node* n) + { + LevelMap levels; + + unsigned long max = compute_levels (n->type_info (), 0, levels); + + for (unsigned long l = 0; l < max + 1; l++) + { + TypeInfoSet dispatched; + + for (LevelMap::const_iterator i = levels.begin (); + i != levels.end (); + i++) + { + if (i->second == l) + { + TraversalMap::const_iterator v = + traversal_map_.find (i->first.type_id ()); + + if (v != traversal_map_.end ()) + { + v->second->traverse (n); + flatten_tree (i->first, dispatched); + } + } + } + + // Remove traversed types from level map. + for (TypeInfoSet::const_iterator i = dispatched.begin (); + i != dispatched.end (); + i++) + { + levels.erase (*i); + } + } + } +} +//$Id$ diff --git a/ACE/contrib/utility/Example/Introspection/Traversal/Traversal.hpp b/ACE/contrib/utility/Example/Introspection/Traversal/Traversal.hpp new file mode 100644 index 00000000000..7ee84523fc0 --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/Traversal/Traversal.hpp @@ -0,0 +1,157 @@ +// file : Example/Introspection/Traversal/Traversal.hpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#ifndef TRAVERSAL_HPP +#define TRAVERSAL_HPP + +#include <map> +#include <iostream> + +#include "Utility/Introspection/Introspection.hpp" + +#include "SyntaxTree.hpp" + +namespace Traversal +{ + class Traverser; + + // + // + // + class Dispatcher + { + public: + virtual + ~Dispatcher () + { + } + + virtual void + dispatch (SyntaxTree::Node* n); + + protected: + void + map (Utility::Introspection::TypeId id, Traverser* t) + { + traversal_map_[id] = t; + } + + private: + typedef + std::map<Utility::Introspection::TypeId, Traverser*> + TraversalMap; + + TraversalMap traversal_map_; + }; + + + // + // + // + class Traverser : public virtual Dispatcher + { + public: + virtual void + traverse (SyntaxTree::Node* n) = 0; + }; + + // + // + // + struct Node : Traverser + { + Node () + { + map (typeid (SyntaxTree::Node), this); + } + + virtual void + traverse (SyntaxTree::Node*) + { + std::cerr << "node" << std::endl; + } + }; + + + // + // + // + struct Declaration : Traverser + { + Declaration () + { + map (typeid (SyntaxTree::Declaration), this); + } + + virtual void + traverse (SyntaxTree::Node*) + { + std::cerr << "declaration" << std::endl; + } + }; + + // + // + // + struct Scope : Traverser + { + Scope () + { + map (typeid (SyntaxTree::Scope), this); + } + + virtual void + traverse (SyntaxTree::Node* n) + { + std::cerr << "scope" << std::endl; + + SyntaxTree::Scope* s = dynamic_cast<SyntaxTree::Scope*> (n); + + for (SyntaxTree::DeclarationList::iterator i = s->content_.begin (); + i != s->content_.end (); + i++) + { + dispatch (*i); + } + } + }; + + // + // + // + struct InterfaceDecl : Traverser + { + InterfaceDecl () + { + map (typeid (SyntaxTree::InterfaceDecl), this); + } + + virtual void + traverse (SyntaxTree::Node*) + { + std::cerr << "interface declaration" << std::endl; + } + }; + + // + // + // + struct InterfaceDef : Traverser + { + InterfaceDef () + { + map (typeid (SyntaxTree::InterfaceDef), this); + } + + virtual void + traverse (SyntaxTree::Node*) + { + std::cerr << "interface definition" << std::endl; + } + }; +} + +#endif // TRAVERSAL_HPP +//$Id$ diff --git a/ACE/contrib/utility/Example/Introspection/Traversal/driver.cpp b/ACE/contrib/utility/Example/Introspection/Traversal/driver.cpp new file mode 100644 index 00000000000..35891ef58cc --- /dev/null +++ b/ACE/contrib/utility/Example/Introspection/Traversal/driver.cpp @@ -0,0 +1,91 @@ +// file : Example/Introspection/Traversal/driver.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#include <iostream> + +#include "SyntaxTree.hpp" +#include "Traversal.hpp" + +int +main () +{ + using namespace SyntaxTree; + + /* + Create a syntax tree that looks something like this: + + scope + { + interface declaration; + + scope + { + interface definition + { + decalartion; + }; + }; + }; + + */ + + Scope s1; + + InterfaceDecl i1; + s1.content_.push_back (&i1); + + Scope s2; + s1.content_.push_back (&s2); + + InterfaceDef i2; + s2.content_.push_back (&i2); + + Declaration d1; + i2.content_.push_back (&d1); + + SyntaxTree::Node* root = &s1; + + // Now different ways of traversing this tree: + + { + std::cout << "test #1" << std::endl; + + struct Generator : Traversal::Declaration, Traversal::Scope + { + }; + + Generator g; + g.dispatch (root); + + std::cout << std::endl; + } + + { + std::cout << "test #2" << std::endl; + + struct Generator : Traversal::Scope, Traversal::InterfaceDecl + { + }; + + Generator g; + g.dispatch (root); + + std::cout << std::endl; + } + + { + std::cout << "test #3" << std::endl; + + struct Generator : Traversal::Scope, Traversal::InterfaceDef + { + }; + + Generator g; + g.dispatch (root); + + std::cout << std::endl; + } +} +//$Id$ |