Line data Source code
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 :
24 : #include <cassert>
25 :
26 : #include <tools/toolsdllapi.h>
27 : #include <vector>
28 :
29 : /**
30 : This implements similar functionality to boost::intrusive_ptr
31 : */
32 :
33 : namespace tools {
34 :
35 : /** T must be a class that extends SvRefBase */
36 : template<typename T> class SAL_DLLPUBLIC_RTTI SvRef {
37 : public:
38 936348 : SvRef(): pObj(0) {}
39 :
40 247830 : SvRef(SvRef const & rObj): pObj(rObj.pObj)
41 : {
42 247830 : if (pObj != 0) pObj->AddNextRef();
43 247830 : }
44 :
45 1038677 : SvRef(T * pObjP): pObj(pObjP)
46 : {
47 1038677 : if (pObj != 0) pObj->AddFirstRef();
48 1038677 : }
49 :
50 2185160 : ~SvRef()
51 : {
52 2185160 : if (pObj != 0) pObj->ReleaseRef();
53 2185160 : }
54 :
55 10867 : void Clear()
56 : {
57 10867 : if (pObj != 0) {
58 6409 : T * pRefObj = pObj;
59 6409 : pObj = 0;
60 6409 : pRefObj->ReleaseRef();
61 : }
62 10867 : }
63 :
64 534439 : SvRef & operator =(SvRef const & rObj)
65 : {
66 534439 : if (rObj.pObj != 0) {
67 482060 : rObj.pObj->AddNextRef();
68 : }
69 534439 : T * pRefObj = pObj;
70 534439 : pObj = rObj.pObj;
71 534439 : if (pRefObj != 0) {
72 55451 : pRefObj->ReleaseRef();
73 : }
74 534439 : return *this;
75 : }
76 :
77 5168884 : bool Is() const { return pObj != 0; }
78 :
79 8865 : T * get() const { return pObj; }
80 :
81 441139 : T * operator &() const { return pObj; }
82 :
83 22531322 : T * operator ->() const { assert(pObj != 0); return pObj; }
84 :
85 589680 : T & operator *() const { assert(pObj != 0); return *pObj; }
86 :
87 54130257 : operator T *() const { return pObj; }
88 :
89 : protected:
90 : T * pObj;
91 : };
92 :
93 : }
94 :
95 : template<typename T>
96 3789 : class SvRefMemberList : private std::vector<T>
97 : {
98 : private:
99 : typedef typename std::vector<T> base_t;
100 :
101 : public:
102 : using base_t::size;
103 : using base_t::front;
104 : using base_t::back;
105 : using base_t::operator[];
106 : using base_t::begin;
107 : using base_t::end;
108 : using typename base_t::iterator;
109 : using typename base_t::const_iterator;
110 : using base_t::rbegin;
111 : using base_t::rend;
112 : using typename base_t::reverse_iterator;
113 : using base_t::empty;
114 :
115 3789 : inline ~SvRefMemberList() { clear(); }
116 3805 : inline void clear()
117 : {
118 36827 : for( typename base_t::const_iterator it = base_t::begin(); it != base_t::end(); ++it )
119 : {
120 33022 : T p = *it;
121 33022 : if( p )
122 33022 : p->ReleaseRef();
123 : }
124 3805 : base_t::clear();
125 3805 : }
126 :
127 33030 : inline void push_back( T p )
128 : {
129 33030 : base_t::push_back( p );
130 33030 : p->AddFirstRef();
131 33030 : }
132 :
133 : inline void insert(const SvRefMemberList& rOther)
134 : {
135 : for( typename base_t::const_iterator it = rOther.begin(); it != rOther.end(); ++it )
136 : {
137 : push_back(*it);
138 : }
139 : }
140 :
141 8 : inline T pop_back()
142 : {
143 8 : T p = base_t::back();
144 8 : base_t::pop_back();
145 8 : if( p )
146 8 : p->ReleaseRef();
147 8 : return p;
148 : }
149 : };
150 :
151 :
152 : /** Classes that want to be referenced-counted via SvRef<T>, should extend this base class */
153 : class TOOLS_DLLPUBLIC SvRefBase
154 : {
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;
158 :
159 : protected:
160 : virtual ~SvRefBase();
161 :
162 : public:
163 1026621 : SvRefBase() : bNoDelete(1), nRefCount(0) {}
164 :
165 19323 : SvRefBase( const SvRefBase & /* rObj */ ) : bNoDelete(1), nRefCount(0) {}
166 :
167 20547 : SvRefBase & operator = ( const SvRefBase & )
168 20547 : { return *this; }
169 :
170 0 : void RestoreNoDelete()
171 0 : { bNoDelete = 1; }
172 :
173 707771 : void AddNextRef()
174 : {
175 : assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
176 707771 : ++nRefCount;
177 707771 : }
178 :
179 1446555 : void AddFirstRef()
180 : {
181 : assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
182 1446555 : if( bNoDelete )
183 846056 : bNoDelete = 0;
184 1446555 : ++nRefCount;
185 1446555 : }
186 :
187 1953665 : void ReleaseRef()
188 : {
189 : assert( nRefCount >= 1);
190 1953665 : if( --nRefCount == 0 && !bNoDelete)
191 : {
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.
195 646329 : nRefCount = 1 << 30;
196 646329 : delete this;
197 : }
198 1953665 : }
199 :
200 47968 : unsigned int GetRefCount() const
201 47968 : { return nRefCount; }
202 : };
203 :
204 : template<typename T>
205 : class SvCompatWeakBase;
206 :
207 : /** SvCompatWeakHdl acts as a intermediary between SvCompatWeakRef<T> and T.
208 : */
209 : template<typename T>
210 6486 : class SvCompatWeakHdl : public SvRefBase
211 : {
212 : friend class SvCompatWeakBase<T>;
213 : T* _pObj;
214 :
215 3250 : SvCompatWeakHdl( T* pObj ) : _pObj( pObj ) {}
216 :
217 : public:
218 3243 : void ResetWeakBase( ) { _pObj = 0; }
219 3115 : T* GetObj() { return _pObj; }
220 : };
221 :
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.
224 : */
225 : template<typename T>
226 : class SvCompatWeakBase
227 : {
228 : tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
229 :
230 : public:
231 : /** Does not use initializer due to compiler warnings,
232 : because the lifetime of the _xHdl object can exceed the lifetime of this class.
233 : */
234 3250 : SvCompatWeakBase( T* pObj ) { _xHdl = new SvCompatWeakHdl<T>( pObj ); }
235 :
236 3243 : ~SvCompatWeakBase() { _xHdl->ResetWeakBase(); }
237 :
238 6365 : SvCompatWeakHdl<T>* GetHdl() { return _xHdl; }
239 : };
240 :
241 : /** We only have one weak reference in LO, in include/sfx2/frame.hxx, class SfxFrameWeak.
242 : */
243 : template<typename T>
244 18732 : class SvCompatWeakRef
245 : {
246 : tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
247 : public:
248 15636 : inline SvCompatWeakRef( ) {}
249 3115 : inline SvCompatWeakRef( T* pObj )
250 3115 : { if( pObj ) _xHdl = pObj->GetHdl(); }
251 3250 : inline SvCompatWeakRef& operator = ( T * pObj )
252 3250 : { _xHdl = pObj ? pObj->GetHdl() : 0; return *this; }
253 3115 : inline bool Is() const
254 3115 : { return _xHdl.Is() && _xHdl->GetObj(); }
255 0 : inline T* operator -> () const
256 0 : { return _xHdl.Is() ? _xHdl->GetObj() : 0; }
257 : inline T* operator & () const
258 : { return _xHdl.Is() ? _xHdl->GetObj() : 0; }
259 0 : inline operator T* () const
260 0 : { return _xHdl.Is() ? _xHdl->GetObj() : 0; }
261 : };
262 :
263 : #endif
264 :
265 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|