| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2017 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_patterns_h |
| 16 |
#define dealii_patterns_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/enable_observer_pointer.h> |
| 22 |
#include <deal.II/base/exceptions.h> |
| 23 |
#include <deal.II/base/point.h> |
| 24 |
#include <deal.II/base/template_constraints.h> |
| 25 |
#include <deal.II/base/tensor.h> |
| 26 |
#include <deal.II/base/utilities.h> |
| 27 |
|
| 28 |
#include <deal.II/fe/component_mask.h> |
| 29 |
|
| 30 |
#include <boost/archive/basic_archive.hpp> |
| 31 |
#include <boost/core/demangle.hpp> |
| 32 |
#include <boost/property_tree/ptree_fwd.hpp> |
| 33 |
#include <boost/property_tree/ptree_serialization.hpp> |
| 34 |
#include <boost/serialization/split_member.hpp> |
| 35 |
|
| 36 |
#include <algorithm> |
| 37 |
#include <array> |
| 38 |
#include <deque> |
| 39 |
#include <limits> |
| 40 |
#include <list> |
| 41 |
#include <map> |
| 42 |
#include <memory> |
| 43 |
#include <set> |
| 44 |
#include <sstream> |
| 45 |
#include <string> |
| 46 |
#include <type_traits> |
| 47 |
#include <unordered_map> |
| 48 |
#include <unordered_set> |
| 49 |
#include <utility> |
| 50 |
#include <vector> |
| 51 |
|
| 52 |
#ifdef DEAL_II_WITH_MAGIC_ENUM |
| 53 |
# include <magic_enum.hpp> |
| 54 |
#endif |
| 55 |
|
| 56 |
DEAL_II_NAMESPACE_OPEN |
| 57 |
|
| 58 |
// forward declarations for interfaces and friendship |
| 59 |
#ifndef DOXYGEN |
| 60 |
class LogStream; |
| 61 |
class MultipleParameterLoop; |
| 62 |
template <int dim> |
| 63 |
class FunctionParser; |
| 64 |
#endif |
| 65 |
|
| 66 |
/** |
| 67 |
* Namespace for a few classes that act as patterns for the ParameterHandler |
| 68 |
* class. These classes implement an interface that checks whether a parameter |
| 69 |
* in an input file matches a certain pattern, such as "being boolean", "an |
| 70 |
* integer value", etc. |
| 71 |
* |
| 72 |
* @ingroup input |
| 73 |
*/ |
| 74 |
namespace Patterns |
| 75 |
{ |
| 76 |
/** |
| 77 |
* Base class to declare common interface. The purpose of this class is |
| 78 |
* mostly to define the interface of patterns, and to force derived classes |
| 79 |
* to have a <tt>clone</tt> function. It is thus, in the languages of the |
| 80 |
* "Design Patterns" book (Gamma et al.), a "prototype". |
| 81 |
*/ |
| 82 |
class PatternBase |
| 83 |
{ |
| 84 |
public: |
| 85 |
/** |
| 86 |
* Make destructor of this and all derived classes virtual. |
| 87 |
*/ |
| 88 |
virtual ~PatternBase() = default; |
| 89 |
|
| 90 |
/** |
| 91 |
* Return <tt>true</tt> if the given string matches the pattern. |
| 92 |
*/ |
| 93 |
virtual bool |
| 94 |
match(const std::string &test_string) const = 0; |
| 95 |
|
| 96 |
/** |
| 97 |
* List of possible description output formats. |
| 98 |
* |
| 99 |
* Capitalization chosen for similarity to ParameterHandler::OutputStyle. |
| 100 |
*/ |
| 101 |
enum OutputStyle |
| 102 |
{ |
| 103 |
/** |
| 104 |
* Simple text suitable for machine parsing in the static public member |
| 105 |
* functions for all of the built in inheriting classes. |
| 106 |
* |
| 107 |
* Preferably human readable, but machine parsing is more critical. |
| 108 |
*/ |
| 109 |
Machine, |
| 110 |
/** |
| 111 |
* Easily human readable plain text format suitable for plain text |
| 112 |
* documentation. |
| 113 |
*/ |
| 114 |
Text, |
| 115 |
/** |
| 116 |
* Easily human readable LaTeX format suitable for printing in manuals. |
| 117 |
*/ |
| 118 |
LaTeX |
| 119 |
}; |
| 120 |
|
| 121 |
/** |
| 122 |
* Return a string describing the pattern. |
| 123 |
*/ |
| 124 |
virtual std::string |
| 125 |
description(const OutputStyle style = Machine) const = 0; |
| 126 |
|
| 127 |
/** |
| 128 |
* Return a pointer to an exact copy of the object. This is necessary |
| 129 |
* since we want to store objects of this type in containers, were we need |
| 130 |
* to copy objects without knowledge of their actual data type (we only |
| 131 |
* have pointers to the base class). |
| 132 |
* |
| 133 |
* Ownership of the objects returned by this function is passed to the |
| 134 |
* caller of this function. |
| 135 |
*/ |
| 136 |
virtual std::unique_ptr<PatternBase> |
| 137 |
clone() const = 0; |
| 138 |
|
| 139 |
/** |
| 140 |
* Determine an estimate for the memory consumption (in bytes) of this |
| 141 |
* object. To avoid unnecessary overhead, we do not force derived classes |
| 142 |
* to provide this function as a virtual overloaded one, but rather try to |
| 143 |
* cast the present object to one of the known derived classes and if that |
| 144 |
* fails then take the size of this base class instead and add 32 byte |
| 145 |
* (this value is arbitrary, it should account for virtual function |
| 146 |
* tables, and some possible data elements). Since there are usually not |
| 147 |
* many thousands of objects of this type around, and since the |
| 148 |
* memory_consumption mechanism is used to find out where memory in the |
| 149 |
* range of many megabytes is, this seems like a reasonable approximation. |
| 150 |
* |
| 151 |
* On the other hand, if you know that your class deviates from this |
| 152 |
* assumption significantly, you can still overload this function. |
| 153 |
*/ |
| 154 |
virtual std::size_t |
| 155 |
memory_consumption() const; |
| 156 |
}; |
| 157 |
|
| 158 |
/** |
| 159 |
* Return pointer to the correct derived class based on description. |
| 160 |
*/ |
| 161 |
std::unique_ptr<PatternBase> |
| 162 |
pattern_factory(const std::string &description); |
| 163 |
|
| 164 |
namespace internal |
| 165 |
{ |
| 166 |
/** |
| 167 |
* Escape the string @p input for the specified @p style so that characters |
| 168 |
* will appear as intended. For example, characters like _ can not be |
| 169 |
* written as is in LateX and have to be escaped as \_. |
| 170 |
*/ |
| 171 |
std::string |
| 172 |
escape(const std::string &input, const PatternBase::OutputStyle style); |
| 173 |
|
| 174 |
} // namespace internal |
| 175 |
|
| 176 |
/** |
| 177 |
* Test for the string being an integer. If bounds are given to the |
| 178 |
* constructor, then the integer given also needs to be within the interval |
| 179 |
* specified by these bounds. Note that unlike common convention in the C++ |
| 180 |
* standard library, both bounds of this interval are inclusive; the reason |
| 181 |
* is that in practice in most cases, one needs closed intervals, but these |
| 182 |
* can only be realized with inclusive bounds for non-integer values. We |
| 183 |
* thus stay consistent by always using closed intervals. |
| 184 |
* |
| 185 |
* If the upper bound given to the constructor is smaller than the |
| 186 |
* lower bound, then every integer is allowed. |
| 187 |
* |
| 188 |
* Giving bounds may be useful if for example a value can only be positive |
| 189 |
* and less than a reasonable upper bound (for example the number of |
| 190 |
* refinement steps to be performed), or in many other cases. |
| 191 |
*/ |
| 192 |
class Integer : public PatternBase |
| 193 |
{ |
| 194 |
public: |
| 195 |
/** |
| 196 |
* Minimal integer value. If the numeric_limits class is available use |
| 197 |
* this information to obtain the extremal values, otherwise set it so |
| 198 |
* that this class understands that all values are allowed. |
| 199 |
*/ |
| 200 |
static const int min_int_value; |
| 201 |
|
| 202 |
/** |
| 203 |
* Maximal integer value. If the numeric_limits class is available use |
| 204 |
* this information to obtain the extremal values, otherwise set it so |
| 205 |
* that this class understands that all values are allowed. |
| 206 |
*/ |
| 207 |
static const int max_int_value; |
| 208 |
|
| 209 |
/** |
| 210 |
* Constructor. Bounds can be specified within which a valid |
| 211 |
* parameter has to be. If the upper bound is smaller than the |
| 212 |
* lower bound, then the entire set of integers is implied. The |
| 213 |
* default values are chosen such that no bounds are enforced on |
| 214 |
* parameters. |
| 215 |
* |
| 216 |
* Note that the range implied by an object of the current type |
| 217 |
* is inclusive of both bounds values, i.e., the @p upper_bound is |
| 218 |
* an allowed value, rather than indicating a half-open value as |
| 219 |
* is often done in other contexts. |
| 220 |
*/ |
| 221 |
Integer(const int lower_bound = min_int_value, |
| 222 |
const int upper_bound = max_int_value); |
| 223 |
|
| 224 |
/** |
| 225 |
* Return <tt>true</tt> if the string is an integer and its value is |
| 226 |
* within the specified range. |
| 227 |
*/ |
| 228 |
virtual bool |
| 229 |
match(const std::string &test_string) const override; |
| 230 |
|
| 231 |
/** |
| 232 |
* Return a description of the pattern that valid strings are expected to |
| 233 |
* match. If bounds were specified to the constructor, then include them |
| 234 |
* into this description. |
| 235 |
*/ |
| 236 |
virtual std::string |
| 237 |
description(const OutputStyle style = Machine) const override; |
| 238 |
|
| 239 |
/** |
| 240 |
* Return a copy of the present object, which is newly allocated on the |
| 241 |
* heap. Ownership of that object is transferred to the caller of this |
| 242 |
* function. |
| 243 |
*/ |
| 244 |
virtual std::unique_ptr<PatternBase> |
| 245 |
clone() const override; |
| 246 |
|
| 247 |
/** |
| 248 |
* Create a new object if the start of description matches |
| 249 |
* description_init. Ownership of that object is transferred to the |
| 250 |
* caller of this function. |
| 251 |
*/ |
| 252 |
static std::unique_ptr<Integer> |
| 253 |
create(const std::string &description); |
| 254 |
|
| 255 |
private: |
| 256 |
/** |
| 257 |
* Value of the lower bound. A number that satisfies the |
| 258 |
* @ref match |
| 259 |
* operation of this class must be equal to this value or larger, if the |
| 260 |
* bounds of the interval for a valid range. |
| 261 |
*/ |
| 262 |
const int lower_bound; |
| 263 |
|
| 264 |
/** |
| 265 |
* Value of the upper bound. A number that satisfies the |
| 266 |
* @ref match |
| 267 |
* operation of this class must be equal to this value or less, if the |
| 268 |
* bounds of the interval for a valid range. |
| 269 |
*/ |
| 270 |
const int upper_bound; |
| 271 |
|
| 272 |
/** |
| 273 |
* Initial part of description |
| 274 |
*/ |
| 275 |
static const char *description_init; |
| 276 |
}; |
| 277 |
|
| 278 |
/** |
| 279 |
* Test for the string being a <tt>double</tt>. If bounds are given to the |
| 280 |
* constructor, then the integer given also needs to be within the interval |
| 281 |
* specified by these bounds. Note that unlike common convention in the C++ |
| 282 |
* standard library, both bounds of this interval are inclusive; the reason |
| 283 |
* is that in practice in most cases, one needs closed intervals, but these |
| 284 |
* can only be realized with inclusive bounds for non-integer values. We |
| 285 |
* thus stay consistent by always using closed intervals. |
| 286 |
* |
| 287 |
* If the upper bound given to the constructor is smaller than the |
| 288 |
* lower bound, then every double precision number is allowed. |
| 289 |
* |
| 290 |
* Giving bounds may be useful if for example a value can only be positive |
| 291 |
* and less than a reasonable upper bound (for example damping parameters |
| 292 |
* are frequently only reasonable if between zero and one), or in many other |
| 293 |
* cases. |
| 294 |
*/ |
| 295 |
class Double : public PatternBase |
| 296 |
{ |
| 297 |
public: |
| 298 |
/** |
| 299 |
* Minimal double value used as default value, taken from |
| 300 |
* <tt>std::numeric_limits</tt>. |
| 301 |
*/ |
| 302 |
static const double min_double_value; |
| 303 |
|
| 304 |
/** |
| 305 |
* Maximal double value used as default value, taken from |
| 306 |
* <tt>std::numeric_limits</tt>. |
| 307 |
*/ |
| 308 |
static const double max_double_value; |
| 309 |
|
| 310 |
/** |
| 311 |
* Constructor. Bounds can be specified within which a valid |
| 312 |
* parameter has to be. If the upper bound is smaller than the |
| 313 |
* lower bound, then the entire set of double precision numbers is |
| 314 |
* implied. The default values are chosen such that no bounds are |
| 315 |
* enforced on parameters. |
| 316 |
*/ |
| 317 |
Double(const double lower_bound = min_double_value, |
| 318 |
const double upper_bound = max_double_value); |
| 319 |
|
| 320 |
/** |
| 321 |
* Return <tt>true</tt> if the string is a number and its value is within |
| 322 |
* the specified range. |
| 323 |
*/ |
| 324 |
virtual bool |
| 325 |
match(const std::string &test_string) const override; |
| 326 |
|
| 327 |
/** |
| 328 |
* Return a description of the pattern that valid strings are expected to |
| 329 |
* match. If bounds were specified to the constructor, then include them |
| 330 |
* into this description. |
| 331 |
*/ |
| 332 |
virtual std::string |
| 333 |
description(const OutputStyle style = Machine) const override; |
| 334 |
|
| 335 |
/** |
| 336 |
* Return a copy of the present object, which is newly allocated on the |
| 337 |
* heap. Ownership of that object is transferred to the caller of this |
| 338 |
* function. |
| 339 |
*/ |
| 340 |
virtual std::unique_ptr<PatternBase> |
| 341 |
clone() const override; |
| 342 |
|
| 343 |
/** |
| 344 |
* Creates a new object on the heap using @p new if the given |
| 345 |
* @p description is a valid format (for example created by calling |
| 346 |
* description() on an existing object), or @p nullptr otherwise. Ownership |
| 347 |
* of the returned object is transferred to the caller of this function, |
| 348 |
* which should be freed using @p delete. |
| 349 |
*/ |
| 350 |
static std::unique_ptr<Double> |
| 351 |
create(const std::string &description); |
| 352 |
|
| 353 |
private: |
| 354 |
/** |
| 355 |
* Value of the lower bound. A number that satisfies the |
| 356 |
* @ref match |
| 357 |
* operation of this class must be equal to this value or larger, if the |
| 358 |
* bounds of the interval form a valid range. |
| 359 |
*/ |
| 360 |
const double lower_bound; |
| 361 |
|
| 362 |
/** |
| 363 |
* Value of the upper bound. A number that satisfies the |
| 364 |
* @ref match |
| 365 |
* operation of this class must be equal to this value or less, if the |
| 366 |
* bounds of the interval form a valid range. |
| 367 |
*/ |
| 368 |
const double upper_bound; |
| 369 |
|
| 370 |
/** |
| 371 |
* Initial part of description |
| 372 |
*/ |
| 373 |
static const char *description_init; |
| 374 |
}; |
| 375 |
|
| 376 |
/** |
| 377 |
* Test for the string being one of a sequence of values given like a |
| 378 |
* regular expression. For example, if the string given to the constructor |
| 379 |
* is <tt>"red|blue|black"</tt>, then the |
| 380 |
* @ref match |
| 381 |
* function returns <tt>true</tt> exactly if the string is either "red" or |
| 382 |
* "blue" or "black". Spaces around the pipe signs do not matter and are |
| 383 |
* eliminated. |
| 384 |
*/ |
| 385 |
class Selection : public PatternBase |
| 386 |
{ |
| 387 |
public: |
| 388 |
/** |
| 389 |
* Constructor. Take the given parameter as the specification of valid |
| 390 |
* strings. |
| 391 |
*/ |
| 392 |
Selection(const std::string &seq); |
| 393 |
|
| 394 |
/** |
| 395 |
* Return <tt>true</tt> if the string is an element of the description |
| 396 |
* list passed to the constructor. |
| 397 |
*/ |
| 398 |
virtual bool |
| 399 |
match(const std::string &test_string) const override; |
| 400 |
|
| 401 |
/** |
| 402 |
* Return a description of the pattern that valid strings are expected to |
| 403 |
* match. Here, this is the list of valid strings passed to the |
| 404 |
* constructor. |
| 405 |
*/ |
| 406 |
virtual std::string |
| 407 |
description(const OutputStyle style = Machine) const override; |
| 408 |
|
| 409 |
/** |
| 410 |
* Return a copy of the present object, which is newly allocated on the |
| 411 |
* heap. Ownership of that object is transferred to the caller of this |
| 412 |
* function. |
| 413 |
*/ |
| 414 |
virtual std::unique_ptr<PatternBase> |
| 415 |
clone() const override; |
| 416 |
|
| 417 |
/** |
| 418 |
* Determine an estimate for the memory consumption (in bytes) of this |
| 419 |
* object. |
| 420 |
*/ |
| 421 |
std::size_t |
| 422 |
memory_consumption() const override; |
| 423 |
|
| 424 |
/** |
| 425 |
* Create a new object if the start of description matches |
| 426 |
* description_init. Ownership of that object is transferred to the |
| 427 |
* caller of this function. |
| 428 |
*/ |
| 429 |
static std::unique_ptr<Selection> |
| 430 |
create(const std::string &description); |
| 431 |
|
| 432 |
private: |
| 433 |
/** |
| 434 |
* List of valid strings as passed to the constructor. We don't make this |
| 435 |
* string constant, as we process it somewhat in the constructor. |
| 436 |
*/ |
| 437 |
std::string sequence; |
| 438 |
|
| 439 |
/** |
| 440 |
* Initial part of description |
| 441 |
*/ |
| 442 |
static const char *description_init; |
| 443 |
}; |
| 444 |
|
| 445 |
|
| 446 |
/** |
| 447 |
* This pattern matches a list of values separated by commas (or another |
| 448 |
* string), each of which have to match a pattern given to the constructor. |
| 449 |
* With two additional parameters, the number of elements this list has to |
| 450 |
* have can be specified. If none is specified, the list may have zero or |
| 451 |
* more entries. |
| 452 |
*/ |
| 453 |
class List : public PatternBase |
| 454 |
{ |
| 455 |
public: |
| 456 |
/** |
| 457 |
* Maximal integer value. If the numeric_limits class is available use |
| 458 |
* this information to obtain the extremal values, otherwise set it so |
| 459 |
* that this class understands that all values are allowed. |
| 460 |
*/ |
| 461 |
static const unsigned int max_int_value; |
| 462 |
|
| 463 |
/** |
| 464 |
* Constructor. Take the given parameter as the specification of valid |
| 465 |
* elements of the list. |
| 466 |
* |
| 467 |
* The three other arguments can be used to denote minimal and maximal |
| 468 |
* allowable lengths of the list, and the string that is used as a |
| 469 |
* separator between elements of the list. |
| 470 |
*/ |
| 471 |
List(const PatternBase &base_pattern, |
| 472 |
const unsigned int min_elements = 0, |
| 473 |
const unsigned int max_elements = max_int_value, |
| 474 |
const std::string &separator = ","); |
| 475 |
|
| 476 |
|
| 477 |
/** |
| 478 |
* Return the internally stored separator. |
| 479 |
*/ |
| 480 |
const std::string & |
| 481 |
get_separator() const; |
| 482 |
|
| 483 |
/** |
| 484 |
* Return the internally stored base pattern. |
| 485 |
*/ |
| 486 |
const PatternBase & |
| 487 |
get_base_pattern() const; |
| 488 |
|
| 489 |
/** |
| 490 |
* Copy constructor. |
| 491 |
*/ |
| 492 |
List(const List &other); |
| 493 |
|
| 494 |
/** |
| 495 |
* Return <tt>true</tt> if the string is a comma-separated list of strings |
| 496 |
* each of which match the pattern given to the constructor. |
| 497 |
*/ |
| 498 |
virtual bool |
| 499 |
match(const std::string &test_string) const override; |
| 500 |
|
| 501 |
/** |
| 502 |
* Return a description of the pattern that valid strings are expected to |
| 503 |
* match. |
| 504 |
*/ |
| 505 |
virtual std::string |
| 506 |
description(const OutputStyle style = Machine) const override; |
| 507 |
|
| 508 |
/** |
| 509 |
* Return a copy of the present object, which is newly allocated on the |
| 510 |
* heap. Ownership of that object is transferred to the caller of this |
| 511 |
* function. |
| 512 |
*/ |
| 513 |
virtual std::unique_ptr<PatternBase> |
| 514 |
clone() const override; |
| 515 |
|
| 516 |
/** |
| 517 |
* Create a new object if the start of description matches |
| 518 |
* description_init. Ownership of that object is transferred to the |
| 519 |
* caller of this function. |
| 520 |
*/ |
| 521 |
static std::unique_ptr<List> |
| 522 |
create(const std::string &description); |
| 523 |
|
| 524 |
/** |
| 525 |
* Determine an estimate for the memory consumption (in bytes) of this |
| 526 |
* object. |
| 527 |
*/ |
| 528 |
std::size_t |
| 529 |
memory_consumption() const override; |
| 530 |
|
| 531 |
/** |
| 532 |
* @addtogroup Exceptions |
| 533 |
* @{ |
| 534 |
*/ |
| 535 |
|
| 536 |
/** |
| 537 |
* Exception. |
| 538 |
*/ |
| 539 |
DeclException2(ExcInvalidRange, |
| 540 |
int, |
| 541 |
int, |
| 542 |
<< "The values " << arg1 << " and " << arg2 |
| 543 |
<< " do not form a valid range."); |
| 544 |
/** @} */ |
| 545 |
private: |
| 546 |
/** |
| 547 |
* Copy of the pattern that each element of the list has to satisfy. |
| 548 |
*/ |
| 549 |
std::unique_ptr<PatternBase> pattern; |
| 550 |
|
| 551 |
/** |
| 552 |
* Minimum number of elements the list must have. |
| 553 |
*/ |
| 554 |
const unsigned int min_elements; |
| 555 |
|
| 556 |
/** |
| 557 |
* Maximum number of elements the list must have. |
| 558 |
*/ |
| 559 |
const unsigned int max_elements; |
| 560 |
|
| 561 |
/** |
| 562 |
* Separator between elements of the list. |
| 563 |
*/ |
| 564 |
const std::string separator; |
| 565 |
|
| 566 |
/** |
| 567 |
* Initial part of description |
| 568 |
*/ |
| 569 |
static const char *description_init; |
| 570 |
}; |
| 571 |
|
| 572 |
|
| 573 |
/** |
| 574 |
* This pattern matches a list of comma-separated values each of which |
| 575 |
* denotes a pair of key and value. Both key and value have to match a |
| 576 |
* pattern given to the constructor. For each entry of the map, parameters |
| 577 |
* have to be entered in the form <code>key: value</code>. In other words, a |
| 578 |
* map is described in the form <code>key1: value1, key2: value2, key3: |
| 579 |
* value3, ...</code>. Two constructor arguments allow to choose a delimiter |
| 580 |
* between pairs other than the comma, and a delimiter between key and value |
| 581 |
* other than colon. |
| 582 |
* |
| 583 |
* With two additional parameters, the number of elements this list has to |
| 584 |
* have can be specified. If none is specified, the map may have zero or |
| 585 |
* more entries. |
| 586 |
*/ |
| 587 |
class Map : public PatternBase |
| 588 |
{ |
| 589 |
public: |
| 590 |
/** |
| 591 |
* Maximal integer value. If the numeric_limits class is available use |
| 592 |
* this information to obtain the extremal values, otherwise set it so |
| 593 |
* that this class understands that all values are allowed. |
| 594 |
*/ |
| 595 |
static const unsigned int max_int_value; |
| 596 |
|
| 597 |
/** |
| 598 |
* Constructor. Take the given parameter as the specification of valid |
| 599 |
* elements of the list. |
| 600 |
* |
| 601 |
* The four other arguments can be used to denote minimal and maximal |
| 602 |
* allowable lengths of the list as well as the separators used to delimit |
| 603 |
* pairs of the map and the symbol used to separate keys and values. |
| 604 |
*/ |
| 605 |
Map(const PatternBase &key_pattern, |
| 606 |
const PatternBase &value_pattern, |
| 607 |
const unsigned int min_elements = 0, |
| 608 |
const unsigned int max_elements = max_int_value, |
| 609 |
const std::string &separator = ",", |
| 610 |
const std::string &key_value_separator = ":"); |
| 611 |
|
| 612 |
/** |
| 613 |
* Copy constructor. |
| 614 |
*/ |
| 615 |
Map(const Map &other); |
| 616 |
|
| 617 |
/** |
| 618 |
* Return <tt>true</tt> if the string is a comma-separated list of strings |
| 619 |
* each of which match the pattern given to the constructor. |
| 620 |
*/ |
| 621 |
virtual bool |
| 622 |
match(const std::string &test_string) const override; |
| 623 |
|
| 624 |
/** |
| 625 |
* Return a description of the pattern that valid strings are expected to |
| 626 |
* match. |
| 627 |
*/ |
| 628 |
virtual std::string |
| 629 |
description(const OutputStyle style = Machine) const override; |
| 630 |
|
| 631 |
/** |
| 632 |
* Return a copy of the present object, which is newly allocated on the |
| 633 |
* heap. Ownership of that object is transferred to the caller of this |
| 634 |
* function. |
| 635 |
*/ |
| 636 |
virtual std::unique_ptr<PatternBase> |
| 637 |
clone() const override; |
| 638 |
|
| 639 |
/** |
| 640 |
* Create a new object if the start of description matches |
| 641 |
* description_init. Ownership of that object is transferred to the |
| 642 |
* caller of this function. |
| 643 |
*/ |
| 644 |
static std::unique_ptr<Map> |
| 645 |
create(const std::string &description); |
| 646 |
|
| 647 |
/** |
| 648 |
* Determine an estimate for the memory consumption (in bytes) of this |
| 649 |
* object. |
| 650 |
*/ |
| 651 |
std::size_t |
| 652 |
memory_consumption() const override; |
| 653 |
|
| 654 |
/** |
| 655 |
* Return a reference to the key pattern. |
| 656 |
*/ |
| 657 |
const PatternBase & |
| 658 |
get_key_pattern() const; |
| 659 |
|
| 660 |
/** |
| 661 |
* Return a reference to the value pattern. |
| 662 |
*/ |
| 663 |
const PatternBase & |
| 664 |
get_value_pattern() const; |
| 665 |
|
| 666 |
/** |
| 667 |
* Return the separator of the map entries. |
| 668 |
*/ |
| 669 |
const std::string & |
| 670 |
get_separator() const; |
| 671 |
|
| 672 |
/** |
| 673 |
* Return the key-value separator. |
| 674 |
*/ |
| 675 |
const std::string & |
| 676 |
get_key_value_separator() const; |
| 677 |
|
| 678 |
/** |
| 679 |
* @addtogroup Exceptions |
| 680 |
* @{ |
| 681 |
*/ |
| 682 |
|
| 683 |
/** |
| 684 |
* Exception. |
| 685 |
*/ |
| 686 |
DeclException2(ExcInvalidRange, |
| 687 |
int, |
| 688 |
int, |
| 689 |
<< "The values " << arg1 << " and " << arg2 |
| 690 |
<< " do not form a valid range."); |
| 691 |
/** @} */ |
| 692 |
private: |
| 693 |
/** |
| 694 |
* Copy of the patterns that each key and each value of the map has to |
| 695 |
* satisfy. |
| 696 |
*/ |
| 697 |
std::unique_ptr<PatternBase> key_pattern; |
| 698 |
std::unique_ptr<PatternBase> value_pattern; |
| 699 |
|
| 700 |
/** |
| 701 |
* Minimum number of elements the list must have. |
| 702 |
*/ |
| 703 |
const unsigned int min_elements; |
| 704 |
|
| 705 |
/** |
| 706 |
* Maximum number of elements the list must have. |
| 707 |
*/ |
| 708 |
const unsigned int max_elements; |
| 709 |
|
| 710 |
/** |
| 711 |
* Separator between elements of the list. |
| 712 |
*/ |
| 713 |
const std::string separator; |
| 714 |
|
| 715 |
|
| 716 |
/** |
| 717 |
* Separator between keys and values. |
| 718 |
*/ |
| 719 |
const std::string key_value_separator; |
| 720 |
|
| 721 |
/** |
| 722 |
* Initial part of description |
| 723 |
*/ |
| 724 |
static const char *description_init; |
| 725 |
}; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
/** |
| 730 |
* This pattern matches colon-separated values of arbitrary types. Each type |
| 731 |
* has to match a pattern given to the constructor. |
| 732 |
* |
| 733 |
* An example usage is the following: |
| 734 |
* |
| 735 |
* @code |
| 736 |
* std::vector< std::unique_ptr<Patterns::PatternBase> > ps; |
| 737 |
* |
| 738 |
* ps.push_back(std::unique_ptr<Patterns::Integer>()); |
| 739 |
* ps.push_back(std::unique_ptr<Patterns::Double>()); |
| 740 |
* ps.push_back(std::unique_ptr<Patterns::Anything>()); |
| 741 |
* |
| 742 |
* Patterns::Tuple pattern(ps, ":"); |
| 743 |
* |
| 744 |
* bool check = ps.match("5 : 3.14 : Ciao"); // check = true |
| 745 |
* @endcode |
| 746 |
* |
| 747 |
* or, if you want to exploit ParameterHandler::add_parameter(): |
| 748 |
* |
| 749 |
* @code |
| 750 |
* using T = std::tuple<std::string, Point<3>, unsigned int>; |
| 751 |
* |
| 752 |
* T a = Patterns::Tools::Convert<T>::to_value("Ciao : 1.0, 2.0, 3.0 : 33"); |
| 753 |
* |
| 754 |
* ParameterHandler prm; |
| 755 |
* prm.add_parameter("A tuple", a); |
| 756 |
* |
| 757 |
* prm.log_parameters(deallog); |
| 758 |
* // DEAL:parameters::A tuple: Ciao : 1.000000, 2.000000, 3.000000 : 33 |
| 759 |
* |
| 760 |
* prm.set("A tuple", "Mondo : 2.0, 3.0, 4.0 : 34"); |
| 761 |
* prm.log_parameters(deallog); |
| 762 |
* // DEAL:parameters::A tuple: Mondo : 2.0, 3.0, 4.0 : 34 |
| 763 |
* |
| 764 |
* deallog << Patterns::Tools::Convert<T>::to_string(a) << std::endl; |
| 765 |
* // DEAL::Mondo : 2.000000, 3.000000, 4.000000 : 34 |
| 766 |
* @endcode |
| 767 |
* |
| 768 |
* The constructor expects a vector of Patterns, and optionally a string |
| 769 |
* specifying the separator to use when parsing the Tuple from a string. |
| 770 |
* |
| 771 |
* The default separator is a colon, owing to the fact that a pair is in fact |
| 772 |
* a tuple with two elements. |
| 773 |
*/ |
| 774 |
class Tuple : public PatternBase |
| 775 |
{ |
| 776 |
public: |
| 777 |
/** |
| 778 |
* Constructor. Use a vector of unique pointers to Patterns to construct |
| 779 |
* the tuple. |
| 780 |
* |
| 781 |
* @param patterns The pattern each object of the Tuple should match |
| 782 |
* @param separator An optional string used to delimit each element |
| 783 |
* Constructor. |
| 784 |
*/ |
| 785 |
Tuple(const std::vector<std::unique_ptr<PatternBase>> &patterns, |
| 786 |
const std::string &separator = ":"); |
| 787 |
|
| 788 |
/** |
| 789 |
* Constructor. Same as above, specialized for const char *. This is |
| 790 |
* necessary to avoid compilers errors due to the variadic constructors |
| 791 |
* provided below. |
| 792 |
*/ |
| 793 |
Tuple(const std::vector<std::unique_ptr<PatternBase>> &patterns, |
| 794 |
const char *separator); |
| 795 |
|
| 796 |
|
| 797 |
/** |
| 798 |
* Constructor. Creates a Tuple from more than one class derived from |
| 799 |
* PatternBase. |
| 800 |
* |
| 801 |
* @param separator What separator to use. |
| 802 |
* @param patterns The list of patterns to use |
| 803 |
*/ |
| 804 |
template <class... PatternTypes> |
| 805 |
Tuple(const std::string &separator, const PatternTypes &...patterns); |
| 806 |
|
| 807 |
/** |
| 808 |
* Constructor. This is needed to allow users to specify |
| 809 |
* directly the separator without using ";". |
| 810 |
* |
| 811 |
* Since we support a pure variadic templates version, without this |
| 812 |
* specialization, the compiler will fail with cryptic errors. |
| 813 |
*/ |
| 814 |
template <class... PatternTypes> |
| 815 |
Tuple(const char *separator, const PatternTypes &...patterns); |
| 816 |
|
| 817 |
/** |
| 818 |
* Constructor. Same as above, using the default separator. |
| 819 |
* |
| 820 |
* @param patterns The list of patterns to use |
| 821 |
*/ |
| 822 |
template <typename... Patterns> |
| 823 |
Tuple(const Patterns &...patterns); |
| 824 |
|
| 825 |
/** |
| 826 |
* Copy constructor. |
| 827 |
*/ |
| 828 |
Tuple(const Tuple &other); |
| 829 |
|
| 830 |
/** |
| 831 |
* Return <tt>true</tt> if the string is a list of strings |
| 832 |
* each of which matches the patterns given to the constructor. |
| 833 |
*/ |
| 834 |
virtual bool |
| 835 |
match(const std::string &test_string) const override; |
| 836 |
|
| 837 |
/** |
| 838 |
* Return a description of the pattern that valid strings are expected to |
| 839 |
* match. |
| 840 |
*/ |
| 841 |
virtual std::string |
| 842 |
description(const OutputStyle style = Machine) const override; |
| 843 |
|
| 844 |
/** |
| 845 |
* Return a copy of the present object, which is newly allocated on the |
| 846 |
* heap. Ownership of that object is transferred to the caller of this |
| 847 |
* function. |
| 848 |
*/ |
| 849 |
virtual std::unique_ptr<PatternBase> |
| 850 |
clone() const override; |
| 851 |
|
| 852 |
/** |
| 853 |
* Create a new object if the start of description matches |
| 854 |
* description_init. Ownership of that object is transferred to the |
| 855 |
* caller of this function. |
| 856 |
*/ |
| 857 |
static std::unique_ptr<Tuple> |
| 858 |
create(const std::string &description); |
| 859 |
|
| 860 |
/** |
| 861 |
* Determine an estimate for the memory consumption (in bytes) of this |
| 862 |
* object. |
| 863 |
*/ |
| 864 |
std::size_t |
| 865 |
memory_consumption() const override; |
| 866 |
|
| 867 |
/** |
| 868 |
* Return a reference to the i-th pattern in the tuple. |
| 869 |
*/ |
| 870 |
const PatternBase & |
| 871 |
get_pattern(const unsigned int i) const; |
| 872 |
|
| 873 |
/** |
| 874 |
* Return the separator of the tuple entries. |
| 875 |
*/ |
| 876 |
const std::string & |
| 877 |
get_separator() const; |
| 878 |
|
| 879 |
private: |
| 880 |
/** |
| 881 |
* Copy of the patterns stored in the Tuple. |
| 882 |
*/ |
| 883 |
std::vector<std::unique_ptr<PatternBase>> patterns; |
| 884 |
|
| 885 |
/** |
| 886 |
* Separator between elements of the list. |
| 887 |
*/ |
| 888 |
const std::string separator; |
| 889 |
|
| 890 |
/** |
| 891 |
* Initial part of description. |
| 892 |
*/ |
| 893 |
static const char *description_init; |
| 894 |
}; |
| 895 |
|
| 896 |
|
| 897 |
/** |
| 898 |
* This class is much like the Selection class, but it allows the input to |
| 899 |
* be a comma-separated list of values which each have to be given in the |
| 900 |
* constructor argument. The input is allowed to be empty or contain values |
| 901 |
* more than once and have an arbitrary number of spaces around commas. Of |
| 902 |
* course commas are not allowed inside the values given to the constructor. |
| 903 |
* |
| 904 |
* For example, if the string to the constructor was <tt>"ucd|gmv|eps"</tt>, |
| 905 |
* then the following would be legal inputs: "eps", "gmv, eps", or "". |
| 906 |
*/ |
| 907 |
class MultipleSelection : public PatternBase |
| 908 |
{ |
| 909 |
public: |
| 910 |
/** |
| 911 |
* Constructor. @p seq is a list of valid options separated by "|". |
| 912 |
*/ |
| 913 |
MultipleSelection(const std::string &seq); |
| 914 |
|
| 915 |
/** |
| 916 |
* Return <tt>true</tt> if the string is an element of the description |
| 917 |
* list passed to the constructor. |
| 918 |
*/ |
| 919 |
virtual bool |
| 920 |
match(const std::string &test_string) const override; |
| 921 |
|
| 922 |
/** |
| 923 |
* Return a description of the pattern that valid strings are expected to |
| 924 |
* match. Here, this is the list of valid strings passed to the |
| 925 |
* constructor. |
| 926 |
*/ |
| 927 |
virtual std::string |
| 928 |
description(const OutputStyle style = Machine) const override; |
| 929 |
|
| 930 |
/** |
| 931 |
* Return a copy of the present object, which is newly allocated on the |
| 932 |
* heap. Ownership of that object is transferred to the caller of this |
| 933 |
* function. |
| 934 |
*/ |
| 935 |
virtual std::unique_ptr<PatternBase> |
| 936 |
clone() const override; |
| 937 |
|
| 938 |
/** |
| 939 |
* Create a new object if the start of description matches |
| 940 |
* description_init. Ownership of that object is transferred to the |
| 941 |
* caller of this function. |
| 942 |
*/ |
| 943 |
static std::unique_ptr<MultipleSelection> |
| 944 |
create(const std::string &description); |
| 945 |
|
| 946 |
/** |
| 947 |
* Determine an estimate for the memory consumption (in bytes) of this |
| 948 |
* object. |
| 949 |
*/ |
| 950 |
std::size_t |
| 951 |
memory_consumption() const override; |
| 952 |
|
| 953 |
/** |
| 954 |
* @addtogroup Exceptions |
| 955 |
* @{ |
| 956 |
*/ |
| 957 |
|
| 958 |
/** |
| 959 |
* Exception. |
| 960 |
*/ |
| 961 |
DeclException1( |
| 962 |
ExcCommasNotAllowed, |
| 963 |
int, |
| 964 |
<< "A comma was found at position " << arg1 |
| 965 |
<< " of your input string, but commas are not allowed here."); |
| 966 |
/** @} */ |
| 967 |
private: |
| 968 |
/** |
| 969 |
* List of valid strings as passed to the constructor. We don't make this |
| 970 |
* string constant, as we process it somewhat in the constructor. |
| 971 |
*/ |
| 972 |
std::string sequence; |
| 973 |
|
| 974 |
/** |
| 975 |
* Initial part of description |
| 976 |
*/ |
| 977 |
static const char *description_init; |
| 978 |
}; |
| 979 |
|
| 980 |
/** |
| 981 |
* Test for the string being either "true" or "false". This is mapped to the |
| 982 |
* Selection class. |
| 983 |
*/ |
| 984 |
class Bool : public Selection |
| 985 |
{ |
| 986 |
public: |
| 987 |
/** |
| 988 |
* Constructor. |
| 989 |
*/ |
| 990 |
Bool(); |
| 991 |
|
| 992 |
/** |
| 993 |
* Return a description of the pattern that valid strings are expected to |
| 994 |
* match. |
| 995 |
*/ |
| 996 |
virtual std::string |
| 997 |
description(const OutputStyle style = Machine) const override; |
| 998 |
|
| 999 |
/** |
| 1000 |
* Return a copy of the present object, which is newly allocated on the |
| 1001 |
* heap. Ownership of that object is transferred to the caller of this |
| 1002 |
* function. |
| 1003 |
*/ |
| 1004 |
virtual std::unique_ptr<PatternBase> |
| 1005 |
clone() const override; |
| 1006 |
|
| 1007 |
/** |
| 1008 |
* Create a new object if the start of description matches |
| 1009 |
* description_init. Ownership of that object is transferred to the |
| 1010 |
* caller of this function. |
| 1011 |
*/ |
| 1012 |
static std::unique_ptr<Bool> |
| 1013 |
create(const std::string &description); |
| 1014 |
|
| 1015 |
private: |
| 1016 |
/** |
| 1017 |
* Initial part of description |
| 1018 |
*/ |
| 1019 |
static const char *description_init; |
| 1020 |
}; |
| 1021 |
|
| 1022 |
/** |
| 1023 |
* Always returns <tt>true</tt> when testing a string. |
| 1024 |
*/ |
| 1025 |
class Anything : public PatternBase |
| 1026 |
{ |
| 1027 |
public: |
| 1028 |
/** |
| 1029 |
* Constructor. (Allow for at least one non-virtual function in this |
| 1030 |
* class, as otherwise sometimes no virtual table is emitted.) |
| 1031 |
*/ |
| 1032 |
Anything() = default; |
| 1033 |
|
| 1034 |
/** |
| 1035 |
* Return <tt>true</tt> if the string matches its constraints, i.e. |
| 1036 |
* always. |
| 1037 |
*/ |
| 1038 |
virtual bool |
| 1039 |
match(const std::string &test_string) const override; |
| 1040 |
|
| 1041 |
/** |
| 1042 |
* Return a description of the pattern that valid strings are expected to |
| 1043 |
* match. Here, this is the string <tt>"[Anything]"</tt>. |
| 1044 |
*/ |
| 1045 |
virtual std::string |
| 1046 |
description(const OutputStyle style = Machine) const override; |
| 1047 |
|
| 1048 |
/** |
| 1049 |
* Return a copy of the present object, which is newly allocated on the |
| 1050 |
* heap. Ownership of that object is transferred to the caller of this |
| 1051 |
* function. |
| 1052 |
*/ |
| 1053 |
virtual std::unique_ptr<PatternBase> |
| 1054 |
clone() const override; |
| 1055 |
|
| 1056 |
/** |
| 1057 |
* Create a new object if the start of description matches |
| 1058 |
* description_init. Ownership of that object is transferred to the |
| 1059 |
* caller of this function. |
| 1060 |
*/ |
| 1061 |
static std::unique_ptr<Anything> |
| 1062 |
create(const std::string &description); |
| 1063 |
|
| 1064 |
private: |
| 1065 |
/** |
| 1066 |
* Initial part of description |
| 1067 |
*/ |
| 1068 |
static const char *description_init; |
| 1069 |
}; |
| 1070 |
|
| 1071 |
|
| 1072 |
/** |
| 1073 |
* A pattern that can be used to indicate when a parameter is intended to be |
| 1074 |
* the name of a file. By itself, this class does not check whether the |
| 1075 |
* string that is given in a parameter file actually corresponds to an |
| 1076 |
* existing file (it could, for example, be the name of a file to which you |
| 1077 |
* want to write output). Functionally, the class is therefore equivalent to |
| 1078 |
* the Anything class. However, it allows to specify the <i>intent</i> of a |
| 1079 |
* parameter. The flag given to the constructor also allows to specify |
| 1080 |
* whether the file is supposed to be an input or output file. |
| 1081 |
* |
| 1082 |
* The reason for the existence of this class is to support graphical user |
| 1083 |
* interfaces for editing parameter files. These may open a file selection |
| 1084 |
* dialog if the filename is supposed to represent an input file. |
| 1085 |
*/ |
| 1086 |
class FileName : public PatternBase |
| 1087 |
{ |
| 1088 |
public: |
| 1089 |
/** |
| 1090 |
* Files can be used for input or output. This can be specified in the |
| 1091 |
* constructor by choosing the flag <tt>type</tt>. |
| 1092 |
*/ |
| 1093 |
enum FileType |
| 1094 |
{ |
| 1095 |
/** |
| 1096 |
* Open for input. |
| 1097 |
*/ |
| 1098 |
input = 0, |
| 1099 |
/** |
| 1100 |
* Open for output. |
| 1101 |
*/ |
| 1102 |
output = 1 |
| 1103 |
}; |
| 1104 |
|
| 1105 |
/** |
| 1106 |
* Constructor. The type of the file can be specified by choosing the |
| 1107 |
* flag. |
| 1108 |
*/ |
| 1109 |
FileName(const FileType type = input); |
| 1110 |
|
| 1111 |
/** |
| 1112 |
* Return <tt>true</tt> if the string matches its constraints, i.e. |
| 1113 |
* always. |
| 1114 |
*/ |
| 1115 |
virtual bool |
| 1116 |
match(const std::string &test_string) const override; |
| 1117 |
|
| 1118 |
/** |
| 1119 |
* Return a description of the pattern that valid strings are expected to |
| 1120 |
* match. Here, this is the string <tt>"[Filename]"</tt>. |
| 1121 |
*/ |
| 1122 |
virtual std::string |
| 1123 |
description(const OutputStyle style = Machine) const override; |
| 1124 |
|
| 1125 |
/** |
| 1126 |
* Return a copy of the present object, which is newly allocated on the |
| 1127 |
* heap. Ownership of that object is transferred to the caller of this |
| 1128 |
* function. |
| 1129 |
*/ |
| 1130 |
virtual std::unique_ptr<PatternBase> |
| 1131 |
clone() const override; |
| 1132 |
|
| 1133 |
/** |
| 1134 |
* file type flag |
| 1135 |
*/ |
| 1136 |
FileType file_type; |
| 1137 |
|
| 1138 |
/** |
| 1139 |
* Create a new object if the start of description matches |
| 1140 |
* description_init. Ownership of that object is transferred to the |
| 1141 |
* caller of this function. |
| 1142 |
*/ |
| 1143 |
static std::unique_ptr<FileName> |
| 1144 |
create(const std::string &description); |
| 1145 |
|
| 1146 |
private: |
| 1147 |
/** |
| 1148 |
* Initial part of description |
| 1149 |
*/ |
| 1150 |
static const char *description_init; |
| 1151 |
}; |
| 1152 |
|
| 1153 |
|
| 1154 |
/** |
| 1155 |
* A pattern that can be used to indicate when a parameter is intended to be |
| 1156 |
* the name of a directory. By itself, this class does not check whether the |
| 1157 |
* string that is given in a parameter file actually corresponds to an |
| 1158 |
* existing directory. Functionally, the class is therefore equivalent to |
| 1159 |
* the Anything class. However, it allows to specify the <i>intent</i> of a |
| 1160 |
* parameter. |
| 1161 |
* |
| 1162 |
* The reason for the existence of this class is to support graphical user |
| 1163 |
* interfaces for editing parameter files. These may open a file selection |
| 1164 |
* dialog to select or create a directory. |
| 1165 |
*/ |
| 1166 |
class DirectoryName : public PatternBase |
| 1167 |
{ |
| 1168 |
public: |
| 1169 |
/** |
| 1170 |
* Constructor. |
| 1171 |
*/ |
| 1172 |
DirectoryName() = default; |
| 1173 |
|
| 1174 |
/** |
| 1175 |
* Return <tt>true</tt> if the string matches its constraints, i.e. |
| 1176 |
* always. |
| 1177 |
*/ |
| 1178 |
virtual bool |
| 1179 |
match(const std::string &test_string) const override; |
| 1180 |
|
| 1181 |
/** |
| 1182 |
* Return a description of the pattern that valid strings are expected to |
| 1183 |
* match. Here, this is the string <tt>"[Filename]"</tt>. |
| 1184 |
*/ |
| 1185 |
virtual std::string |
| 1186 |
description(const OutputStyle style = Machine) const override; |
| 1187 |
|
| 1188 |
/** |
| 1189 |
* Return a copy of the present object, which is newly allocated on the |
| 1190 |
* heap. Ownership of that object is transferred to the caller of this |
| 1191 |
* function. |
| 1192 |
*/ |
| 1193 |
virtual std::unique_ptr<PatternBase> |
| 1194 |
clone() const override; |
| 1195 |
|
| 1196 |
/** |
| 1197 |
* Create a new object if the start of description matches |
| 1198 |
* description_init. Ownership of that object is transferred to the |
| 1199 |
* caller of this function. |
| 1200 |
*/ |
| 1201 |
static std::unique_ptr<DirectoryName> |
| 1202 |
create(const std::string &description); |
| 1203 |
|
| 1204 |
private: |
| 1205 |
/** |
| 1206 |
* Initial part of description |
| 1207 |
*/ |
| 1208 |
static const char *description_init; |
| 1209 |
}; |
| 1210 |
|
| 1211 |
|
| 1212 |
/** |
| 1213 |
* Namespace for a few classes and functions that act on values and patterns, |
| 1214 |
* and allow to convert from non elementary types to strings and vice versa. |
| 1215 |
* |
| 1216 |
* A typical usage of these tools is in the following example: |
| 1217 |
* |
| 1218 |
* @code |
| 1219 |
* using T = std::vector<unsigned int>; |
| 1220 |
* |
| 1221 |
* T vec(3); |
| 1222 |
* vec[0] = 1; |
| 1223 |
* vec[1] = 3; |
| 1224 |
* vec[2] = 5; |
| 1225 |
* |
| 1226 |
* auto pattern = Patterns::Tools::Convert<T>::to_pattern(); |
| 1227 |
* |
| 1228 |
* std::cout << pattern->description() << std::endl; |
| 1229 |
* // [List of <[Integer]> of length 0...4294967295 (inclusive)] |
| 1230 |
* |
| 1231 |
* auto s = Patterns::Tools::Convert<T>::to_string(vec); |
| 1232 |
* std::cout << s << std::endl; |
| 1233 |
* // 1, 2, 3 |
| 1234 |
* |
| 1235 |
* auto vec = Patterns::Tools::Convert<T>::to_value("2,3,4,5"); |
| 1236 |
* // now vec has size 4, and contains the elements 2,3,4,5 |
| 1237 |
* |
| 1238 |
* std::cout << internal::RankInfo<T>::list_rank << std::endl; // Outputs 1 |
| 1239 |
* std::cout << internal::RankInfo<T>::map_rank << std::endl; // Outputs 0 |
| 1240 |
* @endcode |
| 1241 |
* |
| 1242 |
* Convert<T> is used by the function Patterns::Tools::add_parameter() in this |
| 1243 |
* namespace. Internally it uses the internal::RankInfo<T> class to decide how |
| 1244 |
* many different separators are required to convert the given type to a |
| 1245 |
* string. |
| 1246 |
* |
| 1247 |
* For example, to write vectors of vectors, the default is to use "," for the |
| 1248 |
* first (inner) separator, and ";" for the second (outer) separator, i.e. |
| 1249 |
* |
| 1250 |
* @code |
| 1251 |
* std::vector<std::vector<unsigned int>> vec; |
| 1252 |
* vec = Convert<decltype(vec)>::to_value("1,2,3 ; 4,5,6"); |
| 1253 |
* |
| 1254 |
* s = convert<decltype(vec[0])>::to_string(vec[0]); |
| 1255 |
* // s now contains the string "1,2,3" |
| 1256 |
* @endcode |
| 1257 |
* |
| 1258 |
* Separators for Patterns::List and Patterns::Map compatible types are |
| 1259 |
* selected according to the |
| 1260 |
* rank of the list and map objects, using the arrays |
| 1261 |
* Patterns::Tools::internal::default_list_separator and |
| 1262 |
* Patterns::Tools::internal::default_map_separator. |
| 1263 |
* |
| 1264 |
* They are currently set to: |
| 1265 |
* |
| 1266 |
* @code |
| 1267 |
* default_list_separator{{"," , ";" , "|" , "%"}}; |
| 1268 |
* default_map_separator {{":" , "=" , "@" , "#"}}; |
| 1269 |
* @endcode |
| 1270 |
* |
| 1271 |
* When one needs a mixture of Patterns::List and Patterns::Map types, their |
| 1272 |
* RankInfo is computed by taking the maximum of the vector_rank of the Key |
| 1273 |
* and of the Value type, so that, for example, it is possible to have the |
| 1274 |
* following |
| 1275 |
* @code |
| 1276 |
* ... // Build compare class |
| 1277 |
* std::map<std::vector<unsigned int>, std::vector<double>, compare> map; |
| 1278 |
* |
| 1279 |
* map = convert<decltype(map)>::to_value( |
| 1280 |
* "1,2,3 : 5.0,6.0,7.0 ; 8,9,10 : 11.0,12.0,13.0"); |
| 1281 |
* |
| 1282 |
* @endcode |
| 1283 |
* |
| 1284 |
* Some non elementary types are supported, like Point(), or |
| 1285 |
* std::complex<double>. If you wish to support more types, you have to |
| 1286 |
* specialize the Convert struct as well as the RankInfo struct. |
| 1287 |
* |
| 1288 |
* @ingroup input |
| 1289 |
*/ |
| 1290 |
namespace Tools |
| 1291 |
{ |
| 1292 |
/** |
| 1293 |
* Converter class. This class is used to generate strings and Patterns |
| 1294 |
* associated to the given type, and to convert from a string to the given |
| 1295 |
* type and vice versa. |
| 1296 |
* |
| 1297 |
* The second template parameter is used internally to allow for advanced |
| 1298 |
* SFINAE (substitution failure is not an error) tricks used to specialise |
| 1299 |
* this class for arbitrary STL containers and maps. |
| 1300 |
*/ |
| 1301 |
template <class T, class Enable = void> |
| 1302 |
struct Convert |
| 1303 |
{ |
| 1304 |
/** |
| 1305 |
* Return a std::unique_ptr to a Pattern that can be used to interpret a |
| 1306 |
* string as the type of the template argument, and the other way around. |
| 1307 |
* |
| 1308 |
* While the current function (in the general Convert template) is |
| 1309 |
* deleted, it is implemented and available in the specializations of the |
| 1310 |
* Convert |
| 1311 |
* class template for particular kinds of template arguments @p T. |
| 1312 |
*/ |
| 1313 |
static std::unique_ptr<Patterns::PatternBase> |
| 1314 |
to_pattern() = delete; |
| 1315 |
|
| 1316 |
/** |
| 1317 |
* Return a string containing a textual version of the variable s. Use the |
| 1318 |
* pattern passed to perform the conversion, or create and use a default |
| 1319 |
* one. |
| 1320 |
* |
| 1321 |
* While the current function (in the general Convert template) is |
| 1322 |
* deleted, it is implemented and available in the specializations of the |
| 1323 |
* Convert |
| 1324 |
* class template for particular kinds of template arguments @p T. |
| 1325 |
*/ |
| 1326 |
static std::string |
| 1327 |
to_string(const T &s, |
| 1328 |
const Patterns::PatternBase &p = *Convert<T>::to_pattern()) = |
| 1329 |
delete; |
| 1330 |
|
| 1331 |
/** |
| 1332 |
* Convert a string to a value, using the given pattern. Use the pattern |
| 1333 |
* passed to perform the conversion, or create and use a default one. |
| 1334 |
* |
| 1335 |
* While the current function (in the general Convert template) is |
| 1336 |
* deleted, it is implemented and available in the specializations of the |
| 1337 |
* Convert |
| 1338 |
* class template for particular kinds of template arguments @p T. |
| 1339 |
*/ |
| 1340 |
static T |
| 1341 |
to_value(const std::string &s, |
| 1342 |
const Patterns::PatternBase &p = *Convert<T>::to_pattern()) = |
| 1343 |
delete; |
| 1344 |
}; |
| 1345 |
|
| 1346 |
/** |
| 1347 |
* A utility function that simplifies the conversion to strings of |
| 1348 |
* arbitrarily complex types. |
| 1349 |
* |
| 1350 |
* This function calls the method Convert<T>::to_string() with the default |
| 1351 |
* pattern. An example usage is the following: |
| 1352 |
* |
| 1353 |
* @code |
| 1354 |
* auto t = std::make_tuple(1.0, std::make_pair(1, "ciao")); |
| 1355 |
* auto s = Patterns::Tools::to_string(t); |
| 1356 |
* |
| 1357 |
* std::cout << s; // will print "1 % 1 : ciao"" |
| 1358 |
* @endcode |
| 1359 |
* |
| 1360 |
* See the documentation of the class Patterns::Tools::Convert, and of the |
| 1361 |
* helper class Patterns::Tools::RankInfo for details on the way separators |
| 1362 |
* are selected when outputting STL container types. |
| 1363 |
*/ |
| 1364 |
template <typename T> |
| 1365 |
std::string |
| 1366 |
to_string(const T &t); |
| 1367 |
|
| 1368 |
/** |
| 1369 |
* A utility function that simplifies the conversion from strings to |
| 1370 |
* arbitrary types. |
| 1371 |
* |
| 1372 |
* This function calls the method Convert<T>::to_value() with the default |
| 1373 |
* pattern. An example usage is the following: |
| 1374 |
* |
| 1375 |
* @code |
| 1376 |
* auto t = std::make_tuple(1.0, std::make_pair(1, "ciao")); |
| 1377 |
* // replace the value of 't' by the parsed content of the string argument: |
| 1378 |
* Patterns::Tools::to_value("2 % 3 : mondo", t); |
| 1379 |
* |
| 1380 |
* auto s = Patterns::Tools::to_string(t); |
| 1381 |
* std::cout << s; // will print "2 % 3 : mondo"" |
| 1382 |
* @endcode |
| 1383 |
* |
| 1384 |
* See the documentation of the class Patterns::Tools::Convert, and of the |
| 1385 |
* helper class Patterns::Tools::RankInfo for details on the separators you |
| 1386 |
* should use in your string patterns when converting from a string to a |
| 1387 |
* container type. |
| 1388 |
* |
| 1389 |
* Notice that the current content of variable @p t is ignored. Its type is |
| 1390 |
* used to infer how to interpret the string. If the string is successfully |
| 1391 |
* parsed, then @p t will be set to the parsed content of @p s. |
| 1392 |
*/ |
| 1393 |
template <typename T> |
| 1394 |
void |
| 1395 |
to_value(const std::string &s, T &t); |
| 1396 |
|
| 1397 |
/** |
| 1398 |
* @addtogroup Exceptions |
| 1399 |
* @{ |
| 1400 |
*/ |
| 1401 |
|
| 1402 |
/** |
| 1403 |
* Exception. |
| 1404 |
*/ |
| 1405 |
DeclException2(ExcNoMatch, |
| 1406 |
std::string, |
| 1407 |
std::string, |
| 1408 |
<< "The string \"" << arg1 |
| 1409 |
<< "\" does not match the pattern \"" << arg2 << "\""); |
| 1410 |
/** @} */ |
| 1411 |
} // namespace Tools |
| 1412 |
} // namespace Patterns |
| 1413 |
|
| 1414 |
|
| 1415 |
// ---------------------- inline and template functions -------------------- |
| 1416 |
namespace Patterns |
| 1417 |
{ |
| 1418 |
template <class... PatternTypes> |
| 1419 |
Tuple::Tuple(const char *separator, const PatternTypes &...ps) |
| 1420 |
: // forward to the version with std::string argument |
| 1421 |
Tuple(std::string(separator), ps...) |
| 1422 |
{} |
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
template <class... PatternTypes> |
| 1427 |
Tuple::Tuple(const std::string &separator, const PatternTypes &...ps) |
| 1428 |
: separator(separator) |
| 1429 |
{ |
| 1430 |
static_assert(is_base_of_all<PatternBase, PatternTypes...>::value, |
| 1431 |
"Not all of the input arguments of this function " |
| 1432 |
"are derived from PatternBase"); |
| 1433 |
static_assert(sizeof...(ps) > 0, |
| 1434 |
"The number of PatternTypes must be greater than zero!"); |
| 1435 |
const auto pattern_pointers = {(static_cast<const PatternBase *>(&ps))...}; |
| 1436 |
for (const auto p : pattern_pointers) |
| 1437 |
patterns.push_back(p->clone()); |
| 1438 |
} |
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
template <class... PatternTypes> |
| 1443 |
Tuple::Tuple(const PatternTypes &...ps) |
| 1444 |
: // forward to the version with the separator argument |
| 1445 |
Tuple(":", ps...) |
| 1446 |
{} |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
namespace Tools |
| 1451 |
{ |
| 1452 |
namespace internal |
| 1453 |
{ |
| 1454 |
/** |
| 1455 |
* Store information about the rank types of the given class. |
| 1456 |
* |
| 1457 |
* A class has Rank equal to the number of different separators |
| 1458 |
* that are required to uniquely identify its element(s) in a string. |
| 1459 |
* |
| 1460 |
* This class is used to detect whether the class T is compatible |
| 1461 |
* with a Patterns::List pattern or with a Patterns::Map pattern. |
| 1462 |
* |
| 1463 |
* Objects like Point() or std::complex<double> are vector-likes, and |
| 1464 |
* have vector_rank 1. Elementary types, like `int`, `unsigned int`, |
| 1465 |
* `double`, etc. have vector_rank 0. `std::vector`, `std::list` and in |
| 1466 |
* general containers have rank equal to 1 + vector_rank of the contained |
| 1467 |
* type. Similarly for map types. |
| 1468 |
* |
| 1469 |
* A class with list_rank::value = 0 is either elementary or a |
| 1470 |
* map. A class with map_rank::value = 0 is either a List compatible |
| 1471 |
* class, or an elementary type. |
| 1472 |
* |
| 1473 |
* Elementary types are not compatible with Patterns::List, but non |
| 1474 |
* elementary types, like Point(), or std::complex<double>, are compatible |
| 1475 |
* with the List type. Adding more compatible types is a matter of adding |
| 1476 |
* a specialization of this struct for the given type. |
| 1477 |
*/ |
| 1478 |
template <class T, class Enable = void> |
| 1479 |
struct RankInfo |
| 1480 |
{ |
| 1481 |
static constexpr int list_rank = 0; |
| 1482 |
static constexpr int map_rank = 0; |
| 1483 |
}; |
| 1484 |
} // namespace internal |
| 1485 |
|
| 1486 |
// Arithmetic types |
| 1487 |
template <class T> |
| 1488 |
struct Convert<T, std::enable_if_t<std::is_arithmetic_v<T>>> |
| 1489 |
{ |
| 1490 |
template <typename Dummy = T> |
| 1491 |
static std::enable_if_t<std::is_same_v<Dummy, T> && |
| 1492 |
std::is_same_v<T, bool>, |
| 1493 |
std::unique_ptr<Patterns::PatternBase>> |
| 1494 |
to_pattern() |
| 1495 |
{ |
| 1496 |
return std::make_unique<Patterns::Bool>(); |
| 1497 |
} |
| 1498 |
|
| 1499 |
template <typename Dummy = T> |
| 1500 |
static std::enable_if_t<std::is_same_v<Dummy, T> && |
| 1501 |
!std::is_same_v<T, bool> && |
| 1502 |
std::is_integral_v<T>, |
| 1503 |
std::unique_ptr<Patterns::PatternBase>> |
| 1504 |
to_pattern() |
| 1505 |
{ |
| 1506 |
return std::make_unique<Patterns::Integer>( |
| 1507 |
std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max()); |
| 1508 |
} |
| 1509 |
|
| 1510 |
template <typename Dummy = T> |
| 1511 |
static std::enable_if_t<std::is_same_v<Dummy, T> && |
| 1512 |
!std::is_same_v<T, bool> && |
| 1513 |
std::is_floating_point_v<T>, |
| 1514 |
std::unique_ptr<Patterns::PatternBase>> |
| 1515 |
to_pattern() |
| 1516 |
{ |
| 1517 |
return std::make_unique<Patterns::Double>( |
| 1518 |
std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max()); |
| 1519 |
} |
| 1520 |
|
| 1521 |
static std::string |
| 1522 |
to_string(const T &value, |
| 1523 |
const Patterns::PatternBase &p = *Convert<T>::to_pattern()) |
| 1524 |
{ |
| 1525 |
std::stringstream str; |
| 1526 |
if (std::is_same_v<T, unsigned char> || |
| 1527 |
std::is_same_v<T, signed char> || std::is_same_v<T, char>) |
| 1528 |
str << static_cast<int>(value); |
| 1529 |
else if (std::is_same_v<T, bool>) |
| 1530 |
str << (static_cast<bool>(value) ? "true" : "false"); |
| 1531 |
else |
| 1532 |
str << value; |
| 1533 |
AssertThrow(p.match(str.str()), ExcNoMatch(str.str(), p.description())); |
| 1534 |
return str.str(); |
| 1535 |
} |
| 1536 |
|
| 1537 |
static T |
| 1538 |
to_value(const std::string &s, |
| 1539 |
const Patterns::PatternBase &p = *Convert<T>::to_pattern()) |
| 1540 |
{ |
| 1541 |
AssertThrow(p.match(s), ExcNoMatch(s, p.description())); |
| 1542 |
T value; |
| 1543 |
if (std::is_same_v<T, bool>) |
| 1544 |
value = (s == "true"); |
| 1545 |
else |
| 1546 |
{ |
| 1547 |
std::istringstream is(s); |
| 1548 |
if (std::is_same_v<T, unsigned char> || |
| 1549 |
std::is_same_v<T, signed char> || std::is_same_v<T, char>) |
| 1550 |
{ |
| 1551 |
int i; |
| 1552 |
is >> i; |
| 1553 |
value = i; |
| 1554 |
} |
| 1555 |
else |
| 1556 |
is >> value; |
| 1557 |
|
| 1558 |
// If someone passes "123 abc" to the function, the method yields an |
| 1559 |
// integer 123 alright, but the space terminates the read from the |
| 1560 |
// string although there is more to come. This case, however, is |
| 1561 |
// checked for in the call p->match(s) at the beginning of this |
| 1562 |
// function, and would throw earlier. Here it is safe to assume that |
| 1563 |
// if we didn't fail the conversion with the operator >>, then we |
| 1564 |
// are good to go. |
| 1565 |
AssertThrow( |
| 1566 |
!is.fail(), |
| 1567 |
ExcMessage("Failed to convert from \"" + s + "\" to the type \"" + |
| 1568 |
boost::core::demangle(typeid(T).name()) + "\"")); |
| 1569 |
} |
| 1570 |
return value; |
| 1571 |
} |
| 1572 |
}; |
| 1573 |
|
| 1574 |
namespace internal |
| 1575 |
{ |
| 1576 |
constexpr std::array<const char *, 4> default_list_separator{ |
| 1577 |
{",", ";", "|", "%"}}; |
| 1578 |
constexpr std::array<const char *, 4> default_map_separator{ |
| 1579 |
{":", "=", "@", "#"}}; |
| 1580 |
|
| 1581 |
// specialize a type for all of the STL containers and maps |
| 1582 |
template <typename T> |
| 1583 |
struct is_list_compatible : std::false_type |
| 1584 |
{}; |
| 1585 |
template <typename... Args> |
| 1586 |
struct is_list_compatible<std::vector<Args...>> : std::true_type |
| 1587 |
{}; |
| 1588 |
template <typename... Args> |
| 1589 |
struct is_list_compatible<std::deque<Args...>> : std::true_type |
| 1590 |
{}; |
| 1591 |
template <typename... Args> |
| 1592 |
struct is_list_compatible<std::list<Args...>> : std::true_type |
| 1593 |
{}; |
| 1594 |
template <typename... Args> |
| 1595 |
struct is_list_compatible<std::set<Args...>> : std::true_type |
| 1596 |
{}; |
| 1597 |
template <typename... Args> |
| 1598 |
struct is_list_compatible<std::multiset<Args...>> : std::true_type |
| 1599 |
{}; |
| 1600 |
template <typename... Args> |
| 1601 |
struct is_list_compatible<std::unordered_set<Args...>> : std::true_type |
| 1602 |
{}; |
| 1603 |
template <typename... Args> |
| 1604 |
struct is_list_compatible<std::unordered_multiset<Args...>> |
| 1605 |
: std::true_type |
| 1606 |
{}; |
| 1607 |
|
| 1608 |
template <typename T> |
| 1609 |
struct is_map_compatible : std::false_type |
| 1610 |
{}; |
| 1611 |
template <class Key, class T, class Compare, class Allocator> |
| 1612 |
struct is_map_compatible<std::map<Key, T, Compare, Allocator>> |
| 1613 |
: std::true_type |
| 1614 |
{}; |
| 1615 |
template <class Key, class T, class Compare, class Allocator> |
| 1616 |
struct is_map_compatible<std::multimap<Key, T, Compare, Allocator>> |
| 1617 |
: std::true_type |
| 1618 |
{}; |
| 1619 |
template <class Key, class T, class Hash, class KeyEqual, class Allocator> |
| 1620 |
struct is_map_compatible< |
| 1621 |
std::unordered_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type |
| 1622 |
{}; |
| 1623 |
template <class Key, class T, class Hash, class KeyEqual, class Allocator> |
| 1624 |
struct is_map_compatible< |
| 1625 |
std::unordered_multimap<Key, T, Hash, KeyEqual, Allocator>> |
| 1626 |
: std::true_type |
| 1627 |
{}; |
| 1628 |
} // namespace internal |
| 1629 |
|
| 1630 |
// type trait to use the implementation type traits as well as decay the |
| 1631 |
// type |
| 1632 |
template <typename T> |
| 1633 |
struct is_list_compatible |
| 1634 |
{ |
| 1635 |
static constexpr bool value = |
| 1636 |
internal::is_list_compatible<std::decay_t<T>>::value; |
| 1637 |
}; |
| 1638 |
|
| 1639 |
template <typename T> |
| 1640 |
struct is_map_compatible |
| 1641 |
{ |
| 1642 |
static constexpr bool value = |
| 1643 |
internal::is_map_compatible<std::decay_t<T>>::value; |
| 1644 |
}; |
| 1645 |
|
| 1646 |
namespace internal |
| 1647 |
{ |
| 1648 |
// Helper function for list_rank |
| 1649 |
template <class T> |
| 1650 |
constexpr int |
| 1651 |
max_list_rank() |
| 1652 |
{ |
| 1653 |
return RankInfo<T>::list_rank; |
| 1654 |
} |
| 1655 |
|
| 1656 |
template <class T1, class T2, class... Types> |
| 1657 |
constexpr int |
| 1658 |
max_list_rank() |
| 1659 |
{ |
| 1660 |
return std::max(RankInfo<T1>::list_rank, max_list_rank<T2, Types...>()); |
| 1661 |
} |
| 1662 |
|
| 1663 |
// Helper function for map_rank |
| 1664 |
template <class T> |
| 1665 |
constexpr int |
| 1666 |
max_map_rank() |
| 1667 |
{ |
| 1668 |
return RankInfo<T>::map_rank; |
| 1669 |
} |
| 1670 |
|
| 1671 |
template <class T1, class T2, class... Types> |
| 1672 |
constexpr int |
| 1673 |
max_map_rank() |
| 1674 |
{ |
| 1675 |
return std::max(RankInfo<T1>::map_rank, max_map_rank<T2, Types...>()); |
| 1676 |
} |
| 1677 |
|
| 1678 |
// Rank of vector types |
| 1679 |
template <class T> |
| 1680 |
struct RankInfo<T, std::enable_if_t<is_list_compatible<T>::value>> |
| 1681 |
{ |
| 1682 |
static constexpr int list_rank = |
| 1683 |
RankInfo<typename T::value_type>::list_rank + 1; |
| 1684 |
static constexpr int map_rank = |
| 1685 |
RankInfo<typename T::value_type>::map_rank; |
| 1686 |
}; |
| 1687 |
|
| 1688 |
// Rank of map types |
| 1689 |
template <class T> |
| 1690 |
struct RankInfo<T, std::enable_if_t<is_map_compatible<T>::value>> |
| 1691 |
{ |
| 1692 |
static constexpr int list_rank = |
| 1693 |
max_list_rank<typename T::key_type, typename T::mapped_type>() + 1; |
| 1694 |
static constexpr int map_rank = |
| 1695 |
max_map_rank<typename T::key_type, typename T::mapped_type>() + 1; |
| 1696 |
}; |
| 1697 |
|
| 1698 |
// Rank of Tensor types |
| 1699 |
template <int rank, int dim, class Number> |
| 1700 |
struct RankInfo<Tensor<rank, dim, Number>> |
| 1701 |
{ |
| 1702 |
static constexpr int list_rank = rank + RankInfo<Number>::list_rank; |
| 1703 |
static constexpr int map_rank = RankInfo<Number>::map_rank; |
| 1704 |
}; |
| 1705 |
|
| 1706 |
template <int dim, class Number> |
| 1707 |
struct RankInfo<Point<dim, Number>> : RankInfo<Tensor<1, dim, Number>> |
| 1708 |
{}; |
| 1709 |
|
| 1710 |
// Rank of complex types |
| 1711 |
template <class Number> |
| 1712 |
struct RankInfo<std::complex<Number>> |
| 1713 |
{ |
| 1714 |
static constexpr int list_rank = RankInfo<Number>::list_rank + 1; |
| 1715 |
static constexpr int map_rank = RankInfo<Number>::map_rank; |
| 1716 |
}; |
| 1717 |
|
| 1718 |
// Rank of FunctionParser |
| 1719 |
template <int dim> |
| 1720 |
struct RankInfo<std::unique_ptr<FunctionParser<dim>>> |
| 1721 |
{ |
| 1722 |
static constexpr int list_rank = 1; |
| 1723 |
static constexpr int map_rank = 0; |
| 1724 |
}; |
| 1725 |
|
| 1726 |
// Rank of ComponentMask |
| 1727 |
template <> |
| 1728 |
struct RankInfo<ComponentMask> |
| 1729 |
{ |
| 1730 |
static constexpr int list_rank = 1; |
| 1731 |
static constexpr int map_rank = 0; |
| 1732 |
}; |
| 1733 |
|
| 1734 |
// Rank of std::pair |
| 1735 |
template <class Key, class Value> |
| 1736 |
struct RankInfo<std::pair<Key, Value>> |
| 1737 |
{ |
| 1738 |
static constexpr int list_rank = |
| 1739 |
std::max(RankInfo<Key>::list_rank, RankInfo<Value>::list_rank); |
| 1740 |
static constexpr int map_rank = |
| 1741 |
std::max(RankInfo<Key>::map_rank, RankInfo<Value>::map_rank) + 1; |
| 1742 |
}; |
| 1743 |
|
| 1744 |
// Rank of std::tuple types |
| 1745 |
template <class... Types> |
| 1746 |
struct RankInfo<std::tuple<Types...>> |
| 1747 |
{ |
| 1748 |
static constexpr int list_rank = max_list_rank<Types...>(); |
| 1749 |
static constexpr int map_rank = max_map_rank<Types...>() + 1; |
| 1750 |
}; |
| 1751 |
|
| 1752 |
// Rank of std::array types |
| 1753 |
template <class T, std::size_t N> |
| 1754 |
struct RankInfo<std::array<T, N>> |
| 1755 |
{ |
| 1756 |
static constexpr int list_rank = RankInfo<T>::list_rank + 1; |
| 1757 |
static constexpr int map_rank = RankInfo<T>::map_rank; |
| 1758 |
}; |
| 1759 |
} // namespace internal |
| 1760 |
|
| 1761 |
// stl containers |
| 1762 |
template <class T> |
| 1763 |
struct Convert<T, std::enable_if_t<is_list_compatible<T>::value>> |
| 1764 |
{ |
| 1765 |
static std::unique_ptr<Patterns::PatternBase> |
| 1766 |
to_pattern() |
| 1767 |
{ |
| 1768 |
static_assert(internal::RankInfo<T>::list_rank > 0, |
| 1769 |
"Cannot use this class for non List-compatible types."); |
| 1770 |
return std::make_unique<Patterns::List>( |
| 1771 |
*Convert<typename T::value_type>::to_pattern(), |
| 1772 |
0, |
| 1773 |
std::numeric_limits<unsigned int>::max(), |
| 1774 |
internal::default_list_separator[internal::RankInfo<T>::list_rank - |
| 1775 |
1]); |
| 1776 |
} |
| 1777 |
|
| 1778 |
static std::string |
| 1779 |
to_string( |
| 1780 |
const T &t, |
| 1781 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 1782 |
{ |
| 1783 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 1784 |
AssertThrow(p, |
| 1785 |
ExcMessage("I need a List pattern to convert a " |
| 1786 |
"string to a List type.")); |
| 1787 |
auto base_p = p->get_base_pattern().clone(); |
| 1788 |
std::vector<std::string> vec(t.size()); |
| 1789 |
|
| 1790 |
std::transform( |
| 1791 |
t.cbegin(), t.cend(), vec.begin(), [&base_p](const auto &entry) { |
| 1792 |
return Convert<typename T::value_type>::to_string(entry, *base_p); |
| 1793 |
}); |
| 1794 |
|
| 1795 |
std::string s; |
| 1796 |
if (vec.size() > 0) |
| 1797 |
s = vec[0]; |
| 1798 |
for (unsigned int i = 1; i < vec.size(); ++i) |
| 1799 |
s += p->get_separator() + " " + vec[i]; |
| 1800 |
|
| 1801 |
AssertThrow(pattern.match(s), ExcNoMatch(s, p->description())); |
| 1802 |
return s; |
| 1803 |
} |
| 1804 |
|
| 1805 |
static T |
| 1806 |
to_value(const std::string &s, |
| 1807 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 1808 |
{ |
| 1809 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 1810 |
|
| 1811 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 1812 |
AssertThrow(p, |
| 1813 |
ExcMessage("I need a List pattern to convert a string " |
| 1814 |
"to a List type.")); |
| 1815 |
|
| 1816 |
auto base_p = p->get_base_pattern().clone(); |
| 1817 |
T t; |
| 1818 |
|
| 1819 |
auto v = Utilities::split_string_list(s, p->get_separator()); |
| 1820 |
for (const auto &str : v) |
| 1821 |
t.insert(t.end(), |
| 1822 |
Convert<typename T::value_type>::to_value(str, *base_p)); |
| 1823 |
|
| 1824 |
return t; |
| 1825 |
} |
| 1826 |
}; |
| 1827 |
|
| 1828 |
// stl maps |
| 1829 |
template <class T> |
| 1830 |
struct Convert<T, std::enable_if_t<is_map_compatible<T>::value>> |
| 1831 |
{ |
| 1832 |
static std::unique_ptr<Patterns::PatternBase> |
| 1833 |
to_pattern() |
| 1834 |
{ |
| 1835 |
static_assert(internal::RankInfo<T>::list_rank > 0, |
| 1836 |
"Cannot use this class for non List-compatible types."); |
| 1837 |
static_assert(internal::RankInfo<T>::map_rank > 0, |
| 1838 |
"Cannot use this class for non Map-compatible types."); |
| 1839 |
return std::make_unique<Patterns::Map>( |
| 1840 |
*Convert<typename T::key_type>::to_pattern(), |
| 1841 |
*Convert<typename T::mapped_type>::to_pattern(), |
| 1842 |
0, |
| 1843 |
std::numeric_limits<unsigned int>::max(), |
| 1844 |
internal::default_list_separator[internal::RankInfo<T>::list_rank - |
| 1845 |
1], |
| 1846 |
internal::default_map_separator[internal::RankInfo<T>::map_rank - 1]); |
| 1847 |
} |
| 1848 |
|
| 1849 |
static std::string |
| 1850 |
to_string( |
| 1851 |
const T &t, |
| 1852 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 1853 |
{ |
| 1854 |
const auto *p = dynamic_cast<const Patterns::Map *>(&pattern); |
| 1855 |
AssertThrow(p, |
| 1856 |
ExcMessage("I need a Map pattern to convert a string to " |
| 1857 |
"a Map compatible type.")); |
| 1858 |
auto key_p = p->get_key_pattern().clone(); |
| 1859 |
auto val_p = p->get_value_pattern().clone(); |
| 1860 |
std::vector<std::string> vec(t.size()); |
| 1861 |
|
| 1862 |
unsigned int i = 0; |
| 1863 |
for (const auto &ti : t) |
| 1864 |
vec[i++] = |
| 1865 |
Convert<typename T::key_type>::to_string(ti.first, *key_p) + |
| 1866 |
p->get_key_value_separator() + |
| 1867 |
Convert<typename T::mapped_type>::to_string(ti.second, *val_p); |
| 1868 |
|
| 1869 |
std::string s; |
| 1870 |
if (vec.size() > 0) |
| 1871 |
s = vec[0]; |
| 1872 |
for (unsigned int i = 1; i < vec.size(); ++i) |
| 1873 |
s += p->get_separator() + " " + vec[i]; |
| 1874 |
|
| 1875 |
AssertThrow(p->match(s), ExcNoMatch(s, p->description())); |
| 1876 |
return s; |
| 1877 |
} |
| 1878 |
|
| 1879 |
static T |
| 1880 |
to_value(const std::string &s, |
| 1881 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 1882 |
{ |
| 1883 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 1884 |
|
| 1885 |
const auto *p = dynamic_cast<const Patterns::Map *>(&pattern); |
| 1886 |
AssertThrow(p, |
| 1887 |
ExcMessage("I need a Map pattern to convert a " |
| 1888 |
"string to a Map compatible type.")); |
| 1889 |
|
| 1890 |
auto key_p = p->get_key_pattern().clone(); |
| 1891 |
auto val_p = p->get_value_pattern().clone(); |
| 1892 |
T t; |
| 1893 |
|
| 1894 |
auto v = Utilities::split_string_list(s, p->get_separator()); |
| 1895 |
for (const auto &str : v) |
| 1896 |
{ |
| 1897 |
auto key_val = |
| 1898 |
Utilities::split_string_list(str, p->get_key_value_separator()); |
| 1899 |
AssertDimension(key_val.size(), 2); |
| 1900 |
t.insert(std::make_pair( |
| 1901 |
Convert<typename T::key_type>::to_value(key_val[0], *key_p), |
| 1902 |
Convert<typename T::mapped_type>::to_value(key_val[1], *val_p))); |
| 1903 |
} |
| 1904 |
|
| 1905 |
return t; |
| 1906 |
} |
| 1907 |
}; |
| 1908 |
|
| 1909 |
// std::array |
| 1910 |
template <typename ValueType, std::size_t N> |
| 1911 |
struct Convert<std::array<ValueType, N>> |
| 1912 |
{ |
| 1913 |
using T = std::array<ValueType, N>; |
| 1914 |
|
| 1915 |
static std::unique_ptr<Patterns::PatternBase> |
| 1916 |
to_pattern() |
| 1917 |
{ |
| 1918 |
static_assert(internal::RankInfo<T>::list_rank > 0, |
| 1919 |
"Cannot use this class for non List-compatible types."); |
| 1920 |
return std::make_unique<Patterns::List>( |
| 1921 |
*Convert<typename T::value_type>::to_pattern(), |
| 1922 |
N, |
| 1923 |
N, |
| 1924 |
internal::default_list_separator[internal::RankInfo<T>::list_rank - |
| 1925 |
1]); |
| 1926 |
} |
| 1927 |
|
| 1928 |
static std::string |
| 1929 |
to_string( |
| 1930 |
const T &t, |
| 1931 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 1932 |
{ |
| 1933 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 1934 |
AssertThrow(p, |
| 1935 |
ExcMessage("I need a List pattern to convert a " |
| 1936 |
"string to a std::array.")); |
| 1937 |
auto base_p = p->get_base_pattern().clone(); |
| 1938 |
std::vector<std::string> vec(t.size()); |
| 1939 |
|
| 1940 |
std::transform( |
| 1941 |
t.cbegin(), t.cend(), vec.begin(), [&base_p](const auto &entry) { |
| 1942 |
return Convert<typename T::value_type>::to_string(entry, *base_p); |
| 1943 |
}); |
| 1944 |
|
| 1945 |
std::string s; |
| 1946 |
if (vec.size() > 0) |
| 1947 |
s = vec[0]; |
| 1948 |
for (unsigned int i = 1; i < vec.size(); ++i) |
| 1949 |
s += p->get_separator() + " " + vec[i]; |
| 1950 |
|
| 1951 |
AssertThrow(pattern.match(s), ExcNoMatch(s, p->description())); |
| 1952 |
return s; |
| 1953 |
} |
| 1954 |
|
| 1955 |
static T |
| 1956 |
to_value(const std::string &s, |
| 1957 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 1958 |
{ |
| 1959 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 1960 |
|
| 1961 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 1962 |
AssertThrow(p, |
| 1963 |
ExcMessage("I need a List pattern to convert a string " |
| 1964 |
"to a std::array.")); |
| 1965 |
|
| 1966 |
auto base_p = p->get_base_pattern().clone(); |
| 1967 |
T t; |
| 1968 |
|
| 1969 |
auto v = Utilities::split_string_list(s, p->get_separator()); |
| 1970 |
AssertDimension(v.size(), N); |
| 1971 |
for (unsigned int i = 0; i < N; ++i) |
| 1972 |
t[i] = Convert<typename T::value_type>::to_value(v[i], *base_p); |
| 1973 |
return t; |
| 1974 |
} |
| 1975 |
}; |
| 1976 |
|
| 1977 |
// Tensors |
| 1978 |
template <int rank, int dim, class Number> |
| 1979 |
struct Convert<Tensor<rank, dim, Number>> |
| 1980 |
{ |
| 1981 |
using T = Tensor<rank, dim, Number>; |
| 1982 |
static std::unique_ptr<Patterns::PatternBase> |
| 1983 |
to_pattern() |
| 1984 |
{ |
| 1985 |
static_assert(internal::RankInfo<T>::list_rank > 0, |
| 1986 |
"Cannot use this class for non List-compatible types."); |
| 1987 |
return std::make_unique<Patterns::List>( |
| 1988 |
*Convert<typename T::value_type>::to_pattern(), |
| 1989 |
dim, |
| 1990 |
dim, |
| 1991 |
internal::default_list_separator[internal::RankInfo<T>::list_rank - |
| 1992 |
1]); |
| 1993 |
} |
| 1994 |
|
| 1995 |
static std::string |
| 1996 |
to_string( |
| 1997 |
const T &t, |
| 1998 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 1999 |
{ |
| 2000 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 2001 |
AssertThrow(p, |
| 2002 |
ExcMessage("I need a List pattern to convert a string " |
| 2003 |
"to a List compatible type.")); |
| 2004 |
auto base_p = p->get_base_pattern().clone(); |
| 2005 |
std::vector<std::string> vec(dim); |
| 2006 |
|
| 2007 |
for (unsigned int i = 0; i < dim; ++i) |
| 2008 |
vec[i] = Convert<typename T::value_type>::to_string(t[i], *base_p); |
| 2009 |
|
| 2010 |
std::string s; |
| 2011 |
if (vec.size() > 0) |
| 2012 |
s = vec[0]; |
| 2013 |
for (unsigned int i = 1; i < vec.size(); ++i) |
| 2014 |
s += p->get_separator() + " " + vec[i]; |
| 2015 |
|
| 2016 |
AssertThrow(p->match(s), ExcNoMatch(s, p->description())); |
| 2017 |
return s; |
| 2018 |
} |
| 2019 |
|
| 2020 |
static T |
| 2021 |
to_value(const std::string &s, |
| 2022 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2023 |
{ |
| 2024 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 2025 |
|
| 2026 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 2027 |
AssertThrow(p, |
| 2028 |
ExcMessage("I need a List pattern to convert a string " |
| 2029 |
"to a List compatible type.")); |
| 2030 |
|
| 2031 |
auto base_p = p->get_base_pattern().clone(); |
| 2032 |
T t; |
| 2033 |
|
| 2034 |
auto v = Utilities::split_string_list(s, p->get_separator()); |
| 2035 |
unsigned int i = 0; |
| 2036 |
for (const auto &str : v) |
| 2037 |
t[i++] = Convert<typename T::value_type>::to_value(str, *base_p); |
| 2038 |
|
| 2039 |
return t; |
| 2040 |
} |
| 2041 |
}; |
| 2042 |
|
| 2043 |
// Points |
| 2044 |
template <int dim, class Number> |
| 2045 |
struct Convert<Point<dim, Number>> |
| 2046 |
{ |
| 2047 |
using T = Point<dim, Number>; |
| 2048 |
|
| 2049 |
static std::unique_ptr<Patterns::PatternBase> |
| 2050 |
to_pattern() |
| 2051 |
{ |
| 2052 |
return Convert<Tensor<1, dim, Number>>::to_pattern(); |
| 2053 |
} |
| 2054 |
|
| 2055 |
static std::string |
| 2056 |
to_string( |
| 2057 |
const T &t, |
| 2058 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2059 |
{ |
| 2060 |
return Convert<Tensor<1, dim, Number>>::to_string( |
| 2061 |
Tensor<1, dim, Number>(t), pattern); |
| 2062 |
} |
| 2063 |
|
| 2064 |
static T |
| 2065 |
to_value(const std::string &s, |
| 2066 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2067 |
{ |
| 2068 |
return T(Convert<Tensor<1, dim, Number>>::to_value(s, pattern)); |
| 2069 |
} |
| 2070 |
}; |
| 2071 |
|
| 2072 |
// Functions::FunctionParser |
| 2073 |
template <int dim> |
| 2074 |
struct Convert<std::unique_ptr<FunctionParser<dim>>> |
| 2075 |
{ |
| 2076 |
using T = std::unique_ptr<FunctionParser<dim>>; |
| 2077 |
|
| 2078 |
static std::unique_ptr<Patterns::PatternBase> |
| 2079 |
to_pattern() |
| 2080 |
{ |
| 2081 |
static_assert(internal::RankInfo<T>::list_rank > 0, |
| 2082 |
"Cannot use this class for non List-compatible types."); |
| 2083 |
|
| 2084 |
return std::make_unique<Patterns::List>( |
| 2085 |
Patterns::Anything(), |
| 2086 |
1, |
| 2087 |
Patterns::List::max_int_value, |
| 2088 |
internal::default_list_separator[internal::RankInfo<T>::list_rank - |
| 2089 |
1]); |
| 2090 |
} |
| 2091 |
|
| 2092 |
static std::string |
| 2093 |
to_string( |
| 2094 |
const T &t, |
| 2095 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2096 |
{ |
| 2097 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 2098 |
AssertThrow(p, |
| 2099 |
ExcMessage("I need a List pattern to convert a string " |
| 2100 |
"to a List compatible type.")); |
| 2101 |
|
| 2102 |
const auto &expressions = t->get_expressions(); |
| 2103 |
if (expressions.empty()) |
| 2104 |
return std::string(); |
| 2105 |
|
| 2106 |
std::string s = expressions[0]; |
| 2107 |
for (unsigned int i = 1; i < expressions.size(); ++i) |
| 2108 |
s = s + p->get_separator() + expressions[i]; |
| 2109 |
|
| 2110 |
AssertThrow(pattern.match(s), ExcNoMatch(s, p->description())); |
| 2111 |
return s; |
| 2112 |
} |
| 2113 |
|
| 2114 |
static T |
| 2115 |
to_value(const std::string &s, |
| 2116 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2117 |
{ |
| 2118 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 2119 |
|
| 2120 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 2121 |
AssertThrow(p, |
| 2122 |
ExcMessage("I need a List pattern to convert a string " |
| 2123 |
"to a List compatible type.")); |
| 2124 |
|
| 2125 |
const auto expressions = |
| 2126 |
Utilities::split_string_list(s, p->get_separator()); |
| 2127 |
|
| 2128 |
T t = std::make_unique<FunctionParser<dim>>(expressions.size()); |
| 2129 |
const std::string var = |
| 2130 |
FunctionParser<dim>::default_variable_names() + ",t"; |
| 2131 |
const typename FunctionParser<dim>::ConstMap constants; |
| 2132 |
t->initialize(var, expressions, constants, true); |
| 2133 |
return t; |
| 2134 |
} |
| 2135 |
}; |
| 2136 |
|
| 2137 |
// ComponentMask |
| 2138 |
template <> |
| 2139 |
struct Convert<ComponentMask> |
| 2140 |
{ |
| 2141 |
using T = ComponentMask; |
| 2142 |
|
| 2143 |
static std::unique_ptr<Patterns::PatternBase> |
| 2144 |
to_pattern() |
| 2145 |
{ |
| 2146 |
return Convert<std::vector<bool>>::to_pattern(); |
| 2147 |
} |
| 2148 |
|
| 2149 |
static std::string |
| 2150 |
to_string( |
| 2151 |
const T &t, |
| 2152 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2153 |
{ |
| 2154 |
std::vector<bool> mask(t.size()); |
| 2155 |
for (unsigned int i = 0; i < t.size(); ++i) |
| 2156 |
mask[i] = t[i]; |
| 2157 |
|
| 2158 |
return Convert<std::vector<bool>>::to_string(mask, pattern); |
| 2159 |
} |
| 2160 |
|
| 2161 |
static T |
| 2162 |
to_value(const std::string &s, |
| 2163 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2164 |
{ |
| 2165 |
const auto mask = Convert<std::vector<bool>>::to_value(s, pattern); |
| 2166 |
return ComponentMask(mask); |
| 2167 |
} |
| 2168 |
}; |
| 2169 |
|
| 2170 |
// Complex numbers |
| 2171 |
template <class Number> |
| 2172 |
struct Convert<std::complex<Number>> |
| 2173 |
{ |
| 2174 |
using T = std::complex<Number>; |
| 2175 |
|
| 2176 |
static std::unique_ptr<Patterns::PatternBase> |
| 2177 |
to_pattern() |
| 2178 |
{ |
| 2179 |
static_assert(internal::RankInfo<T>::list_rank > 0, |
| 2180 |
"Cannot use this class for non List-compatible types."); |
| 2181 |
return std::make_unique<Patterns::List>( |
| 2182 |
*Convert<typename T::value_type>::to_pattern(), |
| 2183 |
2, |
| 2184 |
2, |
| 2185 |
internal::default_list_separator[internal::RankInfo<T>::list_rank - |
| 2186 |
1]); |
| 2187 |
} |
| 2188 |
|
| 2189 |
static std::string |
| 2190 |
to_string( |
| 2191 |
const T &t, |
| 2192 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2193 |
{ |
| 2194 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 2195 |
AssertThrow(p, |
| 2196 |
ExcMessage("I need a List pattern to convert a string " |
| 2197 |
"to a List compatible type.")); |
| 2198 |
|
| 2199 |
auto base_p = p->get_base_pattern().clone(); |
| 2200 |
std::string s = |
| 2201 |
Convert<typename T::value_type>::to_string(t.real(), *base_p) + |
| 2202 |
p->get_separator() + " " + |
| 2203 |
Convert<typename T::value_type>::to_string(t.imag(), *base_p); |
| 2204 |
|
| 2205 |
AssertThrow(pattern.match(s), ExcNoMatch(s, p->description())); |
| 2206 |
return s; |
| 2207 |
} |
| 2208 |
|
| 2209 |
/** |
| 2210 |
* Convert a string to a value, using the given pattern, or a default one. |
| 2211 |
*/ |
| 2212 |
static T |
| 2213 |
to_value(const std::string &s, |
| 2214 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2215 |
{ |
| 2216 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 2217 |
|
| 2218 |
const auto *p = dynamic_cast<const Patterns::List *>(&pattern); |
| 2219 |
AssertThrow(p, |
| 2220 |
ExcMessage("I need a List pattern to convert a string " |
| 2221 |
"to a List compatible type.")); |
| 2222 |
|
| 2223 |
auto base_p = p->get_base_pattern().clone(); |
| 2224 |
|
| 2225 |
auto v = Utilities::split_string_list(s, p->get_separator()); |
| 2226 |
AssertDimension(v.size(), 2); |
| 2227 |
T t(Convert<typename T::value_type>::to_value(v[0], *base_p), |
| 2228 |
Convert<typename T::value_type>::to_value(v[1], *base_p)); |
| 2229 |
return t; |
| 2230 |
} |
| 2231 |
}; |
| 2232 |
|
| 2233 |
// Strings |
| 2234 |
template <> |
| 2235 |
struct Convert<std::string> |
| 2236 |
{ |
| 2237 |
using T = std::string; |
| 2238 |
|
| 2239 |
static std::unique_ptr<Patterns::PatternBase> |
| 2240 |
to_pattern() |
| 2241 |
{ |
| 2242 |
return std::make_unique<Patterns::Anything>(); |
| 2243 |
} |
| 2244 |
|
| 2245 |
static std::string |
| 2246 |
to_string( |
| 2247 |
const T &t, |
| 2248 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2249 |
{ |
| 2250 |
AssertThrow(pattern.match(t), ExcNoMatch(t, pattern.description())); |
| 2251 |
return t; |
| 2252 |
} |
| 2253 |
|
| 2254 |
static T |
| 2255 |
to_value(const std::string &s, |
| 2256 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2257 |
{ |
| 2258 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 2259 |
return s; |
| 2260 |
} |
| 2261 |
}; |
| 2262 |
|
| 2263 |
// Pairs |
| 2264 |
template <class Key, class Value> |
| 2265 |
struct Convert<std::pair<Key, Value>> |
| 2266 |
{ |
| 2267 |
using T = std::pair<Key, Value>; |
| 2268 |
|
| 2269 |
static std::unique_ptr<Patterns::PatternBase> |
| 2270 |
to_pattern() |
| 2271 |
{ |
| 2272 |
static_assert(internal::RankInfo<T>::map_rank > 0, |
| 2273 |
"Cannot use this class for non Map-compatible types."); |
| 2274 |
return std::make_unique<Patterns::Tuple>( |
| 2275 |
internal::default_map_separator[internal::RankInfo<T>::map_rank - 1], |
| 2276 |
*Convert<Key>::to_pattern(), |
| 2277 |
*Convert<Value>::to_pattern()); |
| 2278 |
} |
| 2279 |
|
| 2280 |
static std::string |
| 2281 |
to_string( |
| 2282 |
const T &t, |
| 2283 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2284 |
{ |
| 2285 |
std::tuple<Key, Value> m(t); |
| 2286 |
std::string s = Convert<decltype(m)>::to_string(m, pattern); |
| 2287 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 2288 |
return s; |
| 2289 |
} |
| 2290 |
|
| 2291 |
static T |
| 2292 |
to_value(const std::string &s, |
| 2293 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2294 |
{ |
| 2295 |
std::tuple<Key, Value> m; |
| 2296 |
m = Convert<decltype(m)>::to_value(s, pattern); |
| 2297 |
return std::make_pair(std::get<0>(m), std::get<1>(m)); |
| 2298 |
} |
| 2299 |
}; |
| 2300 |
|
| 2301 |
// Tuples |
| 2302 |
template <class... Args> |
| 2303 |
struct Convert<std::tuple<Args...>> |
| 2304 |
{ |
| 2305 |
using T = std::tuple<Args...>; |
| 2306 |
|
| 2307 |
static std::unique_ptr<Patterns::PatternBase> |
| 2308 |
to_pattern() |
| 2309 |
{ |
| 2310 |
static_assert(internal::RankInfo<T>::map_rank > 0, |
| 2311 |
"Cannot use this class for non tuple-compatible types."); |
| 2312 |
return std::make_unique<Patterns::Tuple>( |
| 2313 |
internal::default_map_separator[internal::RankInfo<T>::map_rank - 1], |
| 2314 |
*Convert<Args>::to_pattern()...); |
| 2315 |
} |
| 2316 |
|
| 2317 |
static std::string |
| 2318 |
to_string( |
| 2319 |
const T &t, |
| 2320 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2321 |
{ |
| 2322 |
const auto *p = dynamic_cast<const Patterns::Tuple *>(&pattern); |
| 2323 |
AssertThrow(p, |
| 2324 |
ExcMessage("I need a Tuple pattern to convert a tuple " |
| 2325 |
"to a string.")); |
| 2326 |
|
| 2327 |
const auto string_array = Convert<T>::to_string_internal_2(t, *p); |
| 2328 |
std::string str; |
| 2329 |
for (unsigned int i = 0; i < string_array.size(); ++i) |
| 2330 |
str += |
| 2331 |
(i != 0u ? " " + p->get_separator() + " " : "") + string_array[i]; |
| 2332 |
AssertThrow(p->match(str), ExcNoMatch(str, p->description())); |
| 2333 |
return str; |
| 2334 |
} |
| 2335 |
|
| 2336 |
static T |
| 2337 |
to_value(const std::string &s, |
| 2338 |
const Patterns::PatternBase &pattern = *Convert<T>::to_pattern()) |
| 2339 |
{ |
| 2340 |
AssertThrow(pattern.match(s), ExcNoMatch(s, pattern.description())); |
| 2341 |
|
| 2342 |
const auto *p = dynamic_cast<const Patterns::Tuple *>(&pattern); |
| 2343 |
AssertThrow(p, |
| 2344 |
ExcMessage("I need a Tuple pattern to convert a string " |
| 2345 |
"to a tuple type.")); |
| 2346 |
|
| 2347 |
auto v = Utilities::split_string_list(s, p->get_separator()); |
| 2348 |
|
| 2349 |
return Convert<T>::to_value_internal_2(v, *p); |
| 2350 |
} |
| 2351 |
|
| 2352 |
private: |
| 2353 |
template <std::size_t... U> |
| 2354 |
static std::array<std::string, std::tuple_size<T>::value> |
| 2355 |
to_string_internal_1(const T &t, |
| 2356 |
const Patterns::Tuple &pattern, |
| 2357 |
std::index_sequence<U...>) |
| 2358 |
{ |
| 2359 |
std::array<std::string, std::tuple_size<T>::value> a = { |
| 2360 |
{Convert<typename std::tuple_element<U, T>::type>::to_string( |
| 2361 |
std::get<U>(t), pattern.get_pattern(U))...}}; |
| 2362 |
return a; |
| 2363 |
} |
| 2364 |
|
| 2365 |
static std::array<std::string, std::tuple_size<T>::value> |
| 2366 |
to_string_internal_2(const T &t, const Patterns::Tuple &pattern) |
| 2367 |
{ |
| 2368 |
return Convert<T>::to_string_internal_1( |
| 2369 |
t, pattern, std::make_index_sequence<std::tuple_size<T>::value>{}); |
| 2370 |
} |
| 2371 |
|
| 2372 |
template <std::size_t... U> |
| 2373 |
static T |
| 2374 |
to_value_internal_1(const std::vector<std::string> &s, |
| 2375 |
const Patterns::Tuple &pattern, |
| 2376 |
std::index_sequence<U...>) |
| 2377 |
{ |
| 2378 |
return std::make_tuple( |
| 2379 |
Convert<typename std::tuple_element<U, T>::type>::to_value( |
| 2380 |
s[U], pattern.get_pattern(U))...); |
| 2381 |
} |
| 2382 |
|
| 2383 |
static T |
| 2384 |
to_value_internal_2(const std::vector<std::string> &s, |
| 2385 |
const Patterns::Tuple &pattern) |
| 2386 |
{ |
| 2387 |
return Convert<T>::to_value_internal_1( |
| 2388 |
s, pattern, std::make_index_sequence<std::tuple_size<T>::value>{}); |
| 2389 |
} |
| 2390 |
}; |
| 2391 |
|
| 2392 |
#ifdef DEAL_II_WITH_MAGIC_ENUM |
| 2393 |
// Enums |
| 2394 |
template <class T> |
| 2395 |
struct Convert<T, typename std::enable_if<std::is_enum<T>::value>::type> |
| 2396 |
{ |
| 2397 |
static std::unique_ptr<Patterns::PatternBase> |
| 2398 |
to_pattern() |
| 2399 |
{ |
| 2400 |
const auto n = magic_enum::enum_names<T>(); |
| 2401 |
std::vector<std::string> names = {n.begin(), n.end()}; |
| 2402 |
const auto selection = |
| 2403 |
Patterns::Tools::Convert<decltype(names)>::to_string( |
| 2404 |
names, |
| 2405 |
Patterns::List( |
| 2406 |
Patterns::Anything(), names.size(), names.size(), "|")); |
| 2407 |
// Allow parsing a list of enums, and make bitwise or between them |
| 2408 |
return Patterns::List(Patterns::Selection(selection), |
| 2409 |
0, |
| 2410 |
names.size(), |
| 2411 |
"|") |
| 2412 |
.clone(); |
| 2413 |
} |
| 2414 |
|
| 2415 |
static std::string |
| 2416 |
to_string(const T &value, |
| 2417 |
const Patterns::PatternBase &p = *Convert<T>::to_pattern()) |
| 2418 |
{ |
| 2419 |
const auto values = magic_enum::enum_values<T>(); |
| 2420 |
std::vector<std::string> names; |
| 2421 |
for (const auto &v : values) |
| 2422 |
if (magic_enum::bitwise_operators::operator&(value, v) == v) |
| 2423 |
names.push_back(std::string(magic_enum::enum_name(v))); |
| 2424 |
return Patterns::Tools::Convert<decltype(names)>::to_string(names, p); |
| 2425 |
} |
| 2426 |
|
| 2427 |
static T |
| 2428 |
to_value(const std::string &s, |
| 2429 |
const dealii::Patterns::PatternBase &p = *to_pattern()) |
| 2430 |
{ |
| 2431 |
// Make sure we have a valid enum value, or empty value |
| 2432 |
AssertThrow(p.match(s), ExcNoMatch(s, p.description())); |
| 2433 |
T value = T(); |
| 2434 |
std::vector<std::string> value_names; |
| 2435 |
value_names = |
| 2436 |
Patterns::Tools::Convert<decltype(value_names)>::to_value(s, p); |
| 2437 |
for (const auto &name : value_names) |
| 2438 |
{ |
| 2439 |
auto v = magic_enum::enum_cast<T>(name); |
| 2440 |
if (v.has_value()) |
| 2441 |
value = |
| 2442 |
magic_enum::bitwise_operators::operator|(value, v.value()); |
| 2443 |
} |
| 2444 |
return value; |
| 2445 |
} |
| 2446 |
}; |
| 2447 |
#endif |
| 2448 |
|
| 2449 |
// Utility function with default Pattern |
| 2450 |
template <typename T> |
| 2451 |
std::string |
| 2452 |
to_string(const T &t) |
| 2453 |
{ |
| 2454 |
return Convert<T>::to_string(t); |
| 2455 |
} |
| 2456 |
|
| 2457 |
// Utility function with default Pattern |
| 2458 |
template <typename T> |
| 2459 |
void |
| 2460 |
to_value(const std::string &s, T &t) |
| 2461 |
{ |
| 2462 |
t = Convert<T>::to_value(s); |
| 2463 |
} |
| 2464 |
} // namespace Tools |
| 2465 |
} // namespace Patterns |
| 2466 |
|
| 2467 |
|
| 2468 |
DEAL_II_NAMESPACE_CLOSE |
| 2469 |
|
| 2470 |
#endif |