1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef _CPPUHELPER_WEAKREF_HXX_ 28 #define _CPPUHELPER_WEAKREF_HXX_ 29 30 #include <com/sun/star/uno/XInterface.hpp> 31 32 33 namespace com 34 { 35 namespace sun 36 { 37 namespace star 38 { 39 namespace uno 40 { 41 42 /** @internal */ 43 class OWeakRefListener; 44 45 /** The WeakReferenceHelper holds a weak reference to an object. This object must implement 46 the ::com::sun::star::uno::XWeak interface. The implementation is thread safe. 47 */ 48 class WeakReferenceHelper 49 { 50 public: 51 /** Default ctor. Creates an empty weak reference. 52 */ 53 inline WeakReferenceHelper() SAL_THROW( () ) 54 : m_pImpl( 0 ) 55 {} 56 57 /** Copy ctor. Initialize this reference with the same interface as in rWeakRef. 58 59 @param rWeakRef another weak ref 60 */ 61 WeakReferenceHelper( const WeakReferenceHelper & rWeakRef ) SAL_THROW( () ); 62 /** Initialize this reference with the hard interface reference xInt. If the implementation 63 behind xInt does not support XWeak or XInt is null then this reference will be null. 64 65 @param xInt another hard interface reference 66 */ 67 WeakReferenceHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & xInt ) 68 SAL_THROW( () ); 69 /** Releases this reference. 70 */ 71 ~WeakReferenceHelper() SAL_THROW( () ); 72 73 /** Releases this reference and takes over rWeakRef. 74 75 @param rWeakRef another weak ref 76 */ 77 WeakReferenceHelper & SAL_CALL operator = ( const WeakReferenceHelper & rWeakRef ) SAL_THROW( () ); 78 79 /** Releases this reference and takes over hard reference xInt. 80 If the implementation behind xInt does not support XWeak 81 or XInt is null, then this reference is null. 82 83 @param xInt another hard reference 84 */ 85 WeakReferenceHelper & SAL_CALL operator = ( 86 const ::com::sun::star::uno::Reference< 87 ::com::sun::star::uno::XInterface > & xInt ) SAL_THROW( () ); 88 89 /** Returns true if both weak refs reference to the same object. 90 91 @param rObj another weak ref 92 @return true, if both weak refs reference to the same object. 93 */ 94 inline sal_Bool SAL_CALL operator == ( const WeakReferenceHelper & rObj ) const SAL_THROW( () ) 95 { return (get() == rObj.get()); } 96 97 /** Gets a hard reference to the object. 98 99 @return hard reference or null, if the weakly referenced interface has gone 100 */ 101 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL get() const SAL_THROW( () ); 102 /** Gets a hard reference to the object. 103 104 @return hard reference or null, if the weakly referenced interface has gone 105 */ 106 inline SAL_CALL operator Reference< XInterface > () const SAL_THROW( () ) 107 { return get(); } 108 109 /** Releases this reference. 110 111 @since UDK 3.2.12 112 */ 113 void SAL_CALL clear() SAL_THROW( () ); 114 115 protected: 116 /** @internal */ 117 OWeakRefListener * m_pImpl; 118 }; 119 120 /** The WeakReference<> holds a weak reference to an object. This object must implement 121 the ::com::sun::star::uno::XWeak interface. The implementation is thread safe. 122 123 @tplparam interface_type type of interface 124 */ 125 template< class interface_type > 126 class WeakReference : public WeakReferenceHelper 127 { 128 public: 129 /** Default ctor. Creates an empty weak reference. 130 */ 131 inline WeakReference() SAL_THROW( () ) 132 : WeakReferenceHelper() 133 {} 134 135 /** Copy ctor. Initialize this reference with a hard reference. 136 137 @param rRef another hard ref 138 */ 139 inline WeakReference( const Reference< interface_type > & rRef ) SAL_THROW( () ) 140 : WeakReferenceHelper( rRef ) 141 {} 142 143 /** Releases this reference and takes over hard reference xInt. 144 If the implementation behind xInt does not support XWeak 145 or XInt is null, then this reference is null. 146 147 @param xInt another hard reference 148 149 @since UDK 3.2.12 150 */ 151 WeakReference & SAL_CALL operator = ( 152 const ::com::sun::star::uno::Reference< interface_type > & xInt ) 153 SAL_THROW( () ) 154 { WeakReferenceHelper::operator=(xInt); return *this; } 155 156 /** Gets a hard reference to the object. 157 158 @return hard reference or null, if the weakly referenced interface has gone 159 */ 160 inline SAL_CALL operator Reference< interface_type > () const SAL_THROW( () ) 161 { return Reference< interface_type >::query( get() ); } 162 }; 163 164 } 165 } 166 } 167 } 168 169 #endif 170