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
18#include "Teuchos_StandardParameterEntryValidators.hpp"
22
23
24namespace Teuchos {
25
26
65template<class IntegralType>
68{
69
70public:
71
74
77 const XMLObject& xmlObj,
79
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";
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";
118 }
119
121 static const std::string& getStringDocAttributeName() {
122 static const std::string stringDocAttributeName_ = "stringDoc";
124 }
125
127 static const std::string& getDefaultParameterAttributeName() {
128 static const std::string defaultParameterAttributeName_ =
129 "defaultParameterName";
131 }
132
134 static const std::string& getCaseSensitiveAttributeName() {
135 static const std::string caseSensitiveAttributeName_ =
136 "caseSensitive";
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{
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) {
187 }
188 else if (integralValues.size() != 0) {
190 }
191 else {
193 }
194}
195
196
197template<class IntegralType>
201 const ValidatortoIDMap& /*validatorIDsMap*/) const
202{
207 validator, true);
208
210 castedValidator->validStringValues();
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(
264 const ValidatortoIDMap& validatorIDsMap) const;
265
266 #ifdef HAVE_TEUCHOS_DEBUG
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(
314 const ValidatortoIDMap& validatorIDsMap) const;
315
316 #ifdef HAVE_TEUCHOS_DEBUG
319 #endif
320
322
323private:
324
327
329 static const std::string& getAllowIntAttributeName() {
330 static const std::string allowIntAttributeName_ = "allowInt";
332 }
333
335 static const std::string& getAllowDoubleAttributeName() {
336 static const std::string allowDoubleAttributeName_ = "allowDouble";
338 }
339
341 static const std::string& getAllowStringAttributeName() {
342 static const std::string allowStringAttributeName_ = "allowString";
344 }
345
347 static const std::string& getPrefferedTypeAttributeName() {
348 static const std::string prefferedTypeAttributeName_ = "prefferedType";
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(
389 const ValidatortoIDMap& validatorIDsMap) const;
390
391#ifdef HAVE_TEUCHOS_DEBUG
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";
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>
463 const ValidatortoIDMap& /*validatorIDsMap*/) const
464{
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(
510 const ValidatortoIDMap& validatorIDsMap) const;
511
512 #ifdef HAVE_TEUCHOS_DEBUG
515 #endif
516
518
519private:
520
523
525 static const std::string& getFileMustExistAttributeName() {
526 static const std::string fileMustExistAttributeName = "fileMustExist";
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(
566 const ValidatortoIDMap& validatorIDsMap) const;
567
568 #ifdef HAVE_TEUCHOS_DEBUG
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";
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(
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 {
649 MissingValidatorDefinitionException,
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(
666 const RCP<const ParameterEntryValidator> validator,
667 XMLObject& xmlObj,
668 const ValidatortoIDMap& validatorIDsMap) const
669{
670 RCP<const AbstractArrayValidator<ValidatorType, EntryType> > castedValidator =
671 rcp_dynamic_cast<const AbstractArrayValidator<ValidatorType, EntryType> >(
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(
724 {
726 }
727
728#ifdef HAVE_TEUCHOS_DEBUG
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(
776 {
778 }
779
781
782#ifdef HAVE_TEUCHOS_DEBUG
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.
Thrown when xml tag is encountered that is either unrecognized or inappropriate for a given context.
Converts BoolParameterEntryValidators to and from XML.
Class for retrieving a dummy object of type T.
Class defining the traits of the number type being used in an EnhancedNumberValidator.
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
Maps Validators to integers.
ValidatorMap::const_iterator const_iterator
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*.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object.
bool is_null() const
Returns true if the underlying pointer is null.
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
Default traits class that just returns typeid(T).name().
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,...
#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.