1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 * This file is part of LibreOffice published API.
25 #include <sal/config.h>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/XInterface.hpp>
29 #include <cppuhelper/weakref.hxx>
30 #include <cppuhelper/weak.hxx>
31 #include <rtl/ref.hxx>
35 /** The WeakReference<> holds a weak reference to an object.
37 That object must implement the css::uno::XWeak interface.
39 The WeakReference itself is *not* thread safe, just as
40 Reference itself isn't, but the implementation of the listeners etc.
41 behind it *is* thread-safe, so multiple threads can have their own
42 WeakReferences to the same XWeak object.
44 @tparam interface_type Must be a C++ implementation class type, not a UNO interface type. (See
45 the C++20 requires-clause on the get member. That clause is not put on the class as a whole to
46 avoid overly tight requirements on when interface_type needs to be complete.)
48 template <class interface_type
>
49 class SAL_WARN_UNUSED WeakReference
: public css::uno::WeakReferenceHelper
52 /** Default ctor. Creates an empty weak reference.
55 : WeakReferenceHelper()
59 /** Copy ctor. Initialize this reference with a hard reference.
61 @param rRef another hard ref
63 WeakReference(const rtl::Reference
<interface_type
>& rRef
)
64 : WeakReferenceHelper(css::uno::Reference
<css::uno::XWeak
>(rRef
))
68 /** Copy ctor. Initialize this reference with a hard reference.
70 @param rRef another hard ref
72 WeakReference(interface_type
& rRef
)
73 : WeakReferenceHelper(&rRef
)
77 /** Copy ctor. Initialize this reference with a hard reference.
79 @param rRef another hard ref
81 WeakReference(interface_type
* pRef
)
82 : WeakReferenceHelper(
83 css::uno::Reference
<css::uno::XWeak
>(static_cast<cppu::OWeakObject
*>(pRef
)))
87 /** Releases this reference and takes over hard reference xInt.
88 If the implementation behind xInt does not support XWeak
89 or XInt is null, then this reference is null.
91 @param xInt another hard reference
93 WeakReference
& operator=(const rtl::Reference
<interface_type
>& xInt
)
95 WeakReferenceHelper::operator=(xInt
);
99 WeakReference
& operator=(rtl::Reference
<interface_type
>&& xInt
)
101 WeakReferenceHelper::operator=(std::move(xInt
));
105 WeakReference
& operator=(interface_type
* pInt
)
107 WeakReferenceHelper::operator=(
108 css::uno::Reference
<css::uno::XWeak
>(static_cast<::cppu::OWeakObject
*>(pInt
)));
112 /** Gets a hard reference to the object.
114 @return hard reference or null, if the weakly referenced interface has gone
116 rtl::Reference
<interface_type
> SAL_CALL
get() const
117 #if !(defined __clang__ && __clang_major__ <= 15)
118 requires(!cppu::detail::isUnoInterfaceType
<interface_type
>)
121 css::uno::Reference
<css::uno::XInterface
> xInterface
= WeakReferenceHelper::get();
122 // If XInterface is an ambiguous base of interface_type, we have to use dynamic_cast,
123 // otherwise we can use the faster static_cast.
124 if constexpr (std::is_convertible_v
<interface_type
*, css::uno::XInterface
*>)
125 return static_cast<interface_type
*>(xInterface
.get());
127 return dynamic_cast<interface_type
*>(xInterface
.get());
130 /** Gets a hard reference to the object.
132 @return hard reference or null, if the weakly referenced interface has gone
134 operator ::rtl::Reference
<interface_type
>() const { return get(); }
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */