crashtesting: crash seen on exporting forum-it-5909.ods to xlsx
[LibreOffice.git] / include / tools / ref.hxx
blob3f245bf08e1b896f016b9e4fdf80e75c78b83394
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
23 #include <cassert>
24 #include <tools/toolsdllapi.h>
25 #include <utility>
27 /**
28 This implements similar functionality to boost::intrusive_ptr
31 namespace tools {
33 /** T must be a class that extends SvRefBase */
34 template<typename T> class SAL_DLLPUBLIC_RTTI SvRef final {
35 public:
36 SvRef(): pObj(nullptr) {}
38 SvRef(SvRef&& rObj) noexcept
40 pObj = rObj.pObj;
41 rObj.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();
54 ~SvRef()
56 if (pObj != nullptr) pObj->ReleaseRef();
59 void clear()
61 if (pObj != nullptr) {
62 T * pRefObj = pObj;
63 pObj = nullptr;
64 pRefObj->ReleaseRef();
68 SvRef & operator =(SvRef const & rObj)
70 if (rObj.pObj != nullptr) {
71 rObj.pObj->AddNextRef();
73 T * pRefObj = pObj;
74 pObj = rObj.pObj;
75 if (pRefObj != nullptr) {
76 pRefObj->ReleaseRef();
78 return *this;
81 SvRef & operator =(SvRef && rObj)
83 if (pObj != nullptr) {
84 pObj->ReleaseRef();
86 pObj = rObj.pObj;
87 rObj.pObj = nullptr;
88 return *this;
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); }
104 private:
105 T * pObj;
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;
128 protected:
129 virtual ~SvRefBase() COVERITY_NOEXCEPT_FALSE;
131 public:
132 SvRefBase() : nRefCount(0), bNoDelete(1) {}
133 SvRefBase(const SvRefBase &) : nRefCount(0), bNoDelete(1) {}
135 SvRefBase & operator=(const SvRefBase &) { return *this; }
137 void RestoreNoDelete()
138 { bNoDelete = 1; }
140 void AddNextRef()
142 assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
143 ++nRefCount;
146 void AddFirstRef()
148 assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
149 if( bNoDelete )
150 bNoDelete = 0;
151 ++nRefCount;
154 void ReleaseRef()
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.
162 nRefCount = 1 << 30;
163 delete this;
167 unsigned int GetRefCount() const
168 { return nRefCount; }
171 template<typename T>
172 class SvCompatWeakBase;
174 /** SvCompatWeakHdl acts as an intermediary between SvCompatWeakRef<T> and T.
176 template<typename T>
177 class SvCompatWeakHdl final : public SvRefBase
179 friend class SvCompatWeakBase<T>;
180 T* _pObj;
182 SvCompatWeakHdl( T* pObj ) : _pObj( pObj ) {}
184 public:
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.
192 template<typename T>
193 class SvCompatWeakBase
195 tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
197 public:
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.
210 template<typename T>
211 class SAL_WARN_UNUSED SvCompatWeakRef
213 tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
214 public:
215 SvCompatWeakRef( ) {}
216 SvCompatWeakRef( T* pObj )
217 { if( pObj ) _xHdl = pObj->GetHdl(); }
218 #if defined(__COVERITY__)
219 ~SvCompatWeakRef() COVERITY_NOEXCEPT_FALSE {}
220 #endif
221 SvCompatWeakRef& operator = ( T * pObj )
222 { _xHdl = pObj ? pObj->GetHdl() : nullptr; return *this; }
223 bool is() const
224 { return _xHdl.is() && _xHdl->GetObj(); }
225 explicit operator bool() const { return is(); }
226 T* operator -> () const
227 { return _xHdl.is() ? _xHdl->GetObj() : nullptr; }
228 operator T* () const
229 { return _xHdl.is() ? _xHdl->GetObj() : nullptr; }
232 #endif
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */