2021-02-21 12:33:10 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* 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 .
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of LibreOffice published API.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <sal/config.h>
|
|
|
|
|
|
|
|
#include <com/sun/star/uno/Reference.hxx>
|
|
|
|
#include <com/sun/star/uno/XInterface.hpp>
|
|
|
|
#include <cppuhelper/weakref.hxx>
|
2021-04-01 09:40:51 +02:00
|
|
|
#include <cppuhelper/weak.hxx>
|
2021-02-21 12:33:10 +02:00
|
|
|
#include <rtl/ref.hxx>
|
|
|
|
|
|
|
|
namespace unotools
|
|
|
|
{
|
|
|
|
/** The WeakReference<> holds a weak reference to an object.
|
|
|
|
|
|
|
|
That object must implement the css::uno::XWeak interface.
|
|
|
|
|
|
|
|
The WeakReference itself is *not* thread safe, just as
|
|
|
|
Reference itself isn't, but the implementation of the listeners etc.
|
|
|
|
behind it *is* thread-safe, so multiple threads can have their own
|
|
|
|
WeakReferences to the same XWeak object.
|
|
|
|
|
Clarify the use of untools::WeakReference
...which had been introduced in 78040af9acea0ab681aa54ff23844b647bc9b4f3
"loplugin:refcounting in sc" as a variation on css::uno::WeakReference from
include/cppuhelper/weakref.hxx, but without giving much of a rationale.
And, at least for --with-latest-c++ builds using a capable C++20 compiler,
ensure that the given interface_type is such that the static_cast and
dynamic_cast in the implementation of unotools::WeakReference::get are actually
sound: If interface_type could be a UNO interface type, that would imply that
the xInterface obtained from the underlying WeakReferenceHelper::get() could be
a proxy from the C++ UNO bridge, which could (a) be a proxy for only a subtype
of interface_type (e.g., just for XInterface), so that the static_cast would be
broken, and (b) be a proxy for which the vtable's RTTI slot is not set up (see
e.g. the ENABLE_RUNTIME_OPTIMIZATIONS code in
bridges::cpp_uno::shared::VtableFactory::initializeBlock in
bridges/source/cpp_uno/gcc3_linux_x86-64/cpp2uno.cxx), so that the dynamic_cast
could crash. (These issues can even happen when the given interface_type is a
C++ implementation class type, but the given object has been bridged over some
C++ to C++ UNO ("purpose") bridge, but lets leave it at that...)
This required adding some cppu::detail::isUnoInterfaceType predicate to the
include files generated by cppumaker, which can be useful in other places too.
(For the call to isUnoInterfaceType in the requires-clause of
unotools::WeakReference<interface_type>::get to give the correct answer, it is
important that interface_type is a complete type---i.e., the corresponding
codemaker-generated .hpp having been included if interface_type actually were a
UNO include type. But that is already nicely required by the call to
std::is_convertible_v in the implementation of that function, anyway.)
Change-Id: Ia5efd70085d2d6d45fa0995d00dc8def564bbe5f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143601
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2022-12-02 17:01:02 +01:00
|
|
|
@tparam interface_type Must be a C++ implementation class type, not a UNO interface type. (See
|
|
|
|
the C++20 requires-clause on the get member. That clause is not put on the class as a whole to
|
|
|
|
avoid overly tight requirements on when interface_type needs to be complete.)
|
2021-02-21 12:33:10 +02:00
|
|
|
*/
|
|
|
|
template <class interface_type>
|
2024-11-04 11:26:07 +05:00
|
|
|
class SAL_WARN_UNUSED WeakReference : public css::uno::WeakReferenceHelper
|
2021-02-21 12:33:10 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Default ctor. Creates an empty weak reference.
|
|
|
|
*/
|
|
|
|
WeakReference()
|
|
|
|
: WeakReferenceHelper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy ctor. Initialize this reference with a hard reference.
|
|
|
|
|
|
|
|
@param rRef another hard ref
|
|
|
|
*/
|
|
|
|
WeakReference(const rtl::Reference<interface_type>& rRef)
|
2021-05-27 10:27:46 +02:00
|
|
|
: WeakReferenceHelper(css::uno::Reference<css::uno::XWeak>(rRef))
|
2021-02-21 12:33:10 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy ctor. Initialize this reference with a hard reference.
|
|
|
|
|
|
|
|
@param rRef another hard ref
|
|
|
|
*/
|
|
|
|
WeakReference(interface_type& rRef)
|
|
|
|
: WeakReferenceHelper(&rRef)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:41:42 +02:00
|
|
|
/** Copy ctor. Initialize this reference with a hard reference.
|
2021-02-21 12:33:10 +02:00
|
|
|
|
|
|
|
@param rRef another hard ref
|
|
|
|
*/
|
2022-02-21 16:00:52 +02:00
|
|
|
WeakReference(interface_type* pRef)
|
|
|
|
: WeakReferenceHelper(
|
|
|
|
css::uno::Reference<css::uno::XWeak>(static_cast<cppu::OWeakObject*>(pRef)))
|
|
|
|
{
|
|
|
|
}
|
2021-02-21 12:33:10 +02:00
|
|
|
|
|
|
|
/** Releases this reference and takes over hard reference xInt.
|
|
|
|
If the implementation behind xInt does not support XWeak
|
|
|
|
or XInt is null, then this reference is null.
|
|
|
|
|
|
|
|
@param xInt another hard reference
|
|
|
|
*/
|
|
|
|
WeakReference& operator=(const rtl::Reference<interface_type>& xInt)
|
|
|
|
{
|
|
|
|
WeakReferenceHelper::operator=(xInt);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakReference& operator=(rtl::Reference<interface_type>&& xInt)
|
|
|
|
{
|
|
|
|
WeakReferenceHelper::operator=(std::move(xInt));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakReference& operator=(interface_type* pInt)
|
|
|
|
{
|
2022-02-21 16:00:52 +02:00
|
|
|
WeakReferenceHelper::operator=(
|
|
|
|
css::uno::Reference<css::uno::XWeak>(static_cast<::cppu::OWeakObject*>(pInt)));
|
2021-02-21 12:33:10 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets a hard reference to the object.
|
|
|
|
|
|
|
|
@return hard reference or null, if the weakly referenced interface has gone
|
|
|
|
*/
|
|
|
|
rtl::Reference<interface_type> SAL_CALL get() const
|
2023-09-24 12:16:36 +03:00
|
|
|
#if !(defined __clang__ && __clang_major__ <= 15)
|
Clarify the use of untools::WeakReference
...which had been introduced in 78040af9acea0ab681aa54ff23844b647bc9b4f3
"loplugin:refcounting in sc" as a variation on css::uno::WeakReference from
include/cppuhelper/weakref.hxx, but without giving much of a rationale.
And, at least for --with-latest-c++ builds using a capable C++20 compiler,
ensure that the given interface_type is such that the static_cast and
dynamic_cast in the implementation of unotools::WeakReference::get are actually
sound: If interface_type could be a UNO interface type, that would imply that
the xInterface obtained from the underlying WeakReferenceHelper::get() could be
a proxy from the C++ UNO bridge, which could (a) be a proxy for only a subtype
of interface_type (e.g., just for XInterface), so that the static_cast would be
broken, and (b) be a proxy for which the vtable's RTTI slot is not set up (see
e.g. the ENABLE_RUNTIME_OPTIMIZATIONS code in
bridges::cpp_uno::shared::VtableFactory::initializeBlock in
bridges/source/cpp_uno/gcc3_linux_x86-64/cpp2uno.cxx), so that the dynamic_cast
could crash. (These issues can even happen when the given interface_type is a
C++ implementation class type, but the given object has been bridged over some
C++ to C++ UNO ("purpose") bridge, but lets leave it at that...)
This required adding some cppu::detail::isUnoInterfaceType predicate to the
include files generated by cppumaker, which can be useful in other places too.
(For the call to isUnoInterfaceType in the requires-clause of
unotools::WeakReference<interface_type>::get to give the correct answer, it is
important that interface_type is a complete type---i.e., the corresponding
codemaker-generated .hpp having been included if interface_type actually were a
UNO include type. But that is already nicely required by the call to
std::is_convertible_v in the implementation of that function, anyway.)
Change-Id: Ia5efd70085d2d6d45fa0995d00dc8def564bbe5f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143601
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2022-12-02 17:01:02 +01:00
|
|
|
requires(!cppu::detail::isUnoInterfaceType<interface_type>)
|
|
|
|
#endif
|
2021-02-21 12:33:10 +02:00
|
|
|
{
|
|
|
|
css::uno::Reference<css::uno::XInterface> xInterface = WeakReferenceHelper::get();
|
2022-11-03 16:07:11 +02:00
|
|
|
// If XInterface is an ambiguous base of interface_type, we have to use dynamic_cast,
|
|
|
|
// otherwise we can use the faster static_cast.
|
2022-11-04 10:51:23 +02:00
|
|
|
if constexpr (std::is_convertible_v<interface_type*, css::uno::XInterface*>)
|
2022-11-03 16:07:11 +02:00
|
|
|
return static_cast<interface_type*>(xInterface.get());
|
|
|
|
else
|
|
|
|
return dynamic_cast<interface_type*>(xInterface.get());
|
2021-02-21 12:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets a hard reference to the object.
|
|
|
|
|
|
|
|
@return hard reference or null, if the weakly referenced interface has gone
|
|
|
|
*/
|
|
|
|
operator ::rtl::Reference<interface_type>() const { return get(); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|