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 _CPPUHELPER_WEAKREF_HXX_
20 #define _CPPUHELPER_WEAKREF_HXX_
22 #include <com/sun/star/uno/XInterface.hpp>
23 #include "cppuhelperdllapi.h"
35 class OWeakRefListener
;
37 /** The WeakReferenceHelper holds a weak reference to an object. This object must implement
38 the com::sun::star::uno::XWeak interface. The implementation is thread safe.
40 class CPPUHELPER_DLLPUBLIC WeakReferenceHelper
43 /** Default ctor. Creates an empty weak reference.
45 inline WeakReferenceHelper() SAL_THROW(())
49 /** Copy ctor. Initialize this reference with the same interface as in rWeakRef.
51 @param rWeakRef another weak ref
53 WeakReferenceHelper( const WeakReferenceHelper
& rWeakRef
) SAL_THROW(());
54 /** Initialize this reference with the hard interface reference xInt. If the implementation
55 behind xInt does not support XWeak or XInt is null then this reference will be null.
57 @param xInt another hard interface reference
59 WeakReferenceHelper( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> & xInt
)
61 /** Releases this reference.
63 ~WeakReferenceHelper() SAL_THROW(());
65 /** Releases this reference and takes over rWeakRef.
67 @param rWeakRef another weak ref
69 WeakReferenceHelper
& SAL_CALL
operator = ( const WeakReferenceHelper
& rWeakRef
) SAL_THROW(());
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
) SAL_THROW(());
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 sal_Bool SAL_CALL
operator == ( const WeakReferenceHelper
& rObj
) const SAL_THROW(())
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 SAL_THROW(());
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 SAL_THROW(())
101 /** Releases this reference.
105 void SAL_CALL
clear() SAL_THROW(());
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() SAL_THROW(())
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
) SAL_THROW(())
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
)
147 { WeakReferenceHelper::operator=(xInt
); return *this; }
149 /** Gets a hard reference to the object.
151 @return hard reference or null, if the weakly referenced interface has gone
153 inline SAL_CALL
operator Reference
< interface_type
> () const SAL_THROW(())
154 { return Reference
< interface_type
>::query( get() ); }
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */