| 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 |
|
| 27 |
#ifndef BASE_HANDLE_H |
| 28 |
#define BASE_HANDLE_H |
| 29 |
|
| 30 |
#ifndef FC_GLOBAL_H |
| 31 |
# include <FCGlobal.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
class QAtomicInt; |
| 36 |
|
| 37 |
namespace Base |
| 38 |
{ |
| 39 |
|
| 40 |
/** Reference class |
| 41 |
* Implementation of the reference counting pattern. |
| 42 |
* Only able to instantiate with a class inheriting |
| 43 |
* Base::Handled. |
| 44 |
*/ |
| 45 |
template<class T> |
| 46 |
class Reference |
| 47 |
{ |
| 48 |
public: |
| 49 |
//************************************************************************** |
| 50 |
// construction & destruction |
| 51 |
|
| 52 |
/** Pointer and default constructor */ |
| 53 |
Reference() |
| 54 |
: _toHandle(nullptr) |
| 55 |
{} |
| 56 |
|
| 57 |
Reference(T* pointer) |
| 58 |
: _toHandle(pointer) |
| 59 |
{ |
| 60 |
if (_toHandle) { |
| 61 |
_toHandle->ref(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** Copy constructor */ |
| 66 |
Reference(const Reference<T>& ref) |
| 67 |
: _toHandle(ref._toHandle) |
| 68 |
{ |
| 69 |
if (_toHandle) { |
| 70 |
_toHandle->ref(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** destructor |
| 75 |
* Release the reference counter which causes, |
| 76 |
* in case of the last one, the referenced object to |
| 77 |
* be destructed! |
| 78 |
*/ |
| 79 |
~Reference() |
| 80 |
{ |
| 81 |
if (_toHandle) { |
| 82 |
_toHandle->unref(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
//************************************************************************** |
| 87 |
// operator implementation |
| 88 |
|
| 89 |
/** Assign operator from a pointer */ |
| 90 |
Reference<T>& operator=(T* pointer) |
| 91 |
{ |
| 92 |
// check if we want to reassign the same object |
| 93 |
if (_toHandle == pointer) { |
| 94 |
return *this; |
| 95 |
} |
| 96 |
if (_toHandle) { |
| 97 |
_toHandle->unref(); |
| 98 |
} |
| 99 |
_toHandle = pointer; |
| 100 |
if (_toHandle) { |
| 101 |
_toHandle->ref(); |
| 102 |
} |
| 103 |
return *this; |
| 104 |
} |
| 105 |
|
| 106 |
/** Assign operator from a handle */ |
| 107 |
Reference<T>& operator=(const Reference<T>& ref) |
| 108 |
{ |
| 109 |
// check if we want to reassign the same object |
| 110 |
if (_toHandle == ref._toHandle) { |
| 111 |
return *this; |
| 112 |
} |
| 113 |
if (_toHandle) { |
| 114 |
_toHandle->unref(); |
| 115 |
} |
| 116 |
_toHandle = ref._toHandle; |
| 117 |
if (_toHandle) { |
| 118 |
_toHandle->ref(); |
| 119 |
} |
| 120 |
return *this; |
| 121 |
} |
| 122 |
|
| 123 |
/** Dereference operator */ |
| 124 |
T& operator*() const |
| 125 |
{ |
| 126 |
return *_toHandle; |
| 127 |
} |
| 128 |
|
| 129 |
/** Dereference operator */ |
| 130 |
T* operator->() const |
| 131 |
{ |
| 132 |
return _toHandle; |
| 133 |
} |
| 134 |
|
| 135 |
operator T*() const |
| 136 |
{ |
| 137 |
return _toHandle; |
| 138 |
} |
| 139 |
|
| 140 |
//************************************************************************** |
| 141 |
// checking on the state |
| 142 |
|
| 143 |
/// Test if it handles something |
| 144 |
bool isValid() const |
| 145 |
{ |
| 146 |
return _toHandle != nullptr; |
| 147 |
} |
| 148 |
|
| 149 |
/// Test if it does not handle anything |
| 150 |
bool isNull() const |
| 151 |
{ |
| 152 |
return _toHandle == nullptr; |
| 153 |
} |
| 154 |
|
| 155 |
/// Get number of references on the object, including this one |
| 156 |
int getRefCount() const |
| 157 |
{ |
| 158 |
if (_toHandle) { |
| 159 |
return _toHandle->getRefCount(); |
| 160 |
} |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
private: |
| 165 |
T* _toHandle; /** the pointer to the handled object */ |
| 166 |
}; |
| 167 |
|
| 168 |
/** Handled class |
| 169 |
* Implementation of the reference counting pattern. |
| 170 |
*/ |
| 171 |
class BaseExport Handled |
| 172 |
{ |
| 173 |
public: |
| 174 |
Handled(); |
| 175 |
virtual ~Handled(); |
| 176 |
|
| 177 |
void ref() const; |
| 178 |
void unref() const; |
| 179 |
int unrefNoDelete() const; |
| 180 |
|
| 181 |
int getRefCount() const; |
| 182 |
Handled& operator=(const Handled&); |
| 183 |
|
| 184 |
Handled(const Handled&) = delete; |
| 185 |
Handled(Handled&&) = delete; |
| 186 |
Handled& operator=(Handled&&) = delete; |
| 187 |
|
| 188 |
private: |
| 189 |
QAtomicInt* _lRefCount; |
| 190 |
}; |
| 191 |
|
| 192 |
} // namespace Base |
| 193 |
|
| 194 |
#endif // BASE_HANDLE_H |