Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_StandardValidatorXMLConverters.cpp
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 
11 #include "Teuchos_RCP.hpp"
12 
17 namespace Teuchos {
18 
20  const XMLObject& /* xmlObj */,
21  const IDtoValidatorMap& /*validatorIDsMap*/) const
22 {
23  return boolParameterEntryValidator();
24 }
25 
26 
28  const RCP<const ParameterEntryValidator> /* validator */,
29  XMLObject& /* xmlObj */,
30  const ValidatortoIDMap& /*validatorIDsMap*/) const
31 {
32  //RCP<const AnyNumberParameterEntryValidator> castedValidator =
33  // rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(validator, true);
34 
35  // currently no action
36 }
37 
38 #ifdef HAVE_TEUCHOS_DEBUG
40 BoolValidatorXMLConverter::getDummyValidator() const{
42 }
43 #endif
44 
46  const XMLObject& xmlObj,
47  const IDtoValidatorMap& /*validatorIDsMap*/) const
48 {
49 
51  acceptedTypes.allowInt(xmlObj.getRequiredBool(getAllowIntAttributeName()));
52  acceptedTypes.allowDouble(
53  xmlObj.getRequiredBool(getAllowDoubleAttributeName()));
54  acceptedTypes.allowString(
55  xmlObj.getRequiredBool(getAllowStringAttributeName()));
56  return anyNumberParameterEntryValidator(
58  xmlObj.getRequired(getPrefferedTypeAttributeName())),
59  acceptedTypes);
60 }
61 
62 
64  const RCP<const ParameterEntryValidator> validator,
65  XMLObject& xmlObj,
66  const ValidatortoIDMap& /*validatorIDsMap*/) const
67 {
69  rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(validator, true);
70  xmlObj.addBool(
71  getAllowIntAttributeName(), castedValidator->isIntAllowed());
72  xmlObj.addBool(
73  getAllowDoubleAttributeName(), castedValidator->isDoubleAllowed());
74  xmlObj.addBool(
75  getAllowStringAttributeName(), castedValidator->isStringAllowed());
76  xmlObj.addAttribute(getPrefferedTypeAttributeName(),
77  castedValidator->getPrefferedTypeString(
78  castedValidator->getPreferredType()));
79 }
80 
81 #ifdef HAVE_TEUCHOS_DEBUG
83 AnyNumberValidatorXMLConverter::getDummyValidator() const{
85 }
86 #endif
87 
89  const XMLObject& xmlObj,
90  const IDtoValidatorMap& /*validatorIDsMap*/) const
91 {
92  return rcp(
94  xmlObj.getWithDefault<bool>(
95  getFileMustExistAttributeName(),
97  )
98  )
99  );
100 }
101 
102 
104  const RCP<const ParameterEntryValidator> validator,
105  XMLObject& xmlObj,
106  const ValidatortoIDMap& /*validatorIDsMap*/) const
107 {
108  RCP<const FileNameValidator> castedValidator =
109  rcp_dynamic_cast<const FileNameValidator>(validator);
110  xmlObj.addBool(
111  getFileMustExistAttributeName(), castedValidator->fileMustExist());
112 }
113 
114 
115 #ifdef HAVE_TEUCHOS_DEBUG
117 FileNameValidatorXMLConverter::getDummyValidator() const{
119 }
120 #endif
121 
122 
124  const XMLObject& xmlObj,
125  const IDtoValidatorMap& /*validatorIDsMap*/) const
126 {
127  Array<std::string> strings(xmlObj.numChildren());
128  if(xmlObj.numChildren()!=0){
129  for(int i=0; i<xmlObj.numChildren(); ++i){
130  XMLObject currentChild = xmlObj.getChild(i);
131  TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(),
133  "Error converting xmlObject to StringValidator." << std::endl <<
134  "Unrecognized tag: " << currentChild.getTag());
135  strings[i] = (currentChild.getRequired(getStringValueAttributeName()));
136  }
137  }
138  return rcp(new StringValidator(strings));
139 }
140 
141 
143  const RCP<const ParameterEntryValidator> validator,
144  XMLObject& xmlObj,
145  const ValidatortoIDMap& /*validatorIDsMap*/) const
146 {
147  RCP<const StringValidator> castedValidator =
148  rcp_dynamic_cast<const StringValidator>(validator);
149 
150  if(!is_null(validator->validStringValues())){
152  validator->validStringValues()->begin();
153  for(; it != validator->validStringValues()->end(); ++it){
154  XMLObject stringTag(getStringTagName());
155  stringTag.addAttribute(getStringValueAttributeName(), *it);
156  xmlObj.addChild(stringTag);
157  }
158  }
159 }
160 
161 
162 #ifdef HAVE_TEUCHOS_DEBUG
164 StringValidatorXMLConverter::getDummyValidator() const{
166 }
167 #endif
168 
169 } // namespace Teuchos
170 
static bool mustAlreadyExistDefault()
The default value of the mustAlreadyExist parameter in the constructor.
A collection of standard ValidatorXMLConverters.
const std::string & getTag() const
Return the tag of the current node.
Maps Validators to integers.
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
void addBool(const std::string &name, bool val)
Add a bool as an attribute.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void addChild(const XMLObject &child)
Add a child node to the node.
AcceptedTypes & allowString(bool _allowString)
Set allow an std::string value or not.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
static EPreferredType getPrefferedTypeStringEnum(const std::string &enumString)
Gets the preferred type enum associated with a give string.
AcceptedTypes & allowInt(bool _allowInt)
Set allow an int value or not.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
A class for mapping validators to integers.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
bool getRequiredBool(const std::string &name) const
Get a required attribute, returning it as a bool.
int numChildren() const
Return the number of child nodes owned by this node.
AcceptedTypes & allowDouble(bool _allowDouble)
Set allow a double value or not.
Smart reference counting pointer class for automatic garbage collection.
bool fileMustExist() const
Gets the variable describing whether or not this validator wants the file that is specified to alread...
Thrown when xml tag is encountered that is either unrecognized or inappropriate for a given context...
const XMLObject & getChild(int i) const
Return the i-th child node.
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
T getWithDefault(const std::string &name, const T &defaultValue) const
Get an attribute, assigning a default value if the requested attribute does not exist.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
Reference-counted pointer class and non-member templated function implementations.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.