Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_XMLObject.hpp
Go to the documentation of this file.
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 #ifndef Teuchos_XMLOBJECT_H
43 #define Teuchos_XMLOBJECT_H
44 
50 #include "Teuchos_Utils.hpp"
51 
52 namespace Teuchos{
53 
55 class EmptyXMLError : public std::runtime_error
56 {public: EmptyXMLError(const std::string& what_arg) : std::runtime_error(what_arg) {}};
57 
62 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT XMLObject{
63 public:
64 
66 
67 
69  XMLObject() : ptr_() {;}
70 
72  XMLObject(const std::string& tag);
73 
81 
83 
84 
86  XMLObject deepCopy() const ;
88 
90 
91 
93  const std::string& getTag() const;
94 
96  bool hasAttribute(const std::string& name) const;
97 
99  const std::string& getAttribute(const std::string& name) const;
100 
102  const std::string& getRequired(const std::string& name) const;
103 
105  double getRequiredDouble(const std::string& name) const
106  {return std::atof(getRequired(name).c_str());}
107 
109  int getRequiredInt(const std::string& name) const
110  {return std::atoi(getRequired(name).c_str());}
111 
113  template<class T>
114  T getRequired(const std::string& name) const{
115  T toReturn;
116  std::istringstream iss(getRequired(name));
117  iss >> toReturn;
118  return toReturn;
119  }
120 
122  bool getRequiredBool(const std::string& name) const ;
123 
126  template<class T>
127  T getWithDefault(const std::string& name, const T& defaultValue) const{
128  if (hasAttribute(name)){
129  return getRequired<T>(name);
130  }
131  else{
132  return defaultValue;
133  }
134  }
135 
137  int numChildren() const;
138 
140  const XMLObject& getChild(int i) const;
141 
145  int findFirstChild(std::string tagName) const;
146 
148  int numContentLines() const;
149 
151  const std::string& getContentLine(int i) const;
152 
154  std::string toString() const;
155 
157  void print(std::ostream& os, int indent) const;
158 
160  std::string header() const;
161 
163  std::string terminatedHeader() const;
164 
166  std::string footer() const;
167 
169  bool isEmpty() const { return ptr_.get()==0;}
170 
172  void checkTag(const std::string& expected) const ;
174 
176 
177 
179  void addDouble(const std::string& name, double val)
180  {addAttribute(name, Teuchos::toString(val));}
181 
183  void addInt(const std::string& name, int val)
184  {addAttribute(name, Teuchos::toString(val));}
185 
187  void addBool(const std::string& name, bool val)
188  {addAttribute(name, Teuchos::toString(val));}
189 
192  template<class T>
193  void addAttribute(const std::string& name, T value) {
195  "XMLObject::addAttribute: XMLObject is empty");
196  ptr_->addAttribute(name, Teuchos::toString(value));
197  }
198 
199 
201  void addChild(const XMLObject& child);
202 
204  void addContent(const std::string& contentLine);
206 
207  void appendContentLine(const size_t& i, const std::string &str) {
208  ptr_->appendContentLine(i, str);
209  }
210 
211  void removeContentLine(const size_t& i) {
212  ptr_->removeContentLine(i);
213  }
214 
215 protected:
216 
217 //use pragmas to disable some false-positive warnings for windows sharedlibs export
218 #ifdef _MSC_VER
219 #pragma warning(push)
220 #pragma warning(disable:4251)
221 #endif
223 #ifdef _MSC_VER
224 #pragma warning(pop)
225 #endif
226 
227 };
228 
229 
230 template<>
231 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool XMLObject::getRequired<bool>(const std::string& name) const;
232 
233 template<>
234 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int XMLObject::getRequired<int>(const std::string& name) const;
235 
236 template<>
237 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double XMLObject::getRequired<double>(const std::string& name) const;
238 
239 template<>
240 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string XMLObject::getRequired<std::string>(const std::string& name) const;
241 
242 template<>
243 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void XMLObject::addAttribute<const std::string&>(const std::string& name, const std::string& value);
244 
245 
250 inline std::ostream& operator<<(std::ostream& os, const XMLObject& xml)
251 {
252  xml.print(os, 0);
253  return os;
254 }
255 
256 
261 inline std::string toString(const XMLObject& xml)
262 {
263  return xml.toString();
264 }
265 
266 
267 } // namespace Teuchos
268 
269 
270 #endif
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.
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.
Low level implementation of XMLObject.
std::string toString() const
Represent this node and its children as a std::string.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
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.
void print(std::ostream &os, int indent) const
Print this node and its children to stream with the given indentation.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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.