alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
demangledName.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
8
9#include <source_location>
10#include <string>
11#include <string_view>
12
13/** This type is required to be in the global namespace to avoid invalid offsets during demangling */
17
18namespace alpaka::onHost
19{
20 /// \file
21 /// use source_location to derive the demangled type name
22 /// based on:
23 /// https://www.reddit.com/r/cpp/comments/lfi6jt/finally_a_possibly_portable_way_to_convert_types/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
24
25 template<typename T>
26 constexpr auto EmbedTypeIntoSignature()
27 {
28 return std::string_view{std::source_location::current().function_name()};
29 }
30
31 template<typename T>
32 struct Demangled
33 {
34 static constexpr auto name()
35 {
36 constexpr size_t testSignatureLength = sizeof("AlpakaDemangleReferenceType") - 1;
37 auto const DummySignature = EmbedTypeIntoSignature<AlpakaDemangleReferenceType>();
38 // count char's until the type name starts
39 auto const startPosition = DummySignature.find("AlpakaDemangleReferenceType");
40 // count char's after the type information by removing type name information and pre information
41 auto const tailLength = DummySignature.size() - startPosition - testSignatureLength;
42 auto const EmbeddingSignature = EmbedTypeIntoSignature<T>();
43 auto const typeLength = EmbeddingSignature.size() - startPosition - tailLength;
44 return EmbeddingSignature.substr(startPosition, typeLength);
45 }
46 };
47
48 template<typename T>
49 constexpr auto demangledName()
50 {
51 return std::string(Demangled<T>::name());
52 }
53
54 template<typename T>
55 constexpr auto demangledName(T const&)
56 {
57 return std::string(Demangled<T>::name());
58 }
59
60 /** Simplify the C++ signature of a function
61 *
62 * Template parameters will be left out and the alpaka namespace will be removed.
63 */
64 inline std::string simplifyFunctionSignature(std::string const& deName)
65 {
66 std::string simplified;
67 simplified.reserve(deName.size());
68
69 int templateDepth = 0;
70 // Simplify nested templates by removing template arguments, e.g., <...>
71 for(char const c : deName)
72 {
73 if(c == '<')
74 {
75 if(templateDepth++ == 0)
76 simplified += "<...>";
77 continue;
78 }
79 if(c == '>')
80 {
81 if(templateDepth > 0)
82 {
83 --templateDepth;
84 continue;
85 }
86 }
87 if(templateDepth > 0)
88 continue;
89 simplified += c;
90 }
91
92 // Remove "alpaka::" from the signatures
93 std::string withoutAlpaka;
94 withoutAlpaka.reserve(simplified.size());
95 constexpr std::string_view alpakaNamespace = "alpaka::";
96 for(size_t i = 0; i < simplified.size();)
97 {
98 if(simplified.compare(i, alpakaNamespace.size(), alpakaNamespace) == 0)
99 {
100 i += alpakaNamespace.size();
101 continue;
102 }
103 withoutAlpaka += simplified[i++];
104 }
105 simplified = std::move(withoutAlpaka);
106 return simplified;
107 }
108
109} // namespace alpaka::onHost
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
std::string simplifyFunctionSignature(std::string const &deName)
Simplify the C++ signature of a function.
constexpr auto demangledName()
constexpr auto EmbedTypeIntoSignature()
This type is required to be in the global namespace to avoid invalid offsets during demangling.
static constexpr auto name()