kis_shared.h 1.56 KB
Newer Older
Cyrille Berger's avatar
oups  
Cyrille Berger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef _KIS_SHARED_H_
#define _KIS_SHARED_H_

Dmitry Kazakov's avatar
Dmitry Kazakov committed
22
#include <QAtomicInt>
23
#include "kritaglobal_export.h"
Cyrille Berger's avatar
Cyrille Berger committed
24

25
class KRITAGLOBAL_EXPORT KisShared
Boudewijn Rempt's avatar
Boudewijn Rempt committed
26
{
27 28 29
private:
    KisShared(const KisShared& );
    KisShared& operator=(const KisShared& );
Boudewijn Rempt's avatar
Boudewijn Rempt committed
30 31 32 33
protected:
    KisShared();
    ~KisShared();
public:
Cyrille Berger's avatar
Cyrille Berger committed
34 35 36 37 38 39 40
    int refCount() {
        return _ref;
    }
    bool ref() {
        return _ref.ref();
    }
    bool deref() {
41
        Q_ASSERT(_ref > 0);
Cyrille Berger's avatar
Cyrille Berger committed
42 43
        return _ref.deref();
    }
Dmitry Kazakov's avatar
Dmitry Kazakov committed
44

Frederik Gladhorn's avatar
Frederik Gladhorn committed
45
    // see note in kis_shared.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
46
    QAtomicInt* sharedWeakReference() {
47 48 49 50 51
        if(!_sharedWeakReference) {
            _sharedWeakReference = new QAtomicInt();
            _sharedWeakReference->ref();
        }

Dmitry Kazakov's avatar
Dmitry Kazakov committed
52 53 54
        return _sharedWeakReference;
    }

Cyrille Berger's avatar
Cyrille Berger committed
55 56
private:
    QAtomicInt _ref;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
57
    QAtomicInt *_sharedWeakReference;
Cyrille Berger's avatar
oups  
Cyrille Berger committed
58 59 60
};

#endif