/usr/include/trilinos/Teuchos_StandardParameterEntryValidators.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 | // @HEADER
// ***********************************************************************
//
// Teuchos: Common Tools Package
// Copyright (2004) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
#define TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
#include "Teuchos_ParameterEntryValidator.hpp"
#include "Teuchos_ParameterList.hpp"
#include "Teuchos_ParameterListExceptions.hpp"
#include "Teuchos_VerbosityLevel.hpp"
#include "Teuchos_Array.hpp"
#include "Teuchos_Assert.hpp"
#include "Teuchos_StrUtils.hpp"
#include "Teuchos_TypeNameTraits.hpp"
namespace Teuchos {
/** \brief Standard implementation of a ParameterEntryValidator that maps from
* a list of strings to some integral type value.
*
* Objects of this type are meant to be used as both abstract objects passed
* to <tt>Teuchos::ParameterList</tt> objects to be used to validate parameter
* types and values, and to be used by the code that reads parameter values.
* Having a single definition for the types of valids input and outputs for a
* parameter value makes it easier to write error free validated code.
*/
template<class IntegralType>
class StringToIntegralParameterEntryValidator : public ParameterEntryValidator {
public:
/** \name Constructors */
//@{
/** \brief Construct with a mapping from strings to ordinals <tt>0</tt> to
* </tt>n-1</tt>.
*
* \param strings
* [in] Array of unique std::string names.
* \param defaultParameterName
* [in] The default name of the parameter (used in error messages)
*/
StringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
std::string const& defaultParameterName
);
/** \brief Construct with a mapping from strings to aribitrary typed
* integral values.
*
* \param strings
* [in] Array of unique std::string names.
* \param integralValues
* [in] Array that gives the integral values associated with
* <tt>strings[]</tt>
* \param defaultParameterName
* [in] The default name of the parameter (used in error messages)
*
* <b>Preconditions:</b><ul>
* <li> <tt>strings.size() == integralValues.size()</tt>
* </ul>
*/
StringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
ArrayView<const IntegralType> const& integralValues,
std::string const& defaultParameterName
);
/** \brief Construct with a mapping from strings (with documentation) to
* aribitrary typed integral values.
*
* \param strings
* [in] Array of unique std::string names.
* \param stringsDocs
* [in] Array of documentation strings for each std::string value.
* \param integralValues
* [in] Array that gives the integral values associated with
* <tt>strings[]</tt>
* \param defaultParameterName
* [in] The default name of the parameter (used in error messages)
*
* <b>Preconditions:</b><ul>
* <li> <tt>strings.size() == stringDocs.size()</tt>
* <li> <tt>strings.size() == integralValues.size()</tt>
* </ul>
*/
StringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
ArrayView<const std::string> const& stringsDocs,
ArrayView<const IntegralType> const& integralValues,
std::string const& defaultParameterName
);
//@}
/** \name Local non-virtual validated lookup functions */
//@{
/** \brief Perform a mapping from a std::string value to its integral value.
*
* \param str [in] String that is being used to lookup the corresponding
* integral value.
* \param paramName
* [in] Optional name that will be used to generate error messages.
*
* If the std::string name <tt>str</tt> does not exist, the an std::exception will be
* thrown with a very descriptive error message.
*/
IntegralType getIntegralValue(
const std::string &str, const std::string ¶mName = "",
const std::string &sublistName = ""
) const;
/** \brief Perform a mapping from a std::string value embedded in a
* <tt>ParameterEntry</tt> object and return its associated integral value.
*
* \param entry
* [in] The std::string entry.
* \param paramName
* [in] Optional name that will be used to generate error messages.
* \param sublistName
* [in] The name of the sublist.
* \param activeQuery
* [in] If true, then this lookup will be recored as an active query
* which will turn the <tt>isUsed</tt> bool to <tt>true</tt>.
*/
IntegralType getIntegralValue(
const ParameterEntry &entry, const std::string ¶mName = "",
const std::string &sublistName = "", const bool activeQuery = true
) const;
/** \brief Get and validate a std::string value embedded in a
* <tt>ParameterEntry</tt> object.
*
*
* \param entry
* [in] The std::string entry.
* \param paramName
* [in] Optional name that will be used to generate error messages.
* \param sublistName
* [in] The name of the sublist.
* \param activeQuery
* [in] If true, then this lookup will be recored as an active query
* which will turn the <tt>isUsed</tt> bool to <tt>true</tt>.
*/
std::string getStringValue(
const ParameterEntry &entry, const std::string ¶mName = "",
const std::string &sublistName = "", const bool activeQuery = true
) const;
/** \brief Lookup a parameter from a parameter list, perform a mapping from
* a std::string value embedded in the <tt>ParameterEntry</tt> object and return
* its associated integral value.
*/
IntegralType getIntegralValue(
ParameterList ¶mList, const std::string ¶mName,
const std::string &defaultValue
) const;
/** \brief Lookup a parameter from a parameter list, validate the std::string
* value, and return the std::string value.
*/
std::string getStringValue(
ParameterList ¶mList, const std::string ¶mName,
const std::string &defaultValue
) const;
/** \brief Validate the std::string and pass it on..
*
* \param str [in] String that is being used to lookup the corresponding
* integral value.
* \param name [in] Optional name that will be used to generate error messages.
*
* If the std::string name <tt>str</tt> does not exist, the an std::exception will be
* thrown with a very descriptive error message.
*/
std::string validateString(
const std::string &str, const std::string ¶mName = "",
const std::string &sublistName = ""
) const;
//@}
/** \name Overridden from ParameterEntryValidator */
//@{
/** \brief . */
void printDoc(
std::string const& docString,
std::ostream & out
) const;
/** \brief . */
Teuchos::RCP<const Array<std::string> >
validStringValues() const;
/** \brief . */
void validate(
ParameterEntry const& entry,
std::string const& paramName,
std::string const& sublistName
) const;
//@}
private:
typedef std::map<std::string,IntegralType> map_t;
std::string defaultParameterName_;
std::string validValues_;
RCP<const Array<std::string> > validStringValues_;
RCP<const Array<std::string> > validStringValuesDocs_;
map_t map_;
void setValidValues(
ArrayView<const std::string> const& strings,
ArrayView<const std::string> const* stringsDocs = NULL
);
// Not defined and not to be called.
StringToIntegralParameterEntryValidator();
};
/** \brief Nonmember constructor (see implementation).
*
* \relates StringToIntegralParameterEntryValidator
*/
template<class IntegralType>
RCP<StringToIntegralParameterEntryValidator<IntegralType> >
stringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
std::string const& defaultParameterName
);
/** \brief Nonmember constructor (see implementation).
*
* \relates StringToIntegralParameterEntryValidator
*/
template<class IntegralType>
RCP<StringToIntegralParameterEntryValidator<IntegralType> >
stringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
ArrayView<const IntegralType> const& integralValues,
std::string const& defaultParameterName
);
/** \brief Nonmember constructor (see implementation).
*
* \relates StringToIntegralParameterEntryValidator
*/
template<class IntegralType>
RCP<StringToIntegralParameterEntryValidator<IntegralType> >
stringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
ArrayView<const std::string> const& stringsDocs,
ArrayView<const IntegralType> const& integralValues,
std::string const& defaultParameterName
);
/** \brief Set up a std::string parameter that will use an embedded validator to
* allow the extraction of an integral value.
*
* The function <tt>getIntegralValue()</tt> can then be used to extract the
* integral value of the std::string parameter. In this case, the integral value
* return will just be the zero-based index of the std::string value in the list
* <tt>strings</tt>.
*
* \relates ParameterList
*/
template<class IntegralType>
void setStringToIntegralParameter(
std::string const& paramName,
std::string const& defaultValue,
std::string const& docString,
ArrayView<const std::string> const& strings,
ParameterList * paramList
);
/** \brief Set up a std::string parameter that will use an embedded validator to
* allow the extraction of an integral value from a list of integral values.
*
* The function <tt>getIntegralValue()</tt> can then be used to extract the
* integral value of the std::string parameter. In this case, the integral value
* return will just be the zero-based index of the std::string value in the list
* <tt>strings</tt>.
*
* \relates ParameterList
*/
template<class IntegralType>
void setStringToIntegralParameter(
std::string const& paramName,
std::string const& defaultValue,
std::string const& docString,
ArrayView<const std::string> const& strings,
ArrayView<const IntegralType> const& integralValues,
ParameterList * paramList
);
/** \brief Set up a std::string parameter with documentation strings for each valid
* value that will use an embedded validator to allow the extraction of an
* integral value from a list of integral values.
*
* The function <tt>getIntegralValue()</tt> can then be used to extract the
* integral value of the std::string parameter. In this case, the integral value
* return will just be the zero-based index of the std::string value in the list
* <tt>strings</tt>.
*
* \relates ParameterList
*/
template<class IntegralType>
void setStringToIntegralParameter(
std::string const& paramName,
std::string const& defaultValue,
std::string const& docString,
ArrayView<const std::string> const& strings,
ArrayView<const std::string> const& stringsDocs,
ArrayView<const IntegralType> const& integralValues,
ParameterList * paramList
);
/** \brief Get an integral value for a parameter that is assumed to already be set.
*
* This function does a dynamic cast to get the underlying valiator of type
* StringToIntegralParameterEntryValidator<IntegralType>. If this dynamic
* cast failes then an <tt>Exceptions::InvalidParameterType</tt> std::exception is
* thrown with an excellent error message.
*
* \relates ParameterList
*/
template<class IntegralType>
IntegralType getIntegralValue(
ParameterList const& paramList, std::string const& paramName
);
/** \brief Get a std::string value for a parameter that is assumed to already be set.
*
* This function does a dynamic cast to get the underlying valiator of type
* StringToIntegralParameterEntryValidator<IntegralValue>. The default type
* for IntegralValue is int. If this dynamic cast failes then an
* <tt>Exceptions::InvalidParameterType</tt> std::exception is thrown with an
* excellent error message.
*
* \relates ParameterList
*/
template<class IntegralType>
std::string getStringValue(
ParameterList const& paramList, std::string const& paramName
);
/** \brief Get a StringToIntegralParameterEntryValidator<IntegralType> object out of
* a ParameterEntry object.
*
* This function with thrown of the validator does not exist.
*/
template<class IntegralType>
RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
getStringToIntegralParameterEntryValidator(
ParameterEntry const& entry, ParameterList const& paramList,
std::string const& paramName
);
/** \brief Return the std::string name of the verbosity level as it is accepted by the
* verbosity level parameter.
*
* \relates EVerbosityLevel
*/
std::string getVerbosityLevelParameterValueName(
const EVerbosityLevel verbLevel
);
/** \brief Return a validator for <tt>EVerbosityLevel</tt>.
*
* \relates EVerbosityLevel
*/
RCP<StringToIntegralParameterEntryValidator<EVerbosityLevel> >
verbosityLevelParameterEntryValidator(std::string const& defaultParameterName);
/** \brief Standard implementation of a ParameterEntryValidator that accepts
* numbers from a number of different formats and converts them to numbers in
* another format.
*
* Objects of this type are meant to be used as both abstract objects passed
* to <tt>Teuchos::ParameterList</tt> objects to be used to validate parameter
* types and values, and to be used by the code that reads parameter values.
* Having a single definition for the types of valids input and outputs for a
* parameter value makes it easier to write error-free validated code.
*/
class TEUCHOS_LIB_DLL_EXPORT AnyNumberParameterEntryValidator : public ParameterEntryValidator {
public:
/** \name Public types */
//@{
/** \brief Determines what type is the preferred type. */
enum EPreferredType { PREFER_INT, PREFER_DOUBLE, PREFER_STRING };
/** \brief Determines the types that are accepted. */
class AcceptedTypes {
public:
/** \brief Allow all types or not on construction. */
AcceptedTypes( bool allowAllTypesByDefault = true )
:allowInt_(allowAllTypesByDefault),allowDouble_(allowAllTypesByDefault),
allowString_(allowAllTypesByDefault)
{}
/** \brief Set allow an <tt>int</tt> value or not */
AcceptedTypes& allowInt( bool _allowInt )
{ allowInt_ = _allowInt; return *this; }
/** \brief Set allow a <tt>double</tt> value or not */
AcceptedTypes& allowDouble( bool _allowDouble )
{ allowDouble_ = _allowDouble; return *this; }
/** \brief Set allow an <tt>std::string</tt> value or not */
AcceptedTypes& allowString( bool _allowString )
{ allowString_ = _allowString; return *this; }
/** \brief Allow an <tt>int</tt> value? */
bool allowInt() const { return allowInt_; }
/** \brief Allow an <tt>double</tt> value? */
bool allowDouble() const { return allowDouble_; }
/** \brief Allow an <tt>std::string</tt> value? */
bool allowString() const { return allowString_; }
private:
bool allowInt_;
bool allowDouble_;
bool allowString_;
};
//@}
/** \name Constructors */
//@{
/** \brief Construct with a preferrded type of double and accept all
* types.
*/
AnyNumberParameterEntryValidator();
/** \brief Construct with allowed input and output types and the preferred
* type.
*
* \param preferredType
* [in] Determines the preferred type. This enum value is used to
* set the default value in the override <tt>validateAndModify()</tt>.
* \param acceptedType
* [in] Determines the types that are allowed in the parameter list.
*/
AnyNumberParameterEntryValidator(
EPreferredType const preferredType,
AcceptedTypes const& acceptedTypes
);
//@}
/** \name Local non-virtual validated lookup functions */
//@{
/** \brief Get an integer value from a parameter entry. */
int getInt(
const ParameterEntry &entry, const std::string ¶mName = "",
const std::string &sublistName = "", const bool activeQuery = true
) const;
/** \brief Get a double value from a parameter entry. */
double getDouble(
const ParameterEntry &entry, const std::string ¶mName = "",
const std::string &sublistName = "", const bool activeQuery = true
) const;
/** \brief Get a std::string value from a parameter entry. */
std::string getString(
const ParameterEntry &entry, const std::string ¶mName = "",
const std::string &sublistName = "", const bool activeQuery = true
) const;
/** \brief Lookup parameter from a parameter list and return as an int
* value.
*/
int getInt(
ParameterList ¶mList, const std::string ¶mName,
const int defaultValue
) const;
/** \brief Lookup parameter from a parameter list and return as an double
* value.
*/
double getDouble(
ParameterList ¶mList, const std::string ¶mName,
const double defaultValue
) const;
/** \brief Lookup parameter from a parameter list and return as an std::string
* value.
*/
std::string getString(
ParameterList ¶mList, const std::string ¶mName,
const std::string &defaultValue
) const;
//@}
/** \name Overridden from ParameterEntryValidator */
//@{
/** \brief . */
void printDoc(
std::string const& docString,
std::ostream & out
) const;
/** \brief . */
Teuchos::RCP<const Array<std::string> >
validStringValues() const;
/** \brief . */
void validate(
ParameterEntry const& entry,
std::string const& paramName,
std::string const& sublistName
) const;
/** \brief . */
void validateAndModify(
std::string const& paramName,
std::string const& sublistName,
ParameterEntry * entry
) const;
//@}
private:
// ////////////////////////////
// Private data members
EPreferredType preferredType_;
std::string acceptedTypesString_;
//use pragmas to disable some false-positive warnings for windows sharedlibs export
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4251)
#endif
const AcceptedTypes acceptedTypes_;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
// ////////////////////////////
// Private member functions
void finishInitialization();
void throwTypeError(
ParameterEntry const& entry,
std::string const& paramName,
std::string const& sublistName
) const;
};
// Nonmember helper functions
/** \brief Nonmember constructor AnyNumberParameterEntryValidator.
*
* \relates AnyNumberParameterEntryValidator
*/
TEUCHOS_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
anyNumberParameterEntryValidator(
AnyNumberParameterEntryValidator::EPreferredType const preferredType,
AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
);
/** \brief Set an integer parameter that allows for (nearly) any input
* parameter type that is convertible to an int.
*
* \relates ParameterList
*/
TEUCHOS_LIB_DLL_EXPORT void setIntParameter(
std::string const& paramName,
int const value, std::string const& docString,
ParameterList *paramList,
AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
= AnyNumberParameterEntryValidator::AcceptedTypes()
);
/** \brief Set an double parameter that allows for (nearly) any input
* parameter type that is convertible to a double.
*
* \relates ParameterList
*/
TEUCHOS_LIB_DLL_EXPORT void setDoubleParameter(
std::string const& paramName,
double const& value, std::string const& docString,
ParameterList *paramList,
AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
= AnyNumberParameterEntryValidator::AcceptedTypes()
);
/** \brief Set an numeric parameter preferred as a std::string that allows for
* (nearly) any input parameter type that is convertible to a std::string.
*
* \relates ParameterList
*/
TEUCHOS_LIB_DLL_EXPORT void setNumericStringParameter(
std::string const& paramName,
std::string const& value, std::string const& docString,
ParameterList *paramList,
AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
= AnyNumberParameterEntryValidator::AcceptedTypes()
);
/** \brief Get an integer parameter.
*
* If the underlying parameter type is already an integer, then all is good.
* However, if it is not, then a AnyNumberParameterEntryValidator object is
* looked for to extract the type correctly. If no validator is attached to
* the entry, then a new AnyNumberParameterEntryValidator object will be
* created that that will allow the conversion from any supported type.
*
* The parameter must exist or an <tt>Exceptions::InvalidParameterName</tt>
* object will be thrown. The parameters type must be acceptable, or an
* <tt>Exceptions::InvalidParameterType</tt> object will be thown.
*
* \relates ParameterList
*/
TEUCHOS_LIB_DLL_EXPORT int getIntParameter(
ParameterList const& paramList, std::string const& paramName
);
/** \brief Get double integer parameter.
*
* If the underlying parameter type is already a double, then all is good.
* However, if it is not, then a AnyNumberParameterEntryValidator object is
* looked for to extract the type correctly. If no validator is attached to
* the entry, then a new AnyNumberParameterEntryValidator object will be
* created that that will allow the conversion from any supported type.
*
* The parameter must exist or an <tt>Exceptions::InvalidParameterName</tt>
* object will be thrown. The parameters type must be acceptable, or an
* <tt>Exceptions::InvalidParameterType</tt> object will be thown.
*
* \relates ParameterList
*/
TEUCHOS_LIB_DLL_EXPORT double getDoubleParameter(
ParameterList const& paramList,
std::string const& paramName
);
/** \brief Get std::string numeric parameter.
*
* If the underlying parameter type is already a std::string, then all is good.
* However, if it is not, then a AnyNumberParameterEntryValidator object is
* looked for to extract the type correctly. If no validator is attached to
* the entry, then a new AnyNumberParameterEntryValidator object will be
* created that that will allow the conversion from any supported type.
*
* The parameter must exist or an <tt>Exceptions::InvalidParameterName</tt>
* object will be thrown. The parameters type must be acceptable, or an
* <tt>Exceptions::InvalidParameterType</tt> object will be thown.
*
* \relates ParameterList
*/
TEUCHOS_LIB_DLL_EXPORT std::string getNumericStringParameter(
ParameterList const& paramList,
std::string const& paramName
);
// ///////////////////////////
// Implementations
//
// StringToIntegralParameterEntryValidator
//
// Constructors
template<class IntegralType>
StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings, std::string const& defaultParameterName
)
:defaultParameterName_(defaultParameterName)
{
typedef typename map_t::value_type val_t;
for( int i = 0; i < static_cast<int>(strings.size()); ++i ) {
const bool unique = map_.insert( val_t( strings[i], i ) ).second;
TEST_FOR_EXCEPTION(
!unique, std::logic_error
,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \""
<< defaultParameterName_ << "\"."
);
}
setValidValues(strings);
}
template<class IntegralType>
StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings, ArrayView<const IntegralType> const& integralValues
,std::string const& defaultParameterName
)
:defaultParameterName_(defaultParameterName)
{
#ifdef TEUCHOS_DEBUG
TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
#endif
typedef typename map_t::value_type val_t;
for( int i = 0; i < static_cast<int>(strings.size()); ++i ) {
const bool unique = map_.insert( val_t( strings[i], integralValues[i] ) ).second;
TEST_FOR_EXCEPTION(
!unique, std::logic_error
,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \""
<< defaultParameterName_ << "\""
);
}
setValidValues(strings);
}
template<class IntegralType>
StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings
,ArrayView<const std::string> const& stringsDocs
,ArrayView<const IntegralType> const& integralValues
,std::string const& defaultParameterName
)
:defaultParameterName_(defaultParameterName)
{
#ifdef TEUCHOS_DEBUG
TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() );
TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
#endif
typedef typename map_t::value_type val_t;
for( int i = 0; i < static_cast<int>(strings.size()); ++i ) {
const bool unique = map_.insert( val_t( strings[i], integralValues[i] ) ).second;
TEST_FOR_EXCEPTION(
!unique, std::logic_error
,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \""
<< defaultParameterName_ << "\""
);
}
setValidValues(strings,&stringsDocs);
}
// Lookup functions
template<class IntegralType>
IntegralType
StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue(
const std::string &str, const std::string ¶mName
,const std::string &sublistName
) const
{
typename map_t::const_iterator itr = map_.find(str);
TEST_FOR_EXCEPTION_PURE_MSG(
itr == map_.end(), Exceptions::InvalidParameterValue
,"Error, the value \"" << str << "\" is not recognized for the parameter \""
<< ( paramName.length() ? paramName : defaultParameterName_ ) << "\""
<< "\nin the sublist \"" << sublistName << "\"."
<< "\n\nValid values include:"
<< "\n {\n"
<< validValues_
<< " }"
);
return (*itr).second;
}
template<class IntegralType>
IntegralType
StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue(
const ParameterEntry &entry, const std::string ¶mName
,const std::string &sublistName, const bool activeQuery
) const
{
const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) );
TEST_FOR_EXCEPTION_PURE_MSG(
!validType, Exceptions::InvalidParameterType
,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_)
<< "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}"
<< "\nin the sublist \"" << sublistName << "\""
<< "\nhas the wrong type."
<< "\n\nThe correct type is \"string\"!"
);
const std::string
&strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw!
}
template<class IntegralType>
std::string
StringToIntegralParameterEntryValidator<IntegralType>::getStringValue(
const ParameterEntry &entry, const std::string ¶mName
,const std::string &sublistName, const bool activeQuery
) const
{
// Validate the parameter's type and value
this->getIntegralValue(entry,paramName,sublistName,activeQuery);
// Return the std::string value which is now validated!
return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
}
template<class IntegralType>
IntegralType
StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue(
ParameterList ¶mList, const std::string ¶mName
,const std::string &defaultValue
) const
{
const std::string
&strValue = paramList.get(paramName,defaultValue);
return getIntegralValue(strValue,paramName,paramList.name());
}
template<class IntegralType>
std::string
StringToIntegralParameterEntryValidator<IntegralType>::getStringValue(
ParameterList ¶mList, const std::string ¶mName
,const std::string &defaultValue
) const
{
const std::string
&strValue = paramList.get(paramName,defaultValue);
getIntegralValue(strValue,paramName,paramList.name()); // Validate!
return strValue;
}
template<class IntegralType>
std::string
StringToIntegralParameterEntryValidator<IntegralType>::validateString(
const std::string &str, const std::string ¶mName
,const std::string &sublistName
) const
{
getIntegralValue(str,paramName,sublistName); // Validate!
return str;
}
// Overridden from ParameterEntryValidator
template<class IntegralType>
void StringToIntegralParameterEntryValidator<IntegralType>::printDoc(
std::string const& docString
,std::ostream & out
) const
{
StrUtils::printLines(out,"# ",docString);
out << "# Valid std::string values:\n";
out << "# {\n";
if(validStringValuesDocs_.get()) {
for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) {
out << "# \"" << (*validStringValues_)[i] << "\"\n";
StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] );
}
}
else {
StrUtils::printLines(out,"# ",validValues_);
// Note: Above validValues_ has for initial spaces already so indent should
// be correct!
}
out << "# }\n";
}
template<class IntegralType>
Teuchos::RCP<const Array<std::string> >
StringToIntegralParameterEntryValidator<IntegralType>::validStringValues() const
{
return validStringValues_;
}
template<class IntegralType>
void StringToIntegralParameterEntryValidator<IntegralType>::validate(
ParameterEntry const& entry
,std::string const& paramName
,std::string const& sublistName
) const
{
this->getIntegralValue(entry,paramName,sublistName,false);
}
// private
template<class IntegralType>
void StringToIntegralParameterEntryValidator<IntegralType>::setValidValues(
ArrayView<const std::string> const& strings
,ArrayView<const std::string> const* stringsDocs
)
{
validStringValues_ = rcp(new Array<std::string>(strings));
if(stringsDocs)
validStringValuesDocs_ = rcp(new Array<std::string>(*stringsDocs));
// Here I build the list of valid values in the same order as passed in by
// the client!
std::ostringstream oss;
typename map_t::const_iterator itr = map_.begin();
for( int i = 0; i < static_cast<int>(strings.size()); ++i ) {
oss << " \""<<strings[i]<<"\"\n";
}
// Note: Above four spaces is designed for the error output above.
validValues_ = oss.str();
}
} // namespace Teuchos
//
// Nonmember function implementations for StringToIntegralParameterEntryValidator
//
template<class IntegralType>
inline
Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> >
Teuchos::stringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
std::string const& defaultParameterName
)
{
return rcp(
new StringToIntegralParameterEntryValidator<IntegralType>(
strings, defaultParameterName
)
);
}
template<class IntegralType>
inline
Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> >
Teuchos::stringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
ArrayView<const IntegralType> const& integralValues,
std::string const& defaultParameterName
)
{
return rcp(
new StringToIntegralParameterEntryValidator<IntegralType>(
strings, integralValues, defaultParameterName
)
);
}
template<class IntegralType>
inline
Teuchos::RCP< Teuchos::StringToIntegralParameterEntryValidator<IntegralType> >
Teuchos::stringToIntegralParameterEntryValidator(
ArrayView<const std::string> const& strings,
ArrayView<const std::string> const& stringsDocs,
ArrayView<const IntegralType> const& integralValues,
std::string const& defaultParameterName
)
{
return rcp(
new StringToIntegralParameterEntryValidator<IntegralType>(
strings, stringsDocs, integralValues, defaultParameterName
)
);
}
template<class IntegralType>
void Teuchos::setStringToIntegralParameter(
std::string const& paramName,
std::string const& defaultValue,
std::string const& docString,
ArrayView<const std::string> const& strings,
ParameterList * paramList
)
{
typedef ParameterEntryValidator PEV;
TEST_FOR_EXCEPT(0==paramList);
paramList->set(
paramName, defaultValue, docString,
rcp_implicit_cast<const PEV>(
stringToIntegralParameterEntryValidator<IntegralType>(
strings, paramName
)
)
);
}
template<class IntegralType>
void Teuchos::setStringToIntegralParameter(
std::string const& paramName,
std::string const& defaultValue,
std::string const& docString,
ArrayView<const std::string> const& strings,
ArrayView<const IntegralType> const& integralValues,
ParameterList * paramList
)
{
typedef ParameterEntryValidator PEV;
TEST_FOR_EXCEPT(0==paramList);
paramList->set(
paramName, defaultValue, docString,
rcp_implicit_cast<const PEV>(
stringToIntegralParameterEntryValidator<IntegralType>(
strings, integralValues, paramName
)
)
);
}
template<class IntegralType>
void Teuchos::setStringToIntegralParameter(
std::string const& paramName,
std::string const& defaultValue,
std::string const& docString,
ArrayView<const std::string> const& strings,
ArrayView<const std::string> const& stringsDocs,
ArrayView<const IntegralType> const& integralValues,
ParameterList * paramList
)
{
typedef ParameterEntryValidator PEV;
TEST_FOR_EXCEPT(0==paramList);
paramList->set(
paramName, defaultValue, docString,
rcp_implicit_cast<const PEV>(
stringToIntegralParameterEntryValidator<IntegralType>(
strings, stringsDocs, integralValues, paramName
)
)
);
}
template<class IntegralType>
IntegralType Teuchos::getIntegralValue(
ParameterList const& paramList, std::string const& paramName
)
{
const ParameterEntry &entry = paramList.getEntry(paramName);
RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
entry, paramList, paramName
);
return integralValidator->getIntegralValue(
entry, paramName, paramList.name(), true
);
}
template<class IntegralType>
std::string Teuchos::getStringValue(
ParameterList const& paramList, std::string const& paramName
)
{
const ParameterEntry &entry = paramList.getEntry(paramName);
RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
entry, paramList, paramName
);
return integralValidator->getStringValue(
entry, paramName, paramList.name(), true
);
}
template<class IntegralType>
Teuchos::RCP<const Teuchos::StringToIntegralParameterEntryValidator<IntegralType> >
Teuchos::getStringToIntegralParameterEntryValidator(
ParameterEntry const& entry, ParameterList const& paramList,
std::string const& paramName
)
{
RCP<const ParameterEntryValidator>
validator = entry.validator();
TEST_FOR_EXCEPTION_PURE_MSG(
is_null(validator), Exceptions::InvalidParameterType,
"Error! The parameter \""<<paramName<<"\" exists\n"
"in the parameter (sub)list \""<<paramList.name()<<"\"\n"
"but it does not contain any validator needed to extract\n"
"an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!"
);
RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
integralValidator
=
rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >(
validator
);
TEST_FOR_EXCEPTION_PURE_MSG(
is_null(integralValidator), Exceptions::InvalidParameterType,
"Error! The parameter \""<<paramName<<"\" exists\n"
"in the parameter (sub)list \""<<paramList.name()<<"\"\n"
"but it contains the wrong type of validator. The expected validator type\n"
"is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n"
"but the contained validator type is \""<<typeName(*validator)<<"\"!"
);
return integralValidator;
}
#endif // TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
|