Reference documentation for deal.II version GIT relicensing-216-gcda843ca70 2024-03-28 12:20:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
Namespaces | Classes | Functions
Patterns::Tools Namespace Reference

Namespaces

namespace  internal
 

Classes

struct  Convert
 
struct  Convert< ComponentMask >
 
struct  Convert< Point< dim, Number > >
 
struct  Convert< std::array< ValueType, N > >
 
struct  Convert< std::complex< Number > >
 
struct  Convert< std::pair< Key, Value > >
 
struct  Convert< std::string >
 
struct  Convert< std::tuple< Args... > >
 
struct  Convert< std::unique_ptr< FunctionParser< dim > > >
 
struct  Convert< T, std::enable_if_t< is_list_compatible< T >::value > >
 
struct  Convert< T, std::enable_if_t< is_map_compatible< T >::value > >
 
struct  Convert< T, std::enable_if_t< std::is_arithmetic_v< T > > >
 
struct  Convert< Tensor< rank, dim, Number > >
 
struct  Convert< VectorTools::NormType, void >
 
struct  is_list_compatible
 
struct  is_map_compatible
 

Functions

template<typename T >
std::string to_string (const T &t)
 
template<typename T >
void to_value (const std::string &s, T &t)
 
static ::ExceptionBaseExcNoMatch (std::string arg1, std::string arg2)
 

Detailed Description

Namespace for a few classes and functions that act on values and patterns, and allow to convert from non elementary types to strings and vice versa.

A typical usage of these tools is in the following example:

using T = std::vector<unsigned int>;
T vec(3);
vec[0] = 1;
vec[1] = 3;
vec[2] = 5;
std::cout << pattern->description() << std::endl;
// [List of <[Integer]> of length 0...4294967295 (inclusive)]
std::cout << s << std::endl;
// 1, 2, 3
// now vec has size 4, and contains the elements 2,3,4,5
std::cout << internal::RankInfo<T>::list_rank << std::endl; // Outputs 1
std::cout << internal::RankInfo<T>::map_rank << std::endl; // Outputs 0
static std::unique_ptr< Patterns::PatternBase > to_pattern()=delete
static T to_value(const std::string &s, const Patterns::PatternBase &p= *Convert< T >::to_pattern())=delete
static std::string to_string(const T &s, const Patterns::PatternBase &p= *Convert< T >::to_pattern())=delete

Convert<T> is used by the function Patterns::Tools::add_parameter() in this namespace. Internally it uses the internal::RankInfo<T> class to decide how many different separators are required to convert the given type to a string.

For example, to write vectors of vectors, the default is to use "," for the first (inner) separator, and ";" for the second (outer) separator, i.e.

std::vector<std::vector<unsigned int>> vec;
vec = Convert<decltype(vec)>::to_value("1,2,3 ; 4,5,6");
s = convert<decltype(vec[0])>::to_string(vec[0]);
// s now contains the string "1,2,3"
void to_value(const std::string &s, T &t)
Definition patterns.h:2398
std::string to_string(const T &t)
Definition patterns.h:2390

Separators for Patterns::List and Patterns::Map compatible types are selected according to the rank of the list and map objects, using the arrays Patterns::Tools::internal::default_list_separator and Patterns::Tools::internal::default_map_separator.

They are currently set to:

default_list_separator{{"," , ";" , "|" , "%"}};
default_map_separator {{":" , "=" , "@" , "#"}};

When one needs a mixture of Patterns::List and Patterns::Map types, their RankInfo is computed by taking the maximum of the vector_rank of the Key and of the Value type, so that, for example, it is possible to have the following

... // Build compare class
std::map<std::vector<unsigned int>, std::vector<double>, compare> map;
map = convert<decltype(map)>::to_value(
"1,2,3 : 5.0,6.0,7.0 ; 8,9,10 : 11.0,12.0,13.0");

Some non elementary types are supported, like Point(), or std::complex<double>. If you wish to support more types, you have to specialize the Convert struct as well as the RankInfo struct.

Function Documentation

◆ to_string()

template<typename T >
std::string Patterns::Tools::to_string ( const T &  t)
private

A utility function that simplifies the conversion to strings of arbitrarily complex types.

This function calls the method Convert<T>::to_string() with the default pattern. An example usage is the following:

auto t = std::make_tuple(1.0, std::make_pair(1, "ciao"));
std::cout << s; // will print "1 % 1 : ciao""

See the documentation of the class Patterns::Tools::Convert, and of the helper class Patterns::Tools::RankInfo for details on the way separators are selected when outputting STL container types.

Definition at line 2390 of file patterns.h.

◆ to_value()

template<typename T >
void Patterns::Tools::to_value ( const std::string &  s,
T &  t 
)
private

A utility function that simplifies the conversion from strings to arbitrary types.

This function calls the method Convert<T>::to_value() with the default pattern. An example usage is the following:

auto t = std::make_tuple(1.0, std::make_pair(1, "ciao"));
// replace the value of 't' by the parsed content of the string argument:
Patterns::Tools::to_value("2 % 3 : mondo", t);
std::cout << s; // will print "2 % 3 : mondo""

See the documentation of the class Patterns::Tools::Convert, and of the helper class Patterns::Tools::RankInfo for details on the separators you should use in your string patterns when converting from a string to a container type.

Notice that the current content of variable t is ignored. Its type is used to infer how to interpret the string. If the string is successfully parsed, then t will be set to the parsed content of s.

Definition at line 2398 of file patterns.h.