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 <functional>
17#include <memory>
18#include <string>
19#include <typeinfo>
20
21#include <sharg/platform.hpp>
22
23namespace sharg::detail
24{
25
39template <typename type>
41{
42 std::string demangled_name{};
43#if defined(__GNUC__) || defined(__clang__) // clang and gcc only return a mangled name.
44 using safe_ptr_t = std::unique_ptr<char, std::function<void(char *)>>;
45
46 // https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
47 int status{};
48 safe_ptr_t demangled_name_ptr{abi::__cxa_demangle(typeid(type).name(), 0, 0, &status),
49 [](char * name_ptr)
50 {
51 free(name_ptr);
52 }};
53
54 // We exclude status != 0, because this code can't be reached normally, only if there is a defect in the compiler
55 // itself, since the type is directly given by the compiler. See https://github.com/seqan/seqan3/pull/2311.
56 // LCOV_EXCL_START
57 // clang-format off
58 if (status != 0)
59 return std::string{typeid(type).name()} +
60 " (abi::__cxa_demangle error status (" + std::to_string(status) + "): " +
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 // clang-format on
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
73 demangled_name += " const";
75 demangled_name += " &";
77 demangled_name += " &&";
78
79 return demangled_name;
80}();
81
82} // namespace sharg::detail
T addressof(T... args)
std::string const type_name_as_string
Defines the human-readable name of the given type using the typeid operator.
Definition type_name_as_string.hpp:40
T is_same_v
Provides platform and dependency checks.
T to_string(T... args)
Hide me