1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_TOOLS_REF_HXX
20 #define INCLUDED_TOOLS_REF_HXX
22 #include <sal/config.h>
24 #include <tools/toolsdllapi.h>
28 This implements similar functionality to boost::intrusive_ptr
33 /** T must be a class that extends SvRefBase */
34 template<typename T
> class SAL_DLLPUBLIC_RTTI SvRef final
{
36 SvRef(): pObj(nullptr) {}
38 SvRef(SvRef
&& rObj
) noexcept
44 SvRef(SvRef
const & rObj
): pObj(rObj
.pObj
)
46 if (pObj
!= nullptr) pObj
->AddNextRef();
49 SvRef(T
* pObjP
): pObj(pObjP
)
51 if (pObj
!= nullptr) pObj
->AddFirstRef();
56 if (pObj
!= nullptr) pObj
->ReleaseRef();
61 if (pObj
!= nullptr) {
64 pRefObj
->ReleaseRef();
68 SvRef
& operator =(SvRef
const & rObj
)
70 if (rObj
.pObj
!= nullptr) {
71 rObj
.pObj
->AddNextRef();
75 if (pRefObj
!= nullptr) {
76 pRefObj
->ReleaseRef();
81 SvRef
& operator =(SvRef
&& rObj
)
83 if (pObj
!= nullptr) {
91 bool is() const { return pObj
!= nullptr; }
93 explicit operator bool() const { return is(); }
95 T
* get() const { return pObj
; }
97 T
* operator ->() const { assert(pObj
!= nullptr); return pObj
; }
99 T
& operator *() const { assert(pObj
!= nullptr); return *pObj
; }
101 bool operator ==(const SvRef
<T
> &rhs
) const { return pObj
== rhs
.pObj
; }
102 bool operator !=(const SvRef
<T
> &rhs
) const { return !(*this == rhs
); }
109 * This implements similar functionality to std::make_shared.
111 template<typename T
, typename
... Args
>
112 SvRef
<T
> make_ref(Args
&& ... args
)
114 return SvRef
<T
>(new T(std::forward
<Args
>(args
)...));
119 /** Classes that want to be referenced-counted via SvRef<T>, should extend this base class */
120 class TOOLS_DLLPUBLIC SvRefBase
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;
129 virtual ~SvRefBase() COVERITY_NOEXCEPT_FALSE
;
132 SvRefBase() : nRefCount(0), bNoDelete(1) {}
133 SvRefBase(const SvRefBase
&) : nRefCount(0), bNoDelete(1) {}
135 SvRefBase
& operator=(const SvRefBase
&) { return *this; }
137 void RestoreNoDelete()
142 assert( nRefCount
< (1 << 30) && "Do not add refs to dead objects" );
148 assert( nRefCount
< (1 << 30) && "Do not add refs to dead objects" );
156 assert( nRefCount
>= 1);
157 if( --nRefCount
== 0 && !bNoDelete
)
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.
167 unsigned int GetRefCount() const
168 { return nRefCount
; }
172 class SvCompatWeakBase
;
174 /** SvCompatWeakHdl acts as an intermediary between SvCompatWeakRef<T> and T.
177 class SvCompatWeakHdl final
: public SvRefBase
179 friend class SvCompatWeakBase
<T
>;
182 SvCompatWeakHdl( T
* pObj
) : _pObj( pObj
) {}
185 void ResetWeakBase( ) { _pObj
= nullptr; }
186 T
* GetObj() { return _pObj
; }
189 /** We only have one place that extends this, in include/sfx2/frame.hxx, class SfxFrame.
190 Its function is to notify the SvCompatWeakHdl when an SfxFrame object is deleted.
193 class SvCompatWeakBase
195 tools::SvRef
< SvCompatWeakHdl
<T
> > _xHdl
;
198 /** Does not use initializer due to compiler warnings,
199 because the lifetime of the _xHdl object can exceed the lifetime of this class.
201 SvCompatWeakBase( T
* pObj
) { _xHdl
= new SvCompatWeakHdl
<T
>( pObj
); }
203 ~SvCompatWeakBase() { _xHdl
->ResetWeakBase(); }
205 SvCompatWeakHdl
<T
>* GetHdl() { return _xHdl
.get(); }
208 /** We only have one weak reference in LO, in include/sfx2/frame.hxx, class SfxFrameWeak.
211 class SAL_WARN_UNUSED SvCompatWeakRef
213 tools::SvRef
< SvCompatWeakHdl
<T
> > _xHdl
;
215 SvCompatWeakRef( ) {}
216 SvCompatWeakRef( T
* pObj
)
217 { if( pObj
) _xHdl
= pObj
->GetHdl(); }
218 #if defined(__COVERITY__)
219 ~SvCompatWeakRef() COVERITY_NOEXCEPT_FALSE
{}
221 SvCompatWeakRef
& operator = ( T
* pObj
)
222 { _xHdl
= pObj
? pObj
->GetHdl() : nullptr; return *this; }
224 { return _xHdl
.is() && _xHdl
->GetObj(); }
225 explicit operator bool() const { return is(); }
226 T
* operator -> () const
227 { return _xHdl
.is() ? _xHdl
->GetObj() : nullptr; }
229 { return _xHdl
.is() ? _xHdl
->GetObj() : nullptr; }
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */