openMSX
XMLElement.hh
Go to the documentation of this file.
1 #ifndef XMLELEMENT_HH
2 #define XMLELEMENT_HH
3 
4 #include "serialize_constr.hh"
5 #include "serialize_meta.hh"
6 #include <utility>
7 #include <string>
8 #include <vector>
9 #include <memory>
10 
11 namespace StringOp { class Builder; }
12 
13 namespace openmsx {
14 
15 class FileContext;
16 
18 {
19 public:
20  //
21  // Basic functions
22  //
23 
24  // Construction.
25  // (copy, assign, move, destruct are default)
27  explicit XMLElement(string_ref name);
28  XMLElement(string_ref name, string_ref data);
29 
30  // name
31  const std::string& getName() const { return name; }
32  void setName(string_ref name);
33  void clearName();
34 
35  // data
36  const std::string& getData() const { return data; }
37  void setData(string_ref data);
38 
39  // attribute
40  void addAttribute(string_ref name, string_ref value);
41  void setAttribute(string_ref name, string_ref value);
42  void removeAttribute(string_ref name);
43 
44  // child
45  typedef std::vector<XMLElement> Children;
47  void removeChild(const XMLElement& child);
48  const Children& getChildren() const { return children; }
49  bool hasChildren() const { return !children.empty(); }
50 
51  //
52  // Convenience functions
53  //
54 
55  // data
56  bool getDataAsBool() const;
57  int getDataAsInt() const;
58  double getDataAsDouble() const;
59 
60  // attribute
61  bool hasAttribute(string_ref name) const;
62  const std::string& getAttribute(string_ref attName) const;
64  string_ref defaultValue) const;
65  bool getAttributeAsBool(string_ref attName,
66  bool defaultValue = false) const;
67  int getAttributeAsInt(string_ref attName,
68  int defaultValue = 0) const;
69  bool findAttributeInt(string_ref attName,
70  unsigned& result) const;
71 
72  // child
73  const XMLElement* findChild(string_ref name) const;
75  const XMLElement& getChild(string_ref name) const;
77 
79  string_ref name, string_ref attName,
80  string_ref attValue) const;
82  string_ref name, string_ref attName,
83  string_ref attValue);
85  size_t& fromIndex) const;
86 
87  std::vector<const XMLElement*> getChildren(string_ref name) const;
88 
90  string_ref defaultValue = "");
92  string_ref name, string_ref attName,
93  string_ref attValue);
94 
95  const std::string& getChildData(string_ref name) const;
97  string_ref defaultValue) const;
99  bool defaultValue = false) const;
100  int getChildDataAsInt(string_ref name,
101  int defaultValue = 0) const;
102  void setChildData(string_ref name, string_ref value);
103 
104  void removeAllChildren();
105 
106  // various
107  std::string dump() const;
108  static std::string XMLEscape(const std::string& str);
109 
110  template<typename Archive>
111  void serialize(Archive& ar, unsigned version);
112 
113  // For backwards compatibility with older savestates
114  static std::unique_ptr<FileContext> getLastSerializedFileContext();
115 
116 private:
117  typedef std::pair<std::string, std::string> Attribute;
118  typedef std::vector<Attribute> Attributes;
119  Attributes::iterator findAttribute(string_ref name);
120  Attributes::const_iterator findAttribute(string_ref name) const;
121  void dump(StringOp::Builder& result, unsigned indentNum) const;
122 
123  std::string name;
124  std::string data;
125  Children children;
126  Attributes attributes;
127 };
129 
130 } // namespace openmsx
131 
132 #endif