Update git submodules
[LibreOffice.git] / include / unotools / weakref.hxx
blob1d9b29f82e70d017a98c70996f297dc63344c322
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 .
21 * This file is part of LibreOffice published API.
23 #pragma once
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>
32 #include <type_traits>
34 namespace unotools
36 /** The WeakReference<> holds a weak reference to an object.
38 That object must implement the css::uno::XWeak interface.
40 The WeakReference itself is *not* thread safe, just as
41 Reference itself isn't, but the implementation of the listeners etc.
42 behind it *is* thread-safe, so multiple threads can have their own
43 WeakReferences to the same XWeak object.
45 @tparam interface_type Must be a C++ implementation class type, not a UNO interface type. (See
46 the C++20 requires-clause on the get member. That clause is not put on the class as a whole to
47 avoid overly tight requirements on when interface_type needs to be complete.)
49 template <class interface_type>
50 class SAL_WARN_UNUSED WeakReference : public com::sun::star::uno::WeakReferenceHelper
52 public:
53 /** Default ctor. Creates an empty weak reference.
55 WeakReference()
56 : WeakReferenceHelper()
60 /** Copy ctor. Initialize this reference with a hard reference.
62 @param rRef another hard ref
64 WeakReference(const rtl::Reference<interface_type>& rRef)
65 : WeakReferenceHelper(css::uno::Reference<css::uno::XWeak>(rRef))
69 /** Copy ctor. Initialize this reference with a hard reference.
71 @param rRef another hard ref
73 WeakReference(interface_type& rRef)
74 : WeakReferenceHelper(&rRef)
78 /** Copy ctor. Initialize this reference with a hard reference.
80 @param rRef another hard ref
82 WeakReference(interface_type* pRef)
83 : WeakReferenceHelper(
84 css::uno::Reference<css::uno::XWeak>(static_cast<cppu::OWeakObject*>(pRef)))
88 /** Releases this reference and takes over hard reference xInt.
89 If the implementation behind xInt does not support XWeak
90 or XInt is null, then this reference is null.
92 @param xInt another hard reference
94 WeakReference& operator=(const rtl::Reference<interface_type>& xInt)
96 WeakReferenceHelper::operator=(xInt);
97 return *this;
100 WeakReference& operator=(rtl::Reference<interface_type>&& xInt)
102 WeakReferenceHelper::operator=(std::move(xInt));
103 return *this;
106 WeakReference& operator=(interface_type* pInt)
108 WeakReferenceHelper::operator=(
109 css::uno::Reference<css::uno::XWeak>(static_cast<::cppu::OWeakObject*>(pInt)));
110 return *this;
113 /** Gets a hard reference to the object.
115 @return hard reference or null, if the weakly referenced interface has gone
117 rtl::Reference<interface_type> SAL_CALL get() const
118 #if __cplusplus >= 202002L
119 requires(!cppu::detail::isUnoInterfaceType<interface_type>)
120 #endif
122 css::uno::Reference<css::uno::XInterface> xInterface = WeakReferenceHelper::get();
123 // If XInterface is an ambiguous base of interface_type, we have to use dynamic_cast,
124 // otherwise we can use the faster static_cast.
125 if constexpr (std::is_convertible_v<interface_type*, css::uno::XInterface*>)
126 return static_cast<interface_type*>(xInterface.get());
127 else
128 return dynamic_cast<interface_type*>(xInterface.get());
131 /** Gets a hard reference to the object.
133 @return hard reference or null, if the weakly referenced interface has gone
135 operator ::rtl::Reference<interface_type>() const { return get(); }
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */