LibreOffice Module unotools (master) 1
weakref.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20/*
21 * This file is part of LibreOffice published API.
22 */
23#pragma once
24
25#include <sal/config.h>
26
27#include <com/sun/star/uno/Reference.hxx>
28#include <com/sun/star/uno/XInterface.hpp>
30#include <cppuhelper/weak.hxx>
31#include <rtl/ref.hxx>
32#include <type_traits>
33
34namespace unotools
35{
49template <class interface_type>
51{
52public:
56 : WeakReferenceHelper()
57 {
58 }
59
65 : WeakReferenceHelper(css::uno::Reference<css::uno::XWeak>(rRef))
66 {
67 }
68
73 WeakReference(interface_type& rRef)
74 : WeakReferenceHelper(&rRef)
75 {
76 }
77
82 WeakReference(interface_type* pRef)
83 : WeakReferenceHelper(
84 css::uno::Reference<css::uno::XWeak>(static_cast<cppu::OWeakObject*>(pRef)))
85 {
86 }
87
95 {
96 WeakReferenceHelper::operator=(xInt);
97 return *this;
98 }
99
101 {
102 WeakReferenceHelper::operator=(std::move(xInt));
103 return *this;
104 }
105
106 WeakReference& operator=(interface_type* pInt)
107 {
108 WeakReferenceHelper::operator=(
109 css::uno::Reference<css::uno::XWeak>(static_cast<::cppu::OWeakObject*>(pInt)));
110 return *this;
111 }
112
118#if __cplusplus >= 202002L
119 requires(!cppu::detail::isUnoInterfaceType<interface_type>)
120#endif
121 {
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());
129 }
130
135 operator ::rtl::Reference<interface_type>() const { return get(); }
136};
137}
138
139/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
The WeakReference<> holds a weak reference to an object.
Definition: weakref.hxx:51
WeakReference(interface_type &rRef)
Copy ctor.
Definition: weakref.hxx:73
WeakReference & operator=(interface_type *pInt)
Definition: weakref.hxx:106
WeakReference()
Default ctor.
Definition: weakref.hxx:55
rtl::Reference< interface_type > SAL_CALL get() const
Gets a hard reference to the object.
Definition: weakref.hxx:117
WeakReference(const rtl::Reference< interface_type > &rRef)
Copy ctor.
Definition: weakref.hxx:64
WeakReference & operator=(const rtl::Reference< interface_type > &xInt)
Releases this reference and takes over hard reference xInt.
Definition: weakref.hxx:94
WeakReference(interface_type *pRef)
Copy ctor.
Definition: weakref.hxx:82
WeakReference & operator=(rtl::Reference< interface_type > &&xInt)
Definition: weakref.hxx:100
OUString get(TranslateId sContextAndId, const std::locale &loc)
Definition: resmgr.cxx:211
Reference
#define SAL_WARN_UNUSED