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  using Children = std::vector<XMLElement>;
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  // attribute
57  bool hasAttribute(string_ref name) const;
58  const std::string& getAttribute(string_ref attName) const;
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;
71  const XMLElement& getChild(string_ref name) const;
73 
75  string_ref name, string_ref attName,
76  string_ref attValue) const;
78  string_ref name, string_ref attName,
79  string_ref attValue);
81  size_t& fromIndex) const;
82 
83  std::vector<const XMLElement*> getChildren(string_ref name) const;
84 
86  string_ref defaultValue = "");
88  string_ref name, string_ref attName,
89  string_ref attValue);
90 
91  const std::string& getChildData(string_ref name) const;
93  string_ref defaultValue) const;
95  bool defaultValue = false) const;
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
void removeAttribute(string_ref name)
Definition: XMLElement.cc:73
void setData(string_ref data)
Definition: XMLElement.cc:91
bool findAttributeInt(string_ref attName, unsigned &result) const
Definition: XMLElement.cc:269
XMLElement & getCreateChild(string_ref name, string_ref defaultValue="")
Definition: XMLElement.cc:172
void setChildData(string_ref name, string_ref value)
Definition: XMLElement.cc:217
void removeChild(const XMLElement &child)
Definition: XMLElement.cc:40
void setName(string_ref name)
Definition: XMLElement.cc:81
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
const XMLElement & getChild(string_ref name) const
Definition: XMLElement.cc:167
std::string dump() const
Definition: XMLElement.cc:281
const XMLElement * findChild(string_ref name) const
Definition: XMLElement.cc:117
const std::string & getName() const
Definition: XMLElement.hh:30
const std::string & getChildData(string_ref name) const
Definition: XMLElement.cc:193
XMLElement & getCreateChildWithAttribute(string_ref name, string_ref attName, string_ref attValue)
Definition: XMLElement.cc:181
void serialize(Archive &ar, unsigned version)
Definition: XMLElement.cc:363
static std::unique_ptr< FileContext > getLastSerializedFileContext()
Definition: XMLElement.cc:346
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
const std::string & getAttribute(string_ref attName) const
Definition: XMLElement.cc:236
bool getAttributeAsBool(string_ref attName, bool defaultValue=false) const
Definition: XMLElement.cc:253
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
bool hasAttribute(string_ref name) const
Definition: XMLElement.cc:231
bool hasChildren() const
Definition: XMLElement.hh:50
int getChildDataAsInt(string_ref name, int defaultValue=0) const
Definition: XMLElement.cc:211
bool getChildDataAsBool(string_ref name, bool defaultValue=false) const
Definition: XMLElement.cc:205
const Children & getChildren() const
Definition: XMLElement.hh:49
int getAttributeAsInt(string_ref attName, int defaultValue=0) const
Definition: XMLElement.cc:261
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:319
std::vector< XMLElement > Children
Definition: XMLElement.hh:44
XMLElement & addChild(string_ref name)
Definition: XMLElement.cc:29
const XMLElement * findNextChild(string_ref name, size_t &fromIndex) const
Definition: XMLElement.cc:122
void setAttribute(string_ref name, string_ref value)
Definition: XMLElement.cc:63
const XMLElement * findChildWithAttribute(string_ref name, string_ref attName, string_ref attValue) const
Definition: XMLElement.cc:152