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