Code

Committed double code because of the hurry to let you use the axonom-snapping stuff.
[inkscape.git] / src / gc-soft-ptr.h
1 /** \file
2  * Inkscape::GC::soft_ptr - "soft" pointers to avoid finalization cycles
3  *
4  * Copyright 2006 MenTaLguY <mental@rydia.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * See the file COPYING for details.
12  *
13  */
15 #ifndef SEEN_INKSCAPE_GC_SOFT_PTR_H
16 #define SEEN_INKSCAPE_GC_SOFT_PTR_H
18 #include "gc-core.h"
20 namespace Inkscape {
22 namespace GC {
24 /** @brief A class for pointers which can be automatically cleared to break
25  *         finalization cycles.
26  */
27 template <typename T>
28 class soft_ptr {
29 public:
30     soft_ptr(T * const &pointer=NULL) : _pointer(pointer) {
31         _register();
32     }
33     soft_ptr(soft_ptr const &other) : _pointer(other._pointer) {
34         _register();
35     }
37     operator T *() const { return _pointer; }
38     T &operator*() const { return *_pointer; }
39     T *operator->() const { return _pointer; } 
40     T &operator[](int i) const { return _pointer[i]; }
42     soft_ptr &operator=(T * const &pointer) {
43         _pointer = pointer;
44         return *this;
45     }
47     // default copy
49 private:
50     void _register() {
51         void *base=Core::base(this);
52         if (base) {
53             Core::general_register_disappearing_link((void **)&_pointer, base);
54         }
55     }
57     T *_pointer;
58 };
60 }
62 }
64 #endif
65 /*
66   Local Variables:
67   mode:c++
68   c-file-style:"stroustrup"
69   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
70   indent-tabs-mode:nil
71   fill-column:99
72   End:
73 */
74 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :