Teuchos - Trilinos Tools Package Version of the Day
Teuchos_XMLObject.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef Teuchos_XMLOBJECT_H
00043 #define Teuchos_XMLOBJECT_H
00044 
00049 #include "Teuchos_XMLObjectImplem.hpp"
00050 #include "Teuchos_Utils.hpp"
00051 
00052 namespace Teuchos{
00053 
00055 class EmptyXMLError : public std::runtime_error
00056 {public: EmptyXMLError(const std::string& what_arg) : std::runtime_error(what_arg) {}};
00057 
00062 class TEUCHOS_LIB_DLL_EXPORT XMLObject{
00063 public:
00064 
00066 
00067 
00069   XMLObject() : ptr_() {;}
00070 
00072   XMLObject(const std::string& tag);
00073 
00079   XMLObject(XMLObjectImplem* ptr);
00081 
00083 
00084 
00086   XMLObject deepCopy() const ;
00088 
00090 
00091 
00093   const std::string& getTag() const;
00094 
00096   bool hasAttribute(const std::string& name) const;
00097 
00099   const std::string& getAttribute(const std::string& name) const;
00100 
00102   const std::string& getRequired(const std::string& name) const;
00103 
00105   double getRequiredDouble(const std::string& name) const 
00106     {return std::atof(getRequired(name).c_str());}
00107   
00109   int getRequiredInt(const std::string& name) const 
00110     {return std::atoi(getRequired(name).c_str());}
00111 
00113   template<class T>
00114   T getRequired(const std::string& name) const{
00115     T toReturn;
00116     std::istringstream iss(getRequired(name));
00117     iss >> toReturn;
00118     return toReturn;
00119   }
00120 
00122   bool getRequiredBool(const std::string& name) const ;
00123 
00126   template<class T>
00127   T getWithDefault(const std::string& name, const T& defaultValue) const{
00128     if (hasAttribute(name)){
00129       return getRequired<T>(name);
00130     }
00131     else{
00132       return defaultValue;
00133     }
00134   }
00135   
00137   int numChildren() const;
00138 
00140   const XMLObject& getChild(int i) const;
00141 
00145   int findFirstChild(std::string tagName) const;
00146 
00148   int numContentLines() const;
00149 
00151   const std::string& getContentLine(int i) const;
00152 
00154   std::string toString() const;
00155 
00157   void print(std::ostream& os, int indent) const;
00158 
00160   std::string header() const;
00161 
00163   std::string terminatedHeader() const;
00164 
00166   std::string footer() const;
00167 
00169   bool isEmpty() const { return ptr_.get()==0;}
00170 
00172   void checkTag(const std::string& expected) const ;
00174   
00176 
00177 
00179   void addDouble(const std::string& name, double val)
00180     {addAttribute(name, Teuchos::toString(val));}
00181 
00183   void addInt(const std::string& name, int val)
00184     {addAttribute(name, Teuchos::toString(val));}
00185 
00187   void addBool(const std::string& name, bool val)
00188     {addAttribute(name, Teuchos::toString(val));}
00189 
00192   template<class T>
00193   void addAttribute(const std::string& name, T value) {
00194   TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00195          "XMLObject::addAttribute: XMLObject is empty");
00196   ptr_->addAttribute(name, Teuchos::toString(value));
00197   }
00198 
00199       
00201   void addChild(const XMLObject& child);
00202 
00204   void addContent(const std::string& contentLine);
00206 
00207 private:
00208 
00209 //use pragmas to disable some false-positive warnings for windows sharedlibs export
00210 #ifdef _MSC_VER
00211 #pragma warning(push)
00212 #pragma warning(disable:4251)
00213 #endif
00214   RCP<XMLObjectImplem> ptr_;
00215 #ifdef _MSC_VER
00216 #pragma warning(pop)
00217 #endif
00218 
00219 };
00220 
00221 
00222 template<>
00223 TEUCHOS_LIB_DLL_EXPORT bool XMLObject::getRequired<bool>(const std::string& name) const;
00224 
00225 template<>
00226 TEUCHOS_LIB_DLL_EXPORT int XMLObject::getRequired<int>(const std::string& name) const;
00227 
00228 template<>
00229 TEUCHOS_LIB_DLL_EXPORT double XMLObject::getRequired<double>(const std::string& name) const;
00230 
00231 template<>
00232 TEUCHOS_LIB_DLL_EXPORT std::string XMLObject::getRequired<std::string>(const std::string& name) const;
00233 
00234 template<>
00235 TEUCHOS_LIB_DLL_EXPORT void XMLObject::addAttribute<const std::string&>(const std::string& name, const std::string& value);
00236 
00237 
00242 inline std::ostream& operator<<(std::ostream& os, const XMLObject& xml)
00243 {
00244   xml.print(os, 0);
00245   return os;
00246 }
00247 
00248 
00253 inline std::string toString(const XMLObject& xml)
00254 {
00255   return xml.toString();
00256 }
00257 
00258 
00259 } // namespace Teuchos
00260 
00261 
00262 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines