Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ParameterEntryValidator.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_PARAMETER_ENTRY_VALIDATOR_H
11 #define TEUCHOS_PARAMETER_ENTRY_VALIDATOR_H
12 
13 #include "Teuchos_RCP.hpp"
14 #include "Teuchos_Array.hpp"
15 #include "Teuchos_XMLObject.hpp"
16 #include "Teuchos_Describable.hpp"
17 
18 namespace Teuchos {
19 
20 
21 #ifndef DOXYGEN_SHOULD_SKIP_THIS
22 class ParameterEntry;
23 #endif
24 
32 {
33 public:
34 
37 
39  typedef unsigned int ValidatorID;
40 
43 
45 
48 
55  virtual const std::string getXMLTypeName() const=0;
56 
68  virtual void printDoc(
69  std::string const& docString,
70  std::ostream &out
71  ) const = 0;
72 
81  virtual ValidStringsList validStringValues() const = 0;
82 
94  virtual void validate(
95  ParameterEntry const& entry,
96  std::string const& paramName,
97  std::string const& sublistName
98  ) const = 0;
99 
113  virtual void validateAndModify(
114  std::string const& paramName,
115  std::string const& sublistName,
116  ParameterEntry * entry
117  ) const
118  {
119  TEUCHOS_TEST_FOR_EXCEPT(0==entry);
120  this->validate(*entry,paramName,sublistName);
121  }
122 
123  double convertStringToDouble(std::string str) const
124  {
125  #ifdef HAVE_TEUCHOSCORE_CXX11
126  size_t idx = 0;
127  double result = std::stod(str, &idx); // can throw std::invalid_argument
128  if(idx != str.length()) { // check for extra bad format characters
129  throw std::invalid_argument( "String: '" + str + "' had bad formatting for converting to a double." );
130  }
131  return result;
132  #else
133  return std::atof(str.c_str());
134  #endif
135  }
136 
137  int convertStringToInt(std::string str) const
138  {
139  #ifdef HAVE_TEUCHOSCORE_CXX11
140  size_t idx = 0;
141  int result = std::stoi(str, &idx); // can throw std::invalid_argument
142  if(idx != str.length()) { // check for extra bad format characters
143  throw std::invalid_argument( "String: '" + str + "' had bad formatting for converting to an int." );
144  }
145  return result;
146  #else
147  return std::atoi(str.c_str());
148  #endif
149  }
150 
151  int convertStringToLongLong(std::string str) const
152  {
153  size_t idx = 0;
154  long long result = std::stoll(str, &idx); // can throw std::invalid_argument
155  if(idx != str.length()) { // check for extra bad format characters
156  throw std::invalid_argument( "String: '" + str + "' had bad formatting for converting to a long long." );
157  }
158  return result;
159  }
160 
161 };
162 
163 
164 } // namespace Teuchos
165 
166 
167 #endif // TEUCHOS_PARAMETER_ENTRY_VALIDATOR_H
This object is held as the "value" in the Teuchos::ParameterList std::map.
virtual void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
Validate and perhaps modify a parameter entry's value.
double convertStringToDouble(std::string str) const
#define TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT
Abstract interface for an object that can validate a ParameterEntry's value.
Templated array class derived from the STL std::vector.
RCP< const Array< std::string > > ValidStringsList
Base class for all objects that can describe themselves.
Smart reference counting pointer class for automatic garbage collection.
Reference-counted pointer class and non-member templated function implementations.
#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...
An object representation of a subset of XML data.