]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/xml/xmlelement.h
Merge commit '48410b113dd2036e69dbf723a39ec9af02fc9b12'
[xonotic/netradiant.git] / libs / xml / xmlelement.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_XML_XMLELEMENT_H)
23 #define INCLUDED_XML_XMLELEMENT_H
24
25 #include "xml/ixml.h"
26 #include "string/string.h"
27
28 #include <map>
29
30 ///\brief All string pointers passed to an instance of this class are not
31 /// copied and must stay valid for the lifetime of the instance.
32 class StaticElement : public XMLElement
33 {
34   typedef std::map<const char*, const char*, RawStringLess> attrs_t;
35 public:
36   StaticElement(const char* name)
37     : m_name(name)
38   {
39   }
40   void insertAttribute(const char* name, const char* value)
41   {
42     m_attrs.insert(attrs_t::value_type(name, value));
43   }
44   const char* name() const
45   {
46     return m_name;
47   }
48   const char* attribute(const char* name) const
49   {
50     attrs_t::const_iterator i = m_attrs.find(name);
51     if(i != m_attrs.end())
52       return i->second;
53     else
54       return "";
55   }
56   void forEachAttribute(XMLAttrVisitor& visitor) const
57   {
58     for(attrs_t::const_iterator i = m_attrs.begin(); i != m_attrs.end(); ++i)
59     {
60       visitor.visit(i->first, i->second);
61     }
62   }
63 private:
64   const char* m_name;
65   attrs_t m_attrs;
66 };
67
68 ///\brief All string pointers passed to an instance of this class are copied.
69 class DynamicElement : public XMLElement
70 {
71   typedef std::map<CopiedString, CopiedString> attrs_t;
72 public:
73   DynamicElement(const char* name)
74     : m_name(name)
75   {
76   }
77   void insertAttribute(const char* name, const char* value)
78   {
79     m_attrs.insert(attrs_t::value_type(name, value));
80   }
81   const char* name() const
82   {
83     return m_name.c_str();
84   }
85   const char* attribute(const char* name) const
86   {
87     attrs_t::const_iterator i = m_attrs.find(name);
88     if(i != m_attrs.end())
89       return i->second.c_str();
90     else
91       return "";
92   }
93   void forEachAttribute(XMLAttrVisitor& visitor) const
94   {
95     for(attrs_t::const_iterator i = m_attrs.begin(); i != m_attrs.end(); ++i)
96     {
97       visitor.visit(i->first.c_str(), i->second.c_str());
98     }
99   }
100 private:
101   CopiedString m_name;
102   attrs_t m_attrs;
103 };
104
105 #endif