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>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
/*#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__
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#include <cxxabi.h>
|
||||
#endif
|
||||
|
||||
namespace sol {
|
||||
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) {
|
||||
int status;
|
||||
char* unmangled = abi::__cxa_demangle(id.name(), 0, 0, &status);
|
||||
std::string realname = unmangled;
|
||||
free(unmangled);
|
||||
return realname;
|
||||
if(found == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
realname.erase(found, removals[r].size());
|
||||
}
|
||||
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
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
|
||||
#error Implement demangling for this platform
|
||||
#endif // VC++ || GCC || Others
|
||||
|
||||
#error Compiler not supported for demangling
|
||||
#endif // compilers
|
||||
} // detail
|
||||
} // sol
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user