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(string_view name);
26 
27  // name
28  const std::string& getName() const { return name; }
29  void setName(string_view name);
30  void clearName();
31 
32  // data
33  const std::string& getData() const { return data; }
34  void setData(string_view data);
35 
36  // attribute
37  void addAttribute(string_view name, string_view value);
38  void setAttribute(string_view name, string_view value);
39  void removeAttribute(string_view name);
40 
41  // child
42  using Children = std::vector<XMLElement>;
43  // note: returned XMLElement& is validated on the next addChild() call
46  void removeChild(const XMLElement& child);
47  const Children& getChildren() const { return children; }
48  bool hasChildren() const { return !children.empty(); }
49 
50  //
51  // Convenience functions
52  //
53 
54  // attribute
55  bool hasAttribute(string_view name) const;
56  const std::string& getAttribute(string_view attName) const;
58  string_view defaultValue) const;
59  bool getAttributeAsBool(string_view attName,
60  bool defaultValue = false) const;
61  int getAttributeAsInt(string_view attName,
62  int defaultValue = 0) const;
63  bool findAttributeInt(string_view attName,
64  unsigned& result) const;
65 
66  // child
67  const XMLElement* findChild(string_view name) const;
69  const XMLElement& getChild(string_view name) const;
71 
73  string_view name, string_view attName,
74  string_view attValue) const;
76  string_view name, string_view attName,
77  string_view attValue);
79  size_t& fromIndex) const;
80 
81  std::vector<const XMLElement*> getChildren(string_view name) const;
82 
84  string_view defaultValue = {});
86  string_view name, string_view attName,
87  string_view attValue);
88 
89  const std::string& getChildData(string_view name) const;
91  string_view defaultValue) const;
93  bool defaultValue = false) const;
95  int defaultValue = 0) const;
96  void setChildData(string_view name, string_view value);
97 
98  void removeAllChildren();
99 
100  // various
101  std::string dump() const;
102  static std::string XMLEscape(const std::string& str);
103 
104  template<typename Archive>
105  void serialize(Archive& ar, unsigned version);
106 
107  // For backwards compatibility with older savestates
108  static std::unique_ptr<FileContext> getLastSerializedFileContext();
109 
110 private:
111  using Attribute = std::pair<std::string, std::string>;
112  using Attributes = std::vector<Attribute>;
113  Attributes::iterator findAttribute(string_view name);
114  Attributes::const_iterator findAttribute(string_view name) const;
115  void dump(std::string& result, unsigned indentNum) const;
116 
117  std::string name;
118  std::string data;
119  Children children;
120  Attributes attributes;
121 };
123 
124 } // namespace openmsx
125 
126 #endif
bool findAttributeInt(string_view attName, unsigned &result) const
Definition: XMLElement.cc:263
const std::string & getName() const
Definition: XMLElement.hh:28
bool getAttributeAsBool(string_view attName, bool defaultValue=false) const
Definition: XMLElement.cc:247
void setName(string_view name)
Definition: XMLElement.cc:82
const XMLElement * findChild(string_view name) const
Definition: XMLElement.cc:115
void removeChild(const XMLElement &child)
Definition: XMLElement.cc:41
XMLElement & addChild(string_view name)
Definition: XMLElement.cc:30
void setAttribute(string_view name, string_view value)
Definition: XMLElement.cc:64
std::string dump() const
Definition: XMLElement.cc:275
XMLElement & getCreateChildWithAttribute(string_view name, string_view attName, string_view attValue)
Definition: XMLElement.cc:176
int getAttributeAsInt(string_view attName, int defaultValue=0) const
Definition: XMLElement.cc:255
const std::string & getData() const
Definition: XMLElement.hh:33
void serialize(Archive &ar, unsigned version)
Definition: XMLElement.cc:356
static std::unique_ptr< FileContext > getLastSerializedFileContext()
Definition: XMLElement.cc:339
void addAttribute(string_view name, string_view value)
Definition: XMLElement.cc:58
bool hasChildren() const
Definition: XMLElement.hh:48
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
void setData(string_view data)
Definition: XMLElement.cc:92
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:188
const XMLElement * findChildWithAttribute(string_view name, string_view attName, string_view attValue) const
Definition: XMLElement.cc:148
void removeAttribute(string_view name)
Definition: XMLElement.cc:74
int getChildDataAsInt(string_view name, int defaultValue=0) const
Definition: XMLElement.cc:206
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
void setChildData(string_view name, string_view value)
Definition: XMLElement.cc:212
XMLElement & getCreateChild(string_view name, string_view defaultValue={})
Definition: XMLElement.cc:167
static std::string XMLEscape(const std::string &str)
Definition: XMLElement.cc:312
std::vector< XMLElement > Children
Definition: XMLElement.hh:42
bool getChildDataAsBool(string_view name, bool defaultValue=false) const
Definition: XMLElement.cc:200
const std::string & getAttribute(string_view attName) const
Definition: XMLElement.cc:231
const Children & getChildren() const
Definition: XMLElement.hh:47
const XMLElement & getChild(string_view name) const
Definition: XMLElement.cc:162
bool hasAttribute(string_view name) const
Definition: XMLElement.cc:226
const XMLElement * findNextChild(string_view name, size_t &fromIndex) const
Definition: XMLElement.cc:120