ryujin 2.1.1 revision 2062b43aae59b7ee510dec4ae666e29d56c4f0be
patterns_conversion.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: MIT
3// Copyright (C) 2020 - 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
22template <typename T>
23struct Patterns::Tools::
24 Convert<T, typename std::enable_if_t<ConversionHelper<T>::value>> {
25
26 static std::unique_ptr<Patterns::PatternBase> to_pattern()
27 {
28 static const auto conversion_table = ConversionHelper<T>().conversion_table;
29 std::string s;
30 for (const auto &it : conversion_table)
31 s = s.empty() ? std::get<1>(it) : s + "|" + std::get<1>(it);
32 return std::make_unique<Patterns::Selection>(s);
33 }
34
35 static std::string
36 to_string(const T &t,
37 const Patterns::PatternBase & = *Convert<T>::to_pattern())
38 {
39 static const auto conversion_table = ConversionHelper<T>().conversion_table;
40 for (const auto &it : conversion_table) {
41 if (std::get<0>(it) == t)
42 return std::get<1>(it);
43 }
44
45 AssertThrow(false,
46 dealii::ExcMessage("Incomplete conversion table - unable to "
47 "identify matching string"));
48 }
49
50 static T
51 to_value(const std::string &s,
52 const Patterns::PatternBase &pattern = *Convert<T>::to_pattern())
53 {
54 AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description()));
55
56 static const auto conversion_table = ConversionHelper<T>().conversion_table;
57 for (const auto &it : conversion_table) {
58 if (std::get<1>(it) == s)
59 return std::get<0>(it);
60 }
61
62 AssertThrow(false,
63 dealii::ExcMessage("Incomplete conversion table - unable to "
64 "identify matching value"));
65 }
66};
67
68DEAL_II_NAMESPACE_CLOSE
69
70#define LIST(...) __VA_ARGS__
71
72#define DECLARE_ENUM(type, s) \
73 namespace dealii \
74 { \
75 template <> \
76 struct ConversionHelper<type> : std::true_type { \
77 const std::vector<std::tuple<type, std::string>> conversion_table = {s}; \
78 }; \
79 } \
80 static_assert(true, "")
81
82#endif