-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewCast.h
172 lines (113 loc) · 6.24 KB
/
NewCast.h
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright Benjamin Southall 2018.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef NEWCAST_H
#define NEWCAST_H
#include <typeinfo>
#include <string>
#include <list>
#include "GlobalFunctions.h"
const std::type_info& typeOf(const void* sourcePtr);
std::string demangle(const char *symbol);
std::pair<const std::type_info*, const void*> polymorphicProperties(const void *sourcePtr);
extern int fuzzy;
struct OverloadsLogger{
static void addOverload(std::string overloadName, bool ignoreNext = false){
if (!fIgnoreNext)
fStrings.push_back(overloadName);
fIgnoreNext = ignoreNext;
}
static const std::list<std::string>& get(){
return fStrings;
}
static void clear(){
fStrings.clear();
}
private:
static bool fIgnoreNext;
static std::list<std::string> fStrings;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////// Const Pointers
//Use defines not typedefs so as not to confuse autocompletion in IDEs
#define cvp const void*
#define ctir const std::type_info&
#define csp const source*
#define cdp const dest*
// Basic interface. The first version is suitable only for polymorphic types, the second could be supported by non-polymorhpic classes and built-in types, unions etc.
// The first version requires compiler support to be able to extract the runtime type from the void pointer. In practice, this usually means that the vptr is a fixed
// offset from the offset into the object where the pointer points according to the ABI.
cvp new_cast( ctir source_type, cvp source_ptr, ctir destination_type ); //C-Bas
cvp new_cast( cvp source_ptr, ctir destination_type ); //C-Bas-R
// Templates to reduce typing.
template<class source> inline cvp new_cast( csp source_ptr, ctir destination_type ); //C-Src
template<class dest> inline cdp new_cast( ctir source_type, cvp source_ptr ); //C-Dst
template<class dest> inline cdp new_cast( cvp source_ptr ); //C-Dst-R
template<class dest, class source> inline cdp new_cast( csp source_ptr ); //C-SrcDst
template<class source> inline cvp new_cast(csp source_ptr, ctir destination_type) //C-Src
{
DEBUG_IF(2, OverloadsLogger::addOverload("C-Src", true);)
return new_cast(typeid(source), source_ptr, destination_type);
}
template<class dest> inline cdp new_cast(ctir source_type, cvp source_ptr)//C-Dst
{
DEBUG_IF(2, OverloadsLogger::addOverload("C-Dst", true);)
return reinterpret_cast<cdp>(new_cast(source_type, source_ptr, typeid(dest)));
}
template<class dest> inline cdp new_cast(cvp source_ptr)//C-Dst-R
{
DEBUG_IF(2, OverloadsLogger::addOverload("C-Dst-R", true);)
return reinterpret_cast<cdp>(new_cast(source_ptr, typeid(dest)));
}
template<class dest, class source> inline cdp new_cast(csp source_ptr) //C-SrcDst
{
DEBUG_IF(2, OverloadsLogger::addOverload("C-SrcDst", true);)
return reinterpret_cast<cdp>(new_cast(typeid(source), source_ptr, typeid(dest)));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////// Non-const Pointers
//Use defines not typedefs so as not to confuse autocompletion in IDEs
#define nvp void*
#define nsp source*
#define ndp dest*
// Basic interface. The first version is suitable only for polymorphic types, the second could be supported by non-polymorhpic classes and built-in types, unions etc.
// The first version requires compiler support to be able to extract the runtime type from the void pointer. In practice, this usually means that the vptr is a fixed
// offset from the offset into the object where the pointer points according to the ABI.
nvp new_cast( ctir source_type, nvp source_ptr, ctir destination_type ); //N-Bas
nvp new_cast( nvp source_ptr, ctir destination_type ); //N-Bas-R
// Templates to reduce typing.
template<class source> inline nvp new_cast( nsp source_ptr, ctir destination_type ); //N-Src
template<class dest> inline ndp new_cast( ctir source_type, nvp source_ptr ); //N-Dst
template<class dest> inline ndp new_cast( nvp source_ptr ); //N-Dst-R
template<class dest, class source> inline ndp new_cast( nsp source_ptr ); //N-SrcDst
inline nvp new_cast(ctir source_type, nvp source_ptr, ctir destination_type) //N-Bas
{
DEBUG_IF(2, OverloadsLogger::addOverload("N-Bas", true);)
return const_cast<nvp>(new_cast(source_type, const_cast<cvp>(source_ptr), destination_type));
}
inline nvp new_cast(nvp source_ptr, ctir destination_type) //N-Bas-R
{
DEBUG_IF(2, OverloadsLogger::addOverload("N-Bas-R", true);)
return const_cast<nvp>(new_cast(const_cast<cvp>(source_ptr), destination_type));
}
//Templtes
template<class source> inline nvp new_cast(nsp source_ptr, ctir destination_type) //N-Src
{
DEBUG_IF(2, OverloadsLogger::addOverload("N-Src", true);)
return new_cast(typeid(source), source_ptr, destination_type);
}
template<class dest> inline ndp new_cast(ctir source_type, nvp source_ptr)//N-Dst
{
DEBUG_IF(2, OverloadsLogger::addOverload("N-Dst", true);)
return reinterpret_cast<ndp>(new_cast(source_type, source_ptr, typeid(dest)));
}
template<class dest> inline ndp new_cast(nvp source_ptr)//N-Dst-R
{
DEBUG_IF(2, OverloadsLogger::addOverload("N-Dst-R", true);)
return reinterpret_cast<ndp>(new_cast(source_ptr, typeid(dest)));
}
template<class dest, class source> inline ndp new_cast(nsp source_ptr) //N-SrcDst
{
DEBUG_IF(2, OverloadsLogger::addOverload("N-SrcDst", true);)
return reinterpret_cast<ndp>(new_cast(typeid(source), source_ptr, typeid(dest)));
}
#endif // NEWCAST_H