index f439243cdb2ccb49761abc03d9cc00144ef3ebc4..3cca393d228366d7b013d9109d7c207ebaddd85f 100644 (file)
--- a/src/xml/simple-node.cpp
+++ b/src/xml/simple-node.cpp
-/*
- * SimpleNode - simple XML node implementation
- *
- * Copyright 2003-2005 MenTaLguY <mental@rydia.net>
+/** @file
+ * @brief Garbage collected XML node implementation
+ */
+/* Copyright 2003-2005 MenTaLguY <mental@rydia.net>
* Copyright 2003 Nathan Hurst
* Copyright 1999-2003 Lauris Kaplinski
* Copyright 2000-2002 Ximian Inc.
* of the License, or (at your option) any later version.
*
* See the file COPYING for details.
- *
*/
+#include <cstring>
+#include <string>
#include <glib/gstrfuncs.h>
+
#include "xml/simple-node.h"
#include "xml/node-event-vector.h"
#include "xml/node-fns.h"
using Util::rest;
using Util::set_rest;
-SimpleNode::SimpleNode(int code)
+SimpleNode::SimpleNode(int code, Document *document)
: Node(), _name(code), _attributes(), _child_count(0),
_cached_positions_valid(false)
{
- this->_document = NULL;
- this->_document = NULL;
+ g_assert(document != NULL);
+
+ this->_document = document;
this->_parent = this->_next = NULL;
this->_first_child = this->_last_child = NULL;
+
+ _observers.add(_subtree_observers);
}
-SimpleNode::SimpleNode(SimpleNode const &node)
+SimpleNode::SimpleNode(SimpleNode const &node, Document *document)
: Node(),
_cached_position(node._cached_position),
_name(node._name), _attributes(), _content(node._content),
_child_count(node._child_count),
_cached_positions_valid(node._cached_positions_valid)
{
- _document = NULL;
- _document = NULL;
+ g_assert(document != NULL);
+
+ _document = document;
_parent = _next = NULL;
_first_child = _last_child = NULL;
- for ( Node *child = node._first_child ;
- child != NULL ; child = child->next() )
+ for ( SimpleNode *child = node._first_child ;
+ child != NULL ; child = child->_next )
{
- Node *child_copy=child->duplicate();
+ SimpleNode *child_copy=dynamic_cast<SimpleNode *>(child->duplicate(document));
child_copy->_setParent(this);
if (_last_child) {
- _last_child->_setNext(child_copy);
+ _last_child->_next = child_copy;
} else {
_first_child = child_copy;
}
{
_attributes = cons(*iter, _attributes);
}
+
+ _observers.add(_subtree_observers);
}
gchar const *SimpleNode::name() const {
return _parent->_childPosition(*this);
}
-unsigned SimpleNode::_childPosition(Node const &child) const {
+unsigned SimpleNode::_childPosition(SimpleNode const &child) const {
if (!_cached_positions_valid) {
unsigned position=0;
- for ( Node *sibling = _first_child ;
- sibling ; sibling = sibling->next() )
+ for ( SimpleNode *sibling = _first_child ;
+ sibling ; sibling = sibling->_next )
{
- sibling->_setCachedPosition(position);
+ sibling->_cached_position = position;
position++;
}
_cached_positions_valid = true;
}
- return child._cachedPosition();
+ return child._cached_position;
}
Node *SimpleNode::nthChild(unsigned index) {
- Node *child = _first_child;
- for ( ; index > 0 && child ; child = child->next() ) {
+ SimpleNode *child = _first_child;
+ for ( ; index > 0 && child ; child = child->_next ) {
index--;
}
return child;
return false;
}
+void SimpleNode::_setParent(SimpleNode *parent) {
+ if (_parent) {
+ _subtree_observers.remove(_parent->_subtree_observers);
+ }
+ _parent = parent;
+ if (parent) {
+ _subtree_observers.add(parent->_subtree_observers);
+ }
+}
+
void SimpleNode::setContent(gchar const *content) {
ptr_shared<char> old_content=_content;
ptr_shared<char> new_content = ( content ? share_string(content) : ptr_shared<char>() );
_content = new_content;
if ( _content != old_content ) {
- if (_document) {
- _document->logger()->notifyContentChanged(*this, old_content, _content);
- }
-
+ _document->logger()->notifyContentChanged(*this, old_content, _content);
_observers.notifyContentChanged(*this, old_content, _content);
}
}
void
-SimpleNode::setAttribute(gchar const *name, gchar const *value, bool const is_interactive)
+SimpleNode::setAttribute(gchar const *name, gchar const *value, bool const /*is_interactive*/)
{
g_return_if_fail(name && *name);
@@ -336,25 +351,28 @@ SimpleNode::setAttribute(gchar const *name, gchar const *value, bool const is_in
}
if ( new_value != old_value && (!old_value || !new_value || strcmp(old_value, new_value))) {
- if (_document) {
- _document->logger()->notifyAttributeChanged(*this, key, old_value, new_value);
- }
-
+ _document->logger()->notifyAttributeChanged(*this, key, old_value, new_value);
_observers.notifyAttributeChanged(*this, key, old_value, new_value);
}
}
-void SimpleNode::addChild(Node *child, Node *ref) {
- g_assert(child);
- g_assert(!ref || ref->parent() == this);
- g_assert(!child->parent());
+void SimpleNode::addChild(Node *generic_child, Node *generic_ref) {
+ g_assert(generic_child);
+ g_assert(generic_child->document() == _document);
+ g_assert(!generic_ref || generic_ref->document() == _document);
+
+ SimpleNode *child=dynamic_cast<SimpleNode *>(generic_child);
+ SimpleNode *ref=dynamic_cast<SimpleNode *>(generic_ref);
+
+ g_assert(!ref || ref->_parent == this);
+ g_assert(!child->_parent);
Debug::EventTracker<DebugAddChild> tracker(*this, *child, ref);
- Node *next;
+ SimpleNode *next;
if (ref) {
- next = ref->next();
- ref->_setNext(child);
+ next = ref->_next;
+ ref->_next = child;
} else {
next = _first_child;
_first_child = child;
// set cached position if possible when appending
if (!ref) {
// if !next && !ref, child is sole child
- child->_setCachedPosition(0);
+ child->_cached_position = 0;
_cached_positions_valid = true;
} else if (_cached_positions_valid) {
- child->_setCachedPosition(ref->_cachedPosition() + 1);
+ child->_cached_position = ref->_cached_position + 1;
}
} else {
// invalidate cached positions otherwise
}
child->_setParent(this);
- child->_setNext(next);
+ child->_next = next;
_child_count++;
- if (_document) {
- child->_bindDocument(*_document);
- _document->logger()->notifyChildAdded(*this, *child, ref);
- }
-
+ _document->logger()->notifyChildAdded(*this, *child, ref);
_observers.notifyChildAdded(*this, *child, ref);
}
-void SimpleNode::_bindDocument(Document &document) {
- g_assert(!_document || _document == &document);
-
- if (!_document) {
- _document = &document;
-
- for ( Node *child = _first_child ; child != NULL ; child = child->next() ) {
- child->_bindDocument(document);
- }
- }
-}
+void SimpleNode::removeChild(Node *generic_child) {
+ g_assert(generic_child);
+ g_assert(generic_child->document() == _document);
-void SimpleNode::removeChild(Node *child) {
- g_assert(child);
- g_assert(child->parent() == this);
+ SimpleNode *child=dynamic_cast<SimpleNode *>(generic_child);
+ SimpleNode *ref=dynamic_cast<SimpleNode *>(previous_node(child));
- Node *ref = ( child != _first_child ? previous_node(child) : NULL );
+ g_assert(child->_parent == this);
Debug::EventTracker<DebugRemoveChild> tracker(*this, *child);
- Node *next = child->next();
+ SimpleNode *next = child->_next;
if (ref) {
- ref->_setNext(next);
+ ref->_next = next;
} else {
_first_child = next;
}
_cached_positions_valid = false;
}
- child->_setNext(NULL);
+ child->_next = NULL;
child->_setParent(NULL);
_child_count--;
- if (_document) {
- _document->logger()->notifyChildRemoved(*this, *child, ref);
- }
-
+ _document->logger()->notifyChildRemoved(*this, *child, ref);
_observers.notifyChildRemoved(*this, *child, ref);
}
-void SimpleNode::changeOrder(Node *child, Node *ref) {
- g_return_if_fail(child);
+void SimpleNode::changeOrder(Node *generic_child, Node *generic_ref) {
+ g_assert(generic_child);
+ g_assert(generic_child->document() == this->_document);
+ g_assert(!generic_ref || generic_ref->document() == this->_document);
+
+ SimpleNode *const child=dynamic_cast<SimpleNode *>(generic_child);
+ SimpleNode *const ref=dynamic_cast<SimpleNode *>(generic_ref);
+
g_return_if_fail(child->parent() == this);
g_return_if_fail(child != ref);
g_return_if_fail(!ref || ref->parent() == this);
- Node *const prev = previous_node(child);
+ SimpleNode *const prev=dynamic_cast<SimpleNode *>(previous_node(child));
Debug::EventTracker<DebugSetChildPosition> tracker(*this, *child, prev, ref);
if (prev == ref) { return; }
- Node *next;
+ SimpleNode *next;
/* Remove from old position. */
- next=child->next();
+ next = child->_next;
if (prev) {
- prev->_setNext(next);
+ prev->_next = next;
} else {
_first_child = next;
}
/* Insert at new position. */
if (ref) {
- next = ref->next();
- ref->_setNext(child);
+ next = ref->_next;
+ ref->_next = child;
} else {
next = _first_child;
_first_child = child;
}
- child->_setNext(next);
+ child->_next = next;
if (!next) {
_last_child = child;
}
_cached_positions_valid = false;
- if (_document) {
- _document->logger()->notifyChildOrderChanged(*this, *child, prev, ref);
- }
-
+ _document->logger()->notifyChildOrderChanged(*this, *child, prev, ref);
_observers.notifyChildOrderChanged(*this, *child, prev, ref);
}
// a position beyond the end of the list means the end of the list;
// a negative position is the same as an infinitely large position
- Node *ref=NULL;
- for ( Node *sibling = _parent->firstChild() ;
- sibling && pos ; sibling = sibling->next() )
+ SimpleNode *ref=NULL;
+ for ( SimpleNode *sibling = _parent->_first_child ;
+ sibling && pos ; sibling = sibling->_next )
{
if ( sibling != this ) {
ref = sibling;
@@ -510,7 +515,7 @@ void content_changed(Node *node, gchar const *old_content, gchar const *new_cont
reinterpret_cast<NodeObserver *>(data)->notifyContentChanged(*node, Util::share_unsafe((const char *)old_content), Util::share_unsafe((const char *)new_content));
}
-void attr_changed(Node *node, gchar const *name, gchar const *old_value, gchar const *new_value, bool is_interactive, void *data) {
+void attr_changed(Node *node, gchar const *name, gchar const *old_value, gchar const *new_value, bool /*is_interactive*/, void *data) {
reinterpret_cast<NodeObserver *>(data)->notifyAttributeChanged(*node, g_quark_from_string(name), Util::share_unsafe((const char *)old_value), Util::share_unsafe((const char *)new_value));
}
}
}
if (vector->child_added) {
- Node *ref = NULL;
- for ( Node *child = this->_first_child ;
- child ; child = child->next() )
+ SimpleNode *ref = NULL;
+ for ( SimpleNode *child = this->_first_child ;
+ child ; child = child->_next )
{
vector->child_added(this, child, ref, data);
ref = child;
if (rch) {
rch->mergeFrom(child, key);
} else {
- rch = child->duplicate();
+ rch = child->duplicate(_document);
appendChild(rch);
rch->release();
}
} else {
- Node *rch=child->duplicate();
+ Node *rch=child->duplicate(_document);
appendChild(rch);
rch->release();
}
fill-column:99
End:
*/
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :