xml_config.hh

00001 /*
00002 ** TowBowlTactics, an adaptation of the tabletop game Blood Bowl.
00003 **
00004 ** Copyright (C) 2006 The TBT Team.
00005 **
00006 ** This program is free software; you can redistribute it and/or
00007 ** modify it under the terms of the GNU General Public License
00008 ** as published by the Free Software Foundation; either version 2
00009 ** of the License, or (at your option) any later version.
00010 **
00011 ** The complete GNU General Public Licence Notice can be found as the
00012 ** `NOTICE' file in the root directory.
00013 **
00014 ** The TBT Team consists of people listed in the `AUTHORS' file.
00015 */
00016 
00017 #ifndef XML_CONFIG_HH_
00018 # define XML_CONFIG_HH_
00019 
00020 # include "xml/xml.hh"
00021 
00022 BEGIN_NS(xml);
00023 
00074 class XMLConfig : public XML
00075 {
00076 public:
00077   XMLConfig(const std::string& def_file, const std::string& def_loc);
00078   
00079   void switchClientSection(int client_gid) const;
00080 
00085   template <typename T>
00086   T getData(const std::string& section_name,
00087         const std::string& node_name) const;
00088 
00093   template <typename T>
00094   T getAttr(const std::string& section_name,
00095         const std::string& node_name,
00096         const std::string& attr_name) const;
00097 
00100   template <typename T>
00101   void setData(const std::string& section_name,
00102            const std::string& node_name,
00103            const T& value);
00104 
00107   template <typename T>
00108   void setAttr(const std::string& section_name,
00109            const std::string& node_name,
00110            const std::string& attr_name,
00111            const T& value);
00112   
00114   virtual void parse(const std::string& filename);
00115 
00116 private:
00117   virtual const char* getExpectedRoot() const;
00118 
00119   bool switchToSection(const std::string& section_name, bool first_time) const;
00120   mutable std::string client_section_;
00121   std::string default_file_;
00122   std::string default_location_;
00123 };
00124 
00125 template <typename T>
00126 inline T XMLConfig::getData(const std::string& section_name,
00127                 const std::string& node_name) const
00128 {
00129   switchToSection(section_name, true);
00130   try {
00131     return XML::getData<T>(node_name);
00132   } catch (const XMLError&) {
00133     if (!switchToSection(section_name, false))
00134       throw;
00135     return XML::getData<T>(node_name);
00136   }
00137 }
00138 
00139 template <typename T>
00140 inline T XMLConfig::getAttr(const std::string& section_name,
00141                 const std::string& node_name,
00142                 const std::string& attr_name) const
00143 {
00144   switchToSection(section_name, true);
00145   try {
00146     return XML::getAttr<T>(node_name, attr_name);
00147   } catch (const XMLError&) {
00148     if (!switchToSection(section_name, false))
00149       throw;
00150     return XML::getAttr<T>(node_name, attr_name);
00151   }
00152 }
00153 
00154 template <typename T>
00155 inline void XMLConfig::setData(const std::string& section_name,
00156                    const std::string& node_name,
00157                    const T& value)
00158 {
00159   try {
00160     // if the data exists in specific section, write it here.
00161     switchToSection(section_name, true);
00162     XML::getData<T>(node_name);
00163     XML::setData<T>(node_name, value);
00164   } catch (const XMLError&) {
00165     // if the data exists in generic section, write it here.
00166     try {
00167       switchToSection(section_name, false);
00168       XML::getData<T>(node_name);
00169       XML::setData<T>(node_name, value);
00170     } catch (const XMLError&) {
00171       // was not present before. write it in specific section.
00172       switchToSection(section_name, true);
00173       XML::setData<T>(node_name, value);
00174     }
00175   }
00176   
00177   switchToSection(section_name, true);
00178 }
00179 
00180 template <typename T>
00181 inline void XMLConfig::setAttr(const std::string& section_name,
00182             const std::string& node_name,
00183             const std::string& attr_name,
00184             const T& value)
00185 {
00186   try {
00187     // if the attribute exists in specific section, write it here.
00188     switchToSection(section_name, true);
00189     XML::getAttr<T>(node_name, attr_name);
00190     XML::setAttr<T>(node_name, attr_name, value);
00191   } catch (const XMLError&) {
00192     // if the attribute exists in generic section, write it here.
00193     try {
00194       switchToSection(section_name, false);
00195       XML::getAttr<T>(node_name, attr_name);
00196       XML::setAttr<T>(node_name, attr_name, value);
00197     } catch (const XMLError&) {
00198       // was not present before. write it in specific section.
00199       switchToSection(section_name, true);
00200       XML::setAttr<T>(node_name, attr_name, value);
00201     }
00202   }
00203 }
00204 
00205 END_NS(xml);
00206 
00207 #endif /* !XML_CONFIG_HH_ */

Generated on Sat Jun 23 16:07:24 2007 for Stechec/TBT by  doxygen 1.4.7