PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
IOEnum.hpp
Go to the documentation of this file.
1// _LICENSE_HEADER_
2//
3// Copyright (C) 2019 - 2025.
4// Terms register on the GPL-3.0 license.
5//
6// This file can be redistributed and/or modified under the license terms.
7//
8// See top level LICENSE file for more details.
9//
10// This file can be used citing references in CITATION.cff file.
11
12#ifndef __GEDIM_IOEnum_H
13#define __GEDIM_IOEnum_H
14
15#include <iostream>
16
17namespace Gedim
18{
19namespace io_enum
20{
21// ***************************************************************************
22template <auto V> constexpr std::string_view enum_name()
23{
24#if defined(_MSC_VER)
25 constexpr std::string_view f = __FUNCSIG__;
26 constexpr auto start = f.find_last_of('<') + 1;
27 constexpr auto end = f.find_last_of('>');
28#else
29 constexpr std::string_view f = __PRETTY_FUNCTION__;
30 constexpr auto start = f.find("V = ") + 4;
31 constexpr auto end = f.find_first_of(";]", start);
32#endif
33
34 auto name = f.substr(start, end - start);
35
36 if (auto p = name.find_last_of(':'); p != std::string_view::npos)
37 name.remove_prefix(p + 1);
38
39 return name;
40}
41// ***************************************************************************
42template <auto V> constexpr bool is_valid()
43{
44 constexpr auto n = enum_name<V>();
45
46 if (n.empty())
47 return false;
48
49 constexpr char c = n.front();
50
51 return c == '_' || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
52}
53// ***************************************************************************
54template <typename E> struct enum_entry
55{
57 std::string_view name;
58};
59// ***************************************************************************
60template <typename E, int Min, std::size_t... I> constexpr auto make_table(std::index_sequence<I...>)
61{
62 constexpr auto count = (0 + ... + (is_valid<static_cast<E>(Min + int(I))>() ? 1 : 0));
63
64 std::array<enum_entry<E>, count> table{};
65
66 std::size_t j = 0;
67
68 (
69 [&] {
70 constexpr auto v = static_cast<E>(Min + int(I));
71
72 if constexpr (is_valid<v>())
73 table[j++] = {v, enum_name<v>()};
74 }(),
75 ...);
76
77 return table;
78}
79// ***************************************************************************
80template <typename E, int Min = 0, int Max = 64> constexpr std::string_view enum_to_string(E value)
81{
82 constexpr auto table = make_table<E, Min>(std::make_index_sequence<Max - Min>{});
83
84 for (auto const &e : table)
85 if (e.value == value)
86 return e.name;
87
88 return "<unknown>";
89}
90// ***************************************************************************
91} // namespace io_enum
92} // namespace Gedim
93
94#endif
Eigen column vector.
Definition Eigen_Array.hpp:23
constexpr std::string_view enum_name()
Definition IOEnum.hpp:22
constexpr auto make_table(std::index_sequence< I... >)
Definition IOEnum.hpp:60
constexpr std::string_view enum_to_string(E value)
Definition IOEnum.hpp:80
constexpr bool is_valid()
Definition IOEnum.hpp:42
Definition Eigen_Array.cpp:22
Definition IOEnum.hpp:55
E value
Definition IOEnum.hpp:56
std::string_view name
Definition IOEnum.hpp:57