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
|
/************************************************************************
*
* Copyright (C) 2009-2023 IRCAD France
* Copyright (C) 2012-2017 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include <memory>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#ifndef _WIN32
#include <cxxabi.h>
#include <cstdlib>
#endif
#include "core/demangler.hpp"
namespace sight::core
{
static const std::string COLONS = "::";
static const std::string LT = "<";
//------------------------------------------------------------------------------
demangler::demangler(const std::type_info& _t) :
m_name(_t.name())
{
}
//------------------------------------------------------------------------------
demangler::demangler(std::string _s) :
m_name(std::move(_s))
{
}
//------------------------------------------------------------------------------
demangler::~demangler()
= default;
//------------------------------------------------------------------------------
std::string demangler::get_leaf_classname() const
{
std::string demangled(this->demangle());
const std::size_t lt_pos = demangled.find(LT);
std::size_t colons_pos = demangled.rfind(COLONS, lt_pos);
colons_pos = (colons_pos == std::string::npos) ? 0 : colons_pos + COLONS.size();
return demangled.replace(0, colons_pos, "");
}
//------------------------------------------------------------------------------
std::string demangler::get_classname() const
{
std::string demangled(this->demangle());
return demangled;
}
//------------------------------------------------------------------------------
std::string demangler::demangle() const
{
const char* mangled = m_name.c_str();
#ifndef _WIN32
const auto c_demangled =
std::unique_ptr<char, decltype(std::free) *>(
abi::__cxa_demangle(mangled, nullptr, nullptr, nullptr),
std::free
);
std::string res;
if(c_demangled != nullptr)
{
res = c_demangled.get();
return res;
}
res = mangled;
return res;
#else
static std::vector<std::string> keywords;
typedef std::vector<std::string>::iterator keyword_iterator;
if(keywords.empty())
{
keywords.push_back("__cdecl");
keywords.push_back("class ");
keywords.push_back("enum ");
keywords.push_back("struct ");
keywords.push_back("union ");
}
std::string res(mangled);
for(keyword_iterator iter = keywords.begin() ; iter != keywords.end() ; ++iter)
{
while(res.find(*iter) != std::string::npos)
{
res = res.replace(res.find(*iter), iter->size(), "");
}
while(res.find(" *") != std::string::npos)
{
res = res.replace(res.find(" *"), 2, "*");
}
while(res.find(" &") != std::string::npos)
{
res = res.replace(res.find(" &"), 2, "&");
}
}
return res;
#endif
}
//------------------------------------------------------------------------------
} //namespace sight::core
|