Skip to content

Commit 4375000

Browse files
committed
refactor GetNamespace to replace string_view with string and use typeid for namespace extraction
1 parent 6424cd7 commit 4375000

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

modules/core/performance/tests/perf_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ using TestTypes = ::testing::Types<my::nested::Type, my::Another, int>;
263263
TYPED_TEST_SUITE(GetNamespaceTest, TestTypes);
264264

265265
TYPED_TEST(GetNamespaceTest, ExtractsNamespaceCorrectly) {
266-
constexpr std::string_view kNs = ppc::util::GetNamespace<TypeParam>();
266+
std::string kNs = ppc::util::GetNamespace<TypeParam>();
267267

268268
if constexpr (std::is_same_v<TypeParam, my::nested::Type>) {
269269
EXPECT_EQ(kNs, "my::nested");

modules/core/util/include/util.hpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#include <memory>
66
#include <source_location>
77
#include <string>
8-
#include <string_view>
8+
#include <typeinfo>
9+
#ifdef __GNUG__
10+
#include <cxxabi.h>
11+
#endif
912

1013
#include "nlohmann/json_fwd.hpp"
1114

@@ -49,24 +52,16 @@ std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& r
4952
int GetNumThreads();
5053

5154
template <typename T>
52-
constexpr std::string_view GetNamespace() {
53-
#ifdef _MSC_VER
54-
constexpr std::string_view kFunc{__FUNCSIG__};
55-
constexpr std::string_view kKey = "GetNamespace<";
56-
#else
57-
constexpr std::string_view kFunc{__PRETTY_FUNCTION__};
58-
constexpr std::string_view kKey = "T = ";
55+
std::string GetNamespace() {
56+
std::string name = typeid(T).name();
57+
#ifdef __GNUG__
58+
int status = 0;
59+
std::unique_ptr<char, void (*)(void*)> demangled{abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status),
60+
std::free};
61+
name = (status == 0) ? demangled.get() : name;
5962
#endif
60-
61-
auto start = kFunc.find(kKey);
62-
start = (start == std::string_view::npos) ? kFunc.size() : start + kKey.size();
63-
64-
for (auto p : {"class ", "struct ", "enum ", "union "})
65-
if (kFunc.substr(start).starts_with(p)) start += std::string_view{p}.size();
66-
67-
auto ns_type = kFunc.substr(start, kFunc.find(']', start) - start);
68-
auto pos = ns_type.rfind("::");
69-
return (pos != std::string_view::npos) ? ns_type.substr(0, pos) : std::string_view{};
63+
auto pos = name.rfind("::");
64+
return (pos != std::string::npos) ? name.substr(0, pos) : std::string{};
7065
}
7166

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

modules/core/util/tests/util.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Type {};
1212
} // namespace my::nested
1313

1414
TEST(util_tests, extracts_correct_namespace) {
15-
constexpr std::string_view kNs = ppc::util::GetNamespace<my::nested::Type>();
15+
std::string kNs = ppc::util::GetNamespace<my::nested::Type>();
1616
EXPECT_EQ(kNs, "my::nested");
1717
}
1818

@@ -29,17 +29,17 @@ struct TypeInNamespace {};
2929
struct PlainType {};
3030

3131
TEST(GetNamespaceTest, ReturnsExpectedNamespace) {
32-
constexpr auto kNs = ppc::util::GetNamespace<test_ns::TypeInNamespace>();
32+
std::string kNs = ppc::util::GetNamespace<test_ns::TypeInNamespace>();
3333
EXPECT_EQ(kNs, "test_ns");
3434
}
3535

3636
TEST(GetNamespaceTest, ReturnsEmptyIfNoNamespace_PrimitiveType) {
37-
constexpr auto kNs = ppc::util::GetNamespace<int>();
37+
std::string kNs = ppc::util::GetNamespace<int>();
3838
EXPECT_EQ(kNs, "");
3939
}
4040

4141
TEST(GetNamespaceTest, ReturnsEmptyIfNoNamespace_PlainStruct) {
42-
constexpr auto kNs = ppc::util::GetNamespace<PlainType>();
42+
std::string kNs = ppc::util::GetNamespace<PlainType>();
4343
EXPECT_EQ(kNs, "");
4444
}
4545

@@ -48,22 +48,22 @@ struct Nested {};
4848
} // namespace test_ns
4949

5050
TEST(GetNamespaceTest, ReturnsNamespaceCorrectly) {
51-
constexpr auto kNs = ppc::util::GetNamespace<test_ns::Nested>();
51+
std::string kNs = ppc::util::GetNamespace<test_ns::Nested>();
5252
EXPECT_EQ(kNs, "test_ns");
5353
}
5454

5555
struct NoNamespaceType {};
5656

5757
TEST(GetNamespaceTest, NoNamespaceInType) {
58-
constexpr auto kNs = ppc::util::GetNamespace<NoNamespaceType>();
58+
std::string kNs = ppc::util::GetNamespace<NoNamespaceType>();
5959
EXPECT_EQ(kNs, "");
6060
}
6161

6262
template <typename T>
6363
struct NotATemplate {};
6464

6565
TEST(GetNamespaceTest, NoKeyInPrettyFunction) {
66-
constexpr auto kNs = ppc::util::GetNamespace<NotATemplate<void>>();
66+
std::string kNs = ppc::util::GetNamespace<NotATemplate<void>>();
6767
EXPECT_EQ(kNs, "");
6868
}
6969

@@ -72,6 +72,6 @@ struct VeryLongTypeNameWithOnlyLettersAndUnderscores {};
7272
} // namespace crazy
7373

7474
TEST(GetNamespaceTest, NoTerminatorCharactersInPrettyFunction) {
75-
constexpr auto kNs = ppc::util::GetNamespace<crazy::VeryLongTypeNameWithOnlyLettersAndUnderscores>();
75+
std::string kNs = ppc::util::GetNamespace<crazy::VeryLongTypeNameWithOnlyLettersAndUnderscores>();
7676
EXPECT_EQ(kNs, "crazy");
7777
}

0 commit comments

Comments
 (0)