From 17680991ff44a340869f04d7ce00a6889337ed7f Mon Sep 17 00:00:00 2001 From: mental Date: Sun, 1 Jul 2007 20:08:30 +0000 Subject: [PATCH] initial implementation of XML::Subtree API for tracking changes on a whole-subtree basis; the initial implementation does not have very good performance for large subtrees, but should work well for smaller ones --- src/xml/Makefile_insert | 2 + src/xml/subtree.cpp | 106 ++++++++++++++++++++++++++++++++++++++++ src/xml/subtree.h | 73 +++++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 src/xml/subtree.cpp create mode 100644 src/xml/subtree.h diff --git a/src/xml/Makefile_insert b/src/xml/Makefile_insert index 69f8952a9..0a95a2062 100644 --- a/src/xml/Makefile_insert +++ b/src/xml/Makefile_insert @@ -46,6 +46,8 @@ xml_libspxml_a_SOURCES = \ xml/node-event-vector.h \ xml/node-iterators.h \ xml/sp-css-attr.h \ + xml/subtree.cpp \ + xml/subtree.h \ xml/text-node.h \ xml/invalid-operation-exception.h diff --git a/src/xml/subtree.cpp b/src/xml/subtree.cpp new file mode 100644 index 000000000..7c549e7d7 --- /dev/null +++ b/src/xml/subtree.cpp @@ -0,0 +1,106 @@ +/* + * XML::Subtree - proxy for an XML subtree + * + * Copyright 2005 MenTaLguY + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * See the file COPYING for details. + * + */ + +#include "xml/node.h" +#include "xml/subtree.h" +#include "xml/node-iterators.h" + +namespace Inkscape { +namespace XML { + +namespace { + +void recursively(void (Node::*m)(NodeObserver &observer), + Node &node, NodeObserver &observer) +{ + (node.*m)(observer); + for ( NodeSiblingIterator iter = node.firstChild() ; iter ; ++iter ) { + recursively(m, *iter, observer); + } +} + +} + +Subtree::Subtree(Node &root) : _root(&root) { + recursively(&Node::addObserver, *_root, *this); +} + +Subtree::~Subtree() { + finish(); +} + +void Subtree::finish() { + if (_root) { + recursively(&Node::removeObserver, *_root, *this); + _root = NULL; + } +} + +void Subtree::synthesizeEvents(NodeObserver &observer) { + if (_root) { + recursively(&Node::synthesizeEvents, *_root, *this); + } +} + +void Subtree::addObserver(NodeObserver &observer) { + _observers.add(observer); +} + +void Subtree::removeObserver(NodeObserver &observer) { + _observers.remove(observer); +} + +void Subtree::notifyChildAdded(Node &node, Node &child, Node *prev) { + recursively(&Node::addObserver, child, *this); + _observers.notifyChildAdded(node, child, prev); +} + +void Subtree::notifyChildRemoved(Node &node, Node &child, Node *prev) { + recursively(&Node::removeObserver, child, *this); + _observers.notifyChildRemoved(node, child, prev); +} + +void Subtree::notifyChildOrderChanged(Node &node, Node &child, + Node *old_prev, Node *new_prev) +{ + _observers.notifyChildOrderChanged(node, child, old_prev, new_prev); +} + +void Subtree::notifyContentChanged(Node &node, + Util::ptr_shared old_content, + Util::ptr_shared new_content) +{ + _observers.notifyContentChanged(node, old_content, new_content); +} + +void Subtree::notifyAttributeChanged(Node &node, GQuark name, + Util::ptr_shared old_value, + Util::ptr_shared new_value) +{ + _observers.notifyAttributeChanged(node, name, old_value, new_value); +} + +} +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : diff --git a/src/xml/subtree.h b/src/xml/subtree.h new file mode 100644 index 000000000..73a6485d1 --- /dev/null +++ b/src/xml/subtree.h @@ -0,0 +1,73 @@ +/* + * XML::Subtree - proxy for an XML subtree + * + * Copyright 2005 MenTaLguY + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * See the file COPYING for details. + * + */ + +#ifndef SEEN_INKSCAPE_XML_SUBTREE_H +#define SEEN_INKSCAPE_XML_SUBTREE_H + +#include "xml/node-observer.h" +#include "xml/composite-node-observer.h" + +namespace Inkscape { +namespace XML { + +class Node; + +class Subtree : public Inkscape::GC::Anchored, + public Inkscape::GC::Managed<>, + private NodeObserver +{ +public: + Subtree(Node &root); + ~Subtree(); + + void finish(); + + void synthesizeEvents(NodeObserver &observer); + void addObserver(NodeObserver &observer); + void removeObserver(NodeObserver &observer); + +private: + void notifyChildAdded(Node &node, Node &child, Node *prev); + + void notifyChildRemoved(Node &node, Node &child, Node *prev); + + void notifyChildOrderChanged(Node &node, Node &child, + Node *old_prev, Node *new_prev); + + void notifyContentChanged(Node &node, + Util::ptr_shared old_content, + Util::ptr_shared new_content); + + void notifyAttributeChanged(Node &node, GQuark name, + Util::ptr_shared old_value, + Util::ptr_shared new_value); + + Node *_root; + CompositeNodeObserver _observers; +}; + +} +} + +#endif +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : -- 2.30.2