Otclient  14/8/2020
otmlnode.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef OTMLNODE_H
24 #define OTMLNODE_H
25 
26 #include "declarations.h"
27 
29 {
30 public:
31  virtual ~OTMLNode() { }
32 
33  static OTMLNodePtr create(std::string tag = "", bool unique = false);
34  static OTMLNodePtr create(std::string tag, std::string value);
35 
36  std::string tag() { return m_tag; }
37  int size() { return m_children.size(); }
38  std::string source() { return m_source; }
39  std::string rawValue() { return m_value; }
40 
41  bool isUnique() { return m_unique; }
42  bool isNull() { return m_null; }
43 
44  bool hasTag() { return !m_tag.empty(); }
45  bool hasValue() { return !m_value.empty(); }
46  bool hasChildren();
47  bool hasChildAt(const std::string& childTag) { return !!get(childTag); }
48  bool hasChildAtIndex(int childIndex) { return !!getIndex(childIndex); }
49 
50  void setTag(const std::string& tag) { m_tag = tag; }
51  void setValue(const std::string& value) { m_value = value; }
52  void setNull(bool null) { m_null = null; }
53  void setUnique(bool unique) { m_unique = unique; }
54  void setSource(const std::string& source) { m_source = source; }
55 
56  OTMLNodePtr get(const std::string& childTag);
57  OTMLNodePtr getIndex(int childIndex);
58 
59  OTMLNodePtr at(const std::string& childTag);
60  OTMLNodePtr atIndex(int childIndex);
61 
62  void addChild(const OTMLNodePtr& newChild);
63  bool removeChild(const OTMLNodePtr& oldChild);
64  bool replaceChild(const OTMLNodePtr& oldChild, const OTMLNodePtr& newChild);
65  void copy(const OTMLNodePtr& node);
66  void merge(const OTMLNodePtr& node);
67  void clear();
68 
71 
72  template<typename T = std::string>
73  T value();
74  template<typename T = std::string>
75  T valueAt(const std::string& childTag);
76  template<typename T = std::string>
77  T valueAtIndex(int childIndex);
78  template<typename T = std::string>
79  T valueAt(const std::string& childTag, const T& def);
80  template<typename T = std::string>
81  T valueAtIndex(int childIndex, const T& def);
82 
83  template<typename T>
84  void write(const T& v);
85  template<typename T>
86  void writeAt(const std::string& childTag, const T& v);
87  template<typename T>
88  void writeIn(const T& v);
89 
90  virtual std::string emit();
91 
92  OTMLNodePtr asOTMLNode() { return static_self_cast<OTMLNode>(); }
93 
94 protected:
95  OTMLNode() : m_unique(false), m_null(false) { }
96 
98  std::string m_tag;
99  std::string m_value;
100  std::string m_source;
101  bool m_unique;
102  bool m_null;
103 };
104 
105 #include "otmlexception.h"
106 
107 template<>
108 inline std::string OTMLNode::value<std::string>() {
109  std::string value = m_value;
110  if(stdext::starts_with(value, "\"") && stdext::ends_with(value, "\"")) {
111  value = value.substr(1, value.length()-2);
112  stdext::replace_all(value, "\\\\", "\\");
113  stdext::replace_all(value, "\\\"", "\"");
114  stdext::replace_all(value, "\\t", "\t");
115  stdext::replace_all(value, "\\n", "\n");
116  stdext::replace_all(value, "\\'", "\'");
117  }
118  return value;
119 }
120 
121 template<typename T>
123  T ret;
124  if(!stdext::cast(m_value, ret))
125  throw OTMLException(asOTMLNode(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type<T>()));
126  return ret;
127 }
128 
129 template<typename T>
130 T OTMLNode::valueAt(const std::string& childTag) {
131  OTMLNodePtr node = at(childTag);
132  return node->value<T>();
133 }
134 
135 template<typename T>
136 T OTMLNode::valueAtIndex(int childIndex) {
137  OTMLNodePtr node = atIndex(childIndex);
138  return node->value<T>();
139 }
140 
141 template<typename T>
142 T OTMLNode::valueAt(const std::string& childTag, const T& def) {
143  if(OTMLNodePtr node = get(childTag))
144  if(!node->isNull())
145  return node->value<T>();
146  return def;
147 }
148 
149 template<typename T>
150 T OTMLNode::valueAtIndex(int childIndex, const T& def) {
151  if(OTMLNodePtr node = getIndex(childIndex))
152  return node->value<T>();
153  return def;
154 }
155 
156 template<typename T>
157 void OTMLNode::write(const T& v) {
158  m_value = stdext::safe_cast<std::string>(v);
159 }
160 
161 template<typename T>
162 void OTMLNode::writeAt(const std::string& childTag, const T& v) {
163  OTMLNodePtr child = OTMLNode::create(childTag);
164  child->setUnique(true);
165  child->write<T>(v);
166  addChild(child);
167 }
168 
169 template<typename T>
170 void OTMLNode::writeIn(const T& v) {
171  OTMLNodePtr child = OTMLNode::create();
172  child->write<T>(v);
173  addChild(child);
174 }
175 
176 #endif
177 
OTMLNode::writeIn
void writeIn(const T &v)
Definition: otmlnode.h:170
OTMLNode::setTag
void setTag(const std::string &tag)
Definition: otmlnode.h:50
OTMLNode::setSource
void setSource(const std::string &source)
Definition: otmlnode.h:54
OTMLNode::source
std::string source()
Definition: otmlnode.h:38
OTMLNode
Definition: otmlnode.h:28
OTMLNode::hasValue
bool hasValue()
Definition: otmlnode.h:45
OTMLNode::writeAt
void writeAt(const std::string &childTag, const T &v)
Definition: otmlnode.h:162
OTMLException
All OTML errors throw this exception.
Definition: otmlexception.h:29
OTMLNode::write
void write(const T &v)
Definition: otmlnode.h:157
OTMLNode::hasChildren
bool hasChildren()
Definition: otmlnode.cpp:44
OTMLNode::m_null
bool m_null
Definition: otmlnode.h:102
otmlexception.h
OTMLNode::removeChild
bool removeChild(const OTMLNodePtr &oldChild)
Definition: otmlnode.cpp:124
OTMLNode::m_children
OTMLNodeList m_children
Definition: otmlnode.h:97
stdext::cast
bool cast(const T &in, R &out)
Definition: cast.h:37
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
declarations.h
OTMLNode::valueAtIndex
T valueAtIndex(int childIndex)
Definition: otmlnode.h:136
stdext::format
std::string format()
Definition: format.h:82
stdext::starts_with
bool starts_with(const std::string &str, const std::string &test)
Definition: string.cpp:263
OTMLNode::addChild
void addChild(const OTMLNodePtr &newChild)
Definition: otmlnode.cpp:91
OTMLNode::replaceChild
bool replaceChild(const OTMLNodePtr &oldChild, const OTMLNodePtr &newChild)
Definition: otmlnode.cpp:134
OTMLNode::isUnique
bool isUnique()
Definition: otmlnode.h:41
OTMLNode::setNull
void setNull(bool null)
Definition: otmlnode.h:52
OTMLNode::~OTMLNode
virtual ~OTMLNode()
Definition: otmlnode.h:31
OTMLNode::OTMLNode
OTMLNode()
Definition: otmlnode.h:95
OTMLNode::valueAt
T valueAt(const std::string &childTag)
Definition: otmlnode.h:130
OTMLNode::tag
std::string tag()
Definition: otmlnode.h:36
OTMLNode::setUnique
void setUnique(bool unique)
Definition: otmlnode.h:53
OTMLNode::emit
virtual std::string emit()
Definition: otmlnode.cpp:192
OTMLNode::getIndex
OTMLNodePtr getIndex(int childIndex)
Definition: otmlnode.cpp:63
OTMLNode::value
T value()
Definition: otmlnode.h:122
OTMLNode::rawValue
std::string rawValue()
Definition: otmlnode.h:39
OTMLNode::clone
OTMLNodePtr clone()
Definition: otmlnode.cpp:179
OTMLNode::setValue
void setValue(const std::string &value)
Definition: otmlnode.h:51
OTMLNodeList
std::vector< OTMLNodePtr > OTMLNodeList
Definition: declarations.h:35
OTMLNode::size
int size()
Definition: otmlnode.h:37
OTMLNode::get
OTMLNodePtr get(const std::string &childTag)
Definition: otmlnode.cpp:54
OTMLNode::m_value
std::string m_value
Definition: otmlnode.h:99
stdext::ends_with
bool ends_with(const std::string &str, const std::string &test)
Definition: string.cpp:258
OTMLNode::hasTag
bool hasTag()
Definition: otmlnode.h:44
OTMLNode::asOTMLNode
OTMLNodePtr asOTMLNode()
Definition: otmlnode.h:92
OTMLNode::m_source
std::string m_source
Definition: otmlnode.h:100
stdext::shared_object_ptr< OTMLNode >
OTMLNode::merge
void merge(const OTMLNodePtr &node)
Definition: otmlnode.cpp:157
OTMLNode::copy
void copy(const OTMLNodePtr &node)
Definition: otmlnode.cpp:145
stdext::replace_all
void replace_all(std::string &str, const std::string &search, const std::string &replacement)
Definition: string.cpp:268
OTMLNode::hasChildAt
bool hasChildAt(const std::string &childTag)
Definition: otmlnode.h:47
OTMLNode::isNull
bool isNull()
Definition: otmlnode.h:42
OTMLNode::m_tag
std::string m_tag
Definition: otmlnode.h:98
OTMLNode::clear
void clear()
Definition: otmlnode.cpp:165
OTMLNode::hasChildAtIndex
bool hasChildAtIndex(int childIndex)
Definition: otmlnode.h:48
OTMLNode::m_unique
bool m_unique
Definition: otmlnode.h:101
OTMLNode::atIndex
OTMLNodePtr atIndex(int childIndex)
Definition: otmlnode.cpp:84
stdext::shared_object
Definition: shared_object.h:41
OTMLNode::at
OTMLNodePtr at(const std::string &childTag)
Definition: otmlnode.cpp:70
OTMLNode::create
static OTMLNodePtr create(std::string tag="", bool unique=false)
Definition: otmlnode.cpp:27