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>
40 /** The WeakReference<> holds a weak reference to an object.
42 That object must implement the css::uno::XWeak interface.
44 The WeakReference itself is *not* thread safe, just as
45 Reference itself isn't, but the implementation of the listeners etc.
46 behind it *is* thread-safe, so multiple threads can have their own
47 WeakReferences to the same XWeak object.
49 @tparam interface_type type of interface
51 template <class interface_type
>
52 class SAL_WARN_UNUSED WeakReference
: public com::sun::star::uno::WeakReferenceHelper
55 /** Default ctor. Creates an empty weak reference.
58 : WeakReferenceHelper()
62 /** Copy ctor. Initialize this reference with a hard reference.
64 @param rRef another hard ref
66 WeakReference(const rtl::Reference
<interface_type
>& rRef
)
67 : WeakReferenceHelper(rRef
)
71 /** Copy ctor. Initialize this reference with a hard reference.
73 @param rRef another hard ref
75 WeakReference(interface_type
& rRef
)
76 : WeakReferenceHelper(&rRef
)
80 /** Copy ctor. Initialize this reference with a hard reference.
82 @param rRef another hard ref
84 WeakReference(interface_type
* pRef
)
85 : WeakReferenceHelper(static_cast<cppu::OWeakObject
*>(pRef
))
89 /** Releases this reference and takes over hard reference xInt.
90 If the implementation behind xInt does not support XWeak
91 or XInt is null, then this reference is null.
93 @param xInt another hard reference
95 WeakReference
& operator=(const rtl::Reference
<interface_type
>& xInt
)
97 WeakReferenceHelper::operator=(xInt
);
101 WeakReference
& operator=(rtl::Reference
<interface_type
>&& xInt
)
103 WeakReferenceHelper::operator=(std::move(xInt
));
107 WeakReference
& operator=(interface_type
* pInt
)
109 WeakReferenceHelper::operator=(static_cast<::cppu::OWeakObject
*>(pInt
));
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
119 css::uno::Reference
<css::uno::XInterface
> xInterface
= WeakReferenceHelper::get();
120 return dynamic_cast<interface_type
*>(xInterface
.get());
123 /** Gets a hard reference to the object.
125 @return hard reference or null, if the weakly referenced interface has gone
127 operator ::rtl::Reference
<interface_type
>() const { return get(); }
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */