mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Refactor demangle.hpp to look a bit nicer
This commit is contained in:
parent
23ed27df89
commit
44eab5a164
|
@ -24,57 +24,40 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
/*#include <windows.h>
|
|
||||||
#include <DbgHelp.h>
|
|
||||||
*/
|
|
||||||
namespace sol {
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
|
|
||||||
std::string demangle(const std::type_info& id) {
|
|
||||||
/*std::string realname(2048, '\0');
|
|
||||||
DWORD result = UnDecorateSymbolName(id.raw_name(), &realname[0],
|
|
||||||
static_cast<DWORD>(realname.size()), UNDNAME_NAME_ONLY | UNDNAME_32_BIT_DECODE);
|
|
||||||
if (result == 0)
|
|
||||||
return "";
|
|
||||||
realname.resize(result);
|
|
||||||
*/
|
|
||||||
const static std::string removals[ 2 ] = {
|
|
||||||
"struct ",
|
|
||||||
"class "
|
|
||||||
};
|
|
||||||
std::string realname = id.name();
|
|
||||||
for (std::size_t r = 0; r < 2; ++r) {
|
|
||||||
auto found = realname.find(removals[ r ]);
|
|
||||||
if (found == std::string::npos)
|
|
||||||
continue;
|
|
||||||
realname.erase(found, removals[r].size());
|
|
||||||
}
|
|
||||||
return realname;
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif __GNUC__ || __clang__
|
|
||||||
#include <cxxabi.h>
|
#include <cxxabi.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
std::string demangle(const std::type_info& id) {
|
||||||
|
const static std::string removals[2] = { "struct ", "class " };
|
||||||
|
std::string realname = id.name();
|
||||||
|
for(std::size_t r = 0; r < 2; ++r) {
|
||||||
|
auto found = realname.find(removals[r]);
|
||||||
|
|
||||||
std::string demangle(const std::type_info& id) {
|
if(found == std::string::npos) {
|
||||||
int status;
|
continue;
|
||||||
char* unmangled = abi::__cxa_demangle(id.name(), 0, 0, &status);
|
}
|
||||||
std::string realname = unmangled;
|
|
||||||
free(unmangled);
|
realname.erase(found, removals[r].size());
|
||||||
return realname;
|
|
||||||
}
|
}
|
||||||
|
return realname;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(__GNUC__) || defined(__clang__)
|
||||||
|
std::string demangle(const std::type_info& id) {
|
||||||
|
int status;
|
||||||
|
char* unmangled = abi::__cxa_demangle(id.name(), 0, 0, &status);
|
||||||
|
std::string realname = unmangled;
|
||||||
|
free(unmangled);
|
||||||
|
return realname;
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
namespace sol {
|
#error Compiler not supported for demangling
|
||||||
namespace detail {
|
#endif // compilers
|
||||||
|
|
||||||
#error Implement demangling for this platform
|
|
||||||
#endif // VC++ || GCC || Others
|
|
||||||
|
|
||||||
} // detail
|
} // detail
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user