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 <vector>
26
27#include <boost/algorithm/string/join.hpp>
28#include <magic_enum/magic_enum.hpp>
29
30#include <deal.II/base/exceptions.h>
31
32namespace ExaDG
33{
37namespace Utilities
38{
40template<typename Type>
41constexpr bool
43{
44 return (std::is_enum_v<Type> or magic_enum::is_scoped_enum_v<Type>);
45}
46
48template<typename EnumType>
49EnumType
51{
52 return magic_enum::enum_values<EnumType>()[0];
53}
54
56template<typename EnumType>
57std::string
59{
60 auto const enum_strings = magic_enum::enum_names<EnumType>();
61
62 std::vector<std::string> const enums_strings_vec(enum_strings.begin(), enum_strings.end());
63 return boost::algorithm::join(enums_strings_vec, "|");
64}
65
67template<typename EnumType>
68std::string
69enum_to_string(EnumType const enum_type)
70{
71 return (std::string)magic_enum::enum_name(enum_type);
72}
73
75template<typename EnumType>
76void
77string_to_enum(EnumType & enum_type, std::string const & enum_name)
78{
79 auto casted_enum = magic_enum::enum_cast<EnumType>(enum_name);
80 if(casted_enum.has_value())
81 enum_type = casted_enum.value();
82 else
83 AssertThrow(false, dealii::ExcMessage("Could not convert string to enum."));
84}
85
86} // namespace Utilities
87} // namespace ExaDG
88
89#endif /* INCLUDE_EXADG_UTILITIES_ENUM_UTILITIES_H_ */
Definition interpolate.h:32
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:77
std::string serialized_string()
Returns the names of the enums joined with "|".
Definition enum_utilities.h:58
constexpr bool is_enum()
Checks if given type is an enum or enum class.
Definition enum_utilities.h:42
EnumType default_constructor()
Returns the first value of EnumType. This is well-defined as compared to EnumType().
Definition enum_utilities.h:50
std::string enum_to_string(EnumType const enum_type)
Converts and enum to a string, returning the string.
Definition enum_utilities.h:69
Definition driver.cpp:33