Warning: That file was not part of the compilation database. It may have many parsing errors.

1/*
2 * This file is part of the KDE Libraries
3 * Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
4 * 2001 KDE Team
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifndef _KSTATIC_DELETER_H_
24#define _KSTATIC_DELETER_H_
25
26#include <kde3support_export.h>
27
28class K3StaticDeleterBase;
29
30namespace K3StaticDeleterHelpers
31{
32 /**
33 * Registers a static deleter.
34 * @param d the static deleter to register
35 * @see K3StaticDeleterBase
36 * @see K3StaticDeleter
37 */
38 KDE3SUPPORT_EXPORT void registerStaticDeleter(K3StaticDeleterBase *d);
39
40 /**
41 * Unregisters a static deleter.
42 * @param d the static deleter to unregister
43 * @see K3StaticDeleterBase
44 * @see K3StaticDeleter
45 */
46 KDE3SUPPORT_EXPORT void unregisterStaticDeleter(K3StaticDeleterBase *d);
47
48 /**
49 * Calls K3StaticDeleterBase::destructObject() on all
50 * registered static deleters and unregisters them all.
51 * @see K3StaticDeleterBase
52 * @see K3StaticDeleter
53 */
54 KDE3SUPPORT_EXPORT void deleteStaticDeleters();
55} // namespace K3StaticDeleterHelpers
56
57/**
58 * @short Base class for K3StaticDeleter
59 *
60 * Don't use this class directly; this class is used as a base class for
61 * the K3StaticDeleter template to allow polymorphism.
62 *
63 * @see K3StaticDeleter
64 * @see K3StaticDeleterHelpers::registerStaticDeleter()
65 * @see K3StaticDeleterHelpers::unregisterStaticDeleter()
66 * @see K3StaticDeleterHelpers::deleteStaticDeleters()
67 */
68class KDE3SUPPORT_EXPORT K3StaticDeleterBase {
69public:
70 virtual ~K3StaticDeleterBase();
71 /**
72 * Should destruct the resources managed by this K3StaticDeleterBase.
73 * Usually you also want to call it in your destructor.
74 * @see K3StaticDeleterHelpers::deleteStaticDeleters()
75 */
76 virtual void destructObject();
77};
78
79/**
80 * @short Automatically deletes an object on termination
81 *
82 * Little helper class to clean up static objects that are held as a pointer.
83 *
84 * Static deleters are used to manage static resources. They can register
85 * themselves with K3StaticDeleterHelpers. K3StaticDeleterHelpers will call destructObject() when
86 * K3StaticDeleterHelpers::deleteStaticDeleters() is called or when the process
87 * finishes.
88 *
89 * When the library is unloaded, or the app is terminated, all static deleters
90 * will be destroyed, which in turn destroys those static objects properly.
91 * There are some rules that you should accept in the K3StaticDeleter managed
92 * class:
93 * @li Don't rely on the global reference variable in the destructor of the
94 * object, it will be '0' at destruction time.
95 * @li Don't rely on other K3StaticDeleter managed objects in the destructor
96 * of the object, because they may be destroyed before your destructor get called.
97 * This one can be tricky, because you might not know that you are actually using a
98 * K3StaticDeleter managed class. So try to keep your destructor simple.
99 *
100 * A typical use is
101 * \code
102 * static K3StaticDeleter<MyClass> sd;
103 *
104 * MyClass &MyClass::self() {
105 * if (!_self) { sd.setObject(_self, new MyClass()); }
106 * return *_self;
107 * }
108 * \endcode
109 *
110 * @warning Don't delete an object which is managed by %K3StaticDeleter without
111 * calling setObject() with a null pointer.
112 *
113 * @deprecated Use \ref K_GLOBAL_STATIC instead of %K3StaticDeleter.
114 */
115template<class type> class KDE_DEPRECATED K3StaticDeleter : public K3StaticDeleterBase {
116public:
117 /**
118 * Constructor. Initializes the K3StaticDeleter. Note that the static
119 * deleter is not registered by the constructor.
120 */
121 K3StaticDeleter() { deleteit = 0; globalReference = 0; array = false; }
122
123 /**
124 * Sets the object to delete and registers that object to
125 * K3StaticDeleterHelpers. If the given object is 0, the formerly registered
126 * object is unregistered.
127 * @param obj the object to delete
128 * @param isArray tells the destructor to delete an array instead of an object
129 * @deprecated See the other setObject variant.
130 **/
131 KDE_DEPRECATED type *setObject( type *obj, bool isArray = false) {
132 deleteit = obj;
133 globalReference = 0;
134 array = isArray;
135 if (obj)
136 K3StaticDeleterHelpers::registerStaticDeleter(this);
137 else
138 K3StaticDeleterHelpers::unregisterStaticDeleter(this);
139 return obj;
140 }
141
142 /**
143 * Sets the object to delete and registers the object to be
144 * deleted to K3StaticDeleterHelpers. If the given object is 0, the previously
145 * registered object is unregistered.
146 * @param globalRef the static pointer where this object is stored.
147 * This pointer will be reset to 0 after deletion of the object.
148 * @param obj the object to delete
149 * @param isArray tells the destructor to delete an array instead of an object
150 * @return the object to delete, @p obj
151 **/
152 type *setObject( type* & globalRef, type *obj, bool isArray = false) {
153 globalReference = &globalRef;
154 deleteit = obj;
155 array = isArray;
156 if (obj)
157 K3StaticDeleterHelpers::registerStaticDeleter(this);
158 else
159 K3StaticDeleterHelpers::unregisterStaticDeleter(this);
160 globalRef = obj;
161 return obj;
162 }
163
164 /**
165 * Destructs the registered object. This has the same effect as deleting
166 * the K3StaticDeleter.
167 */
168 virtual void destructObject() {
169 if (globalReference)
170 *globalReference = 0;
171 if (array)
172 delete [] deleteit;
173 else
174 delete deleteit;
175 deleteit = 0;
176 }
177
178 /**
179 * Destructor. Unregisters the static deleter and destroys the registered
180 * object by calling destructObject().
181 */
182 virtual ~K3StaticDeleter() {
183 K3StaticDeleterHelpers::unregisterStaticDeleter(this);
184 destructObject();
185 }
186private:
187 type *deleteit;
188 type **globalReference;
189 bool array;
190};
191
192#endif
193

Warning: That file was not part of the compilation database. It may have many parsing errors.