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>
26 #include <tools/toolsdllapi.h>
30 This implements similar functionality to boost::intrusive_ptr
35 /** T must be a class that extends SvRefBase */
36 template<typename T
> class SAL_DLLPUBLIC_RTTI SvRef
{
40 SvRef(SvRef
const & rObj
): pObj(rObj
.pObj
)
42 if (pObj
!= 0) pObj
->AddNextRef();
45 SvRef(T
* pObjP
): pObj(pObjP
)
47 if (pObj
!= 0) pObj
->AddFirstRef();
52 if (pObj
!= 0) pObj
->ReleaseRef();
60 pRefObj
->ReleaseRef();
64 SvRef
& operator =(SvRef
const & rObj
)
67 rObj
.pObj
->AddNextRef();
72 pRefObj
->ReleaseRef();
77 bool Is() const { return pObj
!= 0; }
79 T
* get() const { return pObj
; }
81 T
* operator &() const { return pObj
; }
83 T
* operator ->() const { assert(pObj
!= 0); return pObj
; }
85 T
& operator *() const { assert(pObj
!= 0); return *pObj
; }
87 operator T
*() const { return pObj
; }
96 class SvRefMemberList
: private std::vector
<T
>
99 typedef typename
std::vector
<T
> base_t
;
105 using base_t::operator[];
108 using typename
base_t::iterator
;
109 using typename
base_t::const_iterator
;
110 using base_t::rbegin
;
112 using typename
base_t::reverse_iterator
;
115 inline ~SvRefMemberList() { clear(); }
118 for( typename
base_t::const_iterator it
= base_t::begin(); it
!= base_t::end(); ++it
)
127 inline void push_back( T p
)
129 base_t::push_back( p
);
133 inline void insert(const SvRefMemberList
& rOther
)
135 for( typename
base_t::const_iterator it
= rOther
.begin(); it
!= rOther
.end(); ++it
)
143 T p
= base_t::back();
152 /** Classes that want to be referenced-counted via SvRef<T>, should extend this base class */
153 class TOOLS_DLLPUBLIC SvRefBase
155 // the only reason this is not bool is because MSVC cannot handle mixed type bitfields
156 unsigned int bNoDelete
: 1;
157 unsigned int nRefCount
: 31;
160 virtual ~SvRefBase();
163 SvRefBase() : bNoDelete(1), nRefCount(0) {}
165 SvRefBase( const SvRefBase
& /* rObj */ ) : bNoDelete(1), nRefCount(0) {}
167 SvRefBase
& operator = ( const SvRefBase
& )
170 void RestoreNoDelete()
175 assert( nRefCount
< (1 << 30) && "Do not add refs to dead objects" );
181 assert( nRefCount
< (1 << 30) && "Do not add refs to dead objects" );
189 assert( nRefCount
>= 1);
190 if( --nRefCount
== 0 && !bNoDelete
)
192 // I'm not sure about the original purpose of this line, but right now
193 // it serves the purpose that anything that attempts to do an AddRef()
194 // after an object is deleted will trip an assert.
200 unsigned int GetRefCount() const
201 { return nRefCount
; }
205 class SvCompatWeakBase
;
207 /** SvCompatWeakHdl acts as a intermediary between SvCompatWeakRef<T> and T.
210 class SvCompatWeakHdl
: public SvRefBase
212 friend class SvCompatWeakBase
<T
>;
215 SvCompatWeakHdl( T
* pObj
) : _pObj( pObj
) {}
218 void ResetWeakBase( ) { _pObj
= 0; }
219 T
* GetObj() { return _pObj
; }
222 /** We only have one place that extends this, in include/sfx2/frame.hxx, class SfxFrame.
223 Its function is to notify the SvCompatWeakHdl when an SfxFrame object is deleted.
226 class SvCompatWeakBase
228 tools::SvRef
< SvCompatWeakHdl
<T
> > _xHdl
;
231 /** Does not use initializer due to compiler warnings,
232 because the lifetime of the _xHdl object can exceed the lifetime of this class.
234 SvCompatWeakBase( T
* pObj
) { _xHdl
= new SvCompatWeakHdl
<T
>( pObj
); }
236 ~SvCompatWeakBase() { _xHdl
->ResetWeakBase(); }
238 SvCompatWeakHdl
<T
>* GetHdl() { return _xHdl
; }
241 /** We only have one weak reference in LO, in include/sfx2/frame.hxx, class SfxFrameWeak.
244 class SvCompatWeakRef
246 tools::SvRef
< SvCompatWeakHdl
<T
> > _xHdl
;
248 inline SvCompatWeakRef( ) {}
249 inline SvCompatWeakRef( T
* pObj
)
250 { if( pObj
) _xHdl
= pObj
->GetHdl(); }
251 inline SvCompatWeakRef
& operator = ( T
* pObj
)
252 { _xHdl
= pObj
? pObj
->GetHdl() : 0; return *this; }
253 inline bool Is() const
254 { return _xHdl
.Is() && _xHdl
->GetObj(); }
255 inline T
* operator -> () const
256 { return _xHdl
.Is() ? _xHdl
->GetObj() : 0; }
257 inline T
* operator & () const
258 { return _xHdl
.Is() ? _xHdl
->GetObj() : 0; }
259 inline operator T
* () const
260 { return _xHdl
.Is() ? _xHdl
->GetObj() : 0; }
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */