Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_XMLObject.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_XMLOBJECT_H
11#define Teuchos_XMLOBJECT_H
12
18#include "Teuchos_Utils.hpp"
19
20namespace Teuchos{
21
23class EmptyXMLError : public std::runtime_error
24{public: EmptyXMLError(const std::string& what_arg) : std::runtime_error(what_arg) {}};
25
30class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT XMLObject{
31public:
32
34
35
37 XMLObject() : ptr_() {;}
38
40 XMLObject(const std::string& tag);
41
49
51
52
54 XMLObject deepCopy() const ;
56
58
59
61 const std::string& getTag() const;
62
64 bool hasAttribute(const std::string& name) const;
65
67 const std::string& getAttribute(const std::string& name) const;
68
70 const std::string& getRequired(const std::string& name) const;
71
73 double getRequiredDouble(const std::string& name) const
74 {return std::atof(getRequired(name).c_str());}
75
77 int getRequiredInt(const std::string& name) const
78 {return std::atoi(getRequired(name).c_str());}
79
81 template<class T>
82 T getRequired(const std::string& name) const{
83 T toReturn;
84 std::istringstream iss(getRequired(name));
85 iss >> toReturn;
86 return toReturn;
87 }
88
90 bool getRequiredBool(const std::string& name) const ;
91
94 template<class T>
95 T getWithDefault(const std::string& name, const T& defaultValue) const{
96 if (hasAttribute(name)){
97 return getRequired<T>(name);
98 }
99 else{
100 return defaultValue;
101 }
102 }
103
105 int numChildren() const;
106
108 const XMLObject& getChild(int i) const;
109
113 int findFirstChild(std::string tagName) const;
114
116 int numContentLines() const;
117
119 const std::string& getContentLine(int i) const;
120
122 std::string toString() const;
123
125 void print(std::ostream& os, int indent) const;
126
128 std::string header() const;
129
131 std::string terminatedHeader() const;
132
134 std::string footer() const;
135
137 bool isEmpty() const { return ptr_.get()==0;}
138
140 void checkTag(const std::string& expected) const ;
142
144
145
147 void addDouble(const std::string& name, double val)
148 {addAttribute(name, Teuchos::toString(val));}
149
151 void addInt(const std::string& name, int val)
152 {addAttribute(name, Teuchos::toString(val));}
153
155 void addBool(const std::string& name, bool val)
156 {addAttribute(name, Teuchos::toString(val));}
157
160 template<class T>
161 void addAttribute(const std::string& name, T value) {
163 "XMLObject::addAttribute: XMLObject is empty");
164 ptr_->addAttribute(name, Teuchos::toString(value));
165 }
166
167
169 void addChild(const XMLObject& child);
170
172 void addContent(const std::string& contentLine);
174
175 void appendContentLine(const size_t& i, const std::string &str) {
176 ptr_->appendContentLine(i, str);
177 }
178
179 void removeContentLine(const size_t& i) {
180 ptr_->removeContentLine(i);
181 }
182
183protected:
184
185//use pragmas to disable some false-positive warnings for windows sharedlibs export
186#ifdef _MSC_VER
187#pragma warning(push)
188#pragma warning(disable:4251)
189#endif
190 RCP<XMLObjectImplem> ptr_;
191#ifdef _MSC_VER
192#pragma warning(pop)
193#endif
194
195};
196
197
198template<>
199TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool XMLObject::getRequired<bool>(const std::string& name) const;
200
201template<>
202TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int XMLObject::getRequired<int>(const std::string& name) const;
203
204template<>
205TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double XMLObject::getRequired<double>(const std::string& name) const;
206
207template<>
208TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string XMLObject::getRequired<std::string>(const std::string& name) const;
209
210template<>
211TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void XMLObject::addAttribute<const std::string&>(const std::string& name, const std::string& value);
212
213
218inline std::ostream& operator<<(std::ostream& os, const XMLObject& xml)
219{
220 xml.print(os, 0);
221 return os;
222}
223
224
229inline std::string toString(const XMLObject& xml)
230{
231 return xml.toString();
232}
233
234
235} // namespace Teuchos
236
237
238#endif
A utilities class for Teuchos.
Low level implementation of XMLObject.
Thrown when attempting to parse an empty XML std::string.
Smart reference counting pointer class for automatic garbage collection.
T * get() const
Get the raw C++ pointer to the underlying object.
The XMLObjectImplem class takes care of the low-level implementation details of XMLObject.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
double getRequiredDouble(const std::string &name) const
Get a required attribute, returning it as a double.
void addBool(const std::string &name, bool val)
Add a bool as an attribute.
void addDouble(const std::string &name, double val)
Add a double as an attribute.
int getRequiredInt(const std::string &name) const
Get a required attribute, returning it as an int.
void addInt(const std::string &name, int val)
Add an int as an attribute.
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.
T getRequired(const std::string &name) const
Get a required attribute, returning it as T.
XMLObject()
Empty constructor.
std::string toString(const XMLObject &xml)
Write XMLObject to std::string.
bool isEmpty() const
Find out if a node is empty.
std::ostream & operator<<(std::ostream &os, const XMLObject &xml)
Write XMLObject to os stream.
#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,...