xref: /haiku/src/tests/misc/rtti-test.cpp (revision d4637d3200f007402130c6917aa501c9d17478f1)
1d28317fcSIngo Weinhold #include <stdio.h>
2d28317fcSIngo Weinhold 
3d28317fcSIngo Weinhold #include <string>
4d28317fcSIngo Weinhold #include <typeinfo>
5d28317fcSIngo Weinhold 
6d28317fcSIngo Weinhold #include <Directory.h>
7d28317fcSIngo Weinhold #include <File.h>
8d28317fcSIngo Weinhold 
9d28317fcSIngo Weinhold using std::string;
10d28317fcSIngo Weinhold 
11d28317fcSIngo Weinhold struct A {
AA12d28317fcSIngo Weinhold 	A()	{}
~AA13d28317fcSIngo Weinhold 	virtual ~A() {}
14d28317fcSIngo Weinhold 
15d28317fcSIngo Weinhold 	int a;
16d28317fcSIngo Weinhold };
17d28317fcSIngo Weinhold 
18d28317fcSIngo Weinhold struct B : A {
BB19d28317fcSIngo Weinhold 	B()	{}
~BB20d28317fcSIngo Weinhold 	virtual ~B() {}
21d28317fcSIngo Weinhold 
22d28317fcSIngo Weinhold 	int b;
23d28317fcSIngo Weinhold };
24d28317fcSIngo Weinhold 
25d28317fcSIngo Weinhold struct C : A {
CC26d28317fcSIngo Weinhold 	C()	{}
~CC27d28317fcSIngo Weinhold 	virtual ~C() {}
28d28317fcSIngo Weinhold 
29d28317fcSIngo Weinhold 	int c;
30d28317fcSIngo Weinhold };
31d28317fcSIngo Weinhold 
32d28317fcSIngo Weinhold template<typename A, typename B, typename C>
33d28317fcSIngo Weinhold static void
rtti_test(A * a,const char * inputClass,const char * classNameA,const char * classNameB,const char * classNameC)34d28317fcSIngo Weinhold rtti_test(A *a, const char *inputClass, const char *classNameA,
35d28317fcSIngo Weinhold 	const char *classNameB, const char *classNameC)
36d28317fcSIngo Weinhold {
37d28317fcSIngo Weinhold 	printf("class %s\n", inputClass);
38d28317fcSIngo Weinhold 	printf("  dynamic_cast<%s*>(a): %p\n", classNameA, dynamic_cast<A*>(a));
39d28317fcSIngo Weinhold 	printf("  dynamic_cast<%s*>(a): %p\n", classNameB, dynamic_cast<B*>(a));
40d28317fcSIngo Weinhold 	printf("  dynamic_cast<%s*>(a): %p\n", classNameC, dynamic_cast<C*>(a));
41d28317fcSIngo Weinhold 	const std::type_info *info = &typeid(*a);
42d28317fcSIngo Weinhold 	printf("  typeinfo: %p, name: %s\n", info, (info ? info->name() : NULL));
43d28317fcSIngo Weinhold }
44d28317fcSIngo Weinhold 
45d28317fcSIngo Weinhold int
main()46d28317fcSIngo Weinhold main()
47d28317fcSIngo Weinhold {
48d28317fcSIngo Weinhold 	// test with artificial classes defined in this file
49d28317fcSIngo Weinhold 
50d28317fcSIngo Weinhold 	#define RTTI_TEST(obj, className) rtti_test<A, B, C>(obj, className, \
51d28317fcSIngo Weinhold 		"A", "B", "C")
52d28317fcSIngo Weinhold 
53d28317fcSIngo Weinhold 	A a;
54d28317fcSIngo Weinhold 	B b;
55d28317fcSIngo Weinhold 	C c;
56d28317fcSIngo Weinhold 
57*d4637d32SIngo Weinhold 	printf("A: %p (vtable: %p)\n", &a, *(void**)&a);
58*d4637d32SIngo Weinhold 	printf("B: %p (vtable: %p)\n", &b, *(void**)&b);
59*d4637d32SIngo Weinhold 	printf("C: %p (vtable: %p)\n", &c, *(void**)&c);
60d28317fcSIngo Weinhold 
61d28317fcSIngo Weinhold 	RTTI_TEST(&a, "A");
62d28317fcSIngo Weinhold 	RTTI_TEST(&b, "B");
63d28317fcSIngo Weinhold 	RTTI_TEST(&c, "C");
64d28317fcSIngo Weinhold 
65d28317fcSIngo Weinhold 
66d28317fcSIngo Weinhold 	// test with real classes defined in a library
67d28317fcSIngo Weinhold 
68d28317fcSIngo Weinhold 	#undef RTTI_TEST
69d28317fcSIngo Weinhold 	#define RTTI_TEST(obj, className) rtti_test<BNode, BFile, BDirectory>(obj, \
70d28317fcSIngo Weinhold 		className, "BNode", "BFile", "BDirectory")
71d28317fcSIngo Weinhold 
72d28317fcSIngo Weinhold 	BNode node;
73d28317fcSIngo Weinhold 	BFile file;
74d28317fcSIngo Weinhold 	BDirectory dir;
75d28317fcSIngo Weinhold 
76*d4637d32SIngo Weinhold 	printf("BNode:      %p (vtable: %p)\n", &node, *(void**)&node);
77*d4637d32SIngo Weinhold 	printf("BFile:      %p (vtable: %p)\n", &file, *(void**)&file);
78*d4637d32SIngo Weinhold 	printf("BDirectory: %p (vtable: %p)\n", &dir, *(void**)&dir);
79d28317fcSIngo Weinhold 
80d28317fcSIngo Weinhold 	RTTI_TEST(&node, "BNode");
81d28317fcSIngo Weinhold 	RTTI_TEST(&file, "BFile");
82d28317fcSIngo Weinhold 	RTTI_TEST(&dir, "BDirectory");
83d28317fcSIngo Weinhold 
84d28317fcSIngo Weinhold 	return 0;
85d28317fcSIngo Weinhold }
86