-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeTest.cpp
More file actions
80 lines (63 loc) · 2.16 KB
/
TypeTest.cpp
File metadata and controls
80 lines (63 loc) · 2.16 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "BaseClass.h"
#include <iostream>
class CClass : public Base::BaseClass
{
TYPESYSTEM_HEADER();
public:
int d = 0;
};
TYPESYSTEM_SOURCE(CClass, Base::BaseClass);
template <class T> class TClass : public Base::BaseClass
{
TYPESYSTEM_HEADER();
public:
T data;
};
TYPESYSTEM_SOURCE_TEMPLATE(TClass<int>, Base::BaseClass);
int main()
{
using namespace Base;
Type::init(); // init the type system
// then init each class (actually register this class type), including the BaseClass
BaseClass::init(); // this root class must be initialized
// user classes init
CClass::init();
TClass<int>::init();
BaseClass* t = new TClass<int>();
Type tt = t->getTypeId();
std::cout << tt.getName() << std::endl;
BaseClass* b = new BaseClass();
Type bt = b->getTypeId();
std::cout << bt.getName() << std::endl;
Base::BaseClass* bc = static_cast<Base::BaseClass*>(Base::Type::createInstanceByName("CClass"));
CClass* c2 = type_dynamic_cast<CClass>(bc);
const char* sType = "CClass";
Type ctt = Type::fromName(sType);
CClass* cp;
auto tmp = Base::Type::createInstanceByName("CClass");
Base::BaseClass* base = static_cast<Base::BaseClass*>(tmp);
if (base)
{
if (!base->getTypeId().isDerivedFrom(Base::BaseClass::getClassTypeId()))
{
delete base;
std::cout << "'" << sType << "' is not a Base::BaseClass type";
}
cp = static_cast<CClass*>(base);
}
// auto a = Type::createInstanceByName("AClass"); // return void*
// AClass* Ap = type_dynamic_cast<AClass>(a);
std::cout << cp->getClassTypeId().getName() << std::endl;
// delete, or shared_ptr<>
Type::destruct(); // also call destructType() of all types
//============================================================
Type::init(); // init the type system
// then init each class (actually register this class type), including the BaseClass
BaseClass::init(); // this root class must be initialized
// user classes init
CClass::init();
CClass* c = new CClass();
Type ct = c->getTypeId();
std::cout << ct.getName() << std::endl;
return 0;
}