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 *pointer=NULL) : _pointer(pointer) {
31 _register();
32 }
34 operator T *() const { return static_cast<T *>(_pointer); }
35 T &operator*() const { return *static_cast<T *>(_pointer); }
36 T *operator->() const { return static_cast<T *>(_pointer); }
37 T &operator[](int i) const { return static_cast<T *>(_pointer)[i]; }
39 soft_ptr &operator=(T *pointer) {
40 _pointer = pointer;
41 return *this;
42 }
44 // default copy
46 private:
47 void _register() {
48 void *base=Core::base(this);
49 if (base) {
50 Core::general_register_disappearing_link(&_pointer, base);
51 }
52 }
54 void *_pointer;
55 };
57 }
59 }
61 #endif
62 /*
63 Local Variables:
64 mode:c++
65 c-file-style:"stroustrup"
66 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
67 indent-tabs-mode:nil
68 fill-column:99
69 End:
70 */
71 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :