openMSX
XMLElement.hh
Go to the documentation of this file.
1 #ifndef XMLELEMENT_HH
2 #define XMLELEMENT_HH
3 
4 #include "serialize_meta.hh"
5 #include <utility>
6 #include <string>
7 #include <vector>
8 #include <memory>
9 
10 namespace openmsx {
11 
12 class FileContext;
13 
15 {
16 public:
17  //
18  // Basic functions
19  //
20 
21  // Construction.
22  // (copy, assign, move, destruct are default)
23  XMLElement() = default;
24  explicit XMLElement(std::string name_)
25  : name(std::move(name_)) {}
26  XMLElement(std::string name_, std::string data_)
27  : name(std::move(name_)), data(std::move(data_)) {}
28 
29  // name
30  const std::string& getName() const { return name; }
31  void setName(std::string name_) { name = std::move(name_); }
32  void clearName() { name.clear(); }
33 
34  // data
35  const std::string& getData() const { return data; }
36  void setData(std::string data_) {
37  assert(children.empty()); // no mixed-content elements
38  data = std::move(data_);
39  }
40 
41  // attribute
42  void addAttribute(std::string name, std::string value);
43  void setAttribute(string_view name, std::string value);
44  void removeAttribute(string_view name);
45  bool hasAttribute(string_view name) const;
46  const std::string& getAttribute(string_view attName) const;
48  string_view defaultValue) const;
49  // Returns ptr to attribute value, or nullptr when not found.
50  const std::string* findAttribute(string_view attName) const;
51 
52  // child
53  using Children = std::vector<XMLElement>;
54  // note: returned XMLElement& is validated on the next addChild() call
55  XMLElement& addChild(std::string name);
56  XMLElement& addChild(std::string name, std::string data);
57  void removeChild(const XMLElement& child);
58  const Children& getChildren() const { return children; }
59  bool hasChildren() const { return !children.empty(); }
60 
61  //
62  // Convenience functions
63  //
64 
65  // attribute
66  bool getAttributeAsBool(string_view attName,
67  bool defaultValue = false) const;
68  int getAttributeAsInt(string_view attName,
69  int defaultValue = 0) const;
70  bool findAttributeInt(string_view attName,
71  unsigned& result) const;
72 
73  // child
74  const XMLElement* findChild(string_view name) const;
76  const XMLElement& getChild(string_view name) const;
78 
80  string_view name, string_view attName,
81  string_view attValue) const;
83  string_view name, string_view attName,
84  string_view attValue);
86  size_t& fromIndex) const;
87 
88  std::vector<const XMLElement*> getChildren(string_view name) const;
89 
91  string_view defaultValue = {});
93  string_view name, string_view attName,
94  string_view attValue);
95 
96  const std::string& getChildData(string_view name) const;
98  string_view defaultValue) const;
100  bool defaultValue = false) const;
102  int defaultValue = 0) const;
103  void setChildData(string_view name, std::string value);
104 
105  void removeAllChildren();
106 
107  // various
108  std::string dump() const;
109  static std::string XMLEscape(const std::string& str);
110 
111  template<typename Archive>
112  void serialize(Archive& ar, unsigned version);
113 
114  // For backwards compatibility with older savestates
115  static std::unique_ptr<FileContext> getLastSerializedFileContext();
116 
117 private:
118  using Attribute = std::pair<std::string, std::string>;
119  using Attributes = std::vector<Attribute>;
120  Attributes::iterator getAttributeIter(string_view name);
121  Attributes::const_iterator getAttributeIter(string_view name) const;
122  void dump(std::string& result, unsigned indentNum) const;
123 
124  std::string name;
125  std::string data;
126  Children children;
127  Attributes attributes;
128 };
130 
131 } // namespace openmsx
132 
133 #endif
bool findAttributeInt(string_view attName, unsigned &result) const
Definition: XMLElement.cc:241
const std::string & getName() const
Definition: XMLElement.hh:30
void setName(std::string name_)
Definition: XMLElement.hh:31
bool getAttributeAsBool(string_view attName, bool defaultValue=false) const
Definition: XMLElement.cc:227
const XMLElement * findChild(string_view name) const
Definition: XMLElement.cc:94
void removeChild(const XMLElement &child)
Definition: XMLElement.cc:30
STL namespace.
XMLElement & addChild(std::string name)
Definition: XMLElement.cc:19
std::string dump() const
Definition: XMLElement.cc:252
XMLElement & getCreateChildWithAttribute(string_view name, string_view attName, string_view attValue)
Definition: XMLElement.cc:157
XMLElement(std::string name_, std::string data_)
Definition: XMLElement.hh:26
int getAttributeAsInt(string_view attName, int defaultValue=0) const
Definition: XMLElement.cc:234
const std::string & getData() const
Definition: XMLElement.hh:35
void serialize(Archive &ar, unsigned version)
Definition: XMLElement.cc:333
void setAttribute(string_view name, std::string value)
Definition: XMLElement.cc:59
const std::string * findAttribute(string_view attName) const
Definition: XMLElement.cc:47
static std::unique_ptr< FileContext > getLastSerializedFileContext()
Definition: XMLElement.cc:316
bool hasChildren() const
Definition: XMLElement.hh:59
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
const std::string & getChildData(string_view name) const
Definition: XMLElement.cc:169
const XMLElement * findChildWithAttribute(string_view name, string_view attName, string_view attValue) const
Definition: XMLElement.cc:129
void setData(std::string data_)
Definition: XMLElement.hh:36
void setChildData(string_view name, std::string value)
Definition: XMLElement.cc:193
void removeAttribute(string_view name)
Definition: XMLElement.cc:69
int getChildDataAsInt(string_view name, int defaultValue=0) const
Definition: XMLElement.cc:187
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
XMLElement & getCreateChild(string_view name, string_view defaultValue={})
Definition: XMLElement.cc:148
static std::string XMLEscape(const std::string &str)
Definition: XMLElement.cc:289
std::vector< XMLElement > Children
Definition: XMLElement.hh:53
void addAttribute(std::string name, std::string value)
Definition: XMLElement.cc:53
XMLElement(std::string name_)
Definition: XMLElement.hh:24
bool getChildDataAsBool(string_view name, bool defaultValue=false) const
Definition: XMLElement.cc:181
const std::string & getAttribute(string_view attName) const
Definition: XMLElement.cc:212
const Children & getChildren() const
Definition: XMLElement.hh:58
const XMLElement & getChild(string_view name) const
Definition: XMLElement.cc:143
bool hasAttribute(string_view name) const
Definition: XMLElement.cc:207
const XMLElement * findNextChild(string_view name, size_t &fromIndex) const
Definition: XMLElement.cc:99