CPP-AP 2.7.0
Command-line argument parser for C++20
Loading...
Searching...
No Matches
typing_utility.hpp
Go to the documentation of this file.
1// Copyright (c) 2023-2025 Jakub MusiaƂ
2// This file is part of the CPP-AP project (https://github.com/SpectraL519/cpp-ap).
3// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include <functional>
8#include <memory>
9#include <optional>
10#include <source_location>
11#include <string_view>
12
13namespace ap::detail {
14
15template <typename T>
16using uptr_opt_t = std::optional<std::reference_wrapper<std::unique_ptr<T>>>;
17
18template <typename T>
19constexpr std::string_view get_demangled_type_name() {
20#if defined(__clang__) || defined(__GNUC__)
21 constexpr std::string_view func_name = __PRETTY_FUNCTION__;
22 constexpr std::string_view begin_key = "T = ";
23 constexpr std::string_view end_key = ";]";
24
25 auto start_pos = func_name.find(begin_key) + begin_key.size();
26 auto end_pos = func_name.find_first_of(end_key, start_pos);
27 return func_name.substr(start_pos, end_pos - start_pos);
28#elif defined(_MSC_VER)
29 constexpr std::string_view func_name = __FUNCSIG__;
30 constexpr std::string_view begin_key = "type_name<";
31 constexpr char end_key = '>';
32
33 auto start_pos = func_name.find(begin_key) + begin_key.size();
34 auto end_pos = func_name.find(end_key, start_pos);
35 return func_name.substr(start_pos, end_pos - start_pos);
36#else
37 return typeid(T).name();
38#endif
39}
40
41} // namespace ap::detail
std::optional< std::reference_wrapper< std::unique_ptr< T > > > uptr_opt_t
constexpr std::string_view get_demangled_type_name()