Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
gc-anchored.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Authors:
4 * MenTaLguY <mental@rydia.net>
5 * * Copyright (C) 2004 MenTaLguY
6 *
7 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
8 */
9
10#ifndef SEEN_INKSCAPE_GC_ANCHORED_H
11#define SEEN_INKSCAPE_GC_ANCHORED_H
12
13#include "inkgc/gc-managed.h"
14
15namespace Inkscape::GC {
16
46class Anchored {
47public:
48 void anchor() const;
49 void release() const;
50
51 // for debugging
52 unsigned _anchored_refcount() const {
53 return ( _anchor ? _anchor->refcount : 0 );
54 }
55
56 Anchored(Anchored const &) = delete; // no copy
57 void operator=(Anchored const &) = delete; // no assign
58
59protected:
60 Anchored() : _anchor(nullptr) { anchor(); } // initial refcount of one
61 virtual ~Anchored() = default;
62
63private:
64 struct Anchor : public Managed<SCANNED, MANUAL> {
65 Anchor() : refcount(0),base(nullptr) {}
66 Anchor(Anchored const *obj) : refcount(0) {
67 base = Core::base(const_cast<Anchored *>(obj));
68 }
70 void *base;
71 };
72
73 mutable Anchor *_anchor;
74
75 Anchor *_new_anchor() const;
76 void _free_anchor(Anchor *anchor) const;
77};
78
91template <typename R>
92static R &anchor(R &r) {
93 static_cast<Anchored const &>(const_cast<R const &>(r)).anchor();
94 return r;
95}
96
109template <typename R>
110static R *anchor(R *r) {
111 static_cast<Anchored const *>(const_cast<R const *>(r))->anchor();
112 return r;
113}
114
132template <typename R>
133static R &release(R &r) {
134 static_cast<Anchored const &>(const_cast<R const &>(r)).release();
135 return r;
136}
137
155template <typename R>
156static R *release(R *r) {
157 static_cast<Anchored const *>(const_cast<R const *>(r))->release();
158 return r;
159}
160
161} // namespace Inkscape::GC
162
163#endif // SEEN_INKSCAPE_GC_ANCHORED_H
164
165/*
166 Local Variables:
167 mode:c++
168 c-file-style:"stroustrup"
169 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
170 indent-tabs-mode:nil
171 fill-column:99
172 End:
173*/
174// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
A base class for anchored objects.
Definition gc-anchored.h:46
Anchor * _new_anchor() const
unsigned _anchored_refcount() const
Definition gc-anchored.h:52
void operator=(Anchored const &)=delete
virtual ~Anchored()=default
Anchored(Anchored const &)=delete
void _free_anchor(Anchor *anchor) const
A base class for objects for whom the normal new and delete operators should use the garbage-collecte...
Definition gc-managed.h:27
Base class for GC-managed objects.
Boehm-GC based garbage collector.
static R & anchor(R &r)
Increments the reference count of a anchored object.
Definition gc-anchored.h:92
static R & release(R &r)
Decrements the reference count of a anchored object.
Anchor(Anchored const *obj)
Definition gc-anchored.h:66
static void * base(void *ptr)
Definition gc-core.h:77