OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
type_name_to_string.h
Go to the documentation of this file.
1 // Compile time pretty type name and enum name generator
2 //
3 // From StackOverflow https://stackoverflow.com/a/56766138
4 // Note that prefix and suffix strings may change with compiler versions
5 
6 #pragma once
7 
8 #include <string_view>
9 
10 template <typename T>
11 constexpr auto type_name() noexcept {
12  std::string_view name = "Error: unsupported compiler", prefix, suffix;
13 #ifdef __clang__
14  name = __PRETTY_FUNCTION__;
15  prefix = "auto type_name() [T = ";
16  suffix = "]";
17 #elif defined(__GNUC__)
18  name = __PRETTY_FUNCTION__;
19  prefix = "constexpr auto type_name() [with T = ";
20  suffix = "]";
21 #elif defined(_MSC_VER)
22  name = __FUNCSIG__;
23  prefix = "auto __cdecl type_name<";
24  suffix = ">(void) noexcept";
25 #endif
26  name.remove_prefix(prefix.size());
27  name.remove_suffix(suffix.size());
28  return name;
29 }
30 
31 template <auto E>
32 constexpr auto enum_name() noexcept {
33  static_assert(std::is_enum_v<decltype(E)>);
34  std::string_view name = "Error: unsupported compiler", prefix, suffix;
35 #ifdef __clang__
36  name = __PRETTY_FUNCTION__;
37  prefix = "auto enum_name() [E = ";
38  suffix = "]";
39 #elif defined(__GNUC__)
40  name = __PRETTY_FUNCTION__;
41  prefix = "constexpr auto enum_name() [with auto E = ";
42  suffix = "]";
43 #elif defined(_MSC_VER)
44  name = __FUNCSIG__;
45  prefix = "auto __cdecl enum_name<";
46  suffix = ">(void) noexcept";
47 #endif
48  name.remove_prefix(prefix.size());
49  name.remove_suffix(suffix.size());
50  if (const auto pos = name.find_last_of(":)"); pos != std::string_view::npos) {
51  name.remove_prefix(pos + 1);
52  }
53  return name;
54 }
constexpr auto enum_name() noexcept
std::string suffix(SQLTypes type)
Definition: Codegen.cpp:69
string name
Definition: setup.in.py:72
constexpr auto type_name() noexcept