| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This program is free software; you can redistribute it and/or modify * |
| 9 |
* it under the terms of the GNU Library General Public License (LGPL) * |
| 10 |
* as published by the Free Software Foundation; either version 2 of * |
| 11 |
* the License, or (at your option) any later version. * |
| 12 |
* for detail see the LICENCE text file. * |
| 13 |
* * |
| 14 |
* FreeCAD is distributed in the hope that it will be useful, * |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 17 |
* GNU Library General Public License for more details. * |
| 18 |
* * |
| 19 |
* You should have received a copy of the GNU Library General Public * |
| 20 |
* License along with FreeCAD; if not, write to the Free Software * |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * |
| 22 |
* USA * |
| 23 |
* * |
| 24 |
***************************************************************************/ |
| 25 |
|
| 26 |
#include <cassert> |
| 27 |
#include <iostream> |
| 28 |
|
| 29 |
#include <QAtomicInt> |
| 30 |
|
| 31 |
#include "Handle.h" |
| 32 |
|
| 33 |
|
| 34 |
using namespace Base; |
| 35 |
|
| 36 |
//************************************************************************** |
| 37 |
// Construction/Destruction |
| 38 |
|
| 39 |
Handled::Handled() |
| 40 |
: _lRefCount(new QAtomicInt(0)) |
| 41 |
{} |
| 42 |
|
| 43 |
Handled::~Handled() |
| 44 |
{ |
| 45 |
if (static_cast<int>(*_lRefCount) != 0) { |
| 46 |
std::cerr << "Reference counter of deleted object is not zero!!!!!" << std::endl; |
| 47 |
} |
| 48 |
delete _lRefCount; |
| 49 |
} |
| 50 |
|
| 51 |
void Handled::ref() const |
| 52 |
{ |
| 53 |
_lRefCount->ref(); |
| 54 |
} |
| 55 |
|
| 56 |
void Handled::unref() const |
| 57 |
{ |
| 58 |
assert(*_lRefCount > 0); |
| 59 |
if (!_lRefCount->deref()) { |
| 60 |
delete this; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
int Handled::unrefNoDelete() const |
| 65 |
{ |
| 66 |
int res = _lRefCount->deref(); |
| 67 |
assert(res >= 0); |
| 68 |
return res; |
| 69 |
} |
| 70 |
|
| 71 |
int Handled::getRefCount() const |
| 72 |
{ |
| 73 |
return static_cast<int>(*_lRefCount); |
| 74 |
} |
| 75 |
|
| 76 |
Handled& Handled::operator=(const Handled& /*unused*/) |
| 77 |
{ |
| 78 |
// we must not assign _lRefCount |
| 79 |
return *this; |
| 80 |
} |