SeqAn3 3.4.0-rc.1
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-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 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 <functional>
17#include <memory>
18#include <string>
19#include <typeinfo>
20
22
23namespace seqan3::detail
24{
25
40template <typename type>
41inline std::string const type_name_as_string = []()
42{
43 std::string demangled_name{};
44#if defined(__GNUC__) || defined(__clang__) // clang and gcc only return a mangled name.
45 using safe_ptr_t = std::unique_ptr<char, std::function<void(char *)>>;
46
47 // https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
48 int status{};
49 safe_ptr_t demangled_name_ptr{abi::__cxa_demangle(typeid(type).name(), 0, 0, &status),
50 [](char * name_ptr)
51 {
52 free(name_ptr);
53 }};
54
55 // We exclude status != 0, because this code can't be reached normally, only if there is a defect in the compiler
56 // itself, since the type is directly given by the compiler. See https://github.com/seqan/seqan3/pull/2311.
57 // LCOV_EXCL_START
58 if (status != 0)
59 return std::string{typeid(type).name()} + " (abi::__cxa_demangle error status (" + std::to_string(status)
60 + "): "
61 + (status == -1 ? "A memory allocation failure occurred."
62 : (status == -2 ? "mangled_name is not a valid name under the C++ ABI mangling rules."
63 : (status == -3 ? "One of the arguments is invalid." : "Unknown Error")))
64 + ")";
65 // LCOV_EXCL_STOP
66
67 demangled_name = std::string{std::addressof(*demangled_name_ptr)};
68#else // e.g. MSVC
69 demangled_name = typeid(type).name();
70#endif // defined(__GNUC__) || defined(__clang__)
71
72 if constexpr (std::is_const_v<std::remove_reference_t<type>>)
73 demangled_name += " const";
74 if constexpr (std::is_lvalue_reference_v<type>)
75 demangled_name += " &";
76 if constexpr (std::is_rvalue_reference_v<type>)
77 demangled_name += " &&";
78
79 return demangled_name;
80}();
81
82} // namespace seqan3::detail
T addressof(T... args)
T free(T... args)
Provides platform and dependency checks.
T to_string(T... args)
Hide me