Skip to content

Commit 47f3729

Browse files
authored
fix cib::detail::name type string extraction algorithm (#66)
* fix cib::detail::name type string extraction algorithm * add test case for cib::detail::name * move inline struct definition
1 parent 1d7353a commit 47f3729

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

include/cib/detail/find.hpp

+20-31
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,27 @@ namespace cib::detail {
2323
}
2424
};
2525

26-
// clang-8 and below don't have constexpr find_first_of on std::string_view
27-
CIB_CONSTEVAL std::string_view::size_type find_first_of(std::string_view str, char c) {
28-
using size_type = std::string_view::size_type;
29-
for (size_type i = 0; i < str.size(); i++) {
30-
if (str[i] == c) {
31-
return i;
32-
}
33-
}
34-
35-
return 0;
36-
}
37-
38-
// clang-8 and below don't have constexpr find_last_of on std::string_view
39-
CIB_CONSTEVAL std::string_view::size_type find_last_of(std::string_view str, char c) {
40-
using size_type = std::string_view::size_type;
41-
size_type match = str.size();
42-
for (size_type i = 0; i < str.size(); i++) {
43-
if (str[i] == c) {
44-
match = i;
45-
}
46-
}
47-
48-
return match;
49-
}
50-
5126
template<typename Tag>
52-
CIB_CONSTEVAL static std::string_view name() {
53-
constexpr std::string_view function_name = __PRETTY_FUNCTION__;
54-
constexpr auto lhs = find_first_of(function_name, '<');
55-
constexpr auto rhs = find_last_of(function_name, '>');
56-
constexpr auto service_name = function_name.substr(lhs, rhs - lhs);
57-
return service_name;
27+
constexpr static std::string_view name() {
28+
#if defined(__clang__)
29+
constexpr std::string_view function_name = __PRETTY_FUNCTION__;
30+
constexpr auto lhs = 44;
31+
constexpr auto rhs = function_name.size() - 2;
32+
33+
#elif defined(__GNUC__) || defined(__GNUG__)
34+
constexpr std::string_view function_name = __PRETTY_FUNCTION__;
35+
constexpr auto lhs = 59;
36+
constexpr auto rhs = function_name.size() - 51;
37+
38+
#elif #elif defined(_MSC_VER)
39+
constexpr std::string_view function_name = __FUNCSIG__;
40+
constexpr auto lhs = 0;
41+
constexpr auto rhs = function_name.size();
42+
#else
43+
static_assert(false, "Unknown compiler, can't build type name.");
44+
#endif
45+
46+
return function_name.substr(lhs, rhs - lhs + 1);
5847
}
5948

6049

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_executable(tests
66
detail/type_list.cpp
77
detail/type_pack_element.cpp
88
detail/meta.cpp
9+
detail/type_to_string_view.cpp
910
builder_meta.cpp
1011
callback.cpp
1112
nexus.cpp

test/detail/type_to_string_view.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <cib/cib.hpp>
2+
3+
#include <catch2/catch_test_macros.hpp>
4+
5+
#include <string_view>
6+
7+
struct ABCDEFG;
8+
9+
TEST_CASE("ensure types are converted to full string", "[type_to_string_view]") {
10+
//using std::string_view_literals::sv;
11+
REQUIRE(cib::detail::name<ABCDEFG>() == "ABCDEFG");
12+
}

0 commit comments

Comments
 (0)