SeqAn3 3.4.0
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
type_name_as_string.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#if defined(__GNUC__) || defined(__clang__)
13# include <cxxabi.h>
14#endif // defined(__GNUC__) || defined(__clang__)
15
16#include <cstdlib>
17#include <functional>
18#include <memory>
19#include <string>
20#include <typeinfo>
21
23
24namespace seqan3::detail
25{
26
41template <typename type>
42inline std::string const type_name_as_string = []()
43{
44 std::string demangled_name{};
45#if defined(__GNUC__) || defined(__clang__) // clang and gcc only return a mangled name.
46 using safe_ptr_t = std::unique_ptr<char, std::function<void(char *)>>;
47
48 // https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
49 int status{};
50 safe_ptr_t demangled_name_ptr{abi::__cxa_demangle(typeid(type).name(), 0, 0, &status),
51 [](char * name_ptr)
52 {
53 free(name_ptr);
54 }};
55
56 // We exclude status != 0, because this code can't be reached normally, only if there is a defect in the compiler
57 // itself, since the type is directly given by the compiler. See https://github.com/seqan/seqan3/pull/2311.
58 // LCOV_EXCL_START
59 if (status != 0)
60 {
61 demangled_name =
62 std::string{typeid(type).name()} + " (abi::__cxa_demangle error status (" + std::to_string(status) + "): "
63 + (status == -1 ? "A memory allocation failure occurred."
64 : (status == -2 ? "mangled_name is not a valid name under the C++ ABI mangling rules."
65 : (status == -3 ? "One of the arguments is invalid." : "Unknown Error")))
66 + ")";
67 return demangled_name;
68 }
69 // LCOV_EXCL_STOP
70
71 demangled_name = std::string{std::addressof(*demangled_name_ptr)};
72#else // e.g. MSVC
73 demangled_name = typeid(type).name();
74#endif // defined(__GNUC__) || defined(__clang__)
75
76 if constexpr (std::is_const_v<std::remove_reference_t<type>>)
77 demangled_name += " const";
78 if constexpr (std::is_lvalue_reference_v<type>)
79 demangled_name += " &";
80 if constexpr (std::is_rvalue_reference_v<type>)
81 demangled_name += " &&";
82
83 return demangled_name;
84}();
85
86} // namespace seqan3::detail
T addressof(T... args)
T free(T... args)
Provides platform and dependency checks.
T to_string(T... args)
Hide me