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