Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
20 namespace Teuchos{
21 
23 class EmptyXMLError : public std::runtime_error
24 {public: EmptyXMLError(const std::string& what_arg) : std::runtime_error(what_arg) {}};
25 
31 public:
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 
183 protected:
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
191 #ifdef _MSC_VER
192 #pragma warning(pop)
193 #endif
194 
195 };
196 
197 
198 template<>
199 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool XMLObject::getRequired<bool>(const std::string& name) const;
200 
201 template<>
202 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int XMLObject::getRequired<int>(const std::string& name) const;
203 
204 template<>
205 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double XMLObject::getRequired<double>(const std::string& name) const;
206 
207 template<>
208 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string XMLObject::getRequired<std::string>(const std::string& name) const;
209 
210 template<>
211 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void XMLObject::addAttribute<const std::string&>(const std::string& name, const std::string& value);
212 
213 
218 inline std::ostream& operator<<(std::ostream& os, const XMLObject& xml)
219 {
220  xml.print(os, 0);
221  return os;
222 }
223 
224 
229 inline std::string toString(const XMLObject& xml)
230 {
231  return xml.toString();
232 }
233 
234 
235 } // namespace Teuchos
236 
237 
238 #endif
EmptyXMLError(const std::string &what_arg)
void addBool(const std::string &name, bool val)
Add a bool as an attribute.
std::string toString(const XMLObject &xml)
Write XMLObject to std::string.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
int getRequiredInt(const std::string &name) const
Get a required attribute, returning it as an int.
T getRequired(const std::string &name) const
Get a required attribute, returning it as T.
bool isEmpty() const
Find out if a node is empty.
XMLObject()
Empty constructor.
void removeContentLine(const size_t &i)
Thrown when attempting to parse an empty XML std::string.
double getRequiredDouble(const std::string &name) const
Get a required attribute, returning it as a double.
void appendContentLine(const size_t &i, const std::string &str)
Low level implementation of XMLObject.
std::string toString() const
Represent this node and its children as a std::string.
RCP< XMLObjectImplem > ptr_
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
std::string toString(const HashSet< Key > &h)
std::ostream & operator<<(std::ostream &os, const XMLObject &xml)
Write XMLObject to os stream.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
#define TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT
void print(std::ostream &os, int indent) const
Print this node and its children to stream with the given indentation.
void addInt(const std::string &name, int val)
Add an int as an attribute.
The XMLObjectImplem class takes care of the low-level implementation details of XMLObject.
Smart reference counting pointer class for automatic garbage collection.
T getWithDefault(const std::string &name, const T &defaultValue) const
Get an attribute, assigning a default value if the requested attribute does not exist.
A utilities class for Teuchos.
void addDouble(const std::string &name, double val)
Add a double as an attribute.