Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ParameterEntryValidator.hpp
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 
43 #ifndef TEUCHOS_PARAMETER_ENTRY_VALIDATOR_H
44 #define TEUCHOS_PARAMETER_ENTRY_VALIDATOR_H
45 
46 #include "Teuchos_RCP.hpp"
47 #include "Teuchos_Array.hpp"
48 #include "Teuchos_XMLObject.hpp"
49 #include "Teuchos_Describable.hpp"
50 
51 namespace Teuchos {
52 
53 
54 #ifndef DOXYGEN_SHOULD_SKIP_THIS
55 class ParameterEntry;
56 #endif
57 
64 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ParameterEntryValidator : public Describable
65 {
66 public:
67 
70 
72  typedef unsigned int ValidatorID;
73 
76 
78 
81 
88  virtual const std::string getXMLTypeName() const=0;
89 
101  virtual void printDoc(
102  std::string const& docString,
103  std::ostream &out
104  ) const = 0;
105 
114  virtual ValidStringsList validStringValues() const = 0;
115 
127  virtual void validate(
128  ParameterEntry const& entry,
129  std::string const& paramName,
130  std::string const& sublistName
131  ) const = 0;
132 
146  virtual void validateAndModify(
147  std::string const& paramName,
148  std::string const& sublistName,
149  ParameterEntry * entry
150  ) const
151  {
152  TEUCHOS_TEST_FOR_EXCEPT(0==entry);
153  this->validate(*entry,paramName,sublistName);
154  }
155 
156  double convertStringToDouble(std::string str) const
157  {
158  #ifdef HAVE_TEUCHOSCORE_CXX11
159  size_t idx = 0;
160  double result = std::stod(str, &idx); // can throw std::invalid_argument
161  if(idx != str.length()) { // check for extra bad format characters
162  throw std::invalid_argument( "String: '" + str + "' had bad formatting for converting to a double." );
163  }
164  return result;
165  #else
166  return std::atof(str.c_str());
167  #endif
168  }
169 
170  int convertStringToInt(std::string str) const
171  {
172  #ifdef HAVE_TEUCHOSCORE_CXX11
173  size_t idx = 0;
174  int result = std::stoi(str, &idx); // can throw std::invalid_argument
175  if(idx != str.length()) { // check for extra bad format characters
176  throw std::invalid_argument( "String: '" + str + "' had bad formatting for converting to an int." );
177  }
178  return result;
179  #else
180  return std::atoi(str.c_str());
181  #endif
182  }
183 
184  int convertStringToLongLong(std::string str) const
185  {
186  size_t idx = 0;
187  long long result = std::stoll(str, &idx); // can throw std::invalid_argument
188  if(idx != str.length()) { // check for extra bad format characters
189  throw std::invalid_argument( "String: '" + str + "' had bad formatting for converting to a long long." );
190  }
191  return result;
192  }
193 
194 };
195 
196 
197 } // namespace Teuchos
198 
199 
200 #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.
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.