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) {}
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 T
* get() const { return pObj
; }
95 T
* operator ->() const { assert(pObj
!= nullptr); return pObj
; }
97 T
& operator *() const { assert(pObj
!= nullptr); return *pObj
; }
99 bool operator ==(const SvRef
<T
> &rhs
) const { return pObj
== rhs
.pObj
; }
100 bool operator !=(const SvRef
<T
> &rhs
) const { return !(*this == rhs
); }
107 * This implements similar functionality to std::make_shared.
109 template<typename T
, typename
... Args
>
110 SvRef
<T
> make_ref(Args
&& ... args
)
112 return SvRef
<T
>(new T(std::forward
<Args
>(args
)...));
117 /** Classes that want to be referenced-counted via SvRef<T>, should extend this base class */
118 class TOOLS_DLLPUBLIC SvRefBase
120 // work around a clang 3.5 optimization bug: if the bNoDelete is *first*
121 // it mis-compiles "if (--nRefCount == 0)" and never deletes any object
122 unsigned int nRefCount
: 31;
123 // the only reason this is not bool is because MSVC cannot handle mixed type bitfields
124 unsigned int bNoDelete
: 1;
127 virtual ~SvRefBase();
130 SvRefBase() : nRefCount(0), bNoDelete(1) {}
132 SvRefBase(const SvRefBase
&) : nRefCount(0), bNoDelete(1) {}
134 SvRefBase
& operator = ( const SvRefBase
& )
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 a intermediary between SvCompatWeakRef<T> and T.
177 class SvCompatWeakHdl
: 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 inline SvCompatWeakRef( ) {}
216 inline SvCompatWeakRef( T
* pObj
)
217 { if( pObj
) _xHdl
= pObj
->GetHdl(); }
218 inline SvCompatWeakRef
& operator = ( T
* pObj
)
219 { _xHdl
= pObj
? pObj
->GetHdl() : nullptr; return *this; }
220 inline bool Is() const
221 { return _xHdl
.Is() && _xHdl
->GetObj(); }
222 inline T
* operator -> () const
223 { return _xHdl
.Is() ? _xHdl
->GetObj() : nullptr; }
224 inline operator T
* () const
225 { return _xHdl
.Is() ? _xHdl
->GetObj() : nullptr; }
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */