Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_ParameterEntryValidator.hpp
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
18namespace Teuchos {
19
20
21#ifndef DOXYGEN_SHOULD_SKIP_THIS
22class ParameterEntry;
23#endif
24
31class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ParameterEntryValidator : public Describable
32{
33public:
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
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
Templated array class derived from the STL std::vector.
Reference-counted pointer class and non-member templated function implementations.
An object representation of a subset of XML data.
Base class for all objects that can describe themselves.
Abstract interface for an object that can validate a ParameterEntry's value.
virtual void printDoc(std::string const &docString, std::ostream &out) const =0
Print documentation for this parameter.
virtual ValidStringsList validStringValues() const =0
Return an array of strings of valid values if applicable.
virtual void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const =0
Validate a parameter entry value and throw std::exception (with a great error message) if validation ...
virtual void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
Validate and perhaps modify a parameter entry's value.
virtual const std::string getXMLTypeName() const =0
Get a string that should be used as a value of the type attribute when serializing it to XML.
RCP< const Array< std::string > > ValidStringsList
This object is held as the "value" in the Teuchos::ParameterList std::map.
Smart reference counting pointer class for automatic garbage collection.
#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.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...