1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_TOOLS_REF_HXX
#define INCLUDED_TOOLS_REF_HXX
#include <sal/config.h>
#include <cassert>
#include <tools/toolsdllapi.h>
#include <utility>
/**
This implements similar functionality to boost::intrusive_ptr
*/
namespace tools {
/** T must be a class that extends SvRefBase */
template<typename T> class SAL_DLLPUBLIC_RTTI SvRef final {
public:
SvRef(): pObj(nullptr) {}
SvRef(SvRef&& rObj)
{
pObj = rObj.pObj;
rObj.pObj = nullptr;
}
SvRef(SvRef const & rObj): pObj(rObj.pObj)
{
if (pObj != nullptr) pObj->AddNextRef();
}
SvRef(T * pObjP): pObj(pObjP)
{
if (pObj != nullptr) pObj->AddFirstRef();
}
~SvRef()
{
if (pObj != nullptr) pObj->ReleaseRef();
}
void clear()
{
if (pObj != nullptr) {
T * pRefObj = pObj;
pObj = nullptr;
pRefObj->ReleaseRef();
}
}
SvRef & operator =(SvRef const & rObj)
{
if (rObj.pObj != nullptr) {
rObj.pObj->AddNextRef();
}
T * pRefObj = pObj;
pObj = rObj.pObj;
if (pRefObj != nullptr) {
pRefObj->ReleaseRef();
}
return *this;
}
SvRef & operator =(SvRef && rObj)
{
if (pObj != nullptr) {
pObj->ReleaseRef();
}
pObj = rObj.pObj;
rObj.pObj = nullptr;
return *this;
}
bool is() const { return pObj != nullptr; }
explicit operator bool() const { return is(); }
T * get() const { return pObj; }
T * operator ->() const { assert(pObj != nullptr); return pObj; }
T & operator *() const { assert(pObj != nullptr); return *pObj; }
bool operator ==(const SvRef<T> &rhs) const { return pObj == rhs.pObj; }
bool operator !=(const SvRef<T> &rhs) const { return !(*this == rhs); }
private:
T * pObj;
};
/**
* This implements similar functionality to std::make_shared.
*/
template<typename T, typename... Args>
SvRef<T> make_ref(Args&& ... args)
{
return SvRef<T>(new T(std::forward<Args>(args)...));
}
}
/** Classes that want to be referenced-counted via SvRef<T>, should extend this base class */
class TOOLS_DLLPUBLIC SvRefBase
{
// work around a clang 3.5 optimization bug: if the bNoDelete is *first*
// it mis-compiles "if (--nRefCount == 0)" and never deletes any object
unsigned int nRefCount : 31;
// the only reason this is not bool is because MSVC cannot handle mixed type bitfields
unsigned int bNoDelete : 1;
protected:
virtual ~SvRefBase() COVERITY_NOEXCEPT_FALSE;
public:
SvRefBase() : nRefCount(0), bNoDelete(1) {}
SvRefBase(const SvRefBase &) : nRefCount(0), bNoDelete(1) {}
SvRefBase & operator = ( const SvRefBase & )
{ return *this; }
void RestoreNoDelete()
{ bNoDelete = 1; }
void AddNextRef()
{
assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
++nRefCount;
}
void AddFirstRef()
{
assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
if( bNoDelete )
bNoDelete = 0;
++nRefCount;
}
void ReleaseRef()
{
assert( nRefCount >= 1);
if( --nRefCount == 0 && !bNoDelete)
{
// I'm not sure about the original purpose of this line, but right now
// it serves the purpose that anything that attempts to do an AddRef()
// after an object is deleted will trip an assert.
nRefCount = 1 << 30;
delete this;
}
}
unsigned int GetRefCount() const
{ return nRefCount; }
};
template<typename T>
class SvCompatWeakBase;
/** SvCompatWeakHdl acts as a intermediary between SvCompatWeakRef<T> and T.
*/
template<typename T>
class SvCompatWeakHdl : public SvRefBase
{
friend class SvCompatWeakBase<T>;
T* _pObj;
SvCompatWeakHdl( T* pObj ) : _pObj( pObj ) {}
public:
void ResetWeakBase( ) { _pObj = nullptr; }
T* GetObj() { return _pObj; }
};
/** We only have one place that extends this, in include/sfx2/frame.hxx, class SfxFrame.
Its function is to notify the SvCompatWeakHdl when an SfxFrame object is deleted.
*/
template<typename T>
class SvCompatWeakBase
{
tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
public:
/** Does not use initializer due to compiler warnings,
because the lifetime of the _xHdl object can exceed the lifetime of this class.
*/
SvCompatWeakBase( T* pObj ) { _xHdl = new SvCompatWeakHdl<T>( pObj ); }
~SvCompatWeakBase() { _xHdl->ResetWeakBase(); }
SvCompatWeakHdl<T>* GetHdl() { return _xHdl.get(); }
};
/** We only have one weak reference in LO, in include/sfx2/frame.hxx, class SfxFrameWeak.
*/
template<typename T>
class SAL_WARN_UNUSED SvCompatWeakRef
{
tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
public:
SvCompatWeakRef( ) {}
SvCompatWeakRef( T* pObj )
{ if( pObj ) _xHdl = pObj->GetHdl(); }
#if defined(__COVERITY__)
~SvCompatWeakRef() COVERITY_NOEXCEPT_FALSE {}
#endif
SvCompatWeakRef& operator = ( T * pObj )
{ _xHdl = pObj ? pObj->GetHdl() : nullptr; return *this; }
bool is() const
{ return _xHdl.is() && _xHdl->GetObj(); }
explicit operator bool() const { return is(); }
T* operator -> () const
{ return _xHdl.is() ? _xHdl->GetObj() : nullptr; }
operator T* () const
{ return _xHdl.is() ? _xHdl->GetObj() : nullptr; }
};
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|