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 .
19 #ifndef INCLUDED_CPPUHELPER_WEAKREF_HXX
20 #define INCLUDED_CPPUHELPER_WEAKREF_HXX
22 #include <com/sun/star/uno/Reference.hxx>
23 #include <com/sun/star/uno/XInterface.hpp>
24 #include <cppuhelper/cppuhelperdllapi.h>
36 class OWeakRefListener
;
38 /** The WeakReferenceHelper holds a weak reference to an object. This object must implement
39 the com::sun::star::uno::XWeak interface. The implementation is thread safe.
41 class CPPUHELPER_DLLPUBLIC WeakReferenceHelper
44 /** Default ctor. Creates an empty weak reference.
46 inline WeakReferenceHelper()
50 /** Copy ctor. Initialize this reference with the same interface as in rWeakRef.
52 @param rWeakRef another weak ref
54 WeakReferenceHelper( const WeakReferenceHelper
& rWeakRef
);
55 /** Initialize this reference with the hard interface reference xInt. If the implementation
56 behind xInt does not support XWeak or XInt is null then this reference will be null.
58 @param xInt another hard interface reference
60 WeakReferenceHelper( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> & xInt
);
61 /** Releases this reference.
63 ~WeakReferenceHelper();
65 /** Releases this reference and takes over rWeakRef.
67 @param rWeakRef another weak ref
69 WeakReferenceHelper
& SAL_CALL
operator = ( const WeakReferenceHelper
& rWeakRef
);
71 /** Releases this reference and takes over hard reference xInt.
72 If the implementation behind xInt does not support XWeak
73 or XInt is null, then this reference is null.
75 @param xInt another hard reference
77 WeakReferenceHelper
& SAL_CALL
operator = (
78 const ::com::sun::star::uno::Reference
<
79 ::com::sun::star::uno::XInterface
> & xInt
);
81 /** Returns true if both weak refs reference to the same object.
83 @param rObj another weak ref
84 @return true, if both weak refs reference to the same object.
86 inline bool SAL_CALL
operator == ( const WeakReferenceHelper
& rObj
) const
87 { return (get() == rObj
.get()); }
89 /** Gets a hard reference to the object.
91 @return hard reference or null, if the weakly referenced interface has gone
93 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> SAL_CALL
get() const;
94 /** Gets a hard reference to the object.
96 @return hard reference or null, if the weakly referenced interface has gone
98 inline SAL_CALL
operator Reference
< XInterface
> () const
101 /** Releases this reference.
105 void SAL_CALL
clear();
109 OWeakRefListener
* m_pImpl
;
113 /** The WeakReference<> holds a weak reference to an object. This object must implement
114 the com::sun::star::uno::XWeak interface. The implementation is thread safe.
116 @tparam interface_type type of interface
118 template< class interface_type
>
119 class WeakReference
: public WeakReferenceHelper
122 /** Default ctor. Creates an empty weak reference.
124 inline WeakReference()
125 : WeakReferenceHelper()
128 /** Copy ctor. Initialize this reference with a hard reference.
130 @param rRef another hard ref
132 inline WeakReference( const Reference
< interface_type
> & rRef
)
133 : WeakReferenceHelper( rRef
)
136 /** Releases this reference and takes over hard reference xInt.
137 If the implementation behind xInt does not support XWeak
138 or XInt is null, then this reference is null.
140 @param xInt another hard reference
144 WeakReference
& SAL_CALL
operator = (
145 const ::com::sun::star::uno::Reference
< interface_type
> & xInt
)
146 { WeakReferenceHelper::operator=(xInt
); return *this; }
148 /** Gets a hard reference to the object.
150 @return hard reference or null, if the weakly referenced interface has gone
152 inline SAL_CALL
operator Reference
< interface_type
> () const
153 { return Reference
< interface_type
>::query( get() ); }
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */