gc-anchored.cpp revision cb492d6626459b2b9597ee37f9e7f7813f9b3b23
871fccc39b870543073d7015a9d8c701252b68f9joncruz/*
871fccc39b870543073d7015a9d8c701252b68f9joncruz * Inkscape::GC::Anchored - base class for anchored GC-managed objects
871fccc39b870543073d7015a9d8c701252b68f9joncruz *
871fccc39b870543073d7015a9d8c701252b68f9joncruz * Authors:
871fccc39b870543073d7015a9d8c701252b68f9joncruz * MenTaLguY <mental@rydia.net>
871fccc39b870543073d7015a9d8c701252b68f9joncruz *
871fccc39b870543073d7015a9d8c701252b68f9joncruz * Copyright (C) 2004 MenTaLguY
871fccc39b870543073d7015a9d8c701252b68f9joncruz *
871fccc39b870543073d7015a9d8c701252b68f9joncruz * Released under GNU GPL, read the file 'COPYING' for more information
871fccc39b870543073d7015a9d8c701252b68f9joncruz */
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruz#include <typeinfo>
871fccc39b870543073d7015a9d8c701252b68f9joncruz#include "gc-anchored.h"
871fccc39b870543073d7015a9d8c701252b68f9joncruz#include "debug/event-tracker.h"
871fccc39b870543073d7015a9d8c701252b68f9joncruz#include "debug/event.h"
871fccc39b870543073d7015a9d8c701252b68f9joncruz#include "util/share.h"
871fccc39b870543073d7015a9d8c701252b68f9joncruz#include "util/format.h"
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruznamespace Inkscape {
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruznamespace GC {
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruzclass AnchorEvent : public Debug::Event {
871fccc39b870543073d7015a9d8c701252b68f9joncruzpublic:
871fccc39b870543073d7015a9d8c701252b68f9joncruz enum Type { ANCHOR, RELEASE };
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruz AnchorEvent(GC::Anchored const *object, Type type)
871fccc39b870543073d7015a9d8c701252b68f9joncruz : _base(Util::format("%p", Core::base(const_cast<Anchored *>(object)))),
c32a5405bae84de22982c7007511f2a21a8c9046JazzyNico _object(Util::format("%p", object)),
871fccc39b870543073d7015a9d8c701252b68f9joncruz _class_name(Util::share_static_string(typeid(*object).name())),
479ed85bac2b7467a6678bae572404e33465a5d9acspike _refcount(Util::format("%d", ( type == ANCHOR ? object->_anchored_refcount() + 1 : object->_anchored_refcount() - 1 ))),
871fccc39b870543073d7015a9d8c701252b68f9joncruz _type(type)
871fccc39b870543073d7015a9d8c701252b68f9joncruz {}
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruz static Category category() { return REFCOUNT; }
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruz Util::ptr_shared<char> name() const {
871fccc39b870543073d7015a9d8c701252b68f9joncruz if ( _type == ANCHOR ) {
871fccc39b870543073d7015a9d8c701252b68f9joncruz return Util::share_static_string("gc-anchor");
871fccc39b870543073d7015a9d8c701252b68f9joncruz } else {
871fccc39b870543073d7015a9d8c701252b68f9joncruz return Util::share_static_string("gc-release");
871fccc39b870543073d7015a9d8c701252b68f9joncruz }
871fccc39b870543073d7015a9d8c701252b68f9joncruz }
871fccc39b870543073d7015a9d8c701252b68f9joncruz unsigned propertyCount() const { return 4; }
871fccc39b870543073d7015a9d8c701252b68f9joncruz PropertyPair property(unsigned index) const {
871fccc39b870543073d7015a9d8c701252b68f9joncruz switch (index) {
871fccc39b870543073d7015a9d8c701252b68f9joncruz case 0:
871fccc39b870543073d7015a9d8c701252b68f9joncruz return PropertyPair("base", _base);
871fccc39b870543073d7015a9d8c701252b68f9joncruz case 1:
871fccc39b870543073d7015a9d8c701252b68f9joncruz return PropertyPair("pointer", _object);
c32a5405bae84de22982c7007511f2a21a8c9046JazzyNico case 2:
871fccc39b870543073d7015a9d8c701252b68f9joncruz return PropertyPair("class", _class_name);
871fccc39b870543073d7015a9d8c701252b68f9joncruz case 3:
871fccc39b870543073d7015a9d8c701252b68f9joncruz return PropertyPair("new-refcount", _refcount);
871fccc39b870543073d7015a9d8c701252b68f9joncruz default:
871fccc39b870543073d7015a9d8c701252b68f9joncruz return PropertyPair();
871fccc39b870543073d7015a9d8c701252b68f9joncruz }
871fccc39b870543073d7015a9d8c701252b68f9joncruz }
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruzprivate:
871fccc39b870543073d7015a9d8c701252b68f9joncruz Util::ptr_shared<char> _base;
871fccc39b870543073d7015a9d8c701252b68f9joncruz Util::ptr_shared<char> _object;
871fccc39b870543073d7015a9d8c701252b68f9joncruz Util::ptr_shared<char> _class_name;
871fccc39b870543073d7015a9d8c701252b68f9joncruz Util::ptr_shared<char> _refcount;
871fccc39b870543073d7015a9d8c701252b68f9joncruz Type _type;
871fccc39b870543073d7015a9d8c701252b68f9joncruz};
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruzAnchored::Anchor *Anchored::_new_anchor() const {
871fccc39b870543073d7015a9d8c701252b68f9joncruz return new Anchor(this);
871fccc39b870543073d7015a9d8c701252b68f9joncruz}
09ba3247163582bf2e30e17c4c154aa259ce038acilix
871fccc39b870543073d7015a9d8c701252b68f9joncruzvoid Anchored::_free_anchor(Anchored::Anchor *anchor) const {
871fccc39b870543073d7015a9d8c701252b68f9joncruz delete anchor;
871fccc39b870543073d7015a9d8c701252b68f9joncruz}
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruzvoid Anchored::anchor() const {
871fccc39b870543073d7015a9d8c701252b68f9joncruz Debug::EventTracker<AnchorEvent> tracker(this, AnchorEvent::ANCHOR);
871fccc39b870543073d7015a9d8c701252b68f9joncruz if (!_anchor) {
871fccc39b870543073d7015a9d8c701252b68f9joncruz _anchor = _new_anchor();
871fccc39b870543073d7015a9d8c701252b68f9joncruz }
871fccc39b870543073d7015a9d8c701252b68f9joncruz _anchor->refcount++;
871fccc39b870543073d7015a9d8c701252b68f9joncruz}
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruzvoid Anchored::release() const {
871fccc39b870543073d7015a9d8c701252b68f9joncruz Debug::EventTracker<AnchorEvent> tracker(this, AnchorEvent::RELEASE);
871fccc39b870543073d7015a9d8c701252b68f9joncruz if (!--_anchor->refcount) {
871fccc39b870543073d7015a9d8c701252b68f9joncruz _free_anchor(_anchor);
871fccc39b870543073d7015a9d8c701252b68f9joncruz _anchor = NULL;
b0a706debff59200299511d55822722c5cb3e15fbuliabyak }
871fccc39b870543073d7015a9d8c701252b68f9joncruz}
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruz}
871fccc39b870543073d7015a9d8c701252b68f9joncruz
871fccc39b870543073d7015a9d8c701252b68f9joncruz}
09ba3247163582bf2e30e17c4c154aa259ce038acilix
871fccc39b870543073d7015a9d8c701252b68f9joncruz/*
479ed85bac2b7467a6678bae572404e33465a5d9acspike Local Variables:
871fccc39b870543073d7015a9d8c701252b68f9joncruz mode:c++
871fccc39b870543073d7015a9d8c701252b68f9joncruz c-file-style:"stroustrup"
871fccc39b870543073d7015a9d8c701252b68f9joncruz c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
871fccc39b870543073d7015a9d8c701252b68f9joncruz indent-tabs-mode:nil
871fccc39b870543073d7015a9d8c701252b68f9joncruz fill-column:99
00cd8cdd29c4bcef32835fb75ed803cefeec6d10theAdib End:
254fa123309e1c50f0d93f4bd199b9ceddd0f120theadib*/
871fccc39b870543073d7015a9d8c701252b68f9joncruz// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
254fa123309e1c50f0d93f4bd199b9ceddd0f120theadib