Loading [MathJax]/extensions/tex2jax.js
Fluid structure interaction suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
enum.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2022 by Luca Heltai
4 //
5 // This file is part of the FSI-suite platform, based on the deal.II library.
6 //
7 // The FSI-suite platform is free software; you can use it, redistribute it,
8 // and/or modify it under the terms of the GNU Lesser General Public License as
9 // published by the Free Software Foundation; either version 3.0 of the License,
10 // or (at your option) any later version. The full text of the license can be
11 // found in the file LICENSE at the top level of the FSI-suite platform
12 // distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef parsed_tools_enum_h
17 #define parsed_tools_enum_h
18 
19 #include <deal.II/base/patterns.h>
20 
22 
23 #include "parsed_tools/magic_enum.hpp"
24 
25 namespace dealii
26 {
27  namespace Patterns
28  {
29  namespace Tools
30  {
34  template <class T>
35  struct Convert<T, typename std::enable_if<std::is_enum<T>::value>::type>
36  {
37  static std::unique_ptr<Patterns::PatternBase>
39  {
40  const auto n = magic_enum::enum_names<T>();
41  std::vector<std::string> names = {n.begin(), n.end()};
42  const auto selection =
43  Patterns::Tools::Convert<decltype(names)>::to_string(
44  names,
46  Patterns::Anything(), names.size(), names.size(), "|"));
47  // Allow parsing a list of enums, and make bitwise or between them
48  return Patterns::List(Patterns::Selection(selection),
49  0,
50  names.size(),
51  "|")
52  .clone();
53  }
54 
55  static std::string
56  to_string(const T &value,
57  const Patterns::PatternBase &p = *Convert<T>::to_pattern())
58  {
59  namespace B = magic_enum::bitwise_operators;
60  const auto values = magic_enum::enum_values<T>();
61  std::vector<std::string> names;
62  for (const auto &v : values)
63  if (B::operator&(value, v) == v)
64  names.push_back(std::string(magic_enum::enum_name(v)));
65  return Patterns::Tools::Convert<decltype(names)>::to_string(names, p);
66  }
67 
68  static T
69  to_value(const std::string &s,
70  const dealii::Patterns::PatternBase &p = *to_pattern())
71  {
72  namespace B = magic_enum::bitwise_operators;
73  // Make sure we have a valid enum value, or empty value
74  AssertThrow(p.match(s), ExcNoMatch(s, p.description()));
75  T value = T();
76  std::vector<std::string> value_names;
77  value_names =
78  Patterns::Tools::Convert<decltype(value_names)>::to_value(s, p);
79  for (const auto &name : value_names)
80  {
81  auto v = magic_enum::enum_cast<T>(name);
82  if (v.has_value())
83  value = B::operator|(value, v.value());
84  }
85  return value;
86  }
87  };
88  } // namespace Tools
89  } // namespace Patterns
90 } // namespace dealii
91 #endif
virtual std::unique_ptr< PatternBase > clone() const override
IteratorRange< FilteredIterator< BaseIterator > > operator|(IteratorRange< BaseIterator > i, const Predicate &p)
static ::ExceptionBase & ExcNoMatch(std::string arg1, std::string arg2)
#define AssertThrow(cond, exc)
Type type(const std::string &component_name, const std::string &selected_component)
Return the component type for the given selected component.
Definition: components.cc:186
STL namespace.
static std::string to_string(const T &value, const Patterns::PatternBase &p= *Convert< T >::to_pattern())
Definition: enum.h:56
static T to_value(const std::string &s, const Patterns::PatternBase &p= *to_pattern())
Definition: enum.h:69