From 058e95a59ccb2ab1748392acdfdbbffd516c9c81 Mon Sep 17 00:00:00 2001 From: Adrian Boguszewski Date: Mon, 11 Jul 2016 14:24:52 +0200 Subject: First part of new SPObject children list (bzr r14954.1.17) --- src/sp-object.cpp | 124 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 69 insertions(+), 55 deletions(-) (limited to 'src/sp-object.cpp') diff --git a/src/sp-object.cpp b/src/sp-object.cpp index d1659eedc..36957ab49 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -7,8 +7,9 @@ * Stephen Silver * Jon A. Cruz * Abhishek Sharma + * Adrian Boguszewski * - * Copyright (C) 1999-2008 authors + * Copyright (C) 1999-2016 authors * Copyright (C) 2001-2002 Ximian, Inc. * * Released under GNU GPL, read the file 'COPYING' for more information @@ -16,6 +17,7 @@ #include #include +#include #include "helper/sp-marshal.h" #include "xml/node-event-vector.h" @@ -398,13 +400,12 @@ void SPObject::changeCSS(SPCSSAttr *css, gchar const *attr) } std::vector SPObject::childList(bool add_ref, Action) { - std::vector l; - for ( SPObject *child = firstChild() ; child; child = child->getNext() ) { + std::vector l; + for (auto& child: _children) { if (add_ref) { - sp_object_ref (child); + sp_object_ref(&child); } - - l.push_back(child); + l.push_back(&child); } return l; @@ -467,9 +468,9 @@ void SPObject::requestOrphanCollection() { } void SPObject::_sendDeleteSignalRecursive() { - for (SPObject *child = firstChild(); child; child = child->getNext()) { - child->_delete_signal.emit(child); - child->_sendDeleteSignalRecursive(); + for (auto& child: _children) { + child._delete_signal.emit(&child); + child._sendDeleteSignalRecursive(); } } @@ -497,12 +498,12 @@ void SPObject::deleteObject(bool propagate, bool propagate_descendants) void SPObject::cropToObject(SPObject *except) { std::vector toDelete; - for ( SPObject *child = this->firstChild(); child; child = child->getNext() ) { - if (SP_IS_ITEM(child)) { - if (child->isAncestorOf(except)) { - child->cropToObject(except); - } else if(child != except) { - toDelete.push_back(child); + for (auto& child: _children) { + if (SP_IS_ITEM(&child)) { + if (child.isAncestorOf(except)) { + child.cropToObject(except); + } else if(&child != except) { + toDelete.push_back(&child); } } } @@ -525,6 +526,12 @@ void SPObject::attach(SPObject *object, SPObject *prev) object->parent = this; this->_updateTotalHRefCount(object->_total_hrefcount); + auto it = _children.begin(); + if (prev != nullptr) { + it = ++_children.iterator_to(*prev); + } + _children.insert(it, *object); + SPObject *next; if (prev) { next = prev->next; @@ -541,43 +548,47 @@ void SPObject::attach(SPObject *object, SPObject *prev) object->xml_space.value = this->xml_space.value; } -void SPObject::reorder(SPObject *prev) -{ - //g_return_if_fail(object != NULL); - //g_return_if_fail(SP_IS_OBJECT(object)); - g_return_if_fail(this->parent != NULL); - g_return_if_fail(this != prev); - g_return_if_fail(!prev || SP_IS_OBJECT(prev)); - g_return_if_fail(!prev || prev->parent == this->parent); +void SPObject::reorder(SPObject* obj, SPObject* prev) { + g_return_if_fail(obj != nullptr); + g_return_if_fail(obj->parent); + g_return_if_fail(obj->parent == this); + g_return_if_fail(obj != prev); + g_return_if_fail(!prev || prev->parent == obj->parent); + + auto it = _children.begin(); + if (prev != nullptr) { + it = ++_children.iterator_to(*prev); + } + + _children.splice(it, _children, _children.iterator_to(*obj)); - SPObject *const parent=this->parent; SPObject *old_prev=NULL; - for ( SPObject *child = parent->children ; child && child != this ; + for ( SPObject *child = children ; child && child != obj ; child = child->next ) { old_prev = child; } - SPObject *next=this->next; + SPObject *next=obj->next; if (old_prev) { old_prev->next = next; } else { - parent->children = next; + children = next; } if (!next) { - parent->_last_child = old_prev; + _last_child = old_prev; } if (prev) { next = prev->next; - prev->next = this; + prev->next = obj; } else { - next = parent->children; - parent->children = this; + next = children; + children = obj; } - this->next = next; + obj->next = next; if (!next) { - parent->_last_child = this; + _last_child = obj; } } @@ -589,6 +600,7 @@ void SPObject::detach(SPObject *object) g_return_if_fail(SP_IS_OBJECT(object)); g_return_if_fail(object->parent == this); + _children.erase(_children.iterator_to(*object)); object->releaseReferences(); SPObject *prev=NULL; @@ -618,14 +630,14 @@ void SPObject::detach(SPObject *object) SPObject *SPObject::get_child_by_repr(Inkscape::XML::Node *repr) { g_return_val_if_fail(repr != NULL, NULL); - SPObject *result = 0; + SPObject *result = nullptr; - if ( _last_child && (_last_child->getRepr() == repr) ) { - result = _last_child; // optimization for common scenario + if (_children.size() > 0 && _children.back().getRepr() == repr) { + result = &_children.back(); // optimization for common scenario } else { - for ( SPObject *child = children ; child ; child = child->next ) { - if ( child->getRepr() == repr ) { - result = child; + for (auto& child: _children) { + if (child.getRepr() == repr) { + result = &child; break; } } @@ -656,10 +668,12 @@ void SPObject::child_added(Inkscape::XML::Node *child, Inkscape::XML::Node *ref) void SPObject::release() { SPObject* object = this; - debug("id=%p, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object)); - while (object->children) { - object->detach(object->children); + auto tmp = _children | boost::adaptors::transformed([](SPObject& obj){return &obj;}); + std::vector toRelease(tmp.begin(), tmp.end()); + + for (auto& p: toRelease) { + object->detach(p); } } @@ -680,7 +694,7 @@ void SPObject::order_changed(Inkscape::XML::Node *child, Inkscape::XML::Node * / SPObject *ochild = object->get_child_by_repr(child); g_return_if_fail(ochild != NULL); SPObject *prev = new_ref ? object->get_child_by_repr(new_ref) : NULL; - ochild->reorder(prev); + object->reorder(ochild, prev); ochild->_position_changed_signal.emit(ochild); } @@ -1523,33 +1537,33 @@ bool SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname, bool return true; } -SPObject * SPObject::findFirstChild(gchar const *tagname) const +SPObject* SPObject::findFirstChild(gchar const *tagname) const { - for (SPObject *child = children; child; child = child->next) + for (auto& child: const_cast(this)->_children) { - if (child->repr->type() == Inkscape::XML::ELEMENT_NODE && - !strcmp(child->repr->name(), tagname)) { - return child; + if (child.repr->type() == Inkscape::XML::ELEMENT_NODE && + !strcmp(child.repr->name(), tagname)) { + return &child; } } - return NULL; + return nullptr; } char* SPObject::textualContent() const { GString* text = g_string_new(""); - for (const SPObject *child = firstChild(); child; child = child->next) + for (auto& child: _children) { - Inkscape::XML::NodeType child_type = child->repr->type(); + Inkscape::XML::NodeType child_type = child.repr->type(); if (child_type == Inkscape::XML::ELEMENT_NODE) { - char* new_string = child->textualContent(); + char* new_string = child.textualContent(); g_string_append(text, new_string); g_free(new_string); } else if (child_type == Inkscape::XML::TEXT_NODE) { - g_string_append(text, child->repr->content()); + g_string_append(text, child.repr->content()); } } return g_string_free(text, FALSE); @@ -1566,8 +1580,8 @@ void SPObject::recursivePrintTree( unsigned level ) std::cout << " "; } std::cout << (getId()?getId():"No object id") << std::endl; - for (SPObject *child = children; child; child = child->next) { - child->recursivePrintTree( level+1 ); + for (auto& child: _children) { + child.recursivePrintTree(level + 1); } } -- cgit v1.2.3 From d1947e768272c703674129d5c583204ff2b59251 Mon Sep 17 00:00:00 2001 From: Adrian Boguszewski Date: Wed, 13 Jul 2016 13:36:19 +0200 Subject: Second part of new SPObject children list (bzr r14954.1.19) --- src/sp-object.cpp | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) (limited to 'src/sp-object.cpp') diff --git a/src/sp-object.cpp b/src/sp-object.cpp index 36957ab49..2babf0441 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -118,7 +118,7 @@ static gchar *sp_object_get_unique_id(SPObject *object, */ SPObject::SPObject() : cloned(0), uflags(0), mflags(0), hrefcount(0), _total_hrefcount(0), - document(NULL), parent(NULL), children(NULL), _last_child(NULL), + document(NULL), parent(NULL), children(NULL), next(NULL), id(NULL), repr(NULL), refCount(1),hrefList(std::list()), _successor(NULL), _collection_policy(SPObject::COLLECT_WITH_PARENT), _label(NULL), _default_label(NULL) @@ -541,9 +541,6 @@ void SPObject::attach(SPObject *object, SPObject *prev) this->children = object; } object->next = next; - if (!next) { - this->_last_child = object; - } if (!object->xml_space.set) object->xml_space.value = this->xml_space.value; } @@ -576,9 +573,6 @@ void SPObject::reorder(SPObject* obj, SPObject* prev) { } else { children = next; } - if (!next) { - _last_child = old_prev; - } if (prev) { next = prev->next; prev->next = obj; @@ -587,9 +581,6 @@ void SPObject::reorder(SPObject* obj, SPObject* prev) { children = obj; } obj->next = next; - if (!next) { - _last_child = obj; - } } void SPObject::detach(SPObject *object) @@ -616,9 +607,6 @@ void SPObject::detach(SPObject *object) } else { this->children = next; } - if (!next) { - this->_last_child = prev; - } object->next = NULL; object->parent = NULL; @@ -856,11 +844,9 @@ void SPObject::releaseReferences() { SPObject *SPObject::getPrev() { - SPObject *prev = 0; - for ( SPObject *obj = parent->firstChild(); obj && !prev; obj = obj->getNext() ) { - if (obj->getNext() == this) { - prev = obj; - } + SPObject *prev = nullptr; + if (parent && !parent->_children.empty() && &parent->_children.front() != this) { + prev = &*(--parent->_children.iterator_to(*this)); } return prev; } -- cgit v1.2.3 From 9e210a6d1333c3366681547e3e81593ef69ff73e Mon Sep 17 00:00:00 2001 From: Adrian Boguszewski Date: Thu, 14 Jul 2016 12:56:49 +0200 Subject: Last part of new SPObject children list (bzr r14954.1.20) --- src/sp-object.cpp | 66 +++++++++++++------------------------------------------ 1 file changed, 15 insertions(+), 51 deletions(-) (limited to 'src/sp-object.cpp') diff --git a/src/sp-object.cpp b/src/sp-object.cpp index 2babf0441..587efd4f6 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -118,8 +118,7 @@ static gchar *sp_object_get_unique_id(SPObject *object, */ SPObject::SPObject() : cloned(0), uflags(0), mflags(0), hrefcount(0), _total_hrefcount(0), - document(NULL), parent(NULL), children(NULL), - next(NULL), id(NULL), repr(NULL), refCount(1),hrefList(std::list()), + document(NULL), parent(NULL), id(NULL), repr(NULL), refCount(1), hrefList(std::list()), _successor(NULL), _collection_policy(SPObject::COLLECT_WITH_PARENT), _label(NULL), _default_label(NULL) { @@ -532,15 +531,6 @@ void SPObject::attach(SPObject *object, SPObject *prev) } _children.insert(it, *object); - SPObject *next; - if (prev) { - next = prev->next; - prev->next = object; - } else { - next = this->children; - this->children = object; - } - object->next = next; if (!object->xml_space.set) object->xml_space.value = this->xml_space.value; } @@ -558,29 +548,6 @@ void SPObject::reorder(SPObject* obj, SPObject* prev) { } _children.splice(it, _children, _children.iterator_to(*obj)); - - - SPObject *old_prev=NULL; - for ( SPObject *child = children ; child && child != obj ; - child = child->next ) - { - old_prev = child; - } - - SPObject *next=obj->next; - if (old_prev) { - old_prev->next = next; - } else { - children = next; - } - if (prev) { - next = prev->next; - prev->next = obj; - } else { - next = children; - children = obj; - } - obj->next = next; } void SPObject::detach(SPObject *object) @@ -594,21 +561,6 @@ void SPObject::detach(SPObject *object) _children.erase(_children.iterator_to(*object)); object->releaseReferences(); - SPObject *prev=NULL; - for ( SPObject *child = this->children ; child && child != object ; - child = child->next ) - { - prev = child; - } - - SPObject *next=object->next; - if (prev) { - prev->next = next; - } else { - this->children = next; - } - - object->next = NULL; object->parent = NULL; this->_updateTotalHRefCount(-object->_total_hrefcount); @@ -851,6 +803,15 @@ SPObject *SPObject::getPrev() return prev; } +SPObject* SPObject::getNext() +{ + SPObject *next = nullptr; + if (parent && !parent->_children.empty() && &parent->_children.back() != this) { + next = &*(++parent->_children.iterator_to(*this)); + } + return next; +} + void SPObject::repr_child_added(Inkscape::XML::Node * /*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data) { SPObject *object = SP_OBJECT(data); @@ -1514,8 +1475,11 @@ bool SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname, bool } else { // remove the current content of the 'text' or 'desc' element - SPObject *child; - while (NULL != (child = elem->firstChild())) child->deleteObject(); + auto tmp = elem->_children | boost::adaptors::transformed([](SPObject& obj) { return &obj; }); + std::vector vec(tmp.begin(), tmp.end()); + for (auto &child: vec) { + child->deleteObject(); + } } // add the new content -- cgit v1.2.3 From 24d3f50003ca3cec6a03a7f5267cc4fe5588c69f Mon Sep 17 00:00:00 2001 From: Adrian Boguszewski Date: Thu, 14 Jul 2016 13:17:21 +0200 Subject: Renamed children list in SPObject (bzr r14954.1.21) --- src/sp-object.cpp | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'src/sp-object.cpp') diff --git a/src/sp-object.cpp b/src/sp-object.cpp index 587efd4f6..ccd70f4cb 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -400,7 +400,7 @@ void SPObject::changeCSS(SPCSSAttr *css, gchar const *attr) std::vector SPObject::childList(bool add_ref, Action) { std::vector l; - for (auto& child: _children) { + for (auto& child: children) { if (add_ref) { sp_object_ref(&child); } @@ -467,7 +467,7 @@ void SPObject::requestOrphanCollection() { } void SPObject::_sendDeleteSignalRecursive() { - for (auto& child: _children) { + for (auto& child: children) { child._delete_signal.emit(&child); child._sendDeleteSignalRecursive(); } @@ -497,7 +497,7 @@ void SPObject::deleteObject(bool propagate, bool propagate_descendants) void SPObject::cropToObject(SPObject *except) { std::vector toDelete; - for (auto& child: _children) { + for (auto& child: children) { if (SP_IS_ITEM(&child)) { if (child.isAncestorOf(except)) { child.cropToObject(except); @@ -525,11 +525,11 @@ void SPObject::attach(SPObject *object, SPObject *prev) object->parent = this; this->_updateTotalHRefCount(object->_total_hrefcount); - auto it = _children.begin(); + auto it = children.begin(); if (prev != nullptr) { - it = ++_children.iterator_to(*prev); + it = ++children.iterator_to(*prev); } - _children.insert(it, *object); + children.insert(it, *object); if (!object->xml_space.set) object->xml_space.value = this->xml_space.value; @@ -542,12 +542,12 @@ void SPObject::reorder(SPObject* obj, SPObject* prev) { g_return_if_fail(obj != prev); g_return_if_fail(!prev || prev->parent == obj->parent); - auto it = _children.begin(); + auto it = children.begin(); if (prev != nullptr) { - it = ++_children.iterator_to(*prev); + it = ++children.iterator_to(*prev); } - _children.splice(it, _children, _children.iterator_to(*obj)); + children.splice(it, children, children.iterator_to(*obj)); } void SPObject::detach(SPObject *object) @@ -558,7 +558,7 @@ void SPObject::detach(SPObject *object) g_return_if_fail(SP_IS_OBJECT(object)); g_return_if_fail(object->parent == this); - _children.erase(_children.iterator_to(*object)); + children.erase(children.iterator_to(*object)); object->releaseReferences(); object->parent = NULL; @@ -572,10 +572,10 @@ SPObject *SPObject::get_child_by_repr(Inkscape::XML::Node *repr) g_return_val_if_fail(repr != NULL, NULL); SPObject *result = nullptr; - if (_children.size() > 0 && _children.back().getRepr() == repr) { - result = &_children.back(); // optimization for common scenario + if (children.size() > 0 && children.back().getRepr() == repr) { + result = &children.back(); // optimization for common scenario } else { - for (auto& child: _children) { + for (auto& child: children) { if (child.getRepr() == repr) { result = &child; break; @@ -609,7 +609,7 @@ void SPObject::child_added(Inkscape::XML::Node *child, Inkscape::XML::Node *ref) void SPObject::release() { SPObject* object = this; debug("id=%p, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object)); - auto tmp = _children | boost::adaptors::transformed([](SPObject& obj){return &obj;}); + auto tmp = children | boost::adaptors::transformed([](SPObject& obj){return &obj;}); std::vector toRelease(tmp.begin(), tmp.end()); for (auto& p: toRelease) { @@ -797,8 +797,8 @@ void SPObject::releaseReferences() { SPObject *SPObject::getPrev() { SPObject *prev = nullptr; - if (parent && !parent->_children.empty() && &parent->_children.front() != this) { - prev = &*(--parent->_children.iterator_to(*this)); + if (parent && !parent->children.empty() && &parent->children.front() != this) { + prev = &*(--parent->children.iterator_to(*this)); } return prev; } @@ -806,8 +806,8 @@ SPObject *SPObject::getPrev() SPObject* SPObject::getNext() { SPObject *next = nullptr; - if (parent && !parent->_children.empty() && &parent->_children.back() != this) { - next = &*(++parent->_children.iterator_to(*this)); + if (parent && !parent->children.empty() && &parent->children.back() != this) { + next = &*(++parent->children.iterator_to(*this)); } return next; } @@ -1475,7 +1475,7 @@ bool SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname, bool } else { // remove the current content of the 'text' or 'desc' element - auto tmp = elem->_children | boost::adaptors::transformed([](SPObject& obj) { return &obj; }); + auto tmp = elem->children | boost::adaptors::transformed([](SPObject& obj) { return &obj; }); std::vector vec(tmp.begin(), tmp.end()); for (auto &child: vec) { child->deleteObject(); @@ -1489,7 +1489,7 @@ bool SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname, bool SPObject* SPObject::findFirstChild(gchar const *tagname) const { - for (auto& child: const_cast(this)->_children) + for (auto& child: const_cast(this)->children) { if (child.repr->type() == Inkscape::XML::ELEMENT_NODE && !strcmp(child.repr->name(), tagname)) { @@ -1503,7 +1503,7 @@ char* SPObject::textualContent() const { GString* text = g_string_new(""); - for (auto& child: _children) + for (auto& child: children) { Inkscape::XML::NodeType child_type = child.repr->type(); @@ -1530,7 +1530,7 @@ void SPObject::recursivePrintTree( unsigned level ) std::cout << " "; } std::cout << (getId()?getId():"No object id") << std::endl; - for (auto& child: _children) { + for (auto& child: children) { child.recursivePrintTree(level + 1); } } -- cgit v1.2.3 From ff4fbbc93f67afd6cbf851691833a50d6c76b350 Mon Sep 17 00:00:00 2001 From: Adrian Boguszewski Date: Wed, 27 Jul 2016 12:19:03 +0200 Subject: Renamed some functions, fixed tests (bzr r14954.1.28) --- src/sp-object.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/sp-object.cpp') diff --git a/src/sp-object.cpp b/src/sp-object.cpp index ccd70f4cb..27c788d75 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -151,6 +151,9 @@ SPObject::~SPObject() { sp_object_unref(this->_successor, NULL); this->_successor = NULL; } + if (parent) { + parent->children.erase(parent->children.iterator_to(*this)); + } if( style == NULL ) { // style pointer could be NULL if unreffed too many times. -- cgit v1.2.3