ExaDG
Loading...
Searching...
No Matches
enum_utilities.h
1/* ______________________________________________________________________
2 *
3 * ExaDG - High-Order Discontinuous Galerkin for the Exa-Scale
4 *
5 * Copyright (C) 2021 by the ExaDG authors
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * ______________________________________________________________________
20 */
21
22#ifndef INCLUDE_EXADG_UTILITIES_ENUM_UTILITIES_H_
23#define INCLUDE_EXADG_UTILITIES_ENUM_UTILITIES_H_
24
25#include <boost/algorithm/string/join.hpp>
26#include <magic_enum/magic_enum.hpp>
27
28#include <deal.II/base/exceptions.h>
29
30namespace ExaDG
31{
35namespace Utilities
36{
38template<typename Type>
39constexpr bool
41{
42 return (std::is_enum_v<Type> or magic_enum::is_scoped_enum_v<Type>);
43}
44
46template<typename EnumType>
47EnumType
49{
50 return magic_enum::enum_values<EnumType>()[0];
51}
52
54template<typename EnumType>
55std::string
57{
58 auto const enum_strings = magic_enum::enum_names<EnumType>();
59
60 std::vector<std::string> const enums_strings_vec(enum_strings.begin(), enum_strings.end());
61 return boost::algorithm::join(enums_strings_vec, "|");
62}
63
65template<typename EnumType>
66std::string
67enum_to_string(EnumType const enum_type)
68{
69 return (std::string)magic_enum::enum_name(enum_type);
70}
71
73template<typename EnumType>
74void
75string_to_enum(EnumType & enum_type, std::string const & enum_name)
76{
77 auto casted_enum = magic_enum::enum_cast<EnumType>(enum_name);
78 if(casted_enum.has_value())
79 enum_type = casted_enum.value();
80 else
81 AssertThrow(false, dealii::ExcMessage("Could not convert string to enum."));
82}
83
84} // namespace Utilities
85} // namespace ExaDG
86
87#endif /* INCLUDE_EXADG_UTILITIES_ENUM_UTILITIES_H_ */
void string_to_enum(EnumType &enum_type, std::string const &enum_name)
Converts a string to an enum, which is provided as first function argument.
Definition enum_utilities.h:75
std::string serialized_string()
Returns the names of the enums joined with "|".
Definition enum_utilities.h:56
constexpr bool is_enum()
Checks if given type is an enum or enum class.
Definition enum_utilities.h:40
EnumType default_constructor()
Returns the first value of EnumType. This is well-defined as compared to EnumType().
Definition enum_utilities.h:48
std::string enum_to_string(EnumType const enum_type)
Converts and enum to a string, returning the string.
Definition enum_utilities.h:67
Definition driver.cpp:33