Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_StandardValidatorXMLConverters.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_STANDARDVALIDATORXMLCONVERTERS_HPP
11#define TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
12
16
18#include "Teuchos_StandardParameterEntryValidators.hpp"
22
23
24namespace Teuchos {
25
26
65template<class IntegralType>
68{
69
70public:
71
74
77 const XMLObject& xmlObj,
78 const IDtoValidatorMap& validatorIDsMap) const;
79
83 XMLObject& xmlObj,
84 const ValidatortoIDMap& validatorIDsMap) const;
85
86 #ifdef HAVE_TEUCHOS_DEBUG
89 getDummyValidator() const{
90 return DummyObjectGetter<
92 }
93 #endif
94
96
97private:
98
101
103 static const std::string& getIntegralValueAttributeName() {
104 static const std::string integralValueAttributeName_ = "integralValue";
105 return integralValueAttributeName_;
106 }
107
109 static const std::string& getStringTagName() {
110 static const std::string stringTagName_ = "String";
111 return stringTagName_;
112 }
113
115 static const std::string& getStringValueAttributeName() {
116 static const std::string stringValueAttributeName_ = "stringValue";
117 return stringValueAttributeName_;
118 }
119
121 static const std::string& getStringDocAttributeName() {
122 static const std::string stringDocAttributeName_ = "stringDoc";
123 return stringDocAttributeName_;
124 }
125
127 static const std::string& getDefaultParameterAttributeName() {
128 static const std::string defaultParameterAttributeName_ =
129 "defaultParameterName";
130 return defaultParameterAttributeName_;
131 }
132
134 static const std::string& getCaseSensitiveAttributeName() {
135 static const std::string caseSensitiveAttributeName_ =
136 "caseSensitive";
137 return caseSensitiveAttributeName_;
138 }
139
141
142};
143
144
145//
146// Implementations
147//
148
149
150template<class IntegralType>
153 const XMLObject& xmlObj,
154 const IDtoValidatorMap& /*validatorIDsMap*/) const
155{
156 Array<std::string> strings;
157 Array<std::string> stringDocs;
158 Array<IntegralType> integralValues;
159 for (int i=0; i<xmlObj.numChildren(); ++i) {
160 XMLObject currentChild = xmlObj.getChild(i);
161 TEUCHOS_TEST_FOR_EXCEPTION(currentChild.getTag() != getStringTagName(),
163 "Error converting xmlObject to "
164 "StringToIntegralParameterEntryValidator." << std::endl <<
165 "Unrecognized tag: " << currentChild.getTag());
166 strings.append(currentChild.getRequired(getStringValueAttributeName()));
167 if (currentChild.hasAttribute(getIntegralValueAttributeName())) {
168 integralValues.append(
169 currentChild.getRequired<IntegralType>(
170 getIntegralValueAttributeName()));
171 }
172 if (currentChild.hasAttribute(getStringDocAttributeName())) {
173 stringDocs.append(
174 currentChild.getRequired<std::string>(getStringDocAttributeName()));
175 }
176 }
177 std::string defaultParameterName =
178 xmlObj.getRequired(getDefaultParameterAttributeName());
179
180 // The "caseSensitive" attribute is not required. It is true by default.
181 const bool caseSensitive =
182 xmlObj.getWithDefault<bool> (getCaseSensitiveAttributeName (), true);
183
185 if (stringDocs.size() != 0 && integralValues.size() != 0) {
186 return rcp (new ret_type (strings, stringDocs, integralValues (), defaultParameterName, caseSensitive));
187 }
188 else if (integralValues.size() != 0) {
189 return rcp (new ret_type (strings, integralValues(), defaultParameterName, caseSensitive));
190 }
191 else {
192 return rcp (new ret_type (strings, defaultParameterName, caseSensitive));
193 }
194}
195
196
197template<class IntegralType>
200 XMLObject& xmlObj,
201 const ValidatortoIDMap& /*validatorIDsMap*/) const
202{
204 castedValidator =
207 validator, true);
208
209 RCP<const Array<std::string> > stringValues =
210 castedValidator->validStringValues();
211 RCP<const Array<std::string> > stringDocValues =
212 castedValidator->getStringDocs();
213
214 bool hasStringDocs =
215 !(stringDocValues.is_null()) && (stringDocValues->size() != 0);
216 for (int i =0; i<stringValues->size(); ++i) {
217 XMLObject stringTag(getStringTagName());
218 stringTag.addAttribute(getStringValueAttributeName(), (*stringValues)[i]);
219 stringTag.addAttribute(getIntegralValueAttributeName(),
220 castedValidator->getIntegralValue((*stringValues)[i]));
221 if (hasStringDocs) {
222 stringTag.addAttribute(
223 getStringDocAttributeName(), (*stringDocValues)[i]);
224 }
225 xmlObj.addChild(stringTag);
226 }
227 xmlObj.addAttribute(getDefaultParameterAttributeName(),
228 castedValidator->getDefaultParameterName());
229
230 // Add "caseSensitive" bool attribute here.
231 const bool caseSensitive = castedValidator->isCaseSensitive ();
232 xmlObj.addBool (getCaseSensitiveAttributeName (), caseSensitive);
233
234 xmlObj.addAttribute(getIntegralValueAttributeName(),
236}
237
247class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT BoolValidatorXMLConverter : public ValidatorXMLConverter
248{
249
250public:
251
254
257 const XMLObject& xmlObj,
258 const IDtoValidatorMap& validatorIDsMap) const;
259
261 void convertValidator(
263 XMLObject& xmlObj,
264 const ValidatortoIDMap& validatorIDsMap) const;
265
266 #ifdef HAVE_TEUCHOS_DEBUG
268 RCP<const ParameterEntryValidator> getDummyValidator() const;
269 #endif
270
272
273private:
274
277
278 // currently empty
279
281
282};
283
297class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberValidatorXMLConverter : public ValidatorXMLConverter
298{
299
300public:
301
304
307 const XMLObject& xmlObj,
308 const IDtoValidatorMap& validatorIDsMap) const;
309
311 void convertValidator(
313 XMLObject& xmlObj,
314 const ValidatortoIDMap& validatorIDsMap) const;
315
316 #ifdef HAVE_TEUCHOS_DEBUG
318 RCP<const ParameterEntryValidator> getDummyValidator() const;
319 #endif
320
322
323private:
324
327
329 static const std::string& getAllowIntAttributeName() {
330 static const std::string allowIntAttributeName_ = "allowInt";
331 return allowIntAttributeName_;
332 }
333
335 static const std::string& getAllowDoubleAttributeName() {
336 static const std::string allowDoubleAttributeName_ = "allowDouble";
337 return allowDoubleAttributeName_;
338 }
339
341 static const std::string& getAllowStringAttributeName() {
342 static const std::string allowStringAttributeName_ = "allowString";
343 return allowStringAttributeName_;
344 }
345
347 static const std::string& getPrefferedTypeAttributeName() {
348 static const std::string prefferedTypeAttributeName_ = "prefferedType";
349 return prefferedTypeAttributeName_;
350 }
351
353
354};
355
356
371template<class T>
373{
374
375public:
376
379
382 const XMLObject& xmlObj,
383 const IDtoValidatorMap& validatorIDsMap) const;
384
386 void convertValidator(
388 XMLObject& xmlObj,
389 const ValidatortoIDMap& validatorIDsMap) const;
390
391#ifdef HAVE_TEUCHOS_DEBUG
393 RCP<const ParameterEntryValidator> getDummyValidator() const{
394 return DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject();
395 }
396#endif
397
399
400private:
401
404
406 static const std::string& getMinAttributeName() {
407 static const std::string minAttributeName = "min";
408 return minAttributeName;
409 }
410
412 static const std::string& getMaxAttributeName() {
413 static const std::string maxAttributeName = "max";
414 return maxAttributeName;
415 }
416
418 static const std::string& getStepAttributeName() {
419 static const std::string stepAttributeName = "step";
420 return stepAttributeName;
421 }
422
424 static const std::string& getPrecisionAttributeName() {
425 static const std::string precisionAttributeName = "precision";
426 return precisionAttributeName;
427 }
428
430
431};
432
433
434template<class T>
437 const XMLObject& xmlObj,
438 const IDtoValidatorMap& /*validatorIDsMap*/) const
439{
442 T step = xmlObj.getWithDefault(
443 getStepAttributeName(), EnhancedNumberTraits<T>::defaultStep());
444 toReturn->setStep(step);
445 unsigned short int precision = xmlObj.getWithDefault(
446 getPrecisionAttributeName(),
448 toReturn->setPrecision(precision);
449 if (xmlObj.hasAttribute(getMinAttributeName())) {
450 toReturn->setMin(xmlObj.getRequired<T>(getMinAttributeName()));
451 }
452 if (xmlObj.hasAttribute(getMaxAttributeName())) {
453 toReturn->setMax(xmlObj.getRequired<T>(getMaxAttributeName()));
454 }
455 return toReturn;
456}
457
458
459template<class T>
462 XMLObject& xmlObj,
463 const ValidatortoIDMap& /*validatorIDsMap*/) const
464{
465 RCP<const EnhancedNumberValidator<T> > castedValidator =
467 if (castedValidator->hasMin()) {
468 xmlObj.addAttribute<T>(getMinAttributeName(), castedValidator->getMin());
469 }
470 if (castedValidator->hasMax()) {
471 xmlObj.addAttribute<T>(getMaxAttributeName(), castedValidator->getMax());
472 }
473 xmlObj.addAttribute<T>(getStepAttributeName(), castedValidator->getStep());
474 xmlObj.addAttribute<short unsigned int>(
475 getPrecisionAttributeName(), castedValidator->getPrecision());
476}
477
478
493class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidatorXMLConverter : public ValidatorXMLConverter
494{
495
496public:
497
500
503 const XMLObject& xmlObj,
504 const IDtoValidatorMap& validatorIDsMap) const;
505
507 void convertValidator(
509 XMLObject& xmlObj,
510 const ValidatortoIDMap& validatorIDsMap) const;
511
512 #ifdef HAVE_TEUCHOS_DEBUG
514 RCP<const ParameterEntryValidator> getDummyValidator() const;
515 #endif
516
518
519private:
520
523
525 static const std::string& getFileMustExistAttributeName() {
526 static const std::string fileMustExistAttributeName = "fileMustExist";
527 return fileMustExistAttributeName;
528 }
529
531
532};
533
534
549class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidatorXMLConverter : public ValidatorXMLConverter
550{
551
552public:
553
556
559 const XMLObject& xmlObj,
560 const IDtoValidatorMap& validatorIDsMap) const;
561
563 void convertValidator(
565 XMLObject& xmlObj,
566 const ValidatortoIDMap& validatorIDsMap) const;
567
568 #ifdef HAVE_TEUCHOS_DEBUG
570 RCP<const ParameterEntryValidator> getDummyValidator() const;
571 #endif
572
574
575private:
576
579
581 static const std::string& getStringTagName() {
582 static const std::string stringTagName = "String";
583 return stringTagName;
584 }
585
587 static const std::string& getStringValueAttributeName() {
588 static const std::string stringValueAttributeName = "value";
589 return stringValueAttributeName;
590 }
591
593
594};
595
596template<class ValidatorType, class EntryType>
597class AbstractArrayValidatorXMLConverter : public ValidatorXMLConverter{
598public:
599
602
605 const XMLObject& xmlObj,
606 const IDtoValidatorMap& validatorIDsMap) const;
607
609 void convertValidator(
611 XMLObject& xmlObj,
612 const ValidatortoIDMap& validatorIDsMap) const;
613
615
618
623 getConcreteValidator(RCP<ValidatorType> prototypeValidator) const = 0;
624
626};
627
628
629template<class ValidatorType, class EntryType>
630RCP<ParameterEntryValidator>
631AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertXML(
632 const XMLObject& xmlObj,
633 const IDtoValidatorMap& validatorIDsMap) const
634{
635 RCP<ValidatorType> prototypeValidator;
636 if(xmlObj.hasAttribute(
638 {
640 validatorIDsMap.find(
641 xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
642 getPrototypeIdAttributeName()));
643 if (result != validatorIDsMap.end() ) {
644 prototypeValidator =
645 rcp_dynamic_cast<ValidatorType>(result->second, true);
646 }
647 else {
650 "Could not find prototype validator with id: "
651 << xmlObj.getRequired<ParameterEntryValidator::ValidatorID>(
652 getPrototypeIdAttributeName()) << std::endl<< std::endl);
653 }
654 }
655 else {
656 prototypeValidator = rcp_dynamic_cast<ValidatorType>(
658 xmlObj.getChild(0), validatorIDsMap), true);
659 }
660 return getConcreteValidator(prototypeValidator);
661}
662
663template<class ValidatorType, class EntryType>
664void
665AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>::convertValidator(
667 XMLObject& xmlObj,
668 const ValidatortoIDMap& validatorIDsMap) const
669{
672 validator, true);
673 if(validatorIDsMap.find(castedValidator->getPrototype())
674 == validatorIDsMap.end())
675 {
677 castedValidator->getPrototype(), validatorIDsMap, false));
678 }
679 else{
681 validatorIDsMap.find(castedValidator->getPrototype())->second;
682
683 xmlObj.addAttribute<ParameterEntryValidator::ValidatorID>(
684 getPrototypeIdAttributeName(), prototypeID);
685 }
686}
687
715template<class ValidatorType, class EntryType>
717 public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
718{
721
722 virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
723 RCP<ValidatorType> prototypeValidator) const
724 {
725 return rcp(new ArrayValidator<ValidatorType, EntryType>(prototypeValidator));
726 }
727
728#ifdef HAVE_TEUCHOS_DEBUG
731
732 RCP<const ParameterEntryValidator> getDummyValidator() const{
734 getDummyObject();
735 }
737#endif
738};
739
767template<class ValidatorType, class EntryType>
769 public AbstractArrayValidatorXMLConverter<ValidatorType, EntryType>
770{
773
774 virtual RCP<AbstractArrayValidator<ValidatorType, EntryType> > getConcreteValidator(
775 RCP<ValidatorType> prototypeValidator) const
776 {
777 return rcp(new TwoDArrayValidator<ValidatorType, EntryType>(prototypeValidator));
778 }
779
781
782#ifdef HAVE_TEUCHOS_DEBUG
785
786 RCP<const ParameterEntryValidator> getDummyValidator() const{
788 getDummyObject();
789 }
791#endif
792
793};
794
795
796
797} // namespace Teuchos
798
799
800#endif // TEUCHOS_STANDARDVALIDATORXMLCONVERTERS_HPP
801
A database for ValidatorXMLConverters.
Converts back and forth between XML and ParameterEntryValidators.
Writes an XML object to a parameter list.
Converts AnyNumberParameterEntryValidators to and from XML.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
Takes a validator, wraps it, and applies it to an array.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
size_type size() const
Array< T > & append(const T &x)
Add a new entry at the end of the array.
Thrown when xml tag is encountered that is either unrecognized or inappropriate for a given context.
Converts BoolParameterEntryValidators to and from XML.
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
Class for retrieving a dummy object of type T.
static unsigned short defaultPrecision()
Gets the default precision with which the number type should be displayed.
static T defaultStep()
gets default amount a value of the number type should be incremented by when being utilizied in a UI.
Converts EnhancedNumberValidators to and from XML.
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
Class uesd to validate a particular type of number.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
void convertValidator(const RCP< const ParameterEntryValidator > validator, XMLObject &xmlObj, const ValidatortoIDMap &validatorIDsMap) const
Maps Validators to integers.
ValidatorMap::const_iterator const_iterator
Thrown when a referenced validator can't be found.
Smart reference counting pointer class for automatic garbage collection.
RCP< T2 > rcp_dynamic_cast(const RCP< T1 > &p1, bool throw_on_fail=false)
Dynamic cast of underlying RCP type from T1* to T2*.
bool is_null() const
Returns true if the underlying pointer is null.
Standard implementation of a ParameterEntryValidator that maps from a list of strings to an enum or i...
Convert a StringToIntegralParameterEntryValidator to and from XML.
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
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
RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObj, const IDtoValidatorMap &validatorIDsMap) const
Takes a validator, wraps it, and applies it to a TwoDArray.
static XMLObject convertValidator(RCP< const ParameterEntryValidator > validator, const ValidatortoIDMap &validatorIDsMap, bool assignedID=true)
Given a validator converts the validator to XML.
static RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObject, const IDtoValidatorMap &validatorIDsMap)
Given an XMLObject converts the XMLObject to a ParameterEntryValidator and inserts the validator into...
An abstract base class for converting ParameterEntryValidators to and from XML.
static const std::string & getPrototypeIdAttributeName()
A class for mapping validators to integers.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
void addBool(const std::string &name, bool val)
Add a bool as an attribute.
void addChild(const XMLObject &child)
Add a child node to the node.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.
const std::string & getTag() const
Return the tag of the current node.
T getWithDefault(const std::string &name, const T &defaultValue) const
Get an attribute, assigning a default value if the requested attribute does not exist.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
const XMLObject & getChild(int i) const
Return the i-th child node.
bool hasAttribute(const std::string &name) const
Find out if the current node has an attribute of the specified name.
int numChildren() const
Return the number of child nodes owned by this node.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.