summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-src/src/XMLNode.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/texk/dvisvgm/dvisvgm-src/src/XMLNode.cpp')
-rw-r--r--Build/source/texk/dvisvgm/dvisvgm-src/src/XMLNode.cpp100
1 files changed, 56 insertions, 44 deletions
diff --git a/Build/source/texk/dvisvgm/dvisvgm-src/src/XMLNode.cpp b/Build/source/texk/dvisvgm/dvisvgm-src/src/XMLNode.cpp
index 32607250f11..bfdbbe476ea 100644
--- a/Build/source/texk/dvisvgm/dvisvgm-src/src/XMLNode.cpp
+++ b/Build/source/texk/dvisvgm/dvisvgm-src/src/XMLNode.cpp
@@ -2,7 +2,7 @@
** XMLNode.cpp **
** **
** This file is part of dvisvgm -- a fast DVI to SVG converter **
-** Copyright (C) 2005-2017 Martin Gieseking <martin.gieseking@uos.de> **
+** Copyright (C) 2005-2018 Martin Gieseking <martin.gieseking@uos.de> **
** **
** This program is free software; you can redistribute it and/or **
** modify it under the terms of the GNU General Public License as **
@@ -18,10 +18,10 @@
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/
-#include <config.h>
#include <map>
#include <list>
#include <sstream>
+#include "utility.hpp"
#include "XMLNode.hpp"
#include "XMLString.hpp"
@@ -40,6 +40,12 @@ XMLElementNode::XMLElementNode (const XMLElementNode &node)
}
+XMLElementNode::XMLElementNode (XMLElementNode &&node)
+ : _name(std::move(node._name)), _attributes(std::move(node._attributes)), _children(std::move(node._children))
+{
+}
+
+
void XMLElementNode::clear () {
_attributes.clear();
_children.clear();
@@ -47,50 +53,62 @@ void XMLElementNode::clear () {
void XMLElementNode::addAttribute (const string &name, const string &value) {
- _attributes[name] = value;
+ _attributes.emplace(name, value);
}
void XMLElementNode::addAttribute (const string &name, double value) {
- _attributes[name] = XMLString(value);
+ _attributes.emplace(name, XMLString(value));
}
-void XMLElementNode::append (XMLNode *child) {
+/** Appends a child node to this element. The element also takes the ownership of the child.
+ * @param[in] child node to be appended
+ * @return raw pointer to the appended child node */
+XMLNode* XMLElementNode::append (unique_ptr<XMLNode> &&child) {
if (!child)
- return;
- XMLTextNode *textNode1 = dynamic_cast<XMLTextNode*>(child);
+ return nullptr;
+ XMLTextNode *textNode1 = dynamic_cast<XMLTextNode*>(child.get());
if (!textNode1 || _children.empty())
- _children.emplace_back(unique_ptr<XMLNode>(child));
+ _children.emplace_back(std::move(child));
else {
if (XMLTextNode *textNode2 = dynamic_cast<XMLTextNode*>(_children.back().get()))
- textNode2->append(textNode1); // merge two consecutive text nodes
+ textNode2->append(util::static_unique_ptr_cast<XMLTextNode>(std::move(child))); // merge two consecutive text nodes
else
- _children.emplace_back(unique_ptr<XMLNode>(child));
+ _children.emplace_back(std::move(child));
}
+ return _children.back().get();
}
-void XMLElementNode::append (const string &str) {
+/** Appends a string to this element. If the last child is a text node, the string is
+ * appended there, otherwise a new text node is created.
+ * @param[in] str string to be appended
+ * @return raw pointer to the text node the string was appended to */
+XMLNode* XMLElementNode::append (const string &str) {
if (_children.empty() || !dynamic_cast<XMLTextNode*>(_children.back().get()))
- _children.emplace_back(unique_ptr<XMLNode>(new XMLTextNode(str)));
+ _children.emplace_back(util::make_unique<XMLTextNode>(str));
else
static_cast<XMLTextNode*>(_children.back().get())->append(str);
+ return _children.back().get();
}
-void XMLElementNode::prepend (XMLNode *child) {
+/** Prepends a child node to this element. The element also takes the ownership of the child.
+ * @param[in] child node to be prepended
+ * @return raw pointer to the prepended child node */
+XMLNode* XMLElementNode::prepend (unique_ptr<XMLNode> &&child) {
if (!child)
- return;
- XMLTextNode *textNode1 = dynamic_cast<XMLTextNode*>(child);
- unique_ptr<XMLNode> child_uptr(child);
+ return nullptr;
+ XMLTextNode *textNode1 = dynamic_cast<XMLTextNode*>(child.get());
if (textNode1 && !_children.empty()) {
if (XMLTextNode *textNode2 = dynamic_cast<XMLTextNode*>(_children.front().get())) {
- textNode2->prepend(textNode1); // merge two consecutive text nodes
- return;
+ textNode2->prepend(util::static_unique_ptr_cast<XMLTextNode>(std::move(child))); // merge two consecutive text nodes
+ return textNode2;
}
}
- _children.emplace_front(std::move(child_uptr));
+ _children.emplace_front(std::move(child));
+ return _children.front().get();
}
@@ -100,13 +118,13 @@ void XMLElementNode::prepend (XMLNode *child) {
* @param[in] child node to be inserted
* @param[in] sibling following sibling of 'child'
* @return true on success */
-bool XMLElementNode::insertBefore (XMLNode *child, XMLNode *sibling) {
- ChildList::iterator it = _children.begin();
+bool XMLElementNode::insertBefore (unique_ptr<XMLNode> &&child, XMLNode *sibling) {
+ auto it = _children.begin();
while (it != _children.end() && it->get() != sibling)
++it;
if (it == _children.end())
return false;
- _children.emplace(it, unique_ptr<XMLNode>(child));
+ _children.emplace(it, std::move(child));
return true;
}
@@ -117,13 +135,13 @@ bool XMLElementNode::insertBefore (XMLNode *child, XMLNode *sibling) {
* @param[in] child node to be inserted
* @param[in] sibling preceding sibling of 'child'
* @return true on success */
-bool XMLElementNode::insertAfter (XMLNode *child, XMLNode *sibling) {
- ChildList::iterator it = _children.begin();
+bool XMLElementNode::insertAfter (unique_ptr<XMLNode> &&child, XMLNode *sibling) {
+ auto it = _children.begin();
while (it != _children.end() && it->get() != sibling)
++it;
if (it == _children.end())
return false;
- _children.emplace(++it, unique_ptr<XMLNode>(child));
+ _children.emplace(++it, std::move(child));
return true;
}
@@ -170,7 +188,7 @@ XMLElementNode* XMLElementNode::getFirstDescendant (const char *name, const char
return descendant;
}
}
- return 0;
+ return nullptr;
}
@@ -209,35 +227,33 @@ bool XMLElementNode::hasAttribute (const string &name) const {
/** Returns the value of an attribute.
* @param[in] name name of attribute
* @return attribute value or 0 if attribute doesn't exist */
-const char* XMLElementNode::getAttributeValue(const std::string& name) const {
- AttribMap::const_iterator it = _attributes.find(name);
+const char* XMLElementNode::getAttributeValue (const std::string& name) const {
+ auto it = _attributes.find(name);
if (it != _attributes.end())
return it->second.c_str();
- return 0;
+ return nullptr;
}
//////////////////////
-void XMLTextNode::append (XMLNode *node) {
+void XMLTextNode::append (unique_ptr<XMLNode> &&node) {
if (!node)
return;
- if (XMLTextNode *tn = dynamic_cast<XMLTextNode*>(node))
- append(tn);
+ if (dynamic_cast<XMLTextNode*>(node.get()))
+ append(util::static_unique_ptr_cast<XMLTextNode>(std::move(node)));
else {
// append text representation of the node
ostringstream oss;
node->write(oss);
append(XMLString(oss.str()));
- delete node;
}
}
-void XMLTextNode::append (XMLTextNode *node) {
+void XMLTextNode::append (unique_ptr<XMLTextNode> &&node) {
if (node)
_text += node->_text;
- delete node;
}
@@ -246,16 +262,12 @@ void XMLTextNode::append (const string &str) {
}
-void XMLTextNode::prepend (XMLNode *node) {
- if (XMLTextNode *tn = dynamic_cast<XMLTextNode*>(node))
- _text = tn->_text + _text;
- else
- delete node;
+void XMLTextNode::prepend (unique_ptr<XMLNode> &&node) {
+ if (XMLTextNode *textNode = dynamic_cast<XMLTextNode*>(node.get()))
+ _text = textNode->_text + _text;
}
-
-//////////////////////
-
+/////////////////////////////////////////////////////////////////////
ostream& XMLCDataNode::write (ostream &os) const {
if (!_data.empty())
@@ -269,4 +281,4 @@ void XMLCDataNode::append (string &&str) {
_data = move(str);
else
_data += str;
-} \ No newline at end of file
+}