16#ifndef dealii_patterns_h
17#define dealii_patterns_h
31#include <boost/archive/basic_archive.hpp>
32#include <boost/core/demangle.hpp>
33#include <boost/property_tree/ptree_fwd.hpp>
34#include <boost/property_tree/ptree_serialization.hpp>
35#include <boost/serialization/split_member.hpp>
48#include <unordered_map>
49#include <unordered_set>
91 match(
const std::string &test_string)
const = 0;
133 virtual std::unique_ptr<PatternBase>
158 std::unique_ptr<PatternBase>
226 match(
const std::string &test_string)
const override;
241 virtual std::unique_ptr<PatternBase>
242 clone()
const override;
249 static std::unique_ptr<Integer>
322 match(
const std::string &test_string)
const override;
337 virtual std::unique_ptr<PatternBase>
338 clone()
const override;
347 static std::unique_ptr<Double>
396 match(
const std::string &test_string)
const override;
411 virtual std::unique_ptr<PatternBase>
412 clone()
const override;
426 static std::unique_ptr<Selection>
496 match(
const std::string &test_string)
const override;
510 virtual std::unique_ptr<PatternBase>
511 clone()
const override;
518 static std::unique_ptr<List>
539 <<
"The values " << arg1 <<
" and " << arg2
540 <<
" do not form a valid range.");
619 match(
const std::string &test_string)
const override;
633 virtual std::unique_ptr<PatternBase>
634 clone()
const override;
641 static std::unique_ptr<Map>
686 <<
"The values " << arg1 <<
" and " << arg2
687 <<
" do not form a valid range.");
782 Tuple(
const std::vector<std::unique_ptr<PatternBase>> &
patterns,
790 Tuple(
const std::vector<std::unique_ptr<PatternBase>> &
patterns,
801 template <
class... PatternTypes>
811 template <
class... PatternTypes>
832 match(
const std::string &test_string)
const override;
846 virtual std::unique_ptr<PatternBase>
847 clone()
const override;
854 static std::unique_ptr<Tuple>
880 std::vector<std::unique_ptr<PatternBase>>
patterns;
917 match(
const std::string &test_string)
const override;
932 virtual std::unique_ptr<PatternBase>
933 clone()
const override;
940 static std::unique_ptr<MultipleSelection>
961 <<
"A comma was found at position " << arg1
962 <<
" of your input string, but commas are not allowed here.");
1001 virtual std::unique_ptr<PatternBase>
1002 clone()
const override;
1009 static std::unique_ptr<Bool>
1036 match(
const std::string &test_string)
const override;
1050 virtual std::unique_ptr<PatternBase>
1051 clone()
const override;
1058 static std::unique_ptr<Anything>
1113 match(
const std::string &test_string)
const override;
1127 virtual std::unique_ptr<PatternBase>
1128 clone()
const override;
1140 static std::unique_ptr<FileName>
1176 match(
const std::string &test_string)
const override;
1190 virtual std::unique_ptr<PatternBase>
1191 clone()
const override;
1198 static std::unique_ptr<DirectoryName>
1298 template <
class T,
class Enable =
void>
1310 static std::unique_ptr<Patterns::PatternBase>
1361 template <
typename T>
1390 template <
typename T>
1392 to_value(
const std::string &s, T &t);
1405 <<
"The string \"" << arg1
1406 <<
"\" does not match the pattern \"" << arg2 <<
"\"");
1415 template <
class... PatternTypes>
1418 Tuple(
std::string(separator), ps...)
1423 template <
class... PatternTypes>
1425 : separator(separator)
1428 "Not all of the input arguments of this function "
1429 "are derived from PatternBase");
1430 static_assert(
sizeof...(ps) > 0,
1431 "The number of PatternTypes must be greater than zero!");
1432 const auto pattern_pointers = {(
static_cast<const PatternBase *
>(&ps))...};
1433 for (
const auto p : pattern_pointers)
1439 template <
class... PatternTypes>
1475 template <
class T,
class Enable =
void>
1486 typename
std::enable_if<std::is_arithmetic<T>::value>::type>
1488 template <
typename Dummy = T>
1490 typename std::enable_if<std::is_same<Dummy, T>::value &&
1491 std::is_same<T, bool>::value,
1492 std::unique_ptr<Patterns::PatternBase>>::type
1495 return std::make_unique<Patterns::Bool>();
1498 template <
typename Dummy = T>
1500 typename std::enable_if<std::is_same<Dummy, T>::value &&
1501 !std::is_same<T, bool>::value &&
1502 std::is_integral<T>::value,
1503 std::unique_ptr<Patterns::PatternBase>>::type
1506 return std::make_unique<Patterns::Integer>(
1507 std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
1510 template <
typename Dummy = T>
1512 typename std::enable_if<std::is_same<Dummy, T>::value &&
1513 !std::is_same<T, bool>::value &&
1514 std::is_floating_point<T>::value,
1515 std::unique_ptr<Patterns::PatternBase>>::type
1518 return std::make_unique<Patterns::Double>(
1519 std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
1526 std::stringstream str;
1527 if (std::is_same<T, unsigned char>::value ||
1528 std::is_same<T, signed char>::value || std::is_same<T, char>::value)
1529 str << static_cast<int>(value);
1530 else if (std::is_same<T, bool>::value)
1531 str << (static_cast<bool>(value) ?
"true" :
"false");
1544 if (std::is_same<T, bool>::value)
1545 value = (s ==
"true");
1548 std::istringstream is(s);
1549 if (std::is_same<T, unsigned char>::value ||
1550 std::is_same<T, signed char>::value ||
1551 std::is_same<T, char>::value)
1569 ExcMessage(
"Failed to convert from \"" + s +
"\" to the type \"" +
1570 boost::core::demangle(
typeid(T).name()) +
"\""));
1579 {
",",
";",
"|",
"%"}};
1581 {
":",
"=",
"@",
"#"}};
1584 template <
typename T>
1587 template <
typename... Args>
1590 template <
typename... Args>
1593 template <
typename... Args>
1596 template <
typename... Args>
1599 template <
typename... Args>
1602 template <
typename... Args>
1605 template <
typename... Args>
1610 template <
typename T>
1613 template <
class Key,
class T,
class Compare,
class Allocator>
1617 template <
class Key,
class T,
class Compare,
class Allocator>
1621 template <
class Key,
class T,
class Hash,
class KeyEqual,
class Allocator>
1623 std::unordered_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type
1625 template <
class Key,
class T,
class Hash,
class KeyEqual,
class Allocator>
1627 std::unordered_multimap<Key, T, Hash, KeyEqual, Allocator>>
1634 template <
typename T>
1641 template <
typename T>
1658 template <
class T1,
class T2,
class... Types>
1673 template <
class T1,
class T2,
class... Types>
1684 typename
std::enable_if<is_list_compatible<T>::value>::type>
1696 typename
std::enable_if<is_map_compatible<T>::value>::type>
1699 max_list_rank<typename T::key_type, typename T::mapped_type>() + 1;
1701 max_map_rank<typename T::key_type, typename T::mapped_type>() + 1;
1705 template <
int rank,
int dim,
class Number>
1712 template <
int dim,
class Number>
1717 template <
class Number>
1741 template <
class Key,
class Value>
1751 template <
class... Types>
1759 template <
class T, std::
size_t N>
1770 typename
std::enable_if<is_list_compatible<T>::value>::type>
1772 static std::unique_ptr<Patterns::PatternBase>
1776 "Cannot use this class for non List-compatible types.");
1777 return std::make_unique<Patterns::List>(
1780 std::numeric_limits<unsigned int>::max(),
1792 ExcMessage(
"I need a List pattern to convert a "
1793 "string to a List type."));
1794 auto base_p = p->get_base_pattern().clone();
1795 std::vector<std::string> vec(t.size());
1798 t.cbegin(), t.cend(), vec.begin(), [&base_p](
const auto &entry) {
1799 return Convert<typename T::value_type>::to_string(entry, *base_p);
1805 for (
unsigned int i = 1; i < vec.size(); ++i)
1806 s += p->get_separator() +
" " + vec[i];
1820 ExcMessage(
"I need a List pattern to convert a string "
1821 "to a List type."));
1823 auto base_p = p->get_base_pattern().clone();
1827 for (
const auto &str : v)
1838 typename
std::enable_if<is_map_compatible<T>::value>::type>
1840 static std::unique_ptr<Patterns::PatternBase>
1844 "Cannot use this class for non List-compatible types.");
1846 "Cannot use this class for non Map-compatible types.");
1847 return std::make_unique<Patterns::Map>(
1851 std::numeric_limits<unsigned int>::max(),
1864 ExcMessage(
"I need a Map pattern to convert a string to "
1865 "a Map compatible type."));
1866 auto key_p = p->get_key_pattern().clone();
1867 auto val_p = p->get_value_pattern().clone();
1868 std::vector<std::string> vec(t.size());
1871 for (
const auto &ti : t)
1874 p->get_key_value_separator() +
1880 for (
unsigned int i = 1; i < vec.size(); ++i)
1881 s += p->get_separator() +
" " + vec[i];
1895 ExcMessage(
"I need a Map pattern to convert a "
1896 "string to a Map compatible type."));
1898 auto key_p = p->get_key_pattern().clone();
1899 auto val_p = p->get_value_pattern().clone();
1903 for (
const auto &str : v)
1908 t.insert(std::make_pair(
1918 template <
class ValueType, std::
size_t N>
1921 using T = std::array<ValueType, N>;
1923 static std::unique_ptr<Patterns::PatternBase>
1927 "Cannot use this class for non List-compatible types.");
1928 return std::make_unique<Patterns::List>(
1943 ExcMessage(
"I need a List pattern to convert a "
1944 "string to a std::array."));
1945 auto base_p = p->get_base_pattern().clone();
1946 std::vector<std::string> vec(t.size());
1949 t.cbegin(), t.cend(), vec.begin(), [&base_p](
const auto &entry) {
1950 return Convert<typename T::value_type>::to_string(entry, *base_p);
1956 for (
unsigned int i = 1; i < vec.size(); ++i)
1957 s += p->get_separator() +
" " + vec[i];
1971 ExcMessage(
"I need a List pattern to convert a string "
1972 "to a std::array."));
1974 auto base_p = p->get_base_pattern().clone();
1979 for (
unsigned int i = 0; i < N; ++i)
1986 template <
int rank,
int dim,
class Number>
1990 static std::unique_ptr<Patterns::PatternBase>
1994 "Cannot use this class for non List-compatible types.");
1995 return std::make_unique<Patterns::List>(
2010 ExcMessage(
"I need a List pattern to convert a string "
2011 "to a List compatible type."));
2012 auto base_p = p->get_base_pattern().clone();
2013 std::vector<std::string> vec(dim);
2015 for (
unsigned int i = 0; i < dim; ++i)
2021 for (
unsigned int i = 1; i < vec.size(); ++i)
2022 s += p->get_separator() +
" " + vec[i];
2036 ExcMessage(
"I need a List pattern to convert a string "
2037 "to a List compatible type."));
2039 auto base_p = p->get_base_pattern().clone();
2044 for (
const auto &str : v)
2052 template <
int dim,
class Number>
2057 static std::unique_ptr<Patterns::PatternBase>
2084 using T = std::unique_ptr<FunctionParser<dim>>;
2086 static std::unique_ptr<Patterns::PatternBase>
2090 "Cannot use this class for non List-compatible types.");
2092 return std::make_unique<Patterns::List>(
2107 ExcMessage(
"I need a List pattern to convert a string "
2108 "to a List compatible type."));
2110 const auto &expressions = t->get_expressions();
2111 if (expressions.size() == 0)
2112 return std::string();
2114 std::string s = expressions[0];
2115 for (
unsigned int i = 1; i < expressions.size(); ++i)
2116 s = s + p->get_separator() + expressions[i];
2130 ExcMessage(
"I need a List pattern to convert a string "
2131 "to a List compatible type."));
2133 const auto expressions =
2136 T t = std::make_unique<FunctionParser<dim>>(expressions.size());
2137 const std::string var =
2140 t->initialize(var, expressions, constants,
true);
2151 static std::unique_ptr<Patterns::PatternBase>
2162 std::vector<bool> mask(t.
size());
2163 for (
unsigned int i = 0; i < t.
size(); ++i)
2179 template <
class Number>
2182 using T = std::complex<Number>;
2184 static std::unique_ptr<Patterns::PatternBase>
2188 "Cannot use this class for non List-compatible types.");
2189 return std::make_unique<Patterns::List>(
2204 ExcMessage(
"I need a List pattern to convert a string "
2205 "to a List compatible type."));
2207 auto base_p = p->get_base_pattern().clone();
2210 p->get_separator() +
" " +
2228 ExcMessage(
"I need a List pattern to convert a string "
2229 "to a List compatible type."));
2231 auto base_p = p->get_base_pattern().clone();
2245 using T = std::string;
2247 static std::unique_ptr<Patterns::PatternBase>
2250 return std::make_unique<Patterns::Anything>();
2272 template <
class Key,
class Value>
2275 using T = std::pair<Key, Value>;
2277 static std::unique_ptr<Patterns::PatternBase>
2281 "Cannot use this class for non Map-compatible types.");
2282 return std::make_unique<Patterns::Tuple>(
2293 std::tuple<Key, Value> m(t);
2303 std::tuple<Key, Value> m;
2305 return std::make_pair(std::get<0>(m), std::get<1>(m));
2310 template <
class... Args>
2313 using T = std::tuple<Args...>;
2315 static std::unique_ptr<Patterns::PatternBase>
2319 "Cannot use this class for non tuple-compatible types.");
2320 return std::make_unique<Patterns::Tuple>(
2332 ExcMessage(
"I need a Tuple pattern to convert a tuple "
2337 for (
unsigned int i = 0; i < string_array.size(); ++i)
2339 (i != 0u ?
" " + p->get_separator() +
" " :
"") + string_array[i];
2352 ExcMessage(
"I need a Tuple pattern to convert a string "
2353 "to a tuple type."));
2361 template <std::size_t... U>
2362 static std::array<std::string, std::tuple_size<T>::value>
2365 std::index_sequence<U...>)
2367 std::array<std::string, std::tuple_size<T>::value> a = {
2373 static std::array<std::string, std::tuple_size<T>::value>
2377 t, pattern, std::make_index_sequence<std::tuple_size<T>::value>{});
2380 template <std::size_t... U>
2384 std::index_sequence<U...>)
2386 return std::make_tuple(
2396 s, pattern, std::make_index_sequence<std::tuple_size<T>::value>{});
2401 template <
typename T>
2409 template <
typename T>
unsigned int size() const
std::map< std::string, double > ConstMap
static std::string default_variable_names()
static std::unique_ptr< Double > create(const std::string &description)
static const char * description_init
virtual std::unique_ptr< PatternBase > clone() const override
static const double max_double_value
static const double min_double_value
virtual std::string description(const OutputStyle style=Machine) const override
virtual bool match(const std::string &test_string) const override
static std::unique_ptr< Integer > create(const std::string &description)
static const char * description_init
virtual std::unique_ptr< PatternBase > clone() const override
static const int min_int_value
virtual std::string description(const OutputStyle style=Machine) const override
virtual bool match(const std::string &test_string) const override
static const int max_int_value
std::size_t memory_consumption() const override
static std::unique_ptr< List > create(const std::string &description)
virtual std::string description(const OutputStyle style=Machine) const override
const PatternBase & get_base_pattern() const
virtual bool match(const std::string &test_string) const override
const std::string & get_separator() const
static const unsigned int max_int_value
virtual std::unique_ptr< PatternBase > clone() const override
virtual std::size_t memory_consumption() const
virtual std::unique_ptr< PatternBase > clone() const =0
virtual std::string description(const OutputStyle style=Machine) const =0
virtual bool match(const std::string &test_string) const =0
virtual ~PatternBase()=default
static const char * description_init
virtual std::string description(const OutputStyle style=Machine) const override
virtual std::unique_ptr< PatternBase > clone() const override
virtual bool match(const std::string &test_string) const override
static std::unique_ptr< Selection > create(const std::string &description)
std::size_t memory_consumption() const override
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
virtual std::string description(const OutputStyle style=Machine) const override
virtual bool match(const std::string &test_string) const override
static T to_value_internal_2(const std::vector< std::string > &s, const Patterns::Tuple &pattern)
static std::unique_ptr< Patterns::PatternBase > to_pattern()=delete
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static std::unique_ptr< Patterns::PatternBase > to_pattern()
virtual std::unique_ptr< PatternBase > clone() const override
static std::unique_ptr< Patterns::PatternBase > to_pattern()
std::size_t memory_consumption() const override
static std::unique_ptr< Anything > create(const std::string &description)
std::pair< Key, Value > T
static std::array< std::string, std::tuple_size< T >::value > to_string_internal_2(const T &t, const Patterns::Tuple &pattern)
static std::unique_ptr< Tuple > create(const std::string &description)
static std::unique_ptr< Patterns::PatternBase > to_pattern()
const unsigned int max_elements
static ::ExceptionBase & ExcInvalidRange(int arg1, int arg2)
static std::unique_ptr< Map > create(const std::string &description)
static ::ExceptionBase & ExcInvalidRange(int arg1, int arg2)
std::unique_ptr< FunctionParser< dim > > T
virtual bool match(const std::string &test_string) const override
static const char * description_init
const unsigned int min_elements
static std::unique_ptr< Patterns::PatternBase > to_pattern()
std::unique_ptr< PatternBase > value_pattern
std::vector< std::unique_ptr< PatternBase > > patterns
const unsigned int min_elements
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static const char * description_init
const PatternBase & get_key_pattern() const
virtual bool match(const std::string &test_string) const override
static std::unique_ptr< Patterns::PatternBase > to_pattern()
std::unique_ptr< PatternBase > pattern
virtual bool match(const std::string &test_string) const override
const std::string separator
const unsigned int max_elements
static std::unique_ptr< Patterns::PatternBase > to_pattern()
static ::ExceptionBase & ExcNoMatch(std::string arg1, std::string arg2)
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
constexpr std::array< const char *, 4 > default_map_separator
void to_value(const std::string &s, T &t)
constexpr int max_map_rank()
static std::unique_ptr< Patterns::PatternBase > to_pattern()
static std::unique_ptr< Patterns::PatternBase > to_pattern()
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
virtual std::unique_ptr< PatternBase > clone() const override
static const char * description_init
static const char * description_init
static std::enable_if< std::is_same< Dummy, T >::value &&std::is_same< T, bool >::value, std::unique_ptr< Patterns::PatternBase > >::type to_pattern()
Tuple(const Patterns &...patterns)
static std::unique_ptr< MultipleSelection > create(const std::string &description)
virtual bool match(const std::string &test_string) const override
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
std::string to_string(const T &t)
virtual std::unique_ptr< PatternBase > clone() const override
const std::string separator
virtual std::unique_ptr< PatternBase > clone() const override
virtual std::unique_ptr< PatternBase > clone() const override
virtual std::string description(const OutputStyle style=Machine) const override
virtual std::unique_ptr< PatternBase > clone() const override
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static const char * description_init
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
#define DeclException2(Exception2, type1, type2, outsequence)
#define AssertDimension(dim1, dim2)
static T to_value(const std::string &s, const Patterns::PatternBase &p= *Convert< T >::to_pattern())
static constexpr int list_rank
constexpr int max_list_rank()
static const char * description_init
static std::unique_ptr< FileName > create(const std::string &description)
static const char * description_init
const PatternBase & get_value_pattern() const
std::size_t memory_consumption() const override
static constexpr bool const value
static std::unique_ptr< Bool > create(const std::string &description)
const std::string & get_separator() const
virtual std::string description(const OutputStyle style=Machine) const override
Tuple(const std::vector< std::unique_ptr< PatternBase > > &patterns, const std::string &separator=":")
const std::string & get_separator() const
static std::unique_ptr< Patterns::PatternBase > to_pattern()
static T to_value(const std::string &s, const Patterns::PatternBase &p= *Convert< T >::to_pattern())=delete
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
virtual std::string description(const OutputStyle style=Machine) const override
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
virtual std::string description(const OutputStyle style=Machine) const override
static std::unique_ptr< DirectoryName > create(const std::string &description)
static const unsigned int max_int_value
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
std::unique_ptr< PatternBase > key_pattern
virtual std::string description(const OutputStyle style=Machine) const override
static std::unique_ptr< Patterns::PatternBase > to_pattern()
virtual bool match(const std::string &test_string) const override
const std::string separator
std::array< ValueType, N > T
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
const PatternBase & get_pattern(const unsigned int i) const
std::size_t memory_consumption() const override
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static ::ExceptionBase & ExcCommasNotAllowed(int arg1)
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
#define DeclException1(Exception1, type1, outsequence)
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static std::unique_ptr< Patterns::PatternBase > to_pattern()
static std::array< std::string, std::tuple_size< T >::value > to_string_internal_1(const T &t, const Patterns::Tuple &pattern, std::index_sequence< U... >)
static const char * description_init
virtual std::unique_ptr< PatternBase > clone() const override
static constexpr int map_rank
static std::string to_string(const T &value, const Patterns::PatternBase &p= *Convert< T >::to_pattern())
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static constexpr bool const value
static T to_value_internal_1(const std::vector< std::string > &s, const Patterns::Tuple &pattern, std::index_sequence< U... >)
virtual std::string description(const OutputStyle style=Machine) const override
constexpr std::array< const char *, 4 > default_list_separator
static std::string to_string(const T &s, const Patterns::PatternBase &p= *Convert< T >::to_pattern())=delete
const std::string & get_key_value_separator() const
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static ::ExceptionBase & ExcMessage(std::string arg1)
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
const std::string key_value_separator
static std::string to_string(const T &t, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
static T to_value(const std::string &s, const Patterns::PatternBase &pattern= *Convert< T >::to_pattern())
#define AssertThrow(cond, exc)
std::string escape(const std::string &input, const PatternBase::OutputStyle style)
std::unique_ptr< PatternBase > pattern_factory(const std::string &description)
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)