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 attrName) const;
48  std::string_view getAttribute(std::string_view attrName,
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 attrName) 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 attrName,
68  bool defaultValue = false) const;
69  int getAttributeAsInt(std::string_view attrName,
70  int defaultValue = 0) const;
71  bool findAttributeInt(std::string_view attrName,
72  unsigned& result) const;
73 
74  // child
75  const XMLElement* findChild(std::string_view childName) const;
76  XMLElement* findChild(std::string_view childName);
77  const XMLElement& getChild(std::string_view childName) const;
78  XMLElement& getChild(std::string_view childName);
79 
81  std::string_view childName, std::string_view attrName,
82  std::string_view attValue) const;
84  std::string_view childName, std::string_view attrName,
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 childName) const;
90 
91  XMLElement& getCreateChild(std::string_view childName,
92  std::string_view defaultValue = {});
94  std::string_view childName, std::string_view attrName,
95  std::string_view attValue);
96 
97  const std::string& getChildData(std::string_view childName) const;
98  std::string_view getChildData(std::string_view childName,
99  std::string_view defaultValue) const;
100  bool getChildDataAsBool(std::string_view childName,
101  bool defaultValue = false) const;
102  int getChildDataAsInt(std::string_view childName,
103  int defaultValue = 0) const;
104  void setChildData(std::string_view childName, 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 attrName);
122  Attributes::const_iterator getAttributeIter(std::string_view attrName) 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
openmsx::XMLElement::findAttribute
const std::string * findAttribute(std::string_view attrName) const
Definition: XMLElement.cc:46
openmsx::XMLElement::getChildDataAsBool
bool getChildDataAsBool(std::string_view childName, bool defaultValue=false) const
Definition: XMLElement.cc:180
openmsx::XMLElement::setChildData
void setChildData(std::string_view childName, std::string value)
Definition: XMLElement.cc:192
openmsx::XMLElement::findChildWithAttribute
const XMLElement * findChildWithAttribute(std::string_view childName, std::string_view attrName, std::string_view attValue) const
openmsx::XMLElement::findChild
const XMLElement * findChild(std::string_view childName) const
openmsx::XMLElement::hasAttribute
bool hasAttribute(std::string_view name) const
Definition: XMLElement.cc:206
serialize_meta.hh
openmsx::XMLElement::XMLElement
XMLElement(std::string name_, std::string data_)
Definition: XMLElement.hh:27
openmsx::XMLElement
XMLElement
Definition: XMLElement.cc:348
openmsx::XMLElement::removeChild
void removeChild(const XMLElement &child)
Definition: XMLElement.cc:29
openmsx::XMLElement::getCreateChildWithAttribute
XMLElement & getCreateChildWithAttribute(std::string_view childName, std::string_view attrName, std::string_view attValue)
Definition: XMLElement.cc:156
openmsx::XMLElement::getAttribute
const std::string & getAttribute(std::string_view attrName) const
openmsx::XMLElement::addChild
XMLElement & addChild(std::string name)
Definition: XMLElement.cc:20
openmsx::XMLElement::removeAllChildren
void removeAllChildren()
Definition: XMLElement.cc:201
openmsx::XMLElement::findNextChild
const XMLElement * findNextChild(std::string_view name, size_t &fromIndex) const
Definition: XMLElement.cc:98
openmsx::SERIALIZE_CLASS_VERSION
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
openmsx::XMLElement::serialize
void serialize(Archive &ar, unsigned version)
Definition: XMLElement.cc:331
openmsx::XMLElement::findAttributeInt
bool findAttributeInt(std::string_view attrName, unsigned &result) const
Definition: XMLElement.cc:240
openmsx::XMLElement
Definition: XMLElement.hh:15
openmsx::XMLElement::getLastSerializedFileContext
static std::unique_ptr< FileContext > getLastSerializedFileContext()
Definition: XMLElement.cc:314
openmsx::XMLElement::removeAttribute
void removeAttribute(std::string_view name)
Definition: XMLElement.cc:68
openmsx::FileContext
FileContext
Definition: FileContext.cc:120
openmsx::XMLElement::addAttribute
void addAttribute(std::string name, std::string value)
Definition: XMLElement.cc:52
openmsx::XMLElement::clearName
void clearName()
Definition: XMLElement.hh:33
openmsx::XMLElement::XMLElement
XMLElement(std::string name_)
Definition: XMLElement.hh:25
openmsx::XMLElement::getCreateChild
XMLElement & getCreateChild(std::string_view childName, std::string_view defaultValue={})
Definition: XMLElement.cc:147
openmsx::XMLElement::dump
std::string dump() const
Definition: XMLElement.cc:251
openmsx::XMLElement::getName
const std::string & getName() const
Definition: XMLElement.hh:31
openmsx::XMLElement::Children
std::vector< XMLElement > Children
Definition: XMLElement.hh:54
openmsx::XMLElement::getChildData
const std::string & getChildData(std::string_view childName) const
openmsx::XMLElement::getData
const std::string & getData() const
Definition: XMLElement.hh:36
openmsx::XMLElement::getAttributeAsInt
int getAttributeAsInt(std::string_view attrName, int defaultValue=0) const
Definition: XMLElement.cc:233
openmsx::XMLElement::getChildDataAsInt
int getChildDataAsInt(std::string_view childName, int defaultValue=0) const
Definition: XMLElement.cc:186
openmsx::XMLElement::getAttributeAsBool
bool getAttributeAsBool(std::string_view attrName, bool defaultValue=false) const
Definition: XMLElement.cc:226
openmsx::XMLElement::hasChildren
bool hasChildren() const
Definition: XMLElement.hh:60
openmsx::XMLElement::XMLEscape
static std::string XMLEscape(std::string_view str)
Definition: XMLElement.cc:287
openmsx::XMLElement::setData
void setData(std::string data_)
Definition: XMLElement.hh:37
openmsx::XMLElement::getChildren
const Children & getChildren() const
Definition: XMLElement.hh:59
openmsx::XMLElement::setAttribute
void setAttribute(std::string_view name, std::string value)
Definition: XMLElement.cc:58
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::XMLElement::setName
void setName(std::string name_)
Definition: XMLElement.hh:32
openmsx::XMLElement::XMLElement
XMLElement()=default
openmsx::XMLElement::getChild
const XMLElement & getChild(std::string_view childName) const