Skip to content

Commit f14017c

Browse files
committed
refactor GetNamespace function implementation
1 parent 9c4ff82 commit f14017c

File tree

1 file changed

+11
-52
lines changed

1 file changed

+11
-52
lines changed

modules/core/util/include/util.hpp

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -50,60 +50,19 @@ int GetNumThreads();
5050

5151
template <typename T>
5252
constexpr std::string_view GetNamespace() {
53-
#if defined(__clang__) || defined(__GNUC__)
54-
constexpr std::string_view kFunc = __PRETTY_FUNCTION__;
55-
constexpr std::string_view kKey = "T = ";
56-
57-
auto start = kFunc.find(kKey);
58-
if (start == std::string_view::npos) {
59-
return {};
60-
}
61-
start += kKey.size();
62-
63-
auto end = kFunc.find_first_of(";]> ,", start);
64-
if (end == std::string_view::npos) {
65-
return {};
66-
}
67-
68-
auto full_type = kFunc.substr(start, end - start);
69-
70-
auto ns_end = full_type.rfind("::");
71-
if (ns_end == std::string_view::npos) {
72-
return {};
73-
}
74-
75-
return full_type.substr(0, ns_end);
76-
77-
#elif defined(_MSC_VER)
78-
constexpr std::string_view kFunc = __FUNCSIG__;
79-
constexpr std::string_view kKey = "GetNamespace<";
80-
81-
auto start = kFunc.find(kKey);
82-
if (start == std::string_view::npos) return {};
83-
start += kKey.size();
84-
85-
constexpr std::string_view prefixes[] = {"class ", "struct ", "enum ", "union "};
86-
for (auto prefix : prefixes) {
87-
if (kFunc.substr(start, prefix.size()) == prefix) {
88-
start += prefix.size();
89-
break;
90-
}
91-
}
92-
93-
auto end = kFunc.find('>', start);
94-
if (end == std::string_view::npos) return {};
95-
96-
auto full_type = kFunc.substr(start, end - start);
97-
98-
auto ns_end = full_type.rfind("::");
99-
if (ns_end == std::string_view::npos) return {};
100-
101-
return full_type.substr(0, ns_end);
102-
53+
#if defined(_MSC_VER)
54+
constexpr auto func = std::string_view{__FUNCSIG__};
55+
auto start = func.find("GetNamespace<") + 13;
56+
for (auto p : {"class ", "struct ", "enum ", "union "})
57+
if (func.substr(start).starts_with(p)) start += p.size();
10358
#else
104-
static_assert([] { return false; }(), "Unsupported compiler");
105-
return {};
59+
constexpr auto func = std::string_view{__PRETTY_FUNCTION__};
60+
auto start = func.find("T = ") + 4;
10661
#endif
62+
auto end = func.find_first_of(";]> ,>", start);
63+
if (end == std::string_view::npos) return {};
64+
auto ns_end = func.rfind("::", end);
65+
return (ns_end != std::string_view::npos && ns_end > start) ? func.substr(start, ns_end - start) : std::string_view{};
10766
}
10867

10968
inline std::shared_ptr<nlohmann::json> InitJSONPtr() { return std::make_shared<nlohmann::json>(); }

0 commit comments

Comments
 (0)