Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_StandardParameterEntryValidators.cpp
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42#include "Teuchos_StandardParameterEntryValidators.hpp"
43#include "Teuchos_as.hpp"
44#include <fstream>
45
47 const EVerbosityLevel verbLevel
48 )
49{
50 switch (verbLevel) {
51 case VERB_DEFAULT:
52 return "default";
53 case VERB_NONE:
54 return "none";
55 case VERB_LOW:
56 return "low";
57 case VERB_MEDIUM:
58 return "medium";
59 case VERB_HIGH:
60 return "high";
61 case VERB_EXTREME:
62 return "extreme";
63 default:
65 true, std::invalid_argument, "Teuchos::getVerbosityLevelParameterValue"
66 "Name(const Teuchos::EVerbosityLevel): Input argument " << verbLevel <<
67 " has an invalid value. Valid values are VERB_DEFAULT=" << VERB_DEFAULT
68 << ", VERB_NONE=" << VERB_NONE << ", VERB_LOW=" << VERB_LOW << ", "
69 "VERB_MEDIUM=" << VERB_MEDIUM << ", VERB_HIGH=" << VERB_HIGH << ", AND "
70 "VERB_EXTREME=" << VERB_EXTREME << ".");
71 }
72
73 // NOTE (mfh 15 Sep 2014): Most compilers have figured out that the
74 // return statement below is unreachable. Some older compilers
75 // might not realize this. That's why the return statement was put
76 // there, so that those compilers don't warn that this function
77 // doesn't return a value. If it's a choice between one warning and
78 // another, I would prefer the choice that produces less code and
79 // doesn't have unreachable code (which never gets tested).
80
81 //return ""; // Never get here!
82}
83
84
87 >
89 std::string const& defaultParameterName
90 )
91{
92 return rcp(
93 new StringToIntegralParameterEntryValidator<EVerbosityLevel>(
94 tuple<std::string>(
95 getVerbosityLevelParameterValueName(VERB_DEFAULT),
96 getVerbosityLevelParameterValueName(VERB_NONE),
97 getVerbosityLevelParameterValueName(VERB_LOW),
98 getVerbosityLevelParameterValueName(VERB_MEDIUM),
99 getVerbosityLevelParameterValueName(VERB_HIGH),
100 getVerbosityLevelParameterValueName(VERB_EXTREME)
101 ),
102 tuple<std::string>(
103 "Use level set in code",
104 "Produce no output",
105 "Produce minimal output",
106 "Produce a little more output",
107 "Produce a higher level of output",
108 "Produce the highest level of output"
109 ),
110 tuple<EVerbosityLevel>(
111 VERB_DEFAULT,
112 VERB_NONE,
113 VERB_LOW,
114 VERB_MEDIUM,
115 VERB_HIGH,
116 VERB_EXTREME
117 ),
118 defaultParameterName
119 )
120 );
121}
122
123
124namespace Teuchos {
125
126
127//
128// BoolParameterEntryValidator
129//
130
131
132// Constructors
133
134
135BoolParameterEntryValidator::BoolParameterEntryValidator()
136{
137 finishInitialization();
138}
139
140
141// Local non-virtual validated lookup functions
142
143
145 const ParameterEntry &entry, const std::string &paramName,
146 const std::string &sublistName, const bool activeQuery
147 ) const
148{
149 const any &anyValue = entry.getAny(activeQuery);
150 if( anyValue.type() == typeid(bool) )
151 return any_cast<bool>(anyValue);
152 if( anyValue.type() == typeid(std::string) ) {
153 std::string str = any_cast<std::string>(anyValue);
154
155 // to fix - do we want to make this customizable?
156 if( str == "false" ) {
157 return false;
158 }
159 else if( str == "true" ) {
160 return true;
161 }
162
163 }
164 throwTypeError(entry,paramName,sublistName);
165 return 0; // Will never get here!
166}
167
169 ParameterList &paramList, const std::string &paramName,
170 const int defaultValue
171 ) const
172{
173 const ParameterEntry *entry = paramList.getEntryPtr(paramName);
174 if(entry) return getBool(*entry,paramName,paramList.name(),true);
176}
177
178// Overridden from ParameterEntryValidator
179
181{
182 return "boolValidator";
183}
184
186 std::string const & docString,
187 std::ostream & out
188 ) const
189{
190 StrUtils::printLines(out,"# ",docString);
191 out << "# Accepted types: " << acceptedTypesString_ << ".\n";
192}
193
194
197{
198 return null;
199}
200
201
203 ParameterEntry const& entry,
204 std::string const& paramName,
205 std::string const& sublistName
206 ) const
207{
208 // Validate that the parameter exists and can be converted to a bool.
209 getBool(entry, paramName, sublistName, false);
210}
211
212
214 std::string const& paramName,
215 std::string const& sublistName,
216 ParameterEntry * entry
217 ) const
218{
219 TEUCHOS_TEST_FOR_EXCEPT(0==entry);
220 entry->setValue(getBool(*entry,paramName,sublistName,false), false);
221}
222
223
224// private
225
226
227void BoolParameterEntryValidator::finishInitialization()
228{
229 std::ostringstream oss;
230 oss << "\"bool\"";
231 acceptedTypesString_ = oss.str();
232 oss << "\"string\"";
233 acceptedTypesString_ = oss.str();
234}
235
236
237void BoolParameterEntryValidator::throwTypeError(
238 ParameterEntry const& entry,
239 std::string const& paramName,
240 std::string const& sublistName
241 ) const
242{
243 const std::string &entryName = entry.getAny(false).typeName();
245 true, Exceptions::InvalidParameterType
246 ,"Error, the parameter {paramName=\""<<paramName<<"\""
247 ",type=\""<<entryName<<"\"}"
248 << "\nin the sublist \"" << sublistName << "\""
249 << "\nhas the wrong type."
250 << "\n\nThe accepted types are: " << acceptedTypesString_ << "!";
251 );
252}
253
254//
255// AnyNumberParameterEntryValidator
256//
257
258
259// Constructors
260
261
263 : preferredType_(PREFER_DOUBLE), acceptedTypes_(AcceptedTypes())
264{
265 finishInitialization();
266}
267
268
271 )
272 : preferredType_(preferredType), acceptedTypes_(acceptedTypes)
273{
274 finishInitialization();
275}
276
277
278// Local non-virtual validated lookup functions
279
280
282 const ParameterEntry &entry, const std::string &paramName,
283 const std::string &sublistName, const bool activeQuery
284 ) const
285{
286 const any &anyValue = entry.getAny(activeQuery);
287 if( acceptedTypes_.allowInt() && anyValue.type() == typeid(int) )
288 return any_cast<int>(anyValue);
289 if( acceptedTypes_.allowLongLong() && anyValue.type() == typeid(long long) )
291 if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
293 if( acceptedTypes_.allowString() && anyValue.type() == typeid(std::string) )
294 return convertStringToInt(any_cast<std::string>(anyValue));
295 throwTypeError(entry,paramName,sublistName);
296 return 0; // Will never get here!
297}
298
300 const ParameterEntry &entry, const std::string &paramName,
301 const std::string &sublistName, const bool activeQuery
302 ) const
303{
304 const any &anyValue = entry.getAny(activeQuery);
305 if( acceptedTypes_.allowInt() && anyValue.type() == typeid(int) )
307 if( acceptedTypes_.allowLongLong() && anyValue.type() == typeid(long long) )
309 if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
311 if( acceptedTypes_.allowString() && anyValue.type() == typeid(std::string) )
312 return convertStringToLongLong(any_cast<std::string>(anyValue));
313 throwTypeError(entry,paramName,sublistName);
314 return 0; // Will never get here!
315}
316
318 const ParameterEntry &entry, const std::string &paramName,
319 const std::string &sublistName, const bool activeQuery
320 ) const
321{
322 const any &anyValue = entry.getAny(activeQuery);
323 if( acceptedTypes_.allowInt() && anyValue.type() == typeid(int) )
325 if( acceptedTypes_.allowLongLong() && anyValue.type() == typeid(long long) )
327 if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
329 if( acceptedTypes_.allowString() && anyValue.type() == typeid(std::string) )
330 return convertStringToDouble(any_cast<std::string>(anyValue));
331 throwTypeError(entry,paramName,sublistName);
332 return 0.0; // Will never get here!
333}
334
335
337 const ParameterEntry &entry, const std::string &paramName,
338 const std::string &sublistName, const bool activeQuery
339 ) const
340{
341 const any &anyValue = entry.getAny(activeQuery);
342 if( acceptedTypes_.allowInt() && anyValue.type() == typeid(int) )
344 if( acceptedTypes_.allowLongLong() && anyValue.type() == typeid(long long) )
346 if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
348 if( acceptedTypes_.allowString() && anyValue.type() == typeid(std::string) )
350 throwTypeError(entry,paramName,sublistName);
351 return ""; // Will never get here!
352}
353
354
356 ParameterList &paramList, const std::string &paramName,
357 const int defaultValue
358 ) const
359{
360 const ParameterEntry *entry = paramList.getEntryPtr(paramName);
361 if(entry) return getInt(*entry,paramName,paramList.name(),true);
363}
364
366 ParameterList &paramList, const std::string &paramName,
367 const long long defaultValue
368 ) const
369{
370 const ParameterEntry *entry = paramList.getEntryPtr(paramName);
371 if(entry) return getLongLong(*entry,paramName,paramList.name(),true);
373}
374
376 ParameterList &paramList, const std::string &paramName,
377 const double defaultValue
378 ) const
379{
380 const ParameterEntry *entry = paramList.getEntryPtr(paramName);
381 if(entry) return getDouble(*entry,paramName,paramList.name(),true);
383}
384
385
387 ParameterList &paramList, const std::string &paramName,
388 const std::string &defaultValue
389 ) const
390{
391 const ParameterEntry *entry = paramList.getEntryPtr(paramName);
392 if(entry) return getString(*entry,paramName,paramList.name(),true);
394}
395
397{
398 return acceptedTypes_.allowInt();
399}
400
402{
403 return acceptedTypes_.allowLongLong();
404}
405
407{
408 return acceptedTypes_.allowDouble();
409}
410
412{
413 return acceptedTypes_.allowString();
414}
415
416
419{
420 return preferredType_;
421}
422
423
424// Overridden from ParameterEntryValidator
425
426
428{
429 return "anynumberValidator";
430}
431
432
434 std::string const & docString,
435 std::ostream & out
436 ) const
437{
438 StrUtils::printLines(out,"# ",docString);
439 out << "# Accepted types: " << acceptedTypesString_ << ".\n";
440}
441
442
448
449
451 ParameterEntry const& entry,
452 std::string const& paramName,
453 std::string const& sublistName
454 ) const
455{
456 // Validate that the parameter exists and can be converted to a double.
457 // NOTE: Even if the target type will be an 'int', we don't know that here
458 // so it will be better to assert that a 'double' can be created. The type
459 // 'double' has a very large exponent range and, subject to digit
460 // truncation, a 'double' can represent every 'int' value.
461 getDouble(entry, paramName, sublistName, false);
462}
463
464
466 std::string const& paramName,
467 std::string const& sublistName,
468 ParameterEntry * entry
469 ) const
470{
471 TEUCHOS_TEST_FOR_EXCEPT(0==entry);
472 switch(preferredType_) {
473 case PREFER_INT:
474 entry->setValue(
475 getInt(*entry,paramName,sublistName,false),
476 false // isDefault
477 );
478 break;
479 case PREFER_LONG_LONG:
480 entry->setValue(
481 getLongLong(*entry,paramName,sublistName,false),
482 false // isDefault
483 );
484 break;
485 case PREFER_DOUBLE:
486 entry->setValue(
487 getDouble(*entry,paramName,sublistName,false),
488 false // isDefault
489 );
490 break;
491 case PREFER_STRING:
492 entry->setValue(
493 getString(*entry,paramName,sublistName,false),
494 false // isDefault
495 );
496 break;
497 default:
498 TEUCHOS_TEST_FOR_EXCEPT("Error, Invalid EPreferredType value!");
499 }
500}
501
502
503// private
504
505
506void AnyNumberParameterEntryValidator::finishInitialization()
507{
508
509 std::ostringstream oss;
510 bool addedType = false;
511 if(acceptedTypes_.allowInt()) {
512 oss << "\"int\"";
513 addedType = true;
514 }
515 if(acceptedTypes_.allowLongLong()) {
516 oss << "\"long long\"";
517 addedType = true;
518 }
519 if(acceptedTypes_.allowDouble()) {
520 if(addedType) oss << ", ";
521 oss << "\"double\"";
522 addedType = true;
523 }
524 if(acceptedTypes_.allowString()) {
525 if(addedType) oss << ", ";
526 oss << "\"string\"";
527 addedType = true;
528 }
529 acceptedTypesString_ = oss.str();
530}
531
532
533void AnyNumberParameterEntryValidator::throwTypeError(
534 ParameterEntry const& entry,
535 std::string const& paramName,
536 std::string const& sublistName
537 ) const
538{
539 const std::string &entryName = entry.getAny(false).typeName();
541 true, Exceptions::InvalidParameterType
542 ,"Error, the parameter {paramName=\""<<paramName<<"\""
543 ",type=\""<<entryName<<"\"}"
544 << "\nin the sublist \"" << sublistName << "\""
545 << "\nhas the wrong type."
546 << "\n\nThe accepted types are: " << acceptedTypesString_ << "!";
547 );
548}
549
550
551RCP<AnyNumberParameterEntryValidator>
553{
554 return anyNumberParameterEntryValidator(
555 AnyNumberParameterEntryValidator::PREFER_INT,
557}
558
559
564
565
567{
568 return mustAlreadyExist_;
569}
570
572{
573 return EmptyNameOK_;
574}
575
577{
578 this->mustAlreadyExist_ = shouldFileExist;
579 return mustAlreadyExist_;
580}
581
583{
584 this->EmptyNameOK_ = isEmptyNameOK;
585 return EmptyNameOK_;
586}
587
590{
591 return null;
592}
593
594
595void FileNameValidator::validate(ParameterEntry const &entry, std::string const &paramName,
596 std::string const &sublistName) const
597{
598 const std::string &entryName = entry.getAny(false).typeName();
599 any anyValue = entry.getAny(true);
600 TEUCHOS_TEST_FOR_EXCEPTION(!(anyValue.type() == typeid(std::string) ),
602 "The \"" << paramName << "\"" <<
603 " parameter in the \"" << sublistName <<
604 "\" sublist is has an error." << std::endl << std::endl <<
605 "Error: The value that you entered was the wrong type." << std::endl <<
606 "Parameter: " << paramName << std::endl <<
607 "Type specified: " << entryName << std::endl <<
608 "Type accepted: " << typeid(std::string).name() <<
609 std::endl << std::endl);
610 if(mustAlreadyExist_ && !EmptyNameOK_){
611 std::string fileName = getValue<std::string>(entry);
612 TEUCHOS_TEST_FOR_EXCEPTION(!std::ifstream(fileName.c_str()),
614 "The \"" << paramName << "\"" <<
615 " parameter in the \"" << sublistName <<
616 "\" sublist is has an error." << std::endl << std::endl <<
617 "Error: The file must already exists. The value you entered does " <<
618 "not corresspond to an existing file name." << std::endl <<
619 "Parameter: " << paramName << std::endl <<
620 "File name specified: " << fileName << std::endl << std::endl);
621 }
622}
623
624
625const std::string FileNameValidator::getXMLTypeName() const
626{
627 return "FilenameValidator";
628}
629
630
632 std::string const &docString, std::ostream &out) const
633{
634 StrUtils::printLines(out,"# ",docString);
635 out << "# Validator Used: " << std::endl;
636 out << "# FileName Validator" << std::endl;
637}
638
639
643
644
648
649
654
655
658{
659 validStrings_ = rcp(new Array<std::string>(validStrings));
660 return validStrings_;
661}
662
663
666{
667 return validStrings_;
668}
669
670
672 ParameterEntry const &entry, std::string const &paramName,
673 std::string const &sublistName) const
674{
675 any anyValue = entry.getAny(true);
676 const std::string &entryName = entry.getAny(false).typeName();
677 TEUCHOS_TEST_FOR_EXCEPTION(!(anyValue.type() == typeid(std::string)) ,
679 "The \"" << paramName << "\"" <<
680 " parameter in the \"" << sublistName <<
681 "\" sublist is has an error." << std::endl << std::endl <<
682 "Error: The value that you entered was the wrong type." <<
683 "Parameter: " << paramName << std::endl <<
684 "Type specified: " << entryName << std::endl <<
685 "Type accepted: " << Teuchos::TypeNameTraits<std::string>::name() <<
686 std::endl);
687 if(!validStrings_.is_null()){
689 it = std::find(validStrings_->begin(),
690 validStrings_->end(), getValue<std::string>(entry));
691 TEUCHOS_TEST_FOR_EXCEPTION(it == validStrings_->end(),
693 "The \"" << paramName << "\"" <<
694 " parameter in the \"" << sublistName <<
695 "\" sublist is has an error." << std::endl << std::endl <<
696 "Error: The value that was entered doesn't fall with in "
697 "the range set by the validator." <<
698 "Parameter: " << paramName << std::endl <<
699 "Acceptable Values: " << *validStrings_ << std::endl <<
700 "Value entered: " << getValue<std::string>(entry) << std::endl <<
701 std::endl);
702 }
703}
704
705
706const std::string StringValidator::getXMLTypeName() const
707{
708 return "StringValidator";
709}
710
711
712void StringValidator::printDoc(std::string const &docString,
713 std::ostream &out) const
714{
715 Teuchos::StrUtils::printLines(out,"# ",docString);
716 out << "# Validator Used: " << std::endl;
717 out << "# String Validator" << std::endl;
718 if (validStrings_.get() && validStrings_->size()){
719 out << "# Acceptable Values: " << *validStrings_ << std::endl;
720 }
721}
722
723
727
728
729} // namespace Teuchos
730
731
732// Nonmember helper functions
733
735Teuchos::boolParameterEntryValidator()
736{
737 return rcp(new BoolParameterEntryValidator());
738}
739
741Teuchos::anyNumberParameterEntryValidator()
742{
743 return rcp(new AnyNumberParameterEntryValidator());
744}
745
746
748Teuchos::anyNumberParameterEntryValidator(
749 AnyNumberParameterEntryValidator::EPreferredType const preferredType,
750 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
751 )
752{
753 return rcp(
754 new AnyNumberParameterEntryValidator(
755 preferredType, acceptedTypes
756 )
757 );
758}
759
760void Teuchos::setIntParameter(
761 std::string const& paramName,
762 int const value, std::string const& docString,
763 ParameterList *paramList,
764 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
765 )
766{
767 TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
768 const RCP<const ParameterEntryValidator> paramEntryValidator =
769 anyNumberParameterEntryValidator(
770 AnyNumberParameterEntryValidator::PREFER_INT, acceptedTypes
771 );
772 paramList->set(paramName, value, docString, paramEntryValidator);
773}
774
775
776void Teuchos::setLongLongParameter(
777 std::string const& paramName,
778 long long const value, std::string const& docString,
779 ParameterList *paramList,
780 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
781 )
782{
783 TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
784 const RCP<const ParameterEntryValidator> paramEntryValidator =
785 anyNumberParameterEntryValidator(
786 AnyNumberParameterEntryValidator::PREFER_LONG_LONG, acceptedTypes
787 );
788 paramList->set(paramName, value, docString, paramEntryValidator);
789}
790
791
792void Teuchos::setDoubleParameter(
793 std::string const& paramName,
794 double const& value, std::string const& docString,
795 ParameterList *paramList,
796 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
797 )
798{
799 TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
800 const RCP<const ParameterEntryValidator> paramEntryValidator =
801 anyNumberParameterEntryValidator(
802 AnyNumberParameterEntryValidator::PREFER_DOUBLE, acceptedTypes
803 );
804 paramList->set(paramName, value, docString, paramEntryValidator);
805}
806
807
808void Teuchos::setNumericStringParameter(
809 std::string const& paramName,
810 std::string const& value, std::string const& docString,
811 ParameterList *paramList,
812 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
813 )
814{
815 TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
816 const RCP<const ParameterEntryValidator> paramEntryValidator =
817 anyNumberParameterEntryValidator(
818 AnyNumberParameterEntryValidator::PREFER_STRING, acceptedTypes
819 );
820 paramList->set(paramName, value, docString, paramEntryValidator);
821}
822
823
824int Teuchos::getIntParameter(
825 ParameterList const& paramList,
826 std::string const& paramName
827 )
828{
829 const ParameterEntry &entry = paramList.getEntry(paramName);
830 RCP<const AnyNumberParameterEntryValidator>
831 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
832 entry.validator()
833 );
834 if ( !is_null(anyNumValidator) )
835 return anyNumValidator->getInt(entry,paramName,paramList.name());
836 if ( typeid(int) == entry.getAny().type() )
837 return any_cast<int>(entry.getAny());
838 // Try the do the conversion which might fail!
839 const AnyNumberParameterEntryValidator myAnyNumValidator;
840 return myAnyNumValidator.getInt(entry,paramName,paramList.name());
841}
842
843
844long long Teuchos::getLongLongParameter(
845 ParameterList const& paramList,
846 std::string const& paramName
847 )
848{
849 const ParameterEntry &entry = paramList.getEntry(paramName);
850 RCP<const AnyNumberParameterEntryValidator>
851 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
852 entry.validator()
853 );
854 if ( !is_null(anyNumValidator) )
855 return anyNumValidator->getLongLong(entry,paramName,paramList.name());
856 if ( typeid(long long) == entry.getAny().type() )
857 return any_cast<long long>(entry.getAny());
858 // Try the do the conversion which might fail!
859 const AnyNumberParameterEntryValidator myAnyNumValidator;
860 return myAnyNumValidator.getLongLong(entry,paramName,paramList.name());
861}
862
863
864double Teuchos::getDoubleParameter(
865 ParameterList const& paramList,
866 std::string const& paramName
867 )
868{
869 const ParameterEntry &entry = paramList.getEntry(paramName);
870 RCP<const AnyNumberParameterEntryValidator>
871 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
872 entry.validator()
873 );
874 if ( !is_null(anyNumValidator) )
875 return anyNumValidator->getDouble(entry,paramName,paramList.name());
876 if ( typeid(double) == entry.getAny().type() )
877 return any_cast<double>(entry.getAny());
878 // Try the do the conversion which might fail!
879 const AnyNumberParameterEntryValidator myAnyNumValidator;
880 return myAnyNumValidator.getDouble(entry,paramName,paramList.name());
881}
882
883
884std::string Teuchos::getNumericStringParameter(
885 ParameterList const& paramList,
886 std::string const& paramName
887 )
888{
889 const ParameterEntry &entry = paramList.getEntry(paramName);
890 RCP<const AnyNumberParameterEntryValidator>
891 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
892 entry.validator()
893 );
894 if ( !is_null(anyNumValidator) )
895 return anyNumValidator->getString(entry,paramName,paramList.name());
896 if ( typeid(std::string) == entry.getAny().type() )
897 return any_cast<std::string>(entry.getAny());
898 // Try the do the conversion which might fail!
899 const AnyNumberParameterEntryValidator myAnyNumValidator;
900 return myAnyNumValidator.getString(entry,paramName,paramList.name());
901}
Definition of Teuchos::as, for conversions between types.
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
void printDoc(std::string const &docString, std::ostream &out) const
double getDouble(const ParameterEntry &entry, const std::string &paramName="", const std::string &sublistName="", const bool activeQuery=true) const
Get a double value from a parameter entry. will call std::stod.
long long getLongLong(const ParameterEntry &entry, const std::string &paramName="", const std::string &sublistName="", const bool activeQuery=true) const
Get a long long value from a parameter entry. will call std::stoll Note that std::stoll throws on bad...
std::string getString(const ParameterEntry &entry, const std::string &paramName="", const std::string &sublistName="", const bool activeQuery=true) const
Get a std::string value from a parameter entry.
AnyNumberParameterEntryValidator()
Construct with a preferrded type of double and accept all types.
int getInt(const ParameterEntry &entry, const std::string &paramName="", const std::string &sublistName="", const bool activeQuery=true) const
Get an integer value from a parameter entry. will call std::stoi Note that std::stoi throws on badly ...
bool isLongLongAllowed() const
Lookup whether or not long longs are allowed.
void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
bool isStringAllowed() const
Lookup whether or not strings are allowed.
bool isDoubleAllowed() const
Lookup whether or not doubles are allowed.
EPreferredType getPreferredType() const
Lookup the preferred type.
bool isIntAllowed() const
Lookup whether or not ints are allowed.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
std::vector< T >::const_iterator const_iterator
The type of a const forward iterator.
void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
bool getBool(const ParameterEntry &entry, const std::string &paramName="", const std::string &sublistName="", const bool activeQuery=true) const
Get bool value from a parameter entry.
void printDoc(std::string const &docString, std::ostream &out) const
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
bool fileEmptyNameOK() const
Gets the variable describing whether or not this validator is OK with file name being empty (even if ...
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
bool setFileEmptyNameOK(bool isEmptyNameOK)
Sets whether or not the validator is OK with empty file name (even if fileMustExist() returns true)
bool fileMustExist() const
Gets the variable describing whether or not this validator wants the file that is specified to alread...
FileNameValidator(bool mustAlreadyExist=mustAlreadyExistDefault())
Constructs a FileNameValidator.
bool setFileMustExist(bool shouldFileExist)
Sets whether or not the validator requires the file to already exist.
void printDoc(std::string const &docString, std::ostream &out) const
Abstract interface for an object that can validate a ParameterEntry's value.
This object is held as the "value" in the Teuchos::ParameterList std::map.
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
void setValue(T value, bool isDefault=false, const std::string &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method that uses the input value type to determine the type of parameter.
A list of parameters of arbitrary type.
Smart reference counting pointer class for automatic garbage collection.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object.
bool is_null() const
Returns true if the underlying pointer is null.
T * get() const
Get the raw C++ pointer to the underlying object.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
void printDoc(std::string const &docString, std::ostream &out) const
ValidStringsList setValidStrings(const Teuchos::Array< std::string > &validStrings)
Sets the Array of valid strings and returns what the current array of valid string now is.
Default traits class that just returns typeid(T).name().
static std::string toString(const double &x)
Write a double as a std::string.
Modified boost::any class, which is a container for a templated value.
std::string typeName() const
Return the name of the type.
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
@ VERB_MEDIUM
Generate more output.
@ VERB_HIGH
Generate a high level of output.
@ VERB_EXTREME
Generate the most output possible.
@ VERB_NONE
Generate no output.
@ VERB_DEFAULT
Generate output as defined by the object.
@ VERB_LOW
Generate only a minimal amount of output.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.