1 /** \file
2 * Inkscape::GC::Managed - base class for GC-managed objects
3 *
4 * Copyright 2004 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_MANAGED_H
16 #define SEEN_INKSCAPE_GC_MANAGED_H
18 #include "gc-core.h"
20 namespace Inkscape {
22 namespace GC {
24 /** @brief A base class for objects for whom the normal new and delete
25 * operators should use the garbage-collected allocator
26 */
27 template <ScanPolicy default_scan=SCANNED,
28 CollectionPolicy default_collect=AUTO>
29 class Managed {
30 public:
31 /** @brief Registers a pointer to be cleared when this object becomes
32 * inaccessible.
33 */
34 template <typename T>
35 void clearOnceInaccessible(T **p_ptr) {
36 Core::general_register_disappearing_link(
37 reinterpret_cast<void **>(p_ptr), Core::base(this)
38 );
39 }
41 /** @brief Cancels the registration of a pointer, so it will not be
42 * cleared when this object becomes inacessible.
43 */
44 template <typename T>
45 void cancelClearOnceInaccessible(T **p_ptr) {
46 Core::unregister_disappearing_link(
47 reinterpret_cast<void **>(p_ptr)
48 );
49 }
51 void *operator new(std::size_t size,
52 ScanPolicy scan=default_scan,
53 CollectionPolicy collect=default_collect)
54 throw (std::bad_alloc)
55 {
56 return ::operator new(size, scan, collect);
57 }
59 void *operator new[](std::size_t size,
60 ScanPolicy scan=default_scan,
61 CollectionPolicy collect=default_collect)
62 throw (std::bad_alloc)
63 {
64 return ::operator new[](size, scan, collect);
65 }
67 void operator delete(void *p) { return ::operator delete(p, GC); }
68 };
70 }
72 }
74 #endif
75 /*
76 Local Variables:
77 mode:c++
78 c-file-style:"stroustrup"
79 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
80 indent-tabs-mode:nil
81 fill-column:99
82 End:
83 */
84 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :