From a81eb0f8de4b60f5090b170eea5423df57a87a6b Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sat, 23 Mar 2024 00:31:37 +0800 Subject: [PATCH] feat add PrettyName --- include/sled/reflect/reflect.h | 49 +++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/include/sled/reflect/reflect.h b/include/sled/reflect/reflect.h index 014e348..5567ec2 100644 --- a/include/sled/reflect/reflect.h +++ b/include/sled/reflect/reflect.h @@ -4,9 +4,11 @@ * @license : MIT **/ -#pragma #ifndef SLED_REFLECT_REFLECT_H #define SLED_REFLECT_REFLECT_H +#pragma +#include +#include #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::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 +inline const char * +PrettyName() +{ + static std::string pretty_name = PrettyNameImpl(); + return pretty_name.c_str(); +} +}// namespace sled + #endif// SLED_REFLECT_REFLECT_H