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 StringOp { class Builder; }
11 
12 namespace openmsx {
13 
14 class FileContext;
15 
17 {
18 public:
19  //
20  // Basic functions
21  //
22 
23  // Construction.
24  // (copy, assign, move, destruct are default)
26  explicit XMLElement(string_ref name);
28 
29  // name
30  const std::string& getName() const { return name; }
31  void setName(string_ref name);
32  void clearName();
33 
34  // data
35  const std::string& getData() const { return data; }
36  void setData(string_ref data);
37 
38  // attribute
39  void addAttribute(string_ref name, string_ref value);
40  void setAttribute(string_ref name, string_ref value);
41  void removeAttribute(string_ref name);
42 
43  // child
44  using Children = std::vector<XMLElement>;
45  // note: returned XMLElement& is validated on the next addChild() call
46  XMLElement& addChild(string_ref name);
47  XMLElement& addChild(string_ref name, string_ref data);
48  void removeChild(const XMLElement& child);
49  const Children& getChildren() const { return children; }
50  bool hasChildren() const { return !children.empty(); }
51 
52  //
53  // Convenience functions
54  //
55 
56  // attribute
57  bool hasAttribute(string_ref name) const;
58  const std::string& getAttribute(string_ref attName) const;
59  string_ref getAttribute(string_ref attName,
60  string_ref defaultValue) const;
61  bool getAttributeAsBool(string_ref attName,
62  bool defaultValue = false) const;
63  int getAttributeAsInt(string_ref attName,
64  int defaultValue = 0) const;
65  bool findAttributeInt(string_ref attName,
66  unsigned& result) const;
67 
68  // child
69  const XMLElement* findChild(string_ref name) const;
70  XMLElement* findChild(string_ref name);
71  const XMLElement& getChild(string_ref name) const;
72  XMLElement& getChild(string_ref name);
73 
74  const XMLElement* findChildWithAttribute(
75  string_ref name, string_ref attName,
76  string_ref attValue) const;
77  XMLElement* findChildWithAttribute(
78  string_ref name, string_ref attName,
79  string_ref attValue);
80  const XMLElement* findNextChild(string_ref name,
81  size_t& fromIndex) const;
82 
83  std::vector<const XMLElement*> getChildren(string_ref name) const;
84 
85  XMLElement& getCreateChild(string_ref name,
86  string_ref defaultValue = "");
87  XMLElement& getCreateChildWithAttribute(
88  string_ref name, string_ref attName,
89  string_ref attValue);
90 
91  const std::string& getChildData(string_ref name) const;
92  string_ref getChildData(string_ref name,
93  string_ref defaultValue) const;
94  bool getChildDataAsBool(string_ref name,
95  bool defaultValue = false) const;
96  int getChildDataAsInt(string_ref name,
97  int defaultValue = 0) const;
98  void setChildData(string_ref name, string_ref value);
99 
100  void removeAllChildren();
101 
102  // various
103  std::string dump() const;
104  static std::string XMLEscape(const std::string& str);
105 
106  template<typename Archive>
107  void serialize(Archive& ar, unsigned version);
108 
109  // For backwards compatibility with older savestates
110  static std::unique_ptr<FileContext> getLastSerializedFileContext();
111 
112 private:
113  using Attribute = std::pair<std::string, std::string>;
114  using Attributes = std::vector<Attribute>;
115  Attributes::iterator findAttribute(string_ref name);
116  Attributes::const_iterator findAttribute(string_ref name) const;
117  void dump(StringOp::Builder& result, unsigned indentNum) const;
118 
119  std::string name;
120  std::string data;
121  Children children;
122  Attributes attributes;
123 };
125 
126 } // namespace openmsx
127 
128 #endif
const std::string & getName() const
Definition: XMLElement.hh:30
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
const std::string & getData() const
Definition: XMLElement.hh:35
bool hasChildren() const
Definition: XMLElement.hh:50
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
uint8_t * data()
std::vector< XMLElement > Children
Definition: XMLElement.hh:44
#define SERIALIZE_CLASS_VERSION(CLASS, VERSION)
const Children & getChildren() const
Definition: XMLElement.hh:49
void serialize(Archive &ar, T &t, unsigned version)