feat add PrettyName
All checks were successful
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 1m36s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m1s

This commit is contained in:
tqcq 2024-03-23 00:31:37 +08:00
parent 406b598dbb
commit a81eb0f8de

View File

@ -4,9 +4,11 @@
* @license : MIT
**/
#pragma
#ifndef SLED_REFLECT_REFLECT_H
#define SLED_REFLECT_REFLECT_H
#pragma
#include <cxxabi.h>
#include <string>
#if !defined(__NO_META_PARSER__) && !defined(__META_PARSER__)
#define __META_PARSER__
@ -22,4 +24,49 @@
#define METHOD()
#endif
namespace sled {
namespace {
inline std::string
RemoveUnusedSpace(const std::string &str, const std::string &chars = "[]()<>*&:")
{
std::string result = str.substr(str.find_first_not_of(' '));
result = result.substr(0, result.find_last_not_of(' ') + 1);
for (size_t i = 0; i < result.size();) {
if (result[i] == ' ') {
bool left_is_chars = i > 0 && chars.find(result[i - 1]) != std::string::npos;
bool right_is_chars = (i + 1 < str.size()) && chars.find(result[i + 1]) != std::string::npos;
if (left_is_chars || right_is_chars) {
result.erase(i, 1);
continue;
}
}
++i;
}
return result;
}
template<typename T, typename U = typename std::decay<T>::type>
inline std::string
PrettyNameImpl()
{
const char *name = typeid(U).name();
int status;
char *res = abi::__cxa_demangle(name, nullptr, nullptr, &status);
std::string pretty_name = res;
free(res);
return RemoveUnusedSpace(pretty_name);
}
}// namespace
template<typename T>
inline const char *
PrettyName()
{
static std::string pretty_name = PrettyNameImpl<T>();
return pretty_name.c_str();
}
}// namespace sled
#endif// SLED_REFLECT_REFLECT_H