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
23
namespace
sharg::detail
24
{
25
39
template
<
typename
type>
40
inline
std::string
const
type_name_as_string
= []()
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
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 sharg::detail
std::addressof
T addressof(T... args)
std::string
std::function
functional
sharg::detail::type_name_as_string
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
std::is_same_v
T is_same_v
memory
platform.hpp
Provides platform and dependency checks.
string
std::to_string
T to_string(T... args)
typeinfo
std::unique_ptr
Hide me
Version:
sharg
detail
type_name_as_string.hpp
Generated on Mon Dec 9 2024 14:18:11 for Sharg by
1.10.0