Code

r11516@tres: ted | 2006-04-26 21:30:18 -0700
[inkscape.git] / src / sp-object.cpp
index 4a1a2e09ed84a6a28825af91485be617cbaec2a9..fbe16e957b826874d0657b99eb1b70eb532457d7 100644 (file)
@@ -44,6 +44,9 @@
 #include "xml/repr.h"
 #include "xml/node-fns.h"
 #include "debug/event-tracker.h"
+#include "debug/simple-event.h"
+#include "util/share.h"
+#include "util/format.h"
 
 #include "algorithms/longest-common-suffix.h"
 using std::memcpy;
@@ -192,6 +195,7 @@ sp_object_init(SPObject *object)
     object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
 
     new (&object->_delete_signal) sigc::signal<void, SPObject *>();
+    new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
     object->_successor = NULL;
 
     object->_label = NULL;
@@ -221,58 +225,39 @@ sp_object_finalize(GObject *object)
     }
 
     spobject->_delete_signal.~signal();
+    spobject->_position_changed_signal.~signal();
 }
 
 namespace {
 
-Inkscape::Util::shared_ptr<char> stringify(SPObject *obj) {
-    char *temp=g_strdup_printf("%p", obj);
-    Inkscape::Util::shared_ptr<char> result=Inkscape::Util::share_string(temp);
-    g_free(temp);
-    return result;
-}
+namespace Debug = Inkscape::Debug;
+namespace Util = Inkscape::Util;
 
-Inkscape::Util::shared_ptr<char> stringify(unsigned n) {
-    char *temp=g_strdup_printf("%u", n);
-    Inkscape::Util::shared_ptr<char> result=Inkscape::Util::share_string(temp);
-    g_free(temp);
-    return result;
-}
+typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> BaseRefCountEvent;
 
-class RefEvent : public Inkscape::Debug::Event {
+class RefCountEvent : public BaseRefCountEvent {
 public:
-    enum Type { REF, UNREF };
+    RefCountEvent(SPObject *object, int bias, Util::ptr_shared<char> name)
+    : BaseRefCountEvent(name)
+    {
+        _addProperty("object", Util::format("%p", object));
+        _addProperty("class", Util::share_static_string(g_type_name(G_TYPE_FROM_INSTANCE(object))));
+        _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias));
+    }
+};
 
-    RefEvent(SPObject *object, Type type)
-        : _object(stringify(object)), _refcount(G_OBJECT(object)->ref_count),
-          _type(type)
+class RefEvent : public RefCountEvent {
+public:
+    RefEvent(SPObject *object)
+    : RefCountEvent(object, 1, Util::share_static_string("sp-object-ref"))
     {}
+};
 
-    static Category category() { return REFCOUNT; }
-
-    Inkscape::Util::shared_ptr<char> name() const {
-        if ( _type == REF) {
-            return Inkscape::Util::share_static_string("sp-object-ref");
-        } else {
-            return Inkscape::Util::share_static_string("sp-object-unref");
-        }
-    }
-    unsigned propertyCount() const { return 2; }
-    PropertyPair property(unsigned index) const {
-        switch (index) {
-            case 0:
-                return PropertyPair("object", _object);
-            case 1:
-                return PropertyPair("refcount", stringify( _type == REF ? _refcount + 1 : _refcount - 1 ));
-            default:
-                return PropertyPair();
-        }
-    }
-
-private:
-    Inkscape::Util::shared_ptr<char> _object;
-    unsigned _refcount;
-    Type _type;
+class UnrefEvent : public RefCountEvent {
+public:
+    UnrefEvent(SPObject *object)
+    : RefCountEvent(object, -1, Util::share_static_string("sp-object-unref"))
+    {}
 };
 
 }
@@ -291,11 +276,8 @@ sp_object_ref(SPObject *object, SPObject *owner)
     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
 
-    Inkscape::Debug::EventTracker<> tracker;
-    tracker.set<RefEvent>(object, RefEvent::REF);
-
+    Inkscape::Debug::EventTracker<RefEvent> tracker(object);
     g_object_ref(G_OBJECT(object));
-
     return object;
 }
 
@@ -314,11 +296,8 @@ sp_object_unref(SPObject *object, SPObject *owner)
     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
 
-    Inkscape::Debug::EventTracker<> tracker;
-    tracker.set<RefEvent>(object, RefEvent::UNREF);
-
+    Inkscape::Debug::EventTracker<UnrefEvent> tracker(object);
     g_object_unref(G_OBJECT(object));
-
     return NULL;
 }
 
@@ -657,6 +636,8 @@ sp_object_detach(SPObject *parent, SPObject *object) {
     g_return_if_fail(SP_IS_OBJECT(object));
     g_return_if_fail(object->parent == parent);
 
+    sp_object_invoke_release(object);
+
     SPObject *prev=NULL;
     for ( SPObject *child = parent->children ; child && child != object ;
           child = child->next )
@@ -677,7 +658,6 @@ sp_object_detach(SPObject *parent, SPObject *object) {
     object->next = NULL;
     object->parent = NULL;
 
-    sp_object_invoke_release(object);
     parent->_updateTotalHRefCount(-object->_total_hrefcount);
     sp_object_unref(object, parent);
 }
@@ -771,6 +751,7 @@ static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child
     g_return_if_fail(ochild != NULL);
     SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
     sp_object_reorder(ochild, prev);
+    ochild->_position_changed_signal.emit(ochild);
 }
 
 /**
@@ -867,9 +848,6 @@ sp_object_invoke_release(SPObject *object)
     g_assert(object != NULL);
     g_assert(SP_IS_OBJECT(object));
 
-    // we need to remember our parent
-    // g_assert(!object->parent);
-    g_assert(!object->next);
     g_assert(object->document);
     g_assert(object->repr);