ryujin 2.1.1 revision 1c453cc82f1d29edf537280cd96267402ac73e60
patterns_conversion.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3// Copyright (C) 2022 - 2023 by the ryujin authors
4//
5
6#pragma once
7
8#include <deal.II/base/parameter_acceptor.h>
9
10#include <numeric>
11
12#ifndef DOXYGEN
13
14DEAL_II_NAMESPACE_OPEN
15
16
17template <typename T>
18struct ConversionHelper : std::false_type {
19};
20
21
28template <typename T>
29struct Patterns::Tools::
30 Convert<T, typename std::enable_if_t<ConversionHelper<T>::value>> {
31
32 static std::unique_ptr<Patterns::PatternBase> to_pattern()
33 {
34 static const auto conversion_table = ConversionHelper<T>().conversion_table;
35 std::string s;
36 for (const auto &it : conversion_table)
37 s = s.empty() ? std::get<1>(it) : s + "|" + std::get<1>(it);
38 return std::make_unique<Patterns::Selection>(s);
39 }
40
41 static std::string
42 to_string(const T &t,
43 const Patterns::PatternBase & = *Convert<T>::to_pattern())
44 {
45 static const auto conversion_table = ConversionHelper<T>().conversion_table;
46 for (const auto &it : conversion_table) {
47 if (std::get<0>(it) == t)
48 return std::get<1>(it);
49 }
50
51 AssertThrow(false,
52 dealii::ExcMessage("Incomplete conversion table - unable to "
53 "identify matching string"));
54 }
55
56 static T
57 to_value(const std::string &s,
58 const Patterns::PatternBase &pattern = *Convert<T>::to_pattern())
59 {
60 AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description()));
61
62 static const auto conversion_table = ConversionHelper<T>().conversion_table;
63 for (const auto &it : conversion_table) {
64 if (std::get<1>(it) == s)
65 return std::get<0>(it);
66 }
67
68 AssertThrow(false,
69 dealii::ExcMessage("Incomplete conversion table - unable to "
70 "identify matching value"));
71 }
72};
73
74DEAL_II_NAMESPACE_CLOSE
75
76#define LIST(...) __VA_ARGS__
77
84#define DECLARE_ENUM(type, s) \
85 namespace dealii \
86 { \
87 template <> \
88 struct ConversionHelper<type> : std::true_type { \
89 const std::vector<std::tuple<type, std::string>> conversion_table = {s}; \
90 }; \
91 } \
92 static_assert(true, "")
93
94#endif