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);
27  XMLElement(string_ref name, string_ref data);
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  typedef std::vector<XMLElement> Children;
45  // note: returned XMLElement& is validated on the next addChild() call
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  // data
57  bool getDataAsBool() const;
58  int getDataAsInt() const;
59  double getDataAsDouble() const;
60 
61  // attribute
62  bool hasAttribute(string_ref name) const;
63  const std::string& getAttribute(string_ref attName) const;
65  string_ref defaultValue) const;
66  bool getAttributeAsBool(string_ref attName,
67  bool defaultValue = false) const;
68  int getAttributeAsInt(string_ref attName,
69  int defaultValue = 0) const;
70  bool findAttributeInt(string_ref attName,
71  unsigned& result) const;
72 
73  // child
74  const XMLElement* findChild(string_ref name) const;
76  const XMLElement& getChild(string_ref name) const;
78 
80  string_ref name, string_ref attName,
81  string_ref attValue) const;
83  string_ref name, string_ref attName,
84  string_ref attValue);
86  size_t& fromIndex) const;
87 
88  std::vector<const XMLElement*> getChildren(string_ref name) const;
89 
91  string_ref defaultValue = "");
93  string_ref name, string_ref attName,
94  string_ref attValue);
95 
96  const std::string& getChildData(string_ref name) const;
98  string_ref defaultValue) const;
100  bool defaultValue = false) const;
101  int getChildDataAsInt(string_ref name,
102  int defaultValue = 0) const;
103  void setChildData(string_ref name, string_ref 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  typedef std::pair<std::string, std::string> Attribute;
119  typedef std::vector<Attribute> Attributes;
120  Attributes::iterator findAttribute(string_ref name);
121  Attributes::const_iterator findAttribute(string_ref name) const;
122  void dump(StringOp::Builder& 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
void removeAttribute(string_ref name)
Definition: XMLElement.cc:73
double getDataAsDouble() const
Definition: XMLElement.cc:91
void setData(string_ref data)
Definition: XMLElement.cc:106
bool findAttributeInt(string_ref attName, unsigned &result) const
Definition: XMLElement.cc:284
XMLElement & getCreateChild(string_ref name, string_ref defaultValue="")
Definition: XMLElement.cc:187
void setChildData(string_ref name, string_ref value)
Definition: XMLElement.cc:232
void removeChild(const XMLElement &child)
Definition: XMLElement.cc:40
int getDataAsInt() const
Definition: XMLElement.cc:86
void setName(string_ref name)
Definition: XMLElement.cc:96
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
bool getDataAsBool() const
Definition: XMLElement.cc:81
const XMLElement & getChild(string_ref name) const
Definition: XMLElement.cc:182
std::string dump() const
Definition: XMLElement.cc:296
const XMLElement * findChild(string_ref name) const
Definition: XMLElement.cc:132
const std::string & getName() const
Definition: XMLElement.hh:30
const std::string & getChildData(string_ref name) const
Definition: XMLElement.cc:208
XMLElement & getCreateChildWithAttribute(string_ref name, string_ref attName, string_ref attValue)
Definition: XMLElement.cc:196
void serialize(Archive &ar, unsigned version)
Definition: XMLElement.cc:378
static std::unique_ptr< FileContext > getLastSerializedFileContext()
Definition: XMLElement.cc:361
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
const std::string & getAttribute(string_ref attName) const
Definition: XMLElement.cc:251
bool getAttributeAsBool(string_ref attName, bool defaultValue=false) const
Definition: XMLElement.cc:268
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:7
bool hasAttribute(string_ref name) const
Definition: XMLElement.cc:246
bool hasChildren() const
Definition: XMLElement.hh:50
int getChildDataAsInt(string_ref name, int defaultValue=0) const
Definition: XMLElement.cc:226
bool getChildDataAsBool(string_ref name, bool defaultValue=false) const
Definition: XMLElement.cc:220
const Children & getChildren() const
Definition: XMLElement.hh:49
int getAttributeAsInt(string_ref attName, int defaultValue=0) const
Definition: XMLElement.cc:276
const std::string & getData() const
Definition: XMLElement.hh:35
void addAttribute(string_ref name, string_ref value)
Definition: XMLElement.cc:57
static std::string XMLEscape(const std::string &str)
Definition: XMLElement.cc:334
XMLElement & addChild(string_ref name)
Definition: XMLElement.cc:29
const XMLElement * findNextChild(string_ref name, size_t &fromIndex) const
Definition: XMLElement.cc:137
void setAttribute(string_ref name, string_ref value)
Definition: XMLElement.cc:63
std::vector< XMLElement > Children
Definition: XMLElement.hh:44
const XMLElement * findChildWithAttribute(string_ref name, string_ref attName, string_ref attValue) const
Definition: XMLElement.cc:167