kstaticdeleter.h
/*
* This file is part of the KDE Libraries
* Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
* 2001 KDE Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef _KSTATIC_DELETER_H_
#define _KSTATIC_DELETER_H_
#include <kglobal.h>
class KDECORE_EXPORT KStaticDeleterBase {
public:
virtual ~KStaticDeleterBase() { }
virtual void destructObject();
};
template<class type> class KStaticDeleter : public KStaticDeleterBase {
public:
KStaticDeleter() { deleteit = 0; globalReference = 0; array = false; }
KDE_DEPRECATED type *setObject( type *obj, bool isArray = false) {
deleteit = obj;
globalReference = 0;
array = isArray;
if (obj)
KGlobal::registerStaticDeleter(this);
else
KGlobal::unregisterStaticDeleter(this);
return obj;
}
type *setObject( type* & globalRef, type *obj, bool isArray = false) {
globalReference = &globalRef;
deleteit = obj;
array = isArray;
if (obj)
KGlobal::registerStaticDeleter(this);
else
KGlobal::unregisterStaticDeleter(this);
globalRef = obj;
return obj;
}
virtual void destructObject() {
if (globalReference)
*globalReference = 0;
if (array)
delete [] deleteit;
else
delete deleteit;
deleteit = 0;
}
virtual ~KStaticDeleter() {
KGlobal::unregisterStaticDeleter(this);
destructObject();
}
private:
type *deleteit;
type **globalReference;
bool array;
};
#endif
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
KStaticDeleter::destructObject
virtual void destructObject()
Destructs the object.
Definition: kstaticdeleter.h:120
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to KGlobal.
Definition: kstaticdeleter.h:85
KStaticDeleter::setObject
type * setObject(type *&globalRef, type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to KGlobal.
Definition: kstaticdeleter.h:104
KGlobal::registerStaticDeleter
static void registerStaticDeleter(KStaticDeleterBase *d)
Registers a static deleter.
Definition: kglobal.cpp:169
KGlobal::unregisterStaticDeleter
static void unregisterStaticDeleter(KStaticDeleterBase *d)
Unregisters a static deleter.
Definition: kglobal.cpp:178
KStaticDeleterBase
Static deleters are used to manage static resources.
Definition: kstaticdeleter.h:39

KDE Home | KDE Accessibility Home | Description of Access Keys