Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
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 <cstdlib>
17#include <functional>
18#include <memory>
19#include <string>
20#include <typeinfo>
21
22#include <sharg/platform.hpp>
23
24namespace sharg::detail
25{
26
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 {
60 demangled_name =
61 std::string{typeid(type).name()} + " (abi::__cxa_demangle error status (" + std::to_string(status) + "): "
62 + (status == -1 ? "A memory allocation failure occurred."
63 : (status == -2 ? "mangled_name is not a valid name under the C++ ABI mangling rules."
64 : (status == -3 ? "One of the arguments is invalid." : "Unknown Error")))
65 + ")";
66 return demangled_name;
67 }
68 // LCOV_EXCL_STOP
69
70 demangled_name = std::string{std::addressof(*demangled_name_ptr)};
71#else // e.g. MSVC
72 demangled_name = typeid(type).name();
73#endif // defined(__GNUC__) || defined(__clang__)
74
76 demangled_name += " const";
78 demangled_name += " &";
80 demangled_name += " &&";
81
82 return demangled_name;
83}();
84
85} // namespace sharg::detail
T addressof(T... args)
T free(T... args)
T is_same_v
Provides platform and dependency checks.
T to_string(T... args)
Hide me