Code

switch everyone to simpler debug event API
[inkscape.git] / src / gc-anchored.cpp
1 /*
2  * Inkscape::GC::Anchored - base class for anchored GC-managed objects
3  *
4  * Authors:
5  *   MenTaLguY <mental@rydia.net>
6  *
7  * Copyright (C) 2004 MenTaLguY
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
12 #include <typeinfo>
13 #include "gc-anchored.h"
14 #include "debug/event-tracker.h"
15 #include "debug/simple-event.h"
16 #include "util/share.h"
17 #include "util/format.h"
19 namespace Inkscape {
21 namespace GC {
23 namespace {
25 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> RefCountEvent;
27 class BaseAnchorEvent : public RefCountEvent {
28 public:
29     BaseAnchorEvent(Anchored const *object, int bias,
30                     Util::ptr_shared<char> name)
31     : RefCountEvent(name)
32     {
33         _addProperty("base", Util::format("%p", Core::base(const_cast<Anchored *>(object))));
34         _addProperty("pointer", Util::format("%p", object));
35         _addProperty("class", Util::share_static_string(typeid(*object).name()));
36         _addProperty("new-refcount", Util::format("%d", object->_anchored_refcount() + bias));
37     }
38 };
40 class AnchorEvent : public BaseAnchorEvent {
41 public:
42     AnchorEvent(Anchored const *object)
43     : BaseAnchorEvent(object, 1, Util::share_static_string("gc-anchor"))
44     {}
45 };
47 class ReleaseEvent : public BaseAnchorEvent {
48 public:
49     ReleaseEvent(Anchored const *object)
50     : BaseAnchorEvent(object, -1, Util::share_static_string("gc-release"))
51     {}
52 };
54 }
56 Anchored::Anchor *Anchored::_new_anchor() const {
57     return new Anchor(this);
58 }
60 void Anchored::_free_anchor(Anchored::Anchor *anchor) const {
61     delete anchor;
62 }
64 void Anchored::anchor() const {
65     Debug::EventTracker<AnchorEvent> tracker(this);
66     if (!_anchor) {
67         _anchor = _new_anchor();
68     }
69     _anchor->refcount++;
70 }
72 void Anchored::release() const {
73     Debug::EventTracker<ReleaseEvent> tracker(this);
74     if (!--_anchor->refcount) {
75         _free_anchor(_anchor);
76         _anchor = NULL;
77     }
78 }
80 }
82 }
84 /*
85   Local Variables:
86   mode:c++
87   c-file-style:"stroustrup"
88   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
89   indent-tabs-mode:nil
90   fill-column:99
91   End:
92 */
93 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :