2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-11 09:51:50 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2013-10-23 19:15:52 +02:00
|
|
|
#ifndef INCLUDED_TOOLS_REF_HXX
|
|
|
|
#define INCLUDED_TOOLS_REF_HXX
|
2007-04-11 19:16:34 +00:00
|
|
|
|
2014-06-19 09:28:47 +02:00
|
|
|
#include <sal/config.h>
|
|
|
|
#include <cassert>
|
2013-11-09 15:37:43 -06:00
|
|
|
#include <tools/toolsdllapi.h>
|
2016-02-21 17:51:28 +01:00
|
|
|
#include <utility>
|
2007-04-11 19:16:34 +00:00
|
|
|
|
2014-10-03 10:39:28 +02:00
|
|
|
/**
|
|
|
|
This implements similar functionality to boost::intrusive_ptr
|
|
|
|
*/
|
|
|
|
|
2014-04-07 12:31:09 +02:00
|
|
|
namespace tools {
|
|
|
|
|
2014-07-10 15:44:28 +02:00
|
|
|
/** T must be a class that extends SvRefBase */
|
2016-10-11 08:52:41 +02:00
|
|
|
template<typename T> class SAL_DLLPUBLIC_RTTI SvRef final {
|
2014-04-07 12:31:09 +02:00
|
|
|
public:
|
2016-02-21 17:51:28 +01:00
|
|
|
SvRef(): pObj(nullptr) {}
|
|
|
|
|
2019-08-22 15:42:36 +03:00
|
|
|
SvRef(SvRef&& rObj) noexcept
|
2016-02-21 17:51:28 +01:00
|
|
|
{
|
|
|
|
pObj = rObj.pObj;
|
|
|
|
rObj.pObj = nullptr;
|
|
|
|
}
|
2014-04-07 12:31:09 +02:00
|
|
|
|
|
|
|
SvRef(SvRef const & rObj): pObj(rObj.pObj)
|
2014-07-10 12:12:55 +02:00
|
|
|
{
|
2015-12-10 17:33:54 +01:00
|
|
|
if (pObj != nullptr) pObj->AddNextRef();
|
2014-07-10 12:12:55 +02:00
|
|
|
}
|
2014-04-07 12:31:09 +02:00
|
|
|
|
2014-07-10 12:12:55 +02:00
|
|
|
SvRef(T * pObjP): pObj(pObjP)
|
|
|
|
{
|
2015-12-10 17:33:54 +01:00
|
|
|
if (pObj != nullptr) pObj->AddFirstRef();
|
2014-07-10 12:12:55 +02:00
|
|
|
}
|
2014-04-07 12:31:09 +02:00
|
|
|
|
2014-07-10 12:12:55 +02:00
|
|
|
~SvRef()
|
|
|
|
{
|
2015-12-10 17:33:54 +01:00
|
|
|
if (pObj != nullptr) pObj->ReleaseRef();
|
2014-07-10 12:12:55 +02:00
|
|
|
}
|
2014-04-07 12:31:09 +02:00
|
|
|
|
2017-01-25 12:03:58 +02:00
|
|
|
void clear()
|
2014-07-10 12:12:55 +02:00
|
|
|
{
|
2015-12-10 17:33:54 +01:00
|
|
|
if (pObj != nullptr) {
|
2014-04-07 12:31:09 +02:00
|
|
|
T * pRefObj = pObj;
|
2015-12-10 17:33:54 +01:00
|
|
|
pObj = nullptr;
|
2014-07-10 11:03:27 +02:00
|
|
|
pRefObj->ReleaseRef();
|
2014-04-07 12:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 12:12:55 +02:00
|
|
|
SvRef & operator =(SvRef const & rObj)
|
|
|
|
{
|
2016-10-11 16:27:38 +02:00
|
|
|
if (rObj.pObj != nullptr) {
|
2014-04-07 12:31:09 +02:00
|
|
|
rObj.pObj->AddNextRef();
|
|
|
|
}
|
|
|
|
T * pRefObj = pObj;
|
|
|
|
pObj = rObj.pObj;
|
2015-12-10 17:33:54 +01:00
|
|
|
if (pRefObj != nullptr) {
|
2014-07-10 11:03:27 +02:00
|
|
|
pRefObj->ReleaseRef();
|
2014-04-07 12:31:09 +02:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-08-25 15:08:55 +02:00
|
|
|
SvRef & operator =(SvRef && rObj)
|
|
|
|
{
|
|
|
|
if (pObj != nullptr) {
|
|
|
|
pObj->ReleaseRef();
|
|
|
|
}
|
|
|
|
pObj = rObj.pObj;
|
|
|
|
rObj.pObj = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-01-25 12:03:58 +02:00
|
|
|
bool is() const { return pObj != nullptr; }
|
2014-04-07 12:31:09 +02:00
|
|
|
|
2017-01-25 12:03:58 +02:00
|
|
|
explicit operator bool() const { return is(); }
|
2017-01-25 10:42:28 +02:00
|
|
|
|
2015-02-01 00:49:08 -02:00
|
|
|
T * get() const { return pObj; }
|
|
|
|
|
2015-12-10 17:33:54 +01:00
|
|
|
T * operator ->() const { assert(pObj != nullptr); return pObj; }
|
2014-04-07 12:31:09 +02:00
|
|
|
|
2015-12-10 17:33:54 +01:00
|
|
|
T & operator *() const { assert(pObj != nullptr); return *pObj; }
|
2014-04-07 12:31:09 +02:00
|
|
|
|
2016-10-05 22:00:51 +02:00
|
|
|
bool operator ==(const SvRef<T> &rhs) const { return pObj == rhs.pObj; }
|
|
|
|
bool operator !=(const SvRef<T> &rhs) const { return !(*this == rhs); }
|
2014-04-07 12:31:09 +02:00
|
|
|
|
2016-11-16 08:59:52 +02:00
|
|
|
private:
|
2014-04-07 12:31:09 +02:00
|
|
|
T * pObj;
|
2014-04-07 09:26:06 +02:00
|
|
|
};
|
|
|
|
|
2016-02-21 17:51:28 +01:00
|
|
|
/**
|
|
|
|
* This implements similar functionality to std::make_shared.
|
|
|
|
*/
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
SvRef<T> make_ref(Args&& ... args)
|
|
|
|
{
|
|
|
|
return SvRef<T>(new T(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
2014-04-07 12:31:09 +02:00
|
|
|
}
|
2007-04-11 19:16:34 +00:00
|
|
|
|
2014-07-10 15:44:28 +02:00
|
|
|
/** Classes that want to be referenced-counted via SvRef<T>, should extend this base class */
|
2007-04-11 19:16:34 +00:00
|
|
|
class TOOLS_DLLPUBLIC SvRefBase
|
|
|
|
{
|
2015-10-30 15:22:26 +01:00
|
|
|
// work around a clang 3.5 optimization bug: if the bNoDelete is *first*
|
|
|
|
// it mis-compiles "if (--nRefCount == 0)" and never deletes any object
|
|
|
|
unsigned int nRefCount : 31;
|
2017-04-21 16:35:19 +02:00
|
|
|
// the only reason this is not bool is because MSVC cannot handle mixed type bitfields
|
|
|
|
unsigned int bNoDelete : 1;
|
2012-08-13 22:51:30 +02:00
|
|
|
|
2007-04-11 19:16:34 +00:00
|
|
|
protected:
|
2017-02-10 09:24:16 +00:00
|
|
|
virtual ~SvRefBase() COVERITY_NOEXCEPT_FALSE;
|
2012-08-13 22:51:30 +02:00
|
|
|
|
2007-04-11 19:16:34 +00:00
|
|
|
public:
|
2017-04-21 16:35:19 +02:00
|
|
|
SvRefBase() : nRefCount(0), bNoDelete(1) {}
|
|
|
|
SvRefBase(const SvRefBase &) : nRefCount(0), bNoDelete(1) {}
|
2014-07-15 15:41:33 +02:00
|
|
|
|
2018-07-05 14:29:11 +02:00
|
|
|
SvRefBase & operator=(const SvRefBase &) { return *this; }
|
2007-04-11 19:16:34 +00:00
|
|
|
|
|
|
|
void RestoreNoDelete()
|
2017-04-21 16:35:19 +02:00
|
|
|
{ bNoDelete = 1; }
|
2014-07-11 08:58:37 +02:00
|
|
|
|
|
|
|
void AddNextRef()
|
2007-04-11 19:16:34 +00:00
|
|
|
{
|
2014-07-11 08:58:37 +02:00
|
|
|
assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
|
|
|
|
++nRefCount;
|
2007-04-11 19:16:34 +00:00
|
|
|
}
|
2014-07-10 12:12:55 +02:00
|
|
|
|
2014-10-02 14:37:06 +02:00
|
|
|
void AddFirstRef()
|
2007-04-11 19:16:34 +00:00
|
|
|
{
|
2014-07-11 08:58:37 +02:00
|
|
|
assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
|
|
|
|
if( bNoDelete )
|
2017-04-21 16:35:19 +02:00
|
|
|
bNoDelete = 0;
|
2014-07-11 08:58:37 +02:00
|
|
|
++nRefCount;
|
2007-04-11 19:16:34 +00:00
|
|
|
}
|
2014-07-10 12:12:55 +02:00
|
|
|
|
2014-07-10 11:03:27 +02:00
|
|
|
void ReleaseRef()
|
2007-04-11 19:16:34 +00:00
|
|
|
{
|
2014-07-11 08:58:37 +02:00
|
|
|
assert( nRefCount >= 1);
|
|
|
|
if( --nRefCount == 0 && !bNoDelete)
|
2014-10-03 10:39:28 +02:00
|
|
|
{
|
|
|
|
// I'm not sure about the original purpose of this line, but right now
|
|
|
|
// it serves the purpose that anything that attempts to do an AddRef()
|
|
|
|
// after an object is deleted will trip an assert.
|
|
|
|
nRefCount = 1 << 30;
|
|
|
|
delete this;
|
|
|
|
}
|
2007-04-11 19:16:34 +00:00
|
|
|
}
|
2014-07-10 12:12:55 +02:00
|
|
|
|
2014-07-11 08:58:37 +02:00
|
|
|
unsigned int GetRefCount() const
|
2014-07-10 12:12:55 +02:00
|
|
|
{ return nRefCount; }
|
2007-04-11 19:16:34 +00:00
|
|
|
};
|
|
|
|
|
2014-07-10 17:05:19 +02:00
|
|
|
template<typename T>
|
|
|
|
class SvCompatWeakBase;
|
|
|
|
|
2019-07-20 17:25:54 +02:00
|
|
|
/** SvCompatWeakHdl acts as an intermediary between SvCompatWeakRef<T> and T.
|
2014-07-10 15:44:28 +02:00
|
|
|
*/
|
2014-07-10 17:05:19 +02:00
|
|
|
template<typename T>
|
2019-10-30 09:42:08 +02:00
|
|
|
class SvCompatWeakHdl final : public SvRefBase
|
2007-04-11 19:16:34 +00:00
|
|
|
{
|
2014-07-10 17:05:19 +02:00
|
|
|
friend class SvCompatWeakBase<T>;
|
|
|
|
T* _pObj;
|
2014-07-10 15:44:28 +02:00
|
|
|
|
2014-07-10 17:05:19 +02:00
|
|
|
SvCompatWeakHdl( T* pObj ) : _pObj( pObj ) {}
|
2012-08-13 22:51:30 +02:00
|
|
|
|
2007-04-11 19:16:34 +00:00
|
|
|
public:
|
2015-12-10 17:33:54 +01:00
|
|
|
void ResetWeakBase( ) { _pObj = nullptr; }
|
2014-07-10 17:05:19 +02:00
|
|
|
T* GetObj() { return _pObj; }
|
2007-04-11 19:16:34 +00:00
|
|
|
};
|
|
|
|
|
2014-07-10 17:05:19 +02:00
|
|
|
/** We only have one place that extends this, in include/sfx2/frame.hxx, class SfxFrame.
|
|
|
|
Its function is to notify the SvCompatWeakHdl when an SfxFrame object is deleted.
|
2014-07-10 15:44:28 +02:00
|
|
|
*/
|
2014-07-10 17:05:19 +02:00
|
|
|
template<typename T>
|
2007-04-11 19:16:34 +00:00
|
|
|
class SvCompatWeakBase
|
|
|
|
{
|
2014-07-10 17:05:19 +02:00
|
|
|
tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
|
2012-08-13 22:51:30 +02:00
|
|
|
|
2007-04-11 19:16:34 +00:00
|
|
|
public:
|
2014-07-10 17:05:19 +02:00
|
|
|
/** Does not use initializer due to compiler warnings,
|
|
|
|
because the lifetime of the _xHdl object can exceed the lifetime of this class.
|
|
|
|
*/
|
|
|
|
SvCompatWeakBase( T* pObj ) { _xHdl = new SvCompatWeakHdl<T>( pObj ); }
|
2014-07-10 15:44:28 +02:00
|
|
|
|
2007-04-11 19:16:34 +00:00
|
|
|
~SvCompatWeakBase() { _xHdl->ResetWeakBase(); }
|
2014-07-10 15:44:28 +02:00
|
|
|
|
2016-10-05 22:00:51 +02:00
|
|
|
SvCompatWeakHdl<T>* GetHdl() { return _xHdl.get(); }
|
2007-04-11 19:16:34 +00:00
|
|
|
};
|
|
|
|
|
2014-07-10 17:05:19 +02:00
|
|
|
/** We only have one weak reference in LO, in include/sfx2/frame.hxx, class SfxFrameWeak.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2015-11-24 10:56:03 +02:00
|
|
|
class SAL_WARN_UNUSED SvCompatWeakRef
|
2014-07-10 17:05:19 +02:00
|
|
|
{
|
|
|
|
tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
|
|
|
|
public:
|
2017-03-03 20:57:02 +01:00
|
|
|
SvCompatWeakRef( ) {}
|
|
|
|
SvCompatWeakRef( T* pObj )
|
2014-07-10 17:05:19 +02:00
|
|
|
{ if( pObj ) _xHdl = pObj->GetHdl(); }
|
2017-02-10 09:24:16 +00:00
|
|
|
#if defined(__COVERITY__)
|
|
|
|
~SvCompatWeakRef() COVERITY_NOEXCEPT_FALSE {}
|
|
|
|
#endif
|
2017-03-03 20:57:02 +01:00
|
|
|
SvCompatWeakRef& operator = ( T * pObj )
|
2016-10-11 16:27:38 +02:00
|
|
|
{ _xHdl = pObj ? pObj->GetHdl() : nullptr; return *this; }
|
2017-03-03 20:57:02 +01:00
|
|
|
bool is() const
|
2017-01-25 12:03:58 +02:00
|
|
|
{ return _xHdl.is() && _xHdl->GetObj(); }
|
|
|
|
explicit operator bool() const { return is(); }
|
2017-03-03 20:57:02 +01:00
|
|
|
T* operator -> () const
|
2017-01-25 12:03:58 +02:00
|
|
|
{ return _xHdl.is() ? _xHdl->GetObj() : nullptr; }
|
2017-03-03 20:57:02 +01:00
|
|
|
operator T* () const
|
2017-01-25 12:03:58 +02:00
|
|
|
{ return _xHdl.is() ? _xHdl->GetObj() : nullptr; }
|
2007-04-11 19:16:34 +00:00
|
|
|
};
|
|
|
|
|
2012-08-13 22:51:30 +02:00
|
|
|
#endif
|
2010-10-14 08:27:31 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|