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 .
22 #include "tools/toolsdllapi.h"
25 #define PRV_SV_IMPL_REF_COUNTERS( ClassName, Ref, AddRef, AddNextRef, ReleaseRef, Init, pRefbase ) \
26 inline ClassName##Ref::ClassName##Ref( const ClassName##Ref & rObj ) \
27 { pObj = rObj.pObj; if( pObj ) { Init pRefbase->AddNextRef; } } \
28 inline ClassName##Ref::ClassName##Ref( ClassName * pObjP ) \
29 { pObj = pObjP; if( pObj ) { Init pRefbase->AddRef; } } \
30 inline void ClassName##Ref::Clear() \
34 ClassName* const pRefObj = pRefbase; \
36 pRefObj->ReleaseRef; \
39 inline ClassName##Ref::~ClassName##Ref() \
40 { if( pObj ) { pRefbase->ReleaseRef; } } \
41 inline ClassName##Ref & ClassName##Ref:: \
42 operator = ( const ClassName##Ref & rObj ) \
44 if( rObj.pObj ) rObj.pRefbase->AddNextRef; \
45 ClassName* const pRefObj = pRefbase; \
47 Init if( pRefObj ) { pRefObj->ReleaseRef; } \
50 inline ClassName##Ref & ClassName##Ref::operator = ( ClassName * pObjP ) \
51 { return *this = ClassName##Ref( pObjP ); }
53 #define PRV_SV_DECL_REF_LOCK(ClassName, Ref) \
57 inline ClassName##Ref() { pObj = 0; } \
58 inline ClassName##Ref( const ClassName##Ref & rObj ); \
59 inline ClassName##Ref( ClassName * pObjP ); \
60 inline void Clear(); \
61 inline ~ClassName##Ref(); \
62 inline ClassName##Ref & operator = ( const ClassName##Ref & rObj ); \
63 inline ClassName##Ref & operator = ( ClassName * pObj ); \
64 inline sal_Bool Is() const { return pObj != NULL; } \
65 inline ClassName * operator & () const { return pObj; } \
66 inline ClassName * operator -> () const { return pObj; } \
67 inline ClassName & operator * () const { return *pObj; } \
68 inline operator ClassName * () const { return pObj; }
70 #define PRV_SV_DECL_REF( ClassName ) \
71 PRV_SV_DECL_REF_LOCK( ClassName, Ref )
73 #define SV_DECL_REF( ClassName ) \
75 class ClassName##Ref \
77 PRV_SV_DECL_REF( ClassName ) \
80 #define SV_DECL_LOCK( ClassName ) \
82 class ClassName##Lock \
84 PRV_SV_DECL_REF_LOCK( ClassName, Lock ) \
87 #define SV_IMPL_REF( ClassName ) \
88 PRV_SV_IMPL_REF_COUNTERS( ClassName, Ref, AddRef(), AddNextRef(),\
89 ReleaseReference(), EMPTYARG, pObj )
91 #define SV_IMPL_LOCK( ClassName ) \
92 PRV_SV_IMPL_REF_COUNTERS( ClassName, Lock, OwnerLock( sal_True ), \
93 OwnerLock( sal_True ), OwnerLock( sal_False ), \
96 #define SV_DECL_IMPL_REF(ClassName) \
97 SV_DECL_REF(ClassName) \
98 SV_IMPL_REF(ClassName)
101 class SvRefMemberList
: private std::vector
<T
>
104 typedef typename
std::vector
<T
> base_t
;
110 using base_t::operator[];
113 using typename
base_t::iterator
;
114 using typename
base_t::const_iterator
;
115 using base_t::rbegin
;
117 using typename
base_t::reverse_iterator
;
120 inline ~SvRefMemberList() { clear(); }
123 for( typename
base_t::const_iterator it
= base_t::begin(); it
!= base_t::end(); ++it
)
127 p
->ReleaseReference();
132 inline void push_back( T p
)
134 base_t::push_back( p
);
138 inline void insert(const SvRefMemberList
& rOther
)
140 for( typename
base_t::const_iterator it
= rOther
.begin(); it
!= rOther
.end(); ++it
)
148 T p
= base_t::back();
151 p
->ReleaseReference();
156 #define SV_NO_DELETE_REFCOUNT 0x80000000
158 class TOOLS_DLLPUBLIC SvRefBase
160 sal_uIntPtr nRefCount
;
163 virtual ~SvRefBase();
164 virtual void QueryDelete();
167 SvRefBase() { nRefCount
= SV_NO_DELETE_REFCOUNT
; }
168 SvRefBase( const SvRefBase
& /* rObj */ )
169 { nRefCount
= SV_NO_DELETE_REFCOUNT
; }
170 SvRefBase
& operator = ( const SvRefBase
& ) { return *this; }
172 void RestoreNoDelete()
174 if( nRefCount
< SV_NO_DELETE_REFCOUNT
)
175 nRefCount
+= SV_NO_DELETE_REFCOUNT
;
177 sal_uIntPtr
AddMulRef( sal_uIntPtr n
) { return nRefCount
+= n
; }
178 sal_uIntPtr
AddNextRef() { return ++nRefCount
; }
181 if( nRefCount
>= SV_NO_DELETE_REFCOUNT
)
182 nRefCount
-= SV_NO_DELETE_REFCOUNT
;
185 void ReleaseReference()
190 sal_uIntPtr
ReleaseRef()
192 sal_uIntPtr n
= --nRefCount
;
197 sal_uIntPtr
GetRefCount() const { return nRefCount
; }
204 SV_DECL_IMPL_REF(SvRefBase
)
206 class SvCompatWeakHdl
: public SvRefBase
208 friend class SvCompatWeakBase
;
210 SvCompatWeakHdl( void* pObj
) : _pObj( pObj
) {}
213 void ResetWeakBase( ) { _pObj
= 0; }
214 void* GetObj() { return _pObj
; }
217 SV_DECL_IMPL_REF( SvCompatWeakHdl
)
219 class SvCompatWeakBase
221 SvCompatWeakHdlRef _xHdl
;
224 SvCompatWeakHdl
* GetHdl() { return _xHdl
; }
226 // does not use Initalizer due to compiler warnings
227 SvCompatWeakBase( void* pObj
) { _xHdl
= new SvCompatWeakHdl( pObj
); }
228 ~SvCompatWeakBase() { _xHdl
->ResetWeakBase(); }
231 #define SV_DECL_COMPAT_WEAK( ClassName ) \
232 class ClassName##Weak \
234 SvCompatWeakHdlRef _xHdl; \
236 inline ClassName##Weak( ) {} \
237 inline ClassName##Weak( ClassName* pObj ) { \
238 if( pObj ) _xHdl = pObj->GetHdl(); } \
239 inline void Clear() { _xHdl.Clear(); } \
240 inline ClassName##Weak& operator = ( ClassName * pObj ) { \
241 _xHdl = pObj ? pObj->GetHdl() : 0; return *this; } \
242 inline sal_Bool Is() const { \
243 return _xHdl.Is() && _xHdl->GetObj(); } \
244 inline ClassName * operator & () const { \
245 return (ClassName*) ( _xHdl.Is() ? _xHdl->GetObj() : 0 ); } \
246 inline ClassName * operator -> () const { \
247 return (ClassName*) ( _xHdl.Is() ? _xHdl->GetObj() : 0 ); } \
248 inline ClassName & operator * () const { \
249 return *(ClassName*) _xHdl->GetObj(); } \
250 inline operator ClassName * () const { \
251 return (ClassName*) (_xHdl.Is() ? _xHdl->GetObj() : 0 ); } \
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */