build fix
[LibreOffice.git] / include / tools / ref.hxx
blobc251a7839f7a7f9e6a187288fe6eef65842889e6
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)
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 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); }
102 private:
103 T * pObj;
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;
126 protected:
127 virtual ~SvRefBase();
129 public:
130 SvRefBase() : nRefCount(0), bNoDelete(1) {}
132 SvRefBase(const SvRefBase &) : nRefCount(0), bNoDelete(1) {}
134 SvRefBase & operator = ( const SvRefBase & )
135 { 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 a intermediary between SvCompatWeakRef<T> and T.
176 template<typename T>
177 class SvCompatWeakHdl : 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 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; }
228 #endif
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */