Teuchos - Trilinos Tools Package  Version of the Day
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 
46 std::string Teuchos::getVerbosityLevelParameterValueName(
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  >
88 Teuchos::verbosityLevelParameterEntryValidator(
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,
117  ),
118  defaultParameterName
119  )
120  );
121 }
122 
123 
124 namespace Teuchos {
125 
126 
127 //
128 // BoolParameterEntryValidator
129 //
130 
131 
132 // Constructors
133 
134 
135 BoolParameterEntryValidator::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);
175  return paramList.get(paramName,defaultValue);
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 
227 void BoolParameterEntryValidator::finishInitialization()
228 {
229  std::ostringstream oss;
230  oss << "\"bool\"";
231  acceptedTypesString_ = oss.str();
232  oss << "\"string\"";
233  acceptedTypesString_ = oss.str();
234 }
235 
236 
237 void 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 
270  EPreferredType const preferredType, AcceptedTypes const& acceptedTypes
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) )
290  return as<int>(any_cast<long long>(anyValue));
291  if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
292  return as<int>(any_cast<double>(anyValue));
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) )
306  return as<long long>(any_cast<int>(anyValue));
307  if( acceptedTypes_.allowLongLong() && anyValue.type() == typeid(long long) )
308  return any_cast<long long>(anyValue);
309  if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
310  return as<int>(any_cast<double>(anyValue));
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) )
324  return as<double>(any_cast<int>(anyValue));
325  if( acceptedTypes_.allowLongLong() && anyValue.type() == typeid(long long) )
326  return as<double>(any_cast<long long>(anyValue));
327  if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
328  return any_cast<double>(anyValue);
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) )
343  return Utils::toString(any_cast<int>(anyValue));
344  if( acceptedTypes_.allowLongLong() && anyValue.type() == typeid(long long) )
345  return Utils::toString(any_cast<long long>(anyValue));
346  if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
347  return Utils::toString(any_cast<double>(anyValue));
348  if( acceptedTypes_.allowString() && anyValue.type() == typeid(std::string) )
349  return any_cast<std::string>(anyValue);
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);
362  return paramList.get(paramName,defaultValue);
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);
372  return paramList.get(paramName,defaultValue);
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);
382  return paramList.get(paramName,defaultValue);
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);
393  return paramList.get(paramName,defaultValue);
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 
445 {
446  return null;
447 }
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 
506 void 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 
533 void 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 
551 RCP<AnyNumberParameterEntryValidator>
553 {
554  return anyNumberParameterEntryValidator(
555  AnyNumberParameterEntryValidator::PREFER_INT,
557 }
558 
559 
561  : ParameterEntryValidator(), mustAlreadyExist_(mustAlreadyExist),
562  EmptyNameOK_(false)
563 {}
564 
565 
567 {
568  return mustAlreadyExist_;
569 }
570 
572 {
573  return EmptyNameOK_;
574 }
575 
576 bool FileNameValidator::setFileMustExist(bool shouldFileExist)
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 
595 void 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 
625 const 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 
641  return rcp(new FileNameValidator(true));
642 }
643 
644 
646  : ParameterEntryValidator(), validStrings_(NULL)
647 {}
648 
649 
652  validStrings_(rcp(new Array<std::string>(validStrings)))
653 {}
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 
706 const std::string StringValidator::getXMLTypeName() const
707 {
708  return "StringValidator";
709 }
710 
711 
712 void 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 
725  return rcp(new StringValidator(tuple<std::string>("")));
726 }
727 
728 
729 } // namespace Teuchos
730 
731 
732 // Nonmmeber helper functions
733 
735 Teuchos::boolParameterEntryValidator()
736 {
737  return rcp(new BoolParameterEntryValidator());
738 }
739 
741 Teuchos::anyNumberParameterEntryValidator()
742 {
743  return rcp(new AnyNumberParameterEntryValidator());
744 }
745 
746 
748 Teuchos::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 
760 void 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 =
770  AnyNumberParameterEntryValidator::PREFER_INT, acceptedTypes
771  );
772  paramList->set(paramName, value, docString, paramEntryValidator);
773 }
774 
775 
776 void Teuchos::setDoubleParameter(
777  std::string const& paramName,
778  double 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 =
786  AnyNumberParameterEntryValidator::PREFER_DOUBLE, acceptedTypes
787  );
788  paramList->set(paramName, value, docString, paramEntryValidator);
789 }
790 
791 
792 void Teuchos::setNumericStringParameter(
793  std::string const& paramName,
794  std::string 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 =
802  AnyNumberParameterEntryValidator::PREFER_STRING, acceptedTypes
803  );
804  paramList->set(paramName, value, docString, paramEntryValidator);
805 }
806 
807 
808 int Teuchos::getIntParameter(
809  ParameterList const& paramList,
810  std::string const& paramName
811  )
812 {
813  const ParameterEntry &entry = paramList.getEntry(paramName);
814  RCP<const AnyNumberParameterEntryValidator>
815  anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
816  entry.validator()
817  );
818  if ( !is_null(anyNumValidator) )
819  return anyNumValidator->getInt(entry,paramName,paramList.name());
820  if ( typeid(int) == entry.getAny().type() )
821  return any_cast<int>(entry.getAny());
822  // Try the do the conversion which might fail!
823  const AnyNumberParameterEntryValidator myAnyNumValidator;
824  return myAnyNumValidator.getInt(entry,paramName,paramList.name());
825 }
826 
827 
828 double Teuchos::getDoubleParameter(
829  ParameterList const& paramList,
830  std::string const& paramName
831  )
832 {
833  const ParameterEntry &entry = paramList.getEntry(paramName);
834  RCP<const AnyNumberParameterEntryValidator>
835  anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
836  entry.validator()
837  );
838  if ( !is_null(anyNumValidator) )
839  return anyNumValidator->getDouble(entry,paramName,paramList.name());
840  if ( typeid(double) == entry.getAny().type() )
841  return any_cast<double>(entry.getAny());
842  // Try the do the conversion which might fail!
843  const AnyNumberParameterEntryValidator myAnyNumValidator;
844  return myAnyNumValidator.getDouble(entry,paramName,paramList.name());
845 }
846 
847 
848 std::string Teuchos::getNumericStringParameter(
849  ParameterList const& paramList,
850  std::string const& paramName
851  )
852 {
853  const ParameterEntry &entry = paramList.getEntry(paramName);
854  RCP<const AnyNumberParameterEntryValidator>
855  anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
856  entry.validator()
857  );
858  if ( !is_null(anyNumValidator) )
859  return anyNumValidator->getString(entry,paramName,paramList.name());
860  if ( typeid(std::string) == entry.getAny().type() )
861  return any_cast<std::string>(entry.getAny());
862  // Try the do the conversion which might fail!
863  const AnyNumberParameterEntryValidator myAnyNumValidator;
864  return myAnyNumValidator.getString(entry,paramName,paramList.name());
865 }
Teuchos::AnyNumberParameterEntryValidator::getXMLTypeName
const std::string getXMLTypeName() const
Definition: Teuchos_StandardParameterEntryValidators.cpp:427
Teuchos::DummyObjectGetter::getDummyObject
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
Definition: Teuchos_DummyObjectGetter.hpp:60
Teuchos::TypeNameTraits::name
static std::string name()
Definition: Teuchos_TypeNameTraits.hpp:88
Teuchos::FileNameValidator::getXMLTypeName
const std::string getXMLTypeName() const
Definition: Teuchos_StandardParameterEntryValidators.cpp:625
Teuchos::BoolParameterEntryValidator::getBool
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.
Definition: Teuchos_StandardParameterEntryValidators.cpp:144
Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes::allowInt
AcceptedTypes & allowInt(bool _allowInt)
Set allow an int value or not.
Definition: Teuchos_StandardParameterEntryValidators.hpp:732
Teuchos::StringValidator::setValidStrings
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.
Definition: Teuchos_StandardParameterEntryValidators.cpp:657
TEUCHOS_TEST_FOR_EXCEPT
#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.
Definition: Teuchos_TestForException.hpp:307
Teuchos::BoolParameterEntryValidator::validateAndModify
void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:213
Teuchos::VERB_NONE
Generate no output.
Definition: Teuchos_VerbosityLevel.hpp:64
Teuchos::FileNameValidator::setFileEmptyNameOK
bool setFileEmptyNameOK(bool isEmptyNameOK)
Sets whether or not the validator is OK with empty file name (even if fileMustExist() returns true)
Definition: Teuchos_StandardParameterEntryValidators.cpp:582
Teuchos::ParameterList::getEntryPtr
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
Definition: Teuchos_ParameterList.hpp:1004
Teuchos::StringValidator::validate
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:671
TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Definition: Teuchos_TestForException.hpp:246
Teuchos::ArrayRCP::is_null
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
Teuchos::RCP::rcp
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.
Teuchos::VERB_DEFAULT
Generate output as defined by the object.
Definition: Teuchos_VerbosityLevel.hpp:63
Teuchos::StringValidator
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
Definition: Teuchos_StandardParameterEntryValidators.hpp:1864
Teuchos::AnyNumberParameterEntryValidator::validate
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:450
Teuchos_as.hpp
Definition of Teuchos::as, for conversions between types.
Teuchos::AnyNumberParameterEntryValidator::getInt
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 ...
Definition: Teuchos_StandardParameterEntryValidators.cpp:281
Teuchos::ParameterEntryValidator
Abstract interface for an object that can validate a ParameterEntry's value.
Definition: Teuchos_ParameterEntryValidator.hpp:64
Teuchos::AnyNumberParameterEntryValidator::getDouble
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.
Definition: Teuchos_StandardParameterEntryValidators.cpp:317
Teuchos::ParameterList::get
T & get(const std::string &name, T def_value)
Return the parameter's value, or the default value if it is not there.
Definition: Teuchos_ParameterList.hpp:901
Teuchos::VERB_LOW
Generate only a minimal amount of output.
Definition: Teuchos_VerbosityLevel.hpp:65
Teuchos::AnyNumberParameterEntryValidator::getPreferredType
EPreferredType getPreferredType() const
Lookup the preferred type.
Definition: Teuchos_StandardParameterEntryValidators.cpp:418
Teuchos::rcp
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Definition: Teuchos_RCPDecl.hpp:1224
Teuchos::FileNameValidator::fileMustExist
bool fileMustExist() const
Gets the variable describing whether or not this validator wants the file that is specified to alread...
Definition: Teuchos_StandardParameterEntryValidators.cpp:566
Teuchos::EVerbosityLevel
EVerbosityLevel
Verbosity level.
Definition: Teuchos_VerbosityLevel.hpp:62
Teuchos::Exceptions::InvalidParameterType
Definition: Teuchos_ParameterListExceptions.hpp:73
Teuchos::any::type
const std::type_info & type() const
Return the type of value being stored.
Definition: Teuchos_any.hpp:208
Teuchos::FileNameValidator::FileNameValidator
FileNameValidator(bool mustAlreadyExist=mustAlreadyExistDefault())
Constructs a FileNameValidator.
Definition: Teuchos_StandardParameterEntryValidators.cpp:560
Teuchos::RCP
Smart reference counting pointer class for automatic garbage collection.
Definition: Teuchos_RCPDecl.hpp:429
Teuchos::FileNameValidator::validStringValues
ValidStringsList validStringValues() const
Definition: Teuchos_StandardParameterEntryValidators.cpp:589
Teuchos::AnyNumberParameterEntryValidator::getString
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.
Definition: Teuchos_StandardParameterEntryValidators.cpp:336
Teuchos::VERB_HIGH
Generate a high level of output.
Definition: Teuchos_VerbosityLevel.hpp:67
Teuchos::StringValidator::getXMLTypeName
const std::string getXMLTypeName() const
Definition: Teuchos_StandardParameterEntryValidators.cpp:706
Teuchos::StringValidator::printDoc
void printDoc(std::string const &docString, std::ostream &out) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:712
Teuchos::Array< std::string >
Teuchos::FileNameValidator::setFileMustExist
bool setFileMustExist(bool shouldFileExist)
Sets whether or not the validator requires the file to already exist.
Definition: Teuchos_StandardParameterEntryValidators.cpp:576
Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes::allowLongLong
AcceptedTypes & allowLongLong(bool _allowLongLong)
Set allow an long long value or not.
Definition: Teuchos_StandardParameterEntryValidators.hpp:735
Teuchos::ParameterEntry::setValue
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.
Definition: Teuchos_ParameterEntry.hpp:337
Teuchos::VERB_MEDIUM
Generate more output.
Definition: Teuchos_VerbosityLevel.hpp:66
Teuchos::ParameterEntry::getAny
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
Definition: Teuchos_ParameterEntry.hpp:364
Teuchos::AnyNumberParameterEntryValidator::validStringValues
ValidStringsList validStringValues() const
Definition: Teuchos_StandardParameterEntryValidators.cpp:444
Teuchos::BoolParameterEntryValidator::printDoc
void printDoc(std::string const &docString, std::ostream &out) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:185
Teuchos::AnyNumberParameterEntryValidator::isLongLongAllowed
bool isLongLongAllowed() const
Lookup whether or not long longs are allowed.
Definition: Teuchos_StandardParameterEntryValidators.cpp:401
Teuchos::StringValidator::StringValidator
StringValidator()
Constructs a StringValidator.
Definition: Teuchos_StandardParameterEntryValidators.cpp:645
Teuchos::StrUtils::printLines
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
Definition: Teuchos_StrUtils.cpp:412
Teuchos::AnyNumberParameterEntryValidator::isDoubleAllowed
bool isDoubleAllowed() const
Lookup whether or not doubles are allowed.
Definition: Teuchos_StandardParameterEntryValidators.cpp:406
Teuchos::AnyNumberParameterEntryValidator::validateAndModify
void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:465
Teuchos::RCP::is_null
bool is_null() const
Returns true if the underlying pointer is null.
Definition: Teuchos_RCP.hpp:336
Teuchos::StringToIntegralParameterEntryValidator
Standard implementation of a ParameterEntryValidator that maps from a list of strings to an enum or i...
Definition: Teuchos_StandardParameterEntryValidators.hpp:92
Teuchos::AnyNumberParameterEntryValidator::getLongLong
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...
Definition: Teuchos_StandardParameterEntryValidators.cpp:299
Teuchos::FileNameValidator
Validate a file name entry.
Definition: Teuchos_StandardParameterEntryValidators.hpp:1723
Teuchos::StringValidator::validStringValues
ValidStringsList validStringValues() const
Definition: Teuchos_StandardParameterEntryValidators.cpp:665
Teuchos::Exceptions::InvalidParameterValue
Definition: Teuchos_ParameterListExceptions.hpp:79
Teuchos::any::typeName
std::string typeName() const
Return the name of the type.
Definition: Teuchos_any.hpp:214
Teuchos::FileNameValidator::fileEmptyNameOK
bool fileEmptyNameOK() const
Gets the variable describing whether or not this validator is OK with file name being empty (even if ...
Definition: Teuchos_StandardParameterEntryValidators.cpp:571
Teuchos::BoolParameterEntryValidator::getXMLTypeName
const std::string getXMLTypeName() const
Definition: Teuchos_StandardParameterEntryValidators.cpp:180
Teuchos::RCP::get
T * get() const
Get the raw C++ pointer to the underlying object.
Definition: Teuchos_RCP.hpp:363
Teuchos::AnyNumberParameterEntryValidator::isIntAllowed
bool isIntAllowed() const
Lookup whether or not ints are allowed.
Definition: Teuchos_StandardParameterEntryValidators.cpp:396
Teuchos::ParameterList::name
const std::string & name() const
The name of this ParameterList.
Definition: Teuchos_ParameterList.hpp:1056
Teuchos::ParameterList
A list of parameters of arbitrary type.
Definition: Teuchos_ParameterList.hpp:132
Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes::allowDouble
AcceptedTypes & allowDouble(bool _allowDouble)
Set allow a double value or not.
Definition: Teuchos_StandardParameterEntryValidators.hpp:738
Teuchos::AnyNumberParameterEntryValidator::anyNumberParameterEntryValidator
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP< AnyNumberParameterEntryValidator > anyNumberParameterEntryValidator()
Nonmember constructor AnyNumberParameterEntryValidator.
Teuchos::BoolParameterEntryValidator::validate
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:202
Teuchos
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Teuchos::Utils::toString
static std::string toString(const double &x)
Write a double as a std::string.
Definition: Teuchos_Utils.cpp:104
Teuchos::BoolParameterEntryValidator::validStringValues
ValidStringsList validStringValues() const
Definition: Teuchos_StandardParameterEntryValidators.cpp:196
Teuchos::FileNameValidator::printDoc
void printDoc(std::string const &docString, std::ostream &out) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:631
Teuchos::VERB_EXTREME
Generate the most output possible.
Definition: Teuchos_VerbosityLevel.hpp:68
Teuchos::ParameterEntry
This object is held as the "value" in the Teuchos::ParameterList std::map.
Definition: Teuchos_ParameterEntry.hpp:67
TEUCHOS_TEST_FOR_EXCEPTION
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Definition: Teuchos_TestForException.hpp:170
Teuchos::AnyNumberParameterEntryValidator::EPreferredType
EPreferredType
Determines what type is the preferred type.
Definition: Teuchos_StandardParameterEntryValidators.hpp:718
Teuchos::FileNameValidator::validate
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:595
Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes::allowString
AcceptedTypes & allowString(bool _allowString)
Set allow an std::string value or not.
Definition: Teuchos_StandardParameterEntryValidators.hpp:741
Teuchos::AnyNumberParameterEntryValidator::printDoc
void printDoc(std::string const &docString, std::ostream &out) const
Definition: Teuchos_StandardParameterEntryValidators.cpp:433
Teuchos::AnyNumberParameterEntryValidator::AnyNumberParameterEntryValidator
AnyNumberParameterEntryValidator()
Construct with a preferrded type of double and accept all types.
Definition: Teuchos_StandardParameterEntryValidators.cpp:262
Teuchos::AnyNumberParameterEntryValidator::isStringAllowed
bool isStringAllowed() const
Lookup whether or not strings are allowed.
Definition: Teuchos_StandardParameterEntryValidators.cpp:411
Teuchos::any
Modified boost::any class, which is a container for a templated value.
Definition: Teuchos_any.hpp:154
Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes
Determines the types that are accepted.
Definition: Teuchos_StandardParameterEntryValidators.hpp:722