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