summaryrefslogtreecommitdiff
path: root/ACE/contrib/utility/Example/Introspection/InheritanceTree/inheritance_tree.cpp
blob: 97c4c68dda6e04abba7f3be23f6bf98c044f06b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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$