ctti.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ArmarXCore
17  * @author Christoph Pohl ( christoph dot pohl at kit dot edu )
18  * @date 24.02.22
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 // from https://bitwizeshift.github.io/posts/2021/03/09/getting-an-unmangled-type-name-at-compile-time/
24 #pragma once
25 
26 #include <string>
27 #include <string_view>
28 #include <array> // std::array
29 #include <utility> // std::index_sequence
30 
31 namespace armarx::meta
32 {
33 
34  template <std::size_t...Idxs>
35  constexpr auto substring_as_array(std::string_view str, std::index_sequence<Idxs...>)
36  {
37  return std::array{str[Idxs]..., '\n'};
38  }
39 
40  template <typename T>
41  constexpr auto type_name_array()
42  {
43  #if defined(__clang__)
44  constexpr auto prefix = std::string_view{"[T = "};
45  constexpr auto suffix = std::string_view{"]"};
46  constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
47  #elif defined(__GNUC__)
48  constexpr auto prefix = std::string_view{"with T = "};
49  constexpr auto suffix = std::string_view{"]"};
50  constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
51  #elif defined(_MSC_VER)
52  constexpr auto prefix = std::string_view{"type_name_array<"};
53  constexpr auto suffix = std::string_view{">(void)"};
54  constexpr auto function = std::string_view{__FUNCSIG__};
55  #else
56  # error Unsupported compiler
57  #endif
58 
59  constexpr auto start = function.find(prefix) + prefix.size();
60  constexpr auto end = function.rfind(suffix);
61 
62  static_assert(start < end);
63 
64  constexpr auto name = function.substr(start, (end - start));
65  return substring_as_array(name, std::make_index_sequence<name.size()>{});
66  }
67 
68  template <typename T>
70  {
71  static inline constexpr auto value = type_name_array<T>();
72  };
73 
74  template <typename T>
75  constexpr auto type_name() -> std::string_view
76  {
77  constexpr auto& value = type_name_holder<T>::value;
78  return std::string_view{value.data(), value.size()};
79  }
80 
81 }
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::meta::type_name_array
constexpr auto type_name_array()
Definition: ctti.h:41
armarx::meta::type_name
constexpr auto type_name() -> std::string_view
Definition: ctti.h:75
armarx::meta
Definition: PluginCfgStruct.h:31
armarx::meta::type_name_holder::value
static constexpr auto value
Definition: ctti.h:71
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::meta::type_name_holder
Definition: ctti.h:69
armarx::meta::substring_as_array
constexpr auto substring_as_array(std::string_view str, std::index_sequence< Idxs... >)
Definition: ctti.h:35