Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_ParameterEntryXMLConverter.cpp
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#include "Teuchos_ParameterEntryXMLConverter.hpp"
46#include "Teuchos_ParameterEntryXMLConverterDB.hpp"
47
48namespace Teuchos{
49
50
51ParameterEntry
53 const XMLObject &xmlObj) const
54{
55 #ifdef HAVE_TEUCHOS_DEBUG
59 "Error: this Parameter Entry XML tag has a type different than "
60 "the XMLConverter being used to convert it." <<std::endl <<
61 "Parameter name: " << xmlObj.getRequired(
63 "XML Parameter Entry type: " <<
64 xmlObj.getRequired(getTypeAttributeName()) << std::endl <<
65 "XMLConverter type: " << getTypeAttributeValue() <<
66 std::endl <<std::endl);
67 #endif
68
70 !xmlObj.hasAttribute(getValueAttributeName()),
72 ParameterEntry::getTagName() <<" tags must "
73 "have a " << getValueAttributeName() << " attribute" << std::endl <<
74 "Bad Parameter: " <<
76 std::endl << std::endl);
77
79 bool isDefault = false;
80 bool isUsed = false;
81 std::string docString = "";
82
83
84 if(xmlObj.hasAttribute(getDefaultAttributeName())){
85 isDefault = xmlObj.getRequiredBool(getDefaultAttributeName());
86 }
87
88 if(xmlObj.hasAttribute(getUsedAttributeName())){
89 isUsed = xmlObj.getRequiredBool(getUsedAttributeName());
90 }
91
92 if(xmlObj.hasAttribute(getDocStringAttributeName())){
93 docString = xmlObj.getRequired(getDocStringAttributeName());
94 }
95
96 toReturn.setAnyValue(getAny(xmlObj), isDefault);
97 toReturn.setDocString(docString);
98
99 if(isUsed){
100 toReturn.getAny();
101 }
102
103 return toReturn;
104}
105
106
110 const std::string &name,
113{
114 #ifdef HAVE_TEUCHOS_DEBUG
116 (entry->getAny().typeName() != getTypeAttributeValue())
117 &&
118 (
121 ),
123 "Error: This converter can't convert the given ParameterEntry to XML "
124 "because their types don't match." << std::endl <<
125 "Parameter name: " << name << std::endl <<
126 "Parameter type: " << entry->getAny().typeName() << std::endl <<
127 "Converter type: " << getTypeAttributeValue() << std::endl << std::endl);
128 #endif
129
131 toReturn.addAttribute(
134 toReturn.addAttribute(getDocStringAttributeName(), entry->docString());
135 toReturn.addAttribute(getIdAttributeName(), id);
136 toReturn.addAttribute(
138 toReturn.addBool(getDefaultAttributeName(), entry->isDefault());
139 toReturn.addBool(getUsedAttributeName(), entry->isUsed());
140 if(nonnull(entry->validator())){
142 validatorIDsMap.find(entry->validator()) == validatorIDsMap.end(),
144 "Could not find validator in given ValidatorIDsMap! " <<
145 std::endl << std::endl);
146 toReturn.addAttribute(
148 validatorIDsMap.find(entry->validator())->second);
149 }
150 return toReturn;
151}
152
153
154} // namespace Teuchos
155
Converts back and forth between XML and ParameterEntryValidators.
A collection of Exceptions that can be potentially thrown when converting a ParameterList to and from...
Writes a ParameterList to an XML object.
Thrown when a converter is being used to convert either and XML tag or ParameterEntry with an innappr...
Thrown when a referenced validator can't be found.
Thrown when a parameter entry tag is missing it's value attribute.
static RCP< const ParameterEntryXMLConverter > getDefaultConverter()
Gets the default converter to be used on Parameter Entries.
virtual const std::string getTypeAttributeValue() const =0
Gets a string representing the value that should be assigned to the "type" attribute when converting ...
ParameterEntry fromXMLtoParameterEntry(const XMLObject &xmlObj) const
Converts the given xml into a parameter entry.
XMLObject fromParameterEntrytoXML(RCP< const ParameterEntry > entry, const std::string &name, const ParameterEntry::ParameterEntryID &id, const ValidatortoIDMap &validatorIDsMap) const
Converts the given parameter entry to xml.
virtual const std::string getValueAttributeValue(RCP< const ParameterEntry > entry) const =0
Gets the value to be assigned to the "value" attribute when converting the paramter entry to xml.
This object is held as the "value" in the Teuchos::ParameterList std::map.
static const std::string & getTagName()
Get the string that should be used as the tag name for all parameters when they are serialized to xml...
Smart reference counting pointer class for automatic garbage collection.
static const std::string & getIdAttributeName()
A class for mapping validators to integers.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
static const std::string & getNameAttributeName()
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...